예제 #1
0
        public GameInfo CreateGame(RequestedGame r)
        {
            Game game = new Game();

            Chess chess = new Chess();

            game.FEN    = chess.fen;
            game.Status = "wait";

            if (r.playerColor == "white")
            {
                game.White_ID = r.playerID;
            }

            if (r.playerColor == "black")
            {
                game.Black_ID = r.playerID;
            }

            db.Games.Add(game);
            db.SaveChanges();

            GameInfo gameInfo = new GameInfo();

            gameInfo.gameID = game.ID;
            gameInfo.FEN    = game.FEN;
            return(gameInfo);
        }
예제 #2
0
        public GameInfo GetGame(RequestedGame r)
        {
            /* matchmaking
             * 1. enumerates all games with "wait" status and returns the first
             * 2. we're looking for a game with "white" or "black" empty slot
             * 3. we can reconnect to our game after a while */
            Game game = db
                        .Games
                        .OrderByDescending(g => g.ID)
                        .Where(g =>
                               // new connect
                               (g.Status == "wait" && ((r.playerColor == "white" && g.White_ID == 0) ||
                                                       (r.playerColor == "black" && g.Black_ID == 0)))
                               || // or reconnect
                               (g.Status == "play" && ((r.playerColor == "white" && g.White_ID == r.playerID) ||
                                                       (r.playerColor == "black" && g.Black_ID == r.playerID))))
                        .FirstOrDefault();

            if (game == null)
            {
                return(null);
            }

            return(JoinGame(game, r));
        }
예제 #3
0
        GameInfo JoinGame(Game game, RequestedGame r)
        {
            if (r.playerColor == "white")
            {
                game.White_ID = r.playerID;
            }

            if (r.playerColor == "black")
            {
                game.Black_ID = r.playerID;
            }
            game.Status = "play";

            db.Entry(game).State = System.Data.Entity.EntityState.Modified;// we commit updates to database
            db.SaveChanges();

            GameInfo gameInfo = new GameInfo();

            gameInfo.gameID = game.ID;
            gameInfo.FEN    = game.FEN;
            return(gameInfo);
        }