Пример #1
0
        public IResponse <GameInstance> Play(PlayGameInstanceDto dto)
        {
            var gameInstance = Get(dto.Id);
            var gameStrategy = GameStrategyProvider.Provide(gameInstance.GameId);

            var newGameStateAsJsonStringResponse = gameStrategy != null?gameStrategy.LoadAndPlayAndReturnGameStateAsString(gameInstance, RequestContext.UserId, dto.UserInput) : new Response <string>
            {
                Data = ""
            };
            var response = ResponseMapper.MapMetadata <GameInstance>(newGameStateAsJsonStringResponse);

            if (!response.IsValid || response.Messages.Any(x => x.Contains("wins!")))
            {
                return(response);
            }

            var newGameState = CreateGameStateObject(newGameStateAsJsonStringResponse.Data);

            response.Data = Save(gameInstance, newGameState);

            if (gameStrategy != null)
            {
                SaveGameStateHistory(response.Data.Id, gameInstance.State, $"User Input: {dto.UserInput}");
            }

            return(response);
        }
Пример #2
0
        public IResponse <string> Validate(PlayGameInstanceDto dto)
        {
            var response = new Response <string>();

            var gameInstance = GameInstanceLogic.Get(dto.Id);

            if (gameInstance == null || !gameInstance.Users.Any(x => x.Role == GameInstanceRoles.Player.ToString() && x.UserId == RequestContext.UserId))
            {
                response.Errors.Add("That game is invalid.");
            }
            if (string.IsNullOrWhiteSpace(dto.UserInput))
            {
                response.Errors.Add("User input is invalid.");
            }
            return(response);
        }
Пример #3
0
        public async Task <ActionResult <IResponse <object> > > PlayAsync(PlayGameInstanceDto dto)
        {
            var validationResponse = GameInstanceValidator.Validate(dto);

            if (!validationResponse.IsValid)
            {
                return(BadRequest(validationResponse));
            }

            var gameInstanceResponse = GameInstanceLogic.Play(dto);

            var response = ResponseMapper.MapMetadata <GameInstanceDto>(gameInstanceResponse);

            response.Data = GameInstanceMapper.Map(GameInstanceLogic.Get(dto.Id));
            if (response.IsValid)
            {
                await GameHubContext.Clients.Group(dto.Id.ToString()).SendAsync("Refresh");
            }
            return(Ok(response));
        }