コード例 #1
0
        public async Task <SubmitGameEventResponse> AddPlayerGameEvent(RedisUserModel player, GameEventRequest request)
        {
            GameEventModel eventModel = toGameEventModel(player, request);

            // Ensure the event happens after current time.
            GameTick currentTick = new GameTick(DateTime.FromFileTimeUtc(RoomModel.UnixTimeStarted), DateTime.UtcNow);

            if (eventModel.OccursAtTick <= currentTick.GetTick())
            {
                return(new SubmitGameEventResponse()
                {
                    Status = ResponseFactory.createResponse(ResponseType.INVALID_REQUEST),
                });;
            }

            // TODO: validate event.

            HashEntry[] entries = new[]
            {
                new HashEntry(eventModel.EventId, eventModel.ToByteArray()),
            };

            await RedisConnector.Redis.HashSetAsync(GameEventsKey(), entries);

            await RedisConnector.Redis.SetAddAsync(UserGameEventsKey(player), eventModel.EventId);

            return(new SubmitGameEventResponse()
            {
                Status = ResponseFactory.createResponse(ResponseType.SUCCESS),
                EventId = eventModel.EventId,
            });
        }
コード例 #2
0
        public void GetNextTick()
        {
            GameTick nextTick = _tick.GetNextTick();

            Assert.AreEqual(_time.AddMinutes(GameTick.MinutesPerTick), nextTick.GetDate());
            Assert.AreEqual(_tickNumber + 1, nextTick.GetTick());
        }
コード例 #3
0
        public void GetNextTick()
        {
            GameTick nextTick = _tick.GetNextTick();

            Assert.AreEqual(_time.AddMinutes(GameTick.MINUTES_PER_TICK), nextTick.GetDate(_time));
            Assert.AreEqual(_tickNumber + 1, nextTick.GetTick());
        }
コード例 #4
0
        public async Task <SubmitGameEventResponse> AddPlayerGameEvent(DbUserModel player, GameEventRequest request)
        {
            GameEventModel eventModel = toGameEventModel(player, request);

            // Ensure the event happens after current time.
            GameTick currentTick = new GameTick(DateTime.FromFileTimeUtc(GameConfiguration.UnixTimeStarted), DateTime.UtcNow);

            if (eventModel.OccursAtTick <= currentTick.GetTick())
            {
                return(new SubmitGameEventResponse()
                {
                    Status = ResponseFactory.createResponse(ResponseType.INVALID_REQUEST),
                });;
            }

            // TODO: validate event.

            await MongoConnector.GetGameEventCollection().InsertOneAsync(new GameEventModelMapper(eventModel));

            return(new SubmitGameEventResponse()
            {
                Status = ResponseFactory.createResponse(ResponseType.SUCCESS),
                EventId = eventModel.Id,
            });
        }
コード例 #5
0
        public async Task <DeleteGameEventResponse> RemovePlayerGameEvent(RedisUserModel player, string eventId)
        {
            Guid parsedGuid;

            try
            {
                parsedGuid = Guid.Parse(eventId);
            }
            catch (FormatException)
            {
                return(new DeleteGameEventResponse()
                {
                    Status = ResponseFactory.createResponse(ResponseType.GAME_EVENT_DOES_NOT_EXIST)
                });
            }

            // Get the event to check some things...
            RedisValue eventData = await RedisConnector.Redis.HashGetAsync(GameEventsKey(), parsedGuid.ToString());

            if (!eventData.HasValue)
            {
                return(new DeleteGameEventResponse()
                {
                    Status = ResponseFactory.createResponse(ResponseType.GAME_EVENT_DOES_NOT_EXIST)
                });;
            }

            // Determine if the event has already passed.
            GameEventModel gameEvent   = GameEventModel.Parser.ParseFrom(eventData);
            GameTick       currentTick = new GameTick(DateTime.FromFileTimeUtc(RoomModel.UnixTimeStarted), DateTime.UtcNow);

            if (gameEvent.OccursAtTick <= currentTick.GetTick())
            {
                return(new DeleteGameEventResponse()
                {
                    Status = ResponseFactory.createResponse(ResponseType.INVALID_REQUEST)
                });
            }

            if (gameEvent.IssuedBy != player.UserModel.Id)
            {
                return(new DeleteGameEventResponse()
                {
                    Status = ResponseFactory.createResponse(ResponseType.PERMISSION_DENIED)
                });
            }

            // Remove game Event
            await RedisConnector.Redis.SetRemoveAsync(UserGameEventsKey(player), parsedGuid.ToString());

            await RedisConnector.Redis.HashDeleteAsync(GameEventsKey(), parsedGuid.ToString());

            return(new DeleteGameEventResponse()
            {
                Status = ResponseFactory.createResponse(ResponseType.SUCCESS),
            });
        }
