Example #1
0
            public async Task <Unit> Handle(PlayTurnCommand request, CancellationToken cancellationToken)
            {
                var round = new Round
                {
                    Card     = (Card)request.Card,
                    Next     = (Coordinate)request.NextAnnotation,
                    Previous = (Coordinate)request.PreviousAnnotation,
                    LobbyId  = request.LobbyId
                };

                _onitamaDbContext.Round.Add(round);

                var lobby = await _onitamaDbContext.Lobby.FirstOrDefaultAsync(lobby => lobby.LobbyId == request.LobbyId);

                await _onitamaDbContext.SaveChangesAsync();

                var turnPlayedEvent = new TurnPlayedEvent
                {
                    Round      = round,
                    LobbyId    = request.LobbyId,
                    PlayerTurn = _turnLogic.GetPlayerTurn(lobby)
                };

                await _mediator.Publish(turnPlayedEvent);

                return(Unit.Value);
            }
Example #2
0
        public async Task Handle(PlayerConnectedEvent notification, CancellationToken cancellationToken)
        {
            var player = new Domain.Entities.Player
            {
                Connected  = true,
                Identifier = notification.Identifier,
                Name       = notification.Player
            };

            _onitamaDbContext.Player.Add(player);
            await _onitamaDbContext.SaveChangesAsync();

            await _mediator.Publish(new LobbyJoinedEvent { Player = player });
        }
        public async Task Handle(PlayerDisconnectedEvent notification, CancellationToken cancellationToken)
        {
            var disconnectedPlayer = _onitamaDbContext.Player.FirstOrDefault(player => player.Identifier == notification.Identifier);

            if (disconnectedPlayer != null)
            {
                disconnectedPlayer.Connected = false;
                var lobby = await _onitamaDbContext.Lobby
                            .Include(l => l.Players)
                            .Where(lobby => lobby.Players.Any(player => player.PlayerId == disconnectedPlayer.PlayerId))
                            .FirstOrDefaultAsync();

                await _onitamaDbContext.SaveChangesAsync();

                await _mediator.Publish(new LobbyLeaveEvent { Player = disconnectedPlayer, LobbyId = lobby?.LobbyId ?? -1 });
            }
        }
Example #4
0
            public async Task <CreateGameResponse> Handle(CreateGameCommand request, CancellationToken cancellationToken)
            {
                var lobby = new Domain.Entities.Lobby();

                _onitamaDbContext.Lobby.Add(lobby);

                lobby.RedCards    = request?.RedCard.Select(card => (Card)card).ToList();
                lobby.BlueCards   = request?.BlueCard.Select(card => (Card)card).ToList();
                lobby.NeutralCard = (Card)request.NeutralCard;
                await _onitamaDbContext.SaveChangesAsync();

                var notification = new LobbyCreatedEvent
                {
                    LobbyId = lobby.LobbyId
                };

                await _mediator.Publish(notification);

                return(new CreateGameResponse
                {
                    LobbyId = lobby.LobbyId
                });
            }