コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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;
        }
コード例 #5
0
        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);
        }
コード例 #6
0
 public void RemoveGame(Game game)
 {
     Game removedGame;
     games.TryRemove(game.GameId, out removedGame);
 }
コード例 #7
0
        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();
        }
コード例 #8
0
 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);
     });
 }
コード例 #9
0
        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)));
        }
コード例 #10
0
        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);
        }
コード例 #11
0
 public void ResolveGame(string gameId, Game game)
 {
     Hub.Clients.Caller.gameResolve(new { });
 }
コード例 #12
0
        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();
        }
コード例 #13
0
        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);
            }
        }
コード例 #14
0
 public void GetOwnedCards(Game game)
 {
     Hub.Clients.Caller.receiveWonCards(game.GetWonCards(game.Winner).ToList());
 }
コード例 #15
0
 public CardCommand GetCommand(Game game)
 {
     mind.FocusOn(game);
     return mind.GetDecision();
 }
コード例 #16
0
        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();
        }
コード例 #17
0
 public void Play(Game game, CardCommand command)
 {
     game.PlaceCard(this.ConnectionId, command.CardId, command.Position);
 }
コード例 #18
0
 public void AddGame(Game game)
 {
     games[game.GameId] = game;
 }