예제 #1
0
        private static bool MyColor(GameManager manager, Db.User dbUser, PlayerColor color)
        {
            //prevents someone with same game id, get someone elses side in the game.
            var player = manager.Game.BlackPlayer;

            if (color == PlayerColor.white)
            {
                player = manager.Game.WhitePlayer;
            }

            return(dbUser != null && dbUser.Id == player.Id);
        }
예제 #2
0
        private static async Task ConnectInvite(WebSocket webSocket, Db.User dbUser, string gameInviteId)
        {
            var manager = AllGames.SingleOrDefault(g => g.Game.Id.ToString() == gameInviteId && (g.Client1 == null || g.Client2 == null));

            if (manager == null)
            {
                await webSocket.CloseAsync(WebSocketCloseStatus.InvalidPayloadData, "Invite link has expired", CancellationToken.None);

                return;
            }

            var color = Player.Color.Black;

            if (manager.Client1 != null)
            {
                color = Player.Color.White;
            }

            await manager.ConnectAndListen(webSocket, color, dbUser, false);

            RemoveDissconnected(manager);
            await SendConnectionLost(PlayerColor.white, manager);
        }
예제 #3
0
 internal async Task ConnectAndListen(WebSocket webSocket, Player.Color color, Db.User dbUser, bool playAi)
 {
     if (color == Player.Color.Black)
     {
         Client1               = webSocket;
         Game.BlackPlayer.Id   = dbUser != null ? dbUser.Id : Guid.Empty;
         Game.BlackPlayer.Name = dbUser != null ? dbUser.Name : "Guest";
         if (playAi)
         {
             var aiUser = Db.BgDbContext.GetDbUser(Db.User.AiUser);
             Game.WhitePlayer.Id   = aiUser.Id;
             Game.WhitePlayer.Name = aiUser.Name;
             Engine = new Ai.Engine(Game);
             CreateDbGame();
             StartGame();
             if (Game.CurrentPlayer == Player.Color.White)
             {
                 await EnginMoves(Client1);
             }
         }
         await ListenOn(webSocket);
     }
     else
     {
         if (playAi)
         {
             throw new ApplicationException("Ai always playes as white. This is not expected");
         }
         Client2               = webSocket;
         Game.WhitePlayer.Id   = dbUser != null ? dbUser.Id : Guid.Empty;
         Game.WhitePlayer.Name = dbUser != null ? dbUser.Name : "Guest";
         CreateDbGame();
         StartGame();
         await ListenOn(webSocket);
     }
 }
예제 #4
0
        internal async Task ConnectAndListen(WebSocket webSocket, Player.Color color, Db.User dbUser, bool playAi)
        {
            if (color == Player.Color.Black)
            {
                Client1                = webSocket;
                Game.BlackPlayer.Id    = dbUser != null ? dbUser.Id : Guid.Empty;
                Game.BlackPlayer.Name  = dbUser != null ? dbUser.Name : "Guest";
                Game.BlackPlayer.Photo = dbUser != null && dbUser.ShowPhoto ? dbUser.PhotoUrl : "";
                Game.BlackPlayer.Elo   = dbUser != null ? dbUser.Elo : 0;
                if (Game.IsGoldGame)
                {
                    Game.BlackPlayer.Gold = dbUser != null ? dbUser.Gold - firstBet : 0;
                    Game.Stake            = firstBet * 2;
                }

                if (playAi)
                {
                    var aiUser = Db.BgDbContext.GetDbUser(Db.User.AiUser);
                    Game.WhitePlayer.Id   = aiUser.Id;
                    Game.WhitePlayer.Name = aiUser.Name;
                    // TODO: AI image
                    Game.WhitePlayer.Photo = aiUser.PhotoUrl;
                    Game.WhitePlayer.Elo   = aiUser.Elo;
                    if (Game.IsGoldGame)
                    {
                        Game.WhitePlayer.Gold = aiUser.Gold;
                    }
                    Engine = new Ai.Engine(Game);
                    CreateDbGame();
                    StartGame();
                    if (Game.CurrentPlayer == Player.Color.White)
                    {
                        await EnginMoves(Client1);
                    }
                }
                await ListenOn(webSocket);
            }
            else
            {
                if (playAi)
                {
                    throw new ApplicationException("Ai always plays as white. This is not expected");
                }
                Client2                = webSocket;
                Game.WhitePlayer.Id    = dbUser != null ? dbUser.Id : Guid.Empty;
                Game.WhitePlayer.Name  = dbUser != null ? dbUser.Name : "Guest";
                Game.WhitePlayer.Photo = dbUser != null && dbUser.ShowPhoto ? dbUser.PhotoUrl : "";
                Game.WhitePlayer.Elo   = dbUser != null ? dbUser.Elo : 0;
                if (Game.IsGoldGame)
                {
                    Game.WhitePlayer.Gold = dbUser != null ? dbUser.Gold - firstBet : 0;
                }
                CreateDbGame();
                StartGame();
                await ListenOn(webSocket);
            }
        }
예제 #5
0
        private static async Task <bool> TryReConnect(WebSocket webSocket, HttpContext context, ILogger <GameManager> logger, Db.User dbUser)
        {
            var cookies   = context.Request.Cookies;
            var cookieKey = "backgammon-game-id";

            // Find existing game to reconnect to.
            if (cookies.Any(c => (c.Key == cookieKey)))
            {
                var cookie = GameCookieDto.TryParse(cookies[cookieKey]);
                var color  = cookie.color;

                if (cookie != null)
                {
                    var gameManager = AllGames
                                      .SingleOrDefault(g =>
                                                       g.Game.Id.ToString().Equals(cookie.id) &&
                                                       g.Game.PlayState != Game.State.Ended
                                                       );

                    if (gameManager != null && MyColor(gameManager, dbUser, color))
                    {
                        gameManager.Engine = new Ai.Engine(gameManager.Game);
                        logger.LogInformation($"Restoring game {cookie.id} for {color}");
                        // entering socket loop
                        await gameManager.Restore(color, webSocket);

                        var otherColor = color == PlayerColor.black ?
                                         PlayerColor.white : PlayerColor.black;
                        await SendConnectionLost(otherColor, gameManager);

                        // socket loop exited
                        RemoveDissconnected(gameManager);
                        return(true);
                    }
                }
            }
            return(false);
        }