Ejemplo n.º 1
0
        public PlayerInBattle GetNewGame(Player firstPlayer, int gameType, List<Player> opponents)
        {
            // 1-Create The game
            var game = typeMapping.GetGame(gameType);
            Grid currentState = game.GetNewGame();

            // 2-Preparing listOfPlayers
            var listPlayers = opponents.Select(p=>p.PlayerId).ToList();
            listPlayers.Insert(0, firstPlayer.PlayerId);

            // 2-Link the players Id with player numbers
            PlayerInBattle toReturn = new PlayerInBattle()
            {
                Battle = new Battle
                {
                    CreationTime = DateTime.Now,
                    BattleId = Math.Abs((int)DateTime.Now.Ticks),//String.Format("{0}_{1}_{2}_{3}", game.GetGameType(), firstPlayer.PlayerId, GetElapsedSecondsSinceLastNewYear(), ran.Next(99)),
                    GameType = gameType,
                    GameTypeString = game.ToString(),
                    LastUpdate = DateTime.Now,
                    Players = listPlayers,
                    CurrentState = currentState
                },
                Player = firstPlayer,
                PlayerNumber = 1
            };

            repoPlayerInBattle.Persist(toReturn);
            return toReturn;
        }
        public void Persist(PlayerInBattle playersInBattle)
        {
            int i = 1;
            foreach (int currentPlayerId in playersInBattle.Battle.Players)
            {
                playersInBattle = new PlayerInBattle()
                {
                    Battle = playersInBattle.Battle,
                    Player = MockPlayerRepository.GetInstance().Get(currentPlayerId),
                    PlayerNumber = i
                };

                Tuple<int, int> toPersist = new Tuple<int, int>(currentPlayerId, playersInBattle.Battle.BattleId);
                i++;
                datas[toPersist] = playersInBattle;
            }
        }
Ejemplo n.º 3
0
        public Grid Play(PlayerInBattle playerInBattle, Move move)
        {
            // 1- We play the move
            var currentBattle = playerInBattle.Battle;
            var game = typeMapping.GetGame(currentBattle.GameType);
            var oldState = currentBattle.CurrentState.NextPlayerToPlay;
            var newState = game.Play(currentBattle.CurrentState, move);

            // 2- If the grid has change, we persit the new state
            currentBattle.CurrentState = newState;
            if (newState.DeservePersistence(oldState))
            {
                //See what we want to do....
                repoPlayerInBattle.Persist(playerInBattle);
            }

            // 3- We return the new state to the client
            return newState;
        }
Ejemplo n.º 4
0
        //Integration test for DAO
        public static void Main()
        {
            //PlayerDao.ConnectionString = MyConfiguration.GetString("MikMakConnection", @"Data Source=|DataDirectory|\LocalDb.sdf");
            Console.WriteLine("Test starting...");
            try
            {
                using (Tester test = new Tester())
                {
                    //Player Tests
                    string login1 = DateTime.Now.ToString("HH:mm:ss");
                    string login2 = DateTime.Now.ToString("HH:mm:ss") + "bis";

                    Debug.Assert(test.playerDao.Get(91233) ==null);
                    Debug.Assert(test.playerDao.Get(login1 + "ee") == null);
                    Debug.Assert(test.playerDao.Get(login1 )== null);

                    test.playerDao.CreatePlayer(login1, login1);
                    Debug.Assert(test.playerDao.Get(login1) != null);
                    Debug.Assert(test.playerDao.Get(login2) == null);
                    test.playerDao.CreatePlayer(login2, login2);
                    var player1 = test.playerDao.Get(login1);
                    var player2 = test.playerDao.Get(login2);
                    Debug.Assert(player2 != null);
                    Debug.Assert(test.playerDao.Get(login2).Login == login2);
                    Debug.Assert(test.playerDao.Get(login2).Password == login2);
                    Debug.Assert(test.playerDao.Get(login2).PlayerId != test.playerDao.Get(login1).PlayerId);

                    //For fun, tom should exist!
                    if (test.playerDao.Get("tom") == null)
                    {
                        test.playerDao.CreatePlayer("tom", "tom");
                    }

                    //No battles yet
                    Debug.Assert(test.playerInBattleDao.Get(test.playerDao.Get(login1).PlayerId).Count == 0);
                    Debug.Assert(test.playerInBattleDao.Get(test.playerDao.Get(login2).PlayerId).Count == 0);

                    //CreationBattle
                    PlayerInBattle newBattle = new PlayerInBattle()
                    {
                        Player = player1,
                        PlayerNumber = 1,
                        Battle = new Battle()
                        {
                            BattleId = Math.Abs((int)DateTime.Now.Ticks),
                            CurrentState = new Grid()
                            {
                                CurrentMessage = Message.GetMessage(ClassicMessage.Default),
                                IsGridShifted = true,
                                MoveNumber = 5,
                                NextPlayerToPlay = 1,
                                NumberColumns = 2,
                                NumberLines = 4,
                                PawnLocations = new List<Pawn>()
                                {
                                    new Pawn("p1",1,2){
                                        Player = player1
                                    },
                                    new Pawn("p2",4,2){
                                        Player = player2
                                    }
                                }
                            },
                            GameType = 9,
                            GameTypeString = "testToTrash",
                            Players = new List<int>()
                            {
                                player1.PlayerId,
                                player2.PlayerId
                            }
                        }
                    };
                    test.playerInBattleDao.Persist(newBattle);

                    //Both players should see the battle
                    Debug.Assert(test.playerInBattleDao.Get(test.playerDao.Get(login1).PlayerId).Count == 1);
                    Debug.Assert(test.playerInBattleDao.Get(test.playerDao.Get(login2).PlayerId).Count == 1);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception : " + e);
            }
            Console.WriteLine("End of the test");
        }