public async Task <Unit> Handle(UpdatePlayerStatusCommand request, CancellationToken cancellationToken) { var player = await _context.Players .FindByClientIdAsync(request.PlayerClientId); if (player == null) { throw new NotFoundException("Player", request.PlayerClientId); } if (player.Status == request.NewStatus) { return(Unit.Value); } if (request.NewStatus == PlayerStatus.Left) { // TODO } player.Status = request.NewStatus; await _context.SaveChangesAsync(cancellationToken); var @event = new PlayerUpdatedEvent(false, player.GameId, player.Id); await _mediator.Publish(@event, cancellationToken); return(Unit.Value); }
public async Task <JoinGameResponse> Handle(JoinGameCommand request, CancellationToken cancellationToken) { var game = _context.Games.First(g => g.JoinCode.Equals(request.JoinCode, StringComparison.InvariantCultureIgnoreCase)); if (game.Status != GameStatus.Lobby) { throw new InvalidStatusException(game.Status); } var playerName = _randomNameService.CreatePlayerName(); var player = new GamePlayer { Name = playerName }; game.Players.Add(player); await _context.SaveChangesAsync(cancellationToken); var @event = new PlayerUpdatedEvent(true, player.GameId, player.Id); await _mediator.Publish(@event, cancellationToken); return(new JoinGameResponse { GameClientId = game.ClientId, PlayerClientId = player.ClientId, PlayerId = player.Id }); }
/// <summary> /// Publish a player updated event for any listeners /// </summary> /// <param name="player"></param> /// <param name="notifyPeers">True if we should notify peers of this player data</param> public static void PublishPlayerUpdatedEvent(PlayerData player, bool notifyPeers = false) { PlayerUpdatedEvent?.Invoke(player); if (notifyPeers) { RPC.Instance.SendPlayerUpdated(player); } }
public async Task <Unit> Handle(UpdatePlayerCommand request, CancellationToken cancellationToken) { var player = _context.Players.FindByClientId(request.ClientId); player.Name = request.Name; player.Image = request.Image; await _context.SaveChangesAsync(cancellationToken); var @event = new PlayerUpdatedEvent(false, player.GameId, player.Id); await _mediator.Publish(@event, cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(RemovePlayerCommand request, CancellationToken cancellationToken) { var(game, _) = await _context.Games .Include(g => g.Players) .Include(g => g.Categories) .FindByClientIdAsync(request.GameClientId, request.PlayerClientId, mustBeOwner: true); var player = game.Players.FirstOrDefault(p => p.Id == request.PlayerId); if (player == null) { throw new NotFoundException(nameof(GamePlayer), request.PlayerId); } _context.Players.Remove(player); await _context.SaveChangesAsync(cancellationToken); var @event = new PlayerUpdatedEvent(false, game.Id, player.Id); await _mediator.Publish(@event, cancellationToken); return(Unit.Value); }