public async Task <GameStatusDto> GetGameStatusAsync() { await _distributedCache.GetStringAsync(cacheKey); string gameStatusString = await _distributedCache.GetStringAsync(cacheKey); if (string.IsNullOrEmpty(gameStatusString)) { string[,] randomBoard = new string[boardColumns, boardRows]; for (int i = 0; i < randomBoard.GetLength(0); i++) { for (int j = 0; j < randomBoard.GetLength(1); j++) { if (getBooleanRandom()) { randomBoard[i, j] = defaultCellColor; } else { randomBoard[i, j] = null; } } } GameStatusDto initialGame = new GameStatusDto(boardColumns, boardRows, randomBoard, 0); await SetGameStatusAsync(initialGame); return(initialGame); } GameStatusDto gameStatus = JsonConvert.DeserializeObject <GameStatusDto>(gameStatusString); return(gameStatus); }
public override void HandleServerStateChange(GameStatusDto state) { if (state is BeginningPuzzleStateDto) { Parent.TransitionToState(typeof(PuzzleStartingState)); } }
public override void HandleServerStateChange(GameStatusDto state) { base.HandleServerStateChange(state); if (state is BeginningPuzzleStateDto) { var beginningPuzzleState = (state as BeginningPuzzleStateDto); if (beginningPuzzleState.WinnerOfPreviousPuzzle != null) { if (beginningPuzzleState.WinnerOfPreviousPuzzle == Parent._player.Id) { Parent.GameState = MultiplayerGameState.PlayingRoundWonPuzzle; } else { Parent.GameState = MultiplayerGameState.PlayingRoundLostPuzzle; } } Parent.TransitionToState(typeof(PuzzleStartingState)); } else if (state is RoundCompletedStateDto) { Parent.TransitionToState(typeof(RoundEndedState)); } }
public ChangeGameStatusOutput EndGame(EndGameInput input) { #region Retrieving Game entity GameRepository.Includes.Add(r => r.GameStatus); GameRepository.Includes.Add(r => r.GameStatus.CreatorUser); GameRepository.Includes.Add(r => r.GameStatus.LastModifierUser); Game gameEntity = GameRepository.Get(input.GameId); if (gameEntity == null) { throw new CityQuestItemNotFoundException(CityQuestConsts.CityQuestItemNotFoundExceptionMessageBody, "\"Game\""); } GameRepository.Includes.Clear(); #endregion DateTime realGameEndTime = (DateTime.Now).RoundDateTime(); HandleCalculatingGameStatistics(input.GameId, gameEntity.StartDate, realGameEndTime); GameStatusDto newGameStatus = UpdateGameStatus(input.GameId, gameEntity, null, CityQuestConsts.GameStatusCompletedName); return(new ChangeGameStatusOutput() { NewGameStatus = newGameStatus }); }
private void PassStatusToStateHandler(GameStatusDto status) { LastState = status; if (_stateHandler != null) { _stateHandler.HandleServerStateChange(status); } }
public override void HandleServerStateChange(GameStatusDto state) { base.HandleServerStateChange(state); if (state is RoundStartingStateDto) { Parent.TransitionToState(typeof(GameStartingState)); } }
public ChangeGameStatusOutput ChangeGameStatus(ChangeGameStatusInput input) { GameStatusDto newGameStatus = UpdateGameStatus(input.GameId, null, input.NewGameStatusId, input.NewGameStatusName); return(new ChangeGameStatusOutput() { NewGameStatus = newGameStatus }); }
public async Task <GameStatusDto> ApplyLifeFormAsync(CreateLifeformResourceObject createLifeformResourceObject) { GameStatusDto currentGameStatus = await _gamestatusService.GetGameStatusAsync(); GameStatusDto nextGameStatus = new GameStatusDto(currentGameStatus.Columns, currentGameStatus.Rows, _lifeformFactory.createLifeform(createLifeformResourceObject.Name.ToUpperInvariant(), createLifeformResourceObject.Color, createLifeformResourceObject.Col, createLifeformResourceObject.Row, currentGameStatus.Board) , currentGameStatus.Generation); await _gamestatusService.SetGameStatusAsync(nextGameStatus); return(nextGameStatus); }
public async Task DoWorkAsync() { using (var scope = _services.CreateScope()) { IGamestatusService _gamestatusService = scope.ServiceProvider.GetRequiredService <IGamestatusService>(); IGameEvolutionService _gameEvolutionService = scope.ServiceProvider.GetRequiredService <IGameEvolutionService>(); GameStatusDto gameStatus = await _gamestatusService.GetGameStatusAsync(); GameStatusDto nextGeneration = _gameEvolutionService.Evolve(gameStatus); await _gamestatusService.SetGameStatusAsync(nextGeneration); if (_hubContext?.Clients?.All != null) { await _hubContext?.Clients?.All?.SendAsync("Notify", nextGeneration); } _logger.LogInformation("Timed Background Service is working."); } }
public Task Notify(GameStatusDto gameStatus) { return(Clients.All.SendAsync("GameStatus", gameStatus)); }
public GameStatusChangedMessage(long gameId, long newStatusId, GameStatusDto newGameStatus) { GameId = gameId; NewStatusId = newStatusId; NewGameStatus = newGameStatus; }
public async Task SetGameStatusAsync(GameStatusDto gameStatus) { await _distributedCache.SetStringAsync(cacheKey, JsonConvert.SerializeObject(gameStatus)); }
public GameStatusDto Evolve(GameStatusDto currentGameStatus) { uint nextGeneration = currentGameStatus.Generation + 1; string[,] nextBoard = (string[, ])currentGameStatus.Board.Clone(); int cols = currentGameStatus.Board.GetLength(0); int rows = currentGameStatus.Board.GetLength(1); for (int i = 0; i < currentGameStatus.Board.GetLength(0); i++) { for (int j = 0; j < currentGameStatus.Board.GetLength(1); j++) { int count = 0; SortedDictionary <string, int> colorCounterDictionary = new SortedDictionary <string, int>(); if (i > 0 && !string.IsNullOrEmpty(currentGameStatus.Board[i - 1, j])) { count++; increaseColorCounter(colorCounterDictionary, currentGameStatus.Board[i - 1, j]); } if (i > 0 && j > 0 && !string.IsNullOrEmpty(currentGameStatus.Board[i - 1, j - 1])) { count++; increaseColorCounter(colorCounterDictionary, currentGameStatus.Board[i - 1, j - 1]); } if (i > 0 && j < rows - 1 && !string.IsNullOrEmpty(currentGameStatus.Board[i - 1, j + 1])) { count++; increaseColorCounter(colorCounterDictionary, currentGameStatus.Board[i - 1, j + 1]); } if (j < rows - 1 && !string.IsNullOrEmpty(currentGameStatus.Board[i, j + 1])) { count++; increaseColorCounter(colorCounterDictionary, currentGameStatus.Board[i, j + 1]); } if (j > 0 && !string.IsNullOrEmpty(currentGameStatus.Board[i, j - 1])) { count++; increaseColorCounter(colorCounterDictionary, currentGameStatus.Board[i, j - 1]); } if (i < cols - 1 && !string.IsNullOrEmpty(currentGameStatus.Board[i + 1, j])) { count++; increaseColorCounter(colorCounterDictionary, currentGameStatus.Board[i + 1, j]); } if (i < cols - 1 && j > 0 && !string.IsNullOrEmpty(currentGameStatus.Board[i + 1, j - 1])) { count++; increaseColorCounter(colorCounterDictionary, currentGameStatus.Board[i + 1, j - 1]); } if (i < cols - 1 && j < rows - 1 && !string.IsNullOrEmpty(currentGameStatus.Board[i + 1, j + 1])) { count++; increaseColorCounter(colorCounterDictionary, currentGameStatus.Board[i + 1, j + 1]); } if (!string.IsNullOrEmpty(currentGameStatus.Board[i, j]) && (count < 2 || count > 3)) { nextBoard[i, j] = null; } if (string.IsNullOrEmpty(currentGameStatus.Board[i, j]) && count == 3) { //neighbour's most common color nextBoard[i, j] = colorCounterDictionary.Keys.First(); } } } GameStatusDto nextGameStatus = new GameStatusDto(currentGameStatus.Columns, currentGameStatus.Rows, nextBoard, nextGeneration); return(nextGameStatus); }
public virtual void HandleServerStateChange(GameStatusDto state) { }
/// <summary> /// Is used to update GameStatus for Game entity /// </summary> /// <param name="gameId">game's Id (not required if gameEntity parameter inputed)</param> /// <param name="gameEntity">game's entity (not required if gameId parameter inputed)</param> /// <param name="newGameStatusId">new GameStatus's Id (not required if newGameStatusName parameter inputed)</param> /// <param name="newGameStatusName">new GameStatus's Name (not required if newGameStatusId parameter inputed)</param> /// <returns>new game's status dto</returns> private GameStatusDto UpdateGameStatus(long?gameId, Game gameEntity, long?newGameStatusId, string newGameStatusName) { if (gameEntity == null && gameId != null) { #region Retrieving Game entity GameRepository.Includes.Add(r => r.GameStatus); GameRepository.Includes.Add(r => r.GameStatus.CreatorUser); GameRepository.Includes.Add(r => r.GameStatus.LastModifierUser); gameEntity = GameRepository.Get((long)gameId); GameStatusRepository.Includes.Clear(); #endregion } if (gameEntity == null) { throw new CityQuestItemNotFoundException(CityQuestConsts.CityQuestItemNotFoundExceptionMessageBody, "\"Game\""); } GameStatusDto newGameStatusDto = gameEntity.GameStatus.MapTo <GameStatusDto>(); if (gameEntity.GameStatusId != newGameStatusId) { GameStatusRepository.Includes.Add(r => r.CreatorUser); GameStatusRepository.Includes.Add(r => r.LastModifierUser); GameStatus newGameStatus; try { newGameStatus = newGameStatusId != null?GameStatusRepository.Get((long)newGameStatusId) : GameStatusRepository.Single(r => r.Name == newGameStatusName); } catch { throw new UserFriendlyException("Wrong game status!", "Trying to get wrong game status! Please, contact your system administrator."); } if (!GamePolicy.CanChangeStatusForEntity(gameEntity, gameEntity.GameStatus, newGameStatus)) { throw new CityQuestPolicyException(CityQuestConsts.CQPolicyExceptionChangeStatusDenied, "\"Game\""); } gameEntity.GameStatusId = newGameStatus.Id; gameEntity.GameStatus = null; GameRepository.Update(gameEntity); newGameStatusDto = newGameStatus.MapTo <GameStatusDto>(); UowManager.Current.Completed += (sender, e) => { GameChangesNotifier.RaiseOnGameStatusChanged( new GameStatusChangedMessage(gameEntity.Id, newGameStatus.Id, newGameStatusDto)); }; } return(newGameStatusDto); }