コード例 #6
0
        public void Advance()
        {
            // Check advancing N ticks
            int      ticksToAdvance = 10;
            GameTick tenMoreTicks   = _tick.Advance(ticksToAdvance);

            Assert.AreEqual(_time.AddMinutes(GameTick.MinutesPerTick * ticksToAdvance), tenMoreTicks.GetDate());
            Assert.AreEqual(_tickNumber + ticksToAdvance, tenMoreTicks.GetTick());
        }
コード例 #7
0
        public void FromTick()
        {
            int      numberOfTicks = 4;
            int      minutes       = GameTick.MinutesPerTick * numberOfTicks;
            DateTime newDate       = _time.AddMinutes(minutes);
            GameTick newTick       = GameTick.FromTickNumber(numberOfTicks);

            Assert.AreEqual(_tick.GetTick() + numberOfTicks, newTick.GetTick());
            Assert.AreEqual(newDate, newTick.GetDate());
        }
コード例 #8
0
        public void FromTick()
        {
            int      numberOfTicks = 4;
            double   minutes       = GameTick.MINUTES_PER_TICK * numberOfTicks;
            DateTime newDate       = _time.AddMinutes(minutes);
            GameTick newTick       = new GameTick(numberOfTicks);

            Assert.AreEqual(_tick.GetTick() + numberOfTicks, newTick.GetTick());
            Assert.AreEqual(newDate.ToLongTimeString(), newTick.GetDate(_time).ToLongTimeString());
            Assert.AreEqual(newDate.ToLongDateString(), newTick.GetDate(_time).ToLongDateString());
        }
コード例 #9
0
        public async Task <List <GameEventModel> > GetAllPastGameEvents()
        {
            List <GameEventModel> events = await GetAllGameEvents();

            // Get current game tick
            GameTick currentTick = new GameTick(DateTime.FromFileTimeUtc(RoomModel.UnixTimeStarted), DateTime.UtcNow);

            events.Sort((a, b) => a.OccursAtTick.CompareTo(b.OccursAtTick));
            // Filter
            return(events.FindAll(it => it.OccursAtTick <= currentTick.GetTick()));
        }
コード例 #10
0
        public async Task <DeleteGameEventResponse> RemovePlayerGameEvent(DbUserModel player, string eventId)
        {
            try
            {
                Guid.Parse(eventId);
            }
            catch (FormatException)
            {
                return(new DeleteGameEventResponse()
                {
                    Status = ResponseFactory.createResponse(ResponseType.GAME_EVENT_DOES_NOT_EXIST)
                });
            }

            // Get the event to check some things...
            GameEventModelMapper gameEvent = (await MongoConnector.GetGameEventCollection()
                                              .FindAsync(it => it.Id == eventId))
                                             .ToList()
                                             .FirstOrDefault();

            if (gameEvent == null)
            {
                return(new DeleteGameEventResponse()
                {
                    Status = ResponseFactory.createResponse(ResponseType.GAME_EVENT_DOES_NOT_EXIST)
                });;
            }

            // Determine if the event has already passed.
            GameTick currentTick = new GameTick(DateTime.FromFileTimeUtc(GameConfiguration.UnixTimeStarted), DateTime.UtcNow);

            if (gameEvent.OccursAtTick <= currentTick.GetTick())
            {
                return(new DeleteGameEventResponse()
                {
                    Status = ResponseFactory.createResponse(ResponseType.INVALID_REQUEST)
                });
            }

            if (gameEvent.IssuedBy != player.UserModel.Id)
            {
                return(new DeleteGameEventResponse()
                {
                    Status = ResponseFactory.createResponse(ResponseType.PERMISSION_DENIED)
                });
            }

            // Remove game Event
            MongoConnector.GetGameEventCollection().DeleteOne(it => it.Id == eventId);
            return(new DeleteGameEventResponse()
            {
                Status = ResponseFactory.createResponse(ResponseType.SUCCESS),
            });
        }
