示例#1
0
        public async Task LeaveGame(string gameCode)
        {
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, gameCode);

            Game game   = GameStore.GetGame(gameCode);
            var  player = game.Players.FirstOrDefault(e => e.ConnectionId == Context.ConnectionId);

            if (player == null || game.Status == GameStatus.Completed)
            {
                return;
            }
            if (game.Status == GameStatus.NotStarted)
            {
                game.Players.Remove(player);
                if (game.Players.Count == 0)
                {
                    GameStore.RemoveGame(gameCode);
                    await Clients.Group(GameList).GameListUpdated(GameStore.GetGames());
                }

                return;
            }

            player.ChallengesRemaining = 0;
            if (game.Players.Count == 1)
            {
                game.Status = GameStatus.Completed;
            }
        }