Exemple #1
0
        //eventually StartGame will replace newGame
        public void StartGame(int lobbyId)
        {
            var lobby = _context.Lobby.Include(x => x.LobbyUser)
                        .Single(x => x.LobbyId == lobbyId);

            var gameModel = new Models.Game()
            {
                DateTime = DateTime.Now
            };

            _context.Game.Add(gameModel);
            _context.SaveChanges();

            ISupplyFactory supplyFactory;

            supplyFactory = new RandomizedSupplyFactory();

            var userIds = lobby.LobbyUser.Select(x => x.UserId).ToList();


            var supply = supplyFactory.Create(userIds.Count);

            userIds.Shuffle();

            var players = new List <IPlayer>();

            for (int i = 0; i < userIds.Count; i++)
            {
                var userName = _context.Users.Find(userIds[i]).UserName;
                players.Add(new Player(i + 1, userName));
            }

            var game = new Game.Game(gameModel.GameId, players, supply, new VictoryCondition());

            game.Initialize();

            var gameState = new Models.GameState()
            {
                GameId = game.GameId,
                State  = game.GetGameState()
            };

            _context.GameState.Add(gameState);
            _context.SaveChanges();

            //you're going to configure your client app to listen for this
            Clients.All.SendAsync("Send", supply);
            Clients.All.SendAsync("Game", game.GameId);
            foreach (var player in players)
            {
                Clients.User(player.PlayerName).SendAsync("GoToGame", game.GameId);
                Clients.User(player.PlayerName).SendAsync("Player", player);
            }
        }
Exemple #2
0
        //TODO: rename echo hub or move logic to GameHub
        //TODO: refactor game startup as builder or factory
        //TODO: performance add homogenous pile class for most piles
        //you're going to invoke this method from the client app
        public void NewGame(bool randomizedKingdom)
        {
            var gameModel = new Models.Game()
            {
                DateTime = DateTime.Now
            };

            _context.Game.Add(gameModel);
            _context.SaveChanges();

            ISupplyFactory supplyFactory;

            if (randomizedKingdom)
            {
                supplyFactory = new RandomizedSupplyFactory();
            }
            else
            {
                supplyFactory = new DefaultSupplyFactory();
            }

            var supply = supplyFactory.Create(2);

            var ben   = new Player(1, "*****@*****.**");
            var maria = new Player(2, "*****@*****.**");

            var players = new List <IPlayer>()
            {
                ben, maria
            };

            var defaultVictoryCondition = new VictoryCondition();

            var game = new Game.Game(gameModel.GameId, players, supply, defaultVictoryCondition);

            game.Initialize();

            var gameState = new Models.GameState()
            {
                GameId = game.GameId,
                State  = game.GetGameState()
            };

            _context.GameState.Add(gameState);
            _context.SaveChanges();

            //you're going to configure your client app to listen for this
            Clients.All.SendAsync("Send", supply);
            Clients.All.SendAsync("Game", game.GameId);
            Clients.User(ben.PlayerName).SendAsync("Player", ben);
            Clients.User(maria.PlayerName).SendAsync("Player", maria);
        }