public override async Task <SubmitGameEventResponse> UpdateGameEvent(UpdateGameEventRequest request, ServerCallContext context) { DbUserModel dbUserModel = context.UserState["user"] as DbUserModel; if (dbUserModel == null) { return new SubmitGameEventResponse() { Status = ResponseFactory.createResponse(ResponseType.UNAUTHORIZED) } } ; Room room = await Room.GetRoomFromGuid(request.RoomId); if (room == null) { return new SubmitGameEventResponse() { Status = ResponseFactory.createResponse(ResponseType.ROOM_DOES_NOT_EXIST) } } ; // GameEventToUpdate. return(await room.UpdateGameEvent(dbUserModel, request)); }
public async Task <SubmitGameEventResponse> UpdateGameEvent(DbUserModel player, UpdateGameEventRequest request) { GameEventModel gameEvent = await GetGameEventFromGuid(request.EventId); if (gameEvent == null) { return(new SubmitGameEventResponse() { Status = ResponseFactory.createResponse(ResponseType.GAME_EVENT_DOES_NOT_EXIST) }); } if (gameEvent.IssuedBy != player.UserModel.Id) { return(new SubmitGameEventResponse() { Status = ResponseFactory.createResponse(ResponseType.PERMISSION_DENIED) }); } // Ensure the event happens after current time. GameTick currentTick = new GameTick(DateTime.FromFileTimeUtc(GameConfiguration.UnixTimeStarted), DateTime.UtcNow); if (request.EventData.OccursAtTick <= currentTick.GetTick()) { return(new SubmitGameEventResponse() { Status = ResponseFactory.createResponse(ResponseType.INVALID_REQUEST) }); } // TODO: validate event. // Overwrite data with request data. gameEvent.EventData = request.EventData.EventData; gameEvent.OccursAtTick = request.EventData.OccursAtTick; MongoConnector.GetGameEventCollection().ReplaceOne((it => it.RoomId == GameConfiguration.Id), new GameEventModelMapper(gameEvent)); return(new SubmitGameEventResponse() { Status = ResponseFactory.createResponse(ResponseType.SUCCESS), EventId = gameEvent.Id, }); }
public async Task <SubmitGameEventResponse> UpdateGameEvent(RedisUserModel player, UpdateGameEventRequest request) { GameEventModel gameEvent = await GetGameEventFromGuid(request.EventId); if (gameEvent == null) { return(new SubmitGameEventResponse() { Status = ResponseFactory.createResponse(ResponseType.GAME_EVENT_DOES_NOT_EXIST) }); } if (gameEvent.IssuedBy != player.UserModel.Id) { return(new SubmitGameEventResponse() { Status = ResponseFactory.createResponse(ResponseType.PERMISSION_DENIED) }); } // Ensure the event happens after current time. GameTick currentTick = new GameTick(DateTime.FromFileTimeUtc(RoomModel.UnixTimeStarted), DateTime.UtcNow); if (request.EventData.OccursAtTick <= currentTick.GetTick()) { return(new SubmitGameEventResponse() { Status = ResponseFactory.createResponse(ResponseType.INVALID_REQUEST) }); } // TODO: validate event. gameEvent.EventData = request.EventData.EventData; gameEvent.OccursAtTick = request.EventData.OccursAtTick; HashEntry[] entries = new[] { new HashEntry(gameEvent.EventId, gameEvent.ToByteArray()), }; await RedisConnector.Redis.HashSetAsync(GameEventsKey(), entries); await RedisConnector.Redis.SetAddAsync(UserGameEventsKey(player), gameEvent.EventId); return(new SubmitGameEventResponse() { Status = ResponseFactory.createResponse(ResponseType.SUCCESS), EventId = gameEvent.EventId, }); }