Exemplo n.º 1
0
        public static async Task SaveEntityAsync <T>(this GameContext context, T entity) where T : Entity
        {
            if (entity.Id == 0)
            {
                await context.AddAsync(entity);
            }
            else
            {
                context.Update(entity);
            }

            await context.SaveChangesAsync();
        }
Exemplo n.º 2
0
        public async Task <string> CreateNewGame(string name, string id)
        {
            var roomCode = await MakeRoomCode();

            var gameState = new GameState(roomCode);

            gameState.Players.Add(new Player
            {
                Id      = id,
                Name    = name,
                SeatNum = 1
            });
            await _gameContext.AddAsync(gameState);

            return(roomCode);
        }
Exemplo n.º 3
0
        private async Task ManualPlay(string userName, GameMove move)
        {
            var game = await GetGame();

            if (game.Status != GameStatus.Playing)
            {
                throw new InvalidOperationException("Game not started yet!");
            }

            if (game.NextPlayer != userName)
            {
                throw new InvalidOperationException("Not your turn!");
            }

            move.ColorInString = GetColorInstring(userName, game);
            await _context.AddAsync(move);

            game.NextPlayer = GetNextPlayerName(userName, game);
            if (await Judge(move))
            {
                game.Status = move.ColorInString == "black" ? GameStatus.BlackSideWon : GameStatus.WhiteSideWon;
            }

            await _context.SaveChangesAsync();

            var message = new WebSocketMessage()
            {
                Type    = "GameMove",
                Payload = move
            };
            await _socket.BroadcastMessage(message);

            message = new WebSocketMessage()
            {
                Type    = "Game",
                Payload = game
            };
            await _socket.BroadcastMessage(message);
        }