public async Task StartGame(GenericGameCmd cmd) { var gameEntity = await _repository.GetGame(cmd.GameId); var game = gameEntity.ToModel(); game.Start(shufflePlayers: true); await _repository.ReplaceGame(GameEntity.Create(game)); _inprogressGameStore.Add(game); var startedEvent = new GameStartedEvent { Game = GameSummaryDto.Create(game) }; await Clients.Group(LobbyGroup).GameStarted(startedEvent); }
public async Task <Summary> GenerateAsync(int gameId) { var game = await _gameRepository.GetFullGameDataById(gameId); var gameSummaryDto = new GameSummaryDto(game); string jsonResult = JsonSerializer.Serialize(gameSummaryDto); var summary = new Summary { GameId = gameId, Result = jsonResult }; var existingSummary = _summaryRepository.GetFirstByFilterAsync(s => s.GameId == gameId); if (existingSummary != null) { return(await _summaryRepository.UpdateAsync(summary)); } return(await _summaryRepository.AddAsync(summary)); }
public override async Task OnConnectedAsync() { await Groups.AddToGroupAsync(Context.ConnectionId, LobbyGroup); var games = await _repository.GetStartedGamesByPlayer(GetUserId()); foreach (var game in games) { await Clients.Caller.GameStarted(new GameStartedEvent { Game = GameSummaryDto.Create(game.ToModel()) }); if (game.Status == GameStatus.ENDED) { var endedEvent = new GameEndedEvent { GameId = game.Id, Results = game.Players.Select(p => new PlayerResultDto { Player = PlayerDto.Create(p), Result = p.Result ?? 0 }) }; await Clients.Caller.GameEnded(endedEvent); } } var lobbyGames = await _repository.GetLobbyGames(); foreach (var lobbyGame in lobbyGames) { var gameCreatedEvent = new GameCreatedEvent { GameId = lobbyGame.Id, GameName = lobbyGame.Name, Players = lobbyGame.Players.Select(PlayerDto.Create), }; await Clients.Caller.GameCreated(gameCreatedEvent); } }