public void CreateGame()
        {
            var player = new Player() { User = Hub.Context.User, ConnectionId = Hub.Context.ConnectionId };
            player.CreatePlayHand();

            var game = new Game(player, Rules.Open, TradeRules.One, GameEnded);
            Games.AddGame(game);

            Hub.Groups.Add(Hub.Context.ConnectionId, game.GameId);

            BroadcastGameList();
            Hub.Clients.Caller.updateBoard(game);
            Hub.Clients.Caller.gameJoined(game.GameId);
        }
        public GameResult(Game game)
        {
            DbEntity = new DbGameResult()
            {
                GameId = game.GameId,
                Rules = game.Rules,
                TradeRules = game.TradeRule,
                DateCreated = DateTime.Now,
                DateModified = DateTime.Now,
                CreatedBy = game.FirstPlayer.UserName,
                ModifiedBy = game.FirstPlayer.UserName,
                Resolved = false,
                Season = game.Season
            };

            Compute(game);
        }
        public void CreateGameWithAi()
        {
            var player = new Player() { User = Hub.Context.User, ConnectionId = Hub.Context.ConnectionId };
            player.CreatePlayHand();

            var ai = new AiPlayer(2, 4) { IsReady = true, ConnectionId = "ai_" + IdGenerator.GenerateId() };
            ai.CreatePlayHand();

            var game = new Game(player, Rules.Open, TradeRules.One, GameEnded);
            game.SecondPlayer = ai;
            game.AiMove = AiMove;

            Games.AddGame(game);

            Hub.Groups.Add(Hub.Context.ConnectionId, game.GameId);

            BroadcastGameList();
            Hub.Clients.Caller.updateBoard(game);
            Hub.Clients.Caller.gameJoined(game.GameId);
        }
        public void Compute(Game game)
        {
            var winner = game.Winner ?? game.CurrentPlayer;
            var defeated = game.GetOpponent(game.Winner ?? game.CurrentPlayer);

            double expMod = 0;
            if (forWinner)
            {
                expMod = winner.Hand.HandStrength - defeated.Hand.HandStrength;
            }
            else
            {
                expMod = defeated.Hand.HandStrength - winner.Hand.HandStrength;
            }

            if (expMod < 0)
            {
                expMod = 1 / Math.Abs(expMod);
            }

            if (game.FirstPlayerScore == game.SecondPlayerScore)
            {
                DbEntity.WinnerExpGain = (int)(winner.CardsFlip * 2 / expMod);
                DbEntity.DefeatedExpGain = (int)(defeated.CardsFlip * 2 / expMod);
                DbEntity.WinnerScore = game.FirstPlayerScore;
                DbEntity.WinnerHandStrength = game.FirstPlayer.Hand.HandStrength;
                DbEntity.DefeatedHandStrength = game.SecondPlayer.Hand.HandStrength;
            }
            else
            {
                DbEntity.Winner = game.Winner.DbEntity;
                DbEntity.WinnerHandStrength = game.Winner.Hand.HandStrength;
                DbEntity.Defeated = game.GetOpponent(game.Winner).DbEntity;
                DbEntity.DefeatedHandStrength = game.GetOpponent(game.Winner).Hand.HandStrength;
                DbEntity.WinnerScore = game.GetWinnerScore();

                DbEntity.WinnerCardPtsGain = Math.Abs(game.FirstPlayerScore - game.SecondPlayerScore);
                DbEntity.WinnerExpGain = (int)(winner.CardsFlip * 5 / expMod);
                DbEntity.DefeatedExpGain = (int)(defeated.CardsFlip / expMod);
            }

            DbEntity.WinnerCardsFlip = winner.CardsFlip;
            DbEntity.DefeatedCardsFlip = defeated.CardsFlip;
        }
        public void DeclareReady(Game game)
        {
            var player = game.GetCurrentPlayer(Hub.Context.ConnectionId);
            if (!player.IsReady)
            {
                player.IsReady = true;

                if (new Random().Next(0, 10) > 5)
                {
                    game.NextPlayer();
                }
            }

            Hub.Clients.Group(game.GameId).updateBoard(game);
        }
 public void RemoveGame(Game game)
 {
     Game removedGame;
     games.TryRemove(game.GameId, out removedGame);
 }
        private void ResumeGame(Game game)
        {
            game.ReconnectTimer.Stop();
            var player = game.GetCurrentPlayer(Hub.Context.ConnectionId);
            if (player != null)
                player.IsReady = true;

            Hub.Clients.Group(game.GameId).updateBoard(game);
            Hub.Clients.Group(game.GameId).playerConnected();
        }
 private Task AiMove(Game game)
 {
     return Task.Factory.StartNew(() =>
     {
         game.CurrentPlayer.Play(game, ((AiPlayer)game.CurrentPlayer).GetCommand(game));
         Hub.Clients.Group(game.GameId).updateBoard(game);
     });
 }
        private void GameEnded(Game game)
        {
            // TODO: if called by ai player error wont be handled by interceptor
            GameResult result = null;
            DbRepository.Transacted(() =>
            {
                result = new GameResult(game);
                DbRepository.Current.Add(result.DbEntity);
                game.FirstPlayer.UpdateStanding(result.GetFor(game.FirstPlayer));
                game.SecondPlayer.UpdateStanding(result.GetFor(game.SecondPlayer));
            });

            Games.RemoveGame(game);
            BroadcastGameList();
            Hub.Clients.Group(game.GameId).updateBoard(game);
            Hub.Clients.Caller.receiveResult(result.GetFor(game.GetCurrentPlayer(Hub.Context.ConnectionId)));
        }
        public void PlaceCard(string id, int position, Game game)
        {
            if (game.CurrentPlayer != game.GetCurrentPlayer(Hub.Context.ConnectionId))
            {
                Hub.Clients.Caller.receiveError("Wait for your turn");
                Hub.Clients.Caller.updateBoard(game);
                return;
            }

            game.CurrentPlayer.Play(game, new CardCommand(id, position));

            Hub.Clients.Group(game.GameId).updateBoard(game);
        }
 public void ResolveGame(string gameId, Game game)
 {
     Hub.Clients.Caller.gameResolve(new { });
 }
        public void LeaveGame(Game game)
        {
            Hub.Groups.Remove(Hub.Context.ConnectionId, game.GameId);

            var gameChanged = false;
            if (game.FirstPlayer.ConnectionId == Hub.Context.ConnectionId)
            {
                game.MakeOwner(game.SecondPlayer);

                if (game.FirstPlayer is AiPlayer)
                {
                    game.MakeOwner(null);
                }
                gameChanged = true;
            }
            else if (game.SecondPlayer != null && game.SecondPlayer.ConnectionId == Hub.Context.ConnectionId)
            {
                game.SecondPlayer = null;
                gameChanged = true;
            }

            if (gameChanged)
            {
                if (game.FirstPlayer == null && game.SecondPlayer == null)
                {
                    Games.RemoveGame(game);
                }

                BroadcastGameList();
                Hub.Clients.Group(game.GameId).updateBoard(game);
            }

            Hub.Clients.Caller.gameLeft();
        }
        public void JoinGame(string gameId, Game game)
        {
            Hub.Groups.Add(Hub.Context.ConnectionId, game.GameId);

            if (game.CanJoin)
            {
                if (game.FirstPlayer.ConnectionId != Hub.Context.ConnectionId)
                {
                    game.SecondPlayer = new Player()
                    {
                        User = Hub.Context.User,
                        ConnectionId = Hub.Context.ConnectionId
                    };
                    game.SecondPlayer.CreatePlayHand();

                    BroadcastGameList();
                    Hub.Clients.Group(game.GameId).updateBoard(game);
                    Hub.Clients.Caller.gameJoined(game.GameId);
                }
            }
            else
            {
                Hub.Clients.Caller.updateBoard(game);
                Hub.Clients.Caller.gameJoined(game.GameId);
            }
        }
 public void GetOwnedCards(Game game)
 {
     Hub.Clients.Caller.receiveWonCards(game.GetWonCards(game.Winner).ToList());
 }
 public CardCommand GetCommand(Game game)
 {
     mind.FocusOn(game);
     return mind.GetDecision();
 }
        private void HoldGame(Game game, int reconnectTimeInSec)
        {
            var player = game.GetCurrentPlayer(Hub.Context.ConnectionId);
            if (player != null)
                player.IsReady = false;
            game.ReconnectTimer.Interval = reconnectTimeInSec * 1000;
            game.ReconnectTimer.Start();

            Hub.Clients.Group(game.GameId).updateBoard(game);
            Hub.Clients.Group(game.GameId).playerDisconnected();
        }
 public void Play(Game game, CardCommand command)
 {
     game.PlaceCard(this.ConnectionId, command.CardId, command.Position);
 }
 public void AddGame(Game game)
 {
     games[game.GameId] = game;
 }