コード例 #11
0
        public void FromDate()
        {
            int      numberOfTicks = 4;
            int      minutes       = GameTick.MinutesPerTick * numberOfTicks;
            DateTime newDate       = _time.AddMinutes(minutes);
            GameTick newTick       = GameTick.FromDate(newDate);

            Assert.AreEqual(_tick.GetTick() + numberOfTicks, newTick.GetTick());
            Assert.AreEqual(newDate.ToLongTimeString(), newTick.GetDate().ToLongTimeString());
            Assert.AreEqual(newDate.ToLongDateString(), newTick.GetDate().ToLongDateString());
        }
コード例 #12
0
        public override async Task <GetGameRoomEventsResponse> GetGameRoomEvents(GetGameRoomEventsRequest request, ServerCallContext context)
        {
            RedisUserModel user = context.UserState["user"] as RedisUserModel;

            if (user == null)
            {
                return new GetGameRoomEventsResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.UNAUTHORIZED)
                       }
            }
            ;

            RedisRoomModel room = await RedisRoomModel.GetRoomFromGuid(request.RoomId);

            if (room == null)
            {
                return new GetGameRoomEventsResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.ROOM_DOES_NOT_EXIST)
                       }
            }
            ;

            List <RedisUserModel> playersInGame = await room.GetPlayersInGame();

            if (playersInGame.All(it => it.UserModel.Id != user.UserModel.Id) && !user.HasClaim(UserClaim.Admin))
            {
                return new GetGameRoomEventsResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.PERMISSION_DENIED)
                       }
            }
            ;

            List <GameEventModel> events = await room.GetAllGameEvents();

            // Filter out only the player's events and events that have occurred in the past.
            // Get current tick to determine events in the past.
            GameTick currentTick = new GameTick(DateTime.FromFileTimeUtc(room.RoomModel.UnixTimeStarted), DateTime.UtcNow);

            // Admins see all events :)
            if (!user.HasClaim(UserClaim.Admin))
            {
                events = events.FindAll(it =>
                                        it.OccursAtTick <= currentTick.GetTick() || it.IssuedBy == user.UserModel.Id);
            }

            GetGameRoomEventsResponse response = new GetGameRoomEventsResponse();

            response.GameEvents.AddRange(events);
            response.Status = ResponseFactory.createResponse(ResponseType.SUCCESS);
            return(response);
        }
コード例 #13
0
        public void GetPreviousTick()
        {
            // Check cannot go back before the first tick
            GameTick previousTick = _tick.GetPreviousTick();

            Assert.AreEqual(_tick, previousTick);

            // Advance and then come back
            GameTick startingTick = _tick.GetNextTick().GetPreviousTick();

            Assert.AreEqual(_time, startingTick.GetDate());
            Assert.AreEqual(_tickNumber, startingTick.GetTick());
        }
コード例 #14
0
        public void Rewind()
        {
            int      ticksToReverse = 10;
            GameTick tenTicksBefore = _tick.Rewind(ticksToReverse);

            // Should not be able to reverse
            Assert.AreEqual(true, tenTicksBefore == _tick);

            // Advance and then go back
            GameTick startingTick = _tick.Advance(ticksToReverse).Rewind(ticksToReverse);

            Assert.AreEqual(_time, startingTick.GetDate());
            Assert.AreEqual(_tickNumber, startingTick.GetTick());
        }
コード例 #15
0
        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,
            });
        }
コード例 #16
0
        public async Task <List <GameEventModel> > GetAllPastGameEvents()
        {
            var events = (await MongoConnector.GetGameEventCollection()
                          .FindAsync(it => it.RoomId == GameConfiguration.Id))
                         .ToList()
                         .Select(it => it.ToProto())
                         .ToList();

            // Get current game tick
            var currentTick = new GameTick(DateTime.FromFileTimeUtc(GameConfiguration.UnixTimeStarted), DateTime.UtcNow);

            events.Sort((a, b) => a.OccursAtTick.CompareTo(b.OccursAtTick));
            return(events.FindAll(it => it.OccursAtTick <= currentTick.GetTick()));
        }
コード例 #17
0
        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,
            });
        }
コード例 #18
0
 public void Constructor()
 {
     Assert.AreEqual(_tickNumber, _tick.GetTick());
     Assert.AreEqual(_time, _tick.GetDate());
 }