/// <summary> /// Handle player disconnections /// </summary> /// <param name="exception"></param> /// <returns></returns> public override async Task OnDisconnectedAsync(Exception exception) { var clientGames = _gameService.GetGamesWithPlayer(Context.UserIdentifier); foreach (var gameId in clientGames) { if (_gameService.TryGetGame(gameId, out var game)) { game.SetPlayerOnlineStatus(Context.UserIdentifier, false); await Clients.Group(game.Id).SendAsync("player_left", Context.UserIdentifier); if (game.ShouldPauseGame()) { await Clients.Group(game.Id).SendAsync("pause_game", game.PauseReason); } if (game.State == GameStatus.Cancelled) { await Clients.Group(game.Id).SendAsync("game_cancelled"); } if (game.OnlinePlayerCount == 0) { _gameService.RemoveGame(game.Id); } } } await base.OnDisconnectedAsync(exception); }
public IActionResult JoinGame([FromRoute(Name = "id")] string id, [FromBody] JoinGamePost postData) { if (!_gameService.TryGetGame(postData.Id, out var game)) { return(NotFound()); } if (game.Passcode != postData.Code) { return(Forbid()); } var player = new Player(User.FindFirst("id").Value, User.FindFirst(ClaimTypes.Name).Value); game.AddPlayer(player.Id, player); return(Ok()); }