public void PlayerInAGameCanSubmitAnEvent()
        {
            SubmitGameEventResponse eventResponse = client.SubmitGameEvent(new SubmitGameEventRequest()
            {
                EventData = new GameEventRequest()
                {
                    EventData    = ByteString.CopyFromUtf8("MyEventData"),
                    OccursAtTick = 42,
                },
                RoomId = gameId,
            });

            Assert.AreEqual(eventResponse.Status.IsSuccess, true);
            Assert.IsTrue(eventResponse.EventId != null);

            // Submitting player can see their own events
            GetGameRoomEventsResponse gameEvents = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId
            });

            Assert.AreEqual(gameEvents.Status.IsSuccess, true);
            Assert.AreEqual(1, gameEvents.GameEvents.Count);
            Assert.IsTrue(gameEvents.GameEvents.Any(it => it.Id == eventResponse.EventId));
        }
        public void PlayerCanViewTheirOwnEventsThatOccurInTheFutureButOthersCannot()
        {
            SubmitGameEventResponse eventResponse = client.SubmitGameEvent(new SubmitGameEventRequest()
            {
                EventData = new GameEventRequest()
                {
                    EventData    = ByteString.CopyFromUtf8("MyEventData"),
                    OccursAtTick = 42,
                },
                RoomId = gameId,
            });

            Assert.AreEqual(eventResponse.Status.IsSuccess, true);
            Assert.IsTrue(eventResponse.EventId != null);

            // Submitting player can see their own events
            GetGameRoomEventsResponse gameEvents = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId
            });

            Assert.AreEqual(gameEvents.Status.IsSuccess, true);
            Assert.AreEqual(1, gameEvents.GameEvents.Count);
            Assert.IsTrue(gameEvents.GameEvents.Any(it => it.Id == eventResponse.EventId));

            authHelper.loginToAccount(playerTwoInGame);
            // Other player cannot see the first player's plans as they are in the future
            GetGameRoomEventsResponse playerTwoGameEvents = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId
            });

            Assert.AreEqual(playerTwoGameEvents.Status.IsSuccess, true);
            Assert.AreEqual(0, playerTwoGameEvents.GameEvents.Count);
        }
        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);
        }
        public void PlayerCannotDeleteAnotherPlayersEvent()
        {
            SubmitGameEventResponse eventResponse = client.SubmitGameEvent(new SubmitGameEventRequest()
            {
                EventData = new GameEventRequest()
                {
                    EventData    = ByteString.CopyFromUtf8("MyEventData"),
                    OccursAtTick = 42,
                },
                RoomId = gameId,
            });

            Assert.AreEqual(eventResponse.Status.IsSuccess, true);
            Assert.IsTrue(eventResponse.EventId != null);

            GetGameRoomEventsResponse gameEventsBeforeDelete = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId
            });

            Assert.AreEqual(gameEventsBeforeDelete.Status.IsSuccess, true);
            Assert.AreEqual(1, gameEventsBeforeDelete.GameEvents.Count);
            Assert.IsTrue(gameEventsBeforeDelete.GameEvents.Any(it => it.Id == eventResponse.EventId));

            authHelper.loginToAccount(playerTwoInGame);

            DeleteGameEventResponse deleteResponse = client.DeleteGameEvent(new DeleteGameEventRequest()
            {
                EventId = eventResponse.EventId,
                RoomId  = gameId,
            });

            Assert.AreEqual(deleteResponse.Status.IsSuccess, false);
            Assert.AreEqual(deleteResponse.Status.Detail, ResponseType.PERMISSION_DENIED.ToString());

            // Login to player 1 to see the event in the future.
            authHelper.loginToAccount(playerOneInGame);
            GetGameRoomEventsResponse gameEventsAfterDelete = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId
            });

            Assert.AreEqual(gameEventsAfterDelete.Status.IsSuccess, true);
            Assert.AreEqual(1, gameEventsAfterDelete.GameEvents.Count);
        }
        public void PlayersCanViewAnyEventThatHasAlreadyOccurred()
        {
            // Submit 3 close game events, and 2 far game events.
            SubmitGameEventRequest request = new SubmitGameEventRequest()
            {
                EventData = new GameEventRequest()
                {
                    EventData    = ByteString.CopyFromUtf8("MyEventData"),
                    OccursAtTick = 5,
                },
                RoomId = gameId,
            };

            client.SubmitGameEvent(request);
            client.SubmitGameEvent(request);
            client.SubmitGameEvent(request);

            request.EventData.OccursAtTick = 10467;

            client.SubmitGameEvent(request);
            client.SubmitGameEvent(request);

            Thread.Sleep(5000);

            // Submitting player can see their own events
            GetGameRoomEventsResponse gameEventsForSubmitter = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId
            });

            Assert.AreEqual(gameEventsForSubmitter.Status.IsSuccess, true);
            Assert.AreEqual(5, gameEventsForSubmitter.GameEvents.Count);

            authHelper.loginToAccount(playerTwoInGame);
            // Other player can only see events that have passed.
            GetGameRoomEventsResponse gameEventsForOther = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId
            });

            Assert.AreEqual(gameEventsForOther.Status.IsSuccess, true);
            Assert.AreEqual(3, gameEventsForOther.GameEvents.Count);
        }
        public void PlayerCannotUpdateAGameEventThatHasAlreadyOccurred()
        {
            SubmitGameEventResponse eventResponse = client.SubmitGameEvent(new SubmitGameEventRequest()
            {
                EventData = new GameEventRequest()
                {
                    EventData    = ByteString.CopyFromUtf8("MyEventData"),
                    OccursAtTick = 2,
                },
                RoomId = gameId,
            });

            Assert.AreEqual(eventResponse.Status.IsSuccess, true);
            Assert.IsTrue(eventResponse.EventId != null);

            // Submitting player can see their own events
            GetGameRoomEventsResponse gameEvents = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId
            });

            Assert.AreEqual(gameEvents.Status.IsSuccess, true);
            Assert.AreEqual(1, gameEvents.GameEvents.Count);
            Assert.IsTrue(gameEvents.GameEvents.Any(it => it.Id == eventResponse.EventId));

            Thread.Sleep(3000);

            // Attempt to delete
            SubmitGameEventResponse updateResponse = client.UpdateGameEvent(new UpdateGameEventRequest()
            {
                EventId   = eventResponse.EventId,
                EventData = new GameEventRequest()
                {
                    EventData    = ByteString.CopyFromUtf8("NewEventData"),
                    OccursAtTick = 42,
                },
                RoomId = gameId,
            });

            Assert.AreEqual(updateResponse.Status.IsSuccess, true);
        }
        public void AdminsCanSeeAllGameEvents()
        {
            // Submit 3 close game events, and 2 far game events.
            SubmitGameEventRequest request = new SubmitGameEventRequest()
            {
                EventData = new GameEventRequest()
                {
                    EventData    = ByteString.CopyFromUtf8("MyEventData"),
                    OccursAtTick = 123,
                },
                RoomId = gameId,
            };

            client.SubmitGameEvent(request);
            client.SubmitGameEvent(request);
            client.SubmitGameEvent(request);

            request.EventData.OccursAtTick = 10467;

            client.SubmitGameEvent(request);
            client.SubmitGameEvent(request);

            SuperUser admin = authHelper.CreateSuperUser();

            client.Login(new AuthorizationRequest()
            {
                Username = admin.DbUserModel.UserModel.Username,
                Password = admin.password,
            });

            GetGameRoomEventsResponse eventResponse = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId,
            });

            Assert.AreEqual(eventResponse.Status.IsSuccess, true);

            // Admin should be able to see all 5 events!
            Assert.AreEqual(5, eventResponse.GameEvents.Count);
        }
        public void PlayerCannotUpdateAnotherPlayersEvent()
        {
            SubmitGameEventResponse eventResponse = client.SubmitGameEvent(new SubmitGameEventRequest()
            {
                EventData = new GameEventRequest()
                {
                    EventData    = ByteString.CopyFromUtf8("MyEventData"),
                    OccursAtTick = 42,
                },
                RoomId = gameId,
            });

            Assert.AreEqual(eventResponse.Status.IsSuccess, true);
            Assert.IsTrue(eventResponse.EventId != null);

            GetGameRoomEventsResponse gameEvents = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId
            });

            Assert.AreEqual(gameEvents.Status.IsSuccess, true);
            Assert.AreEqual(1, gameEvents.GameEvents.Count);
            Assert.IsTrue(gameEvents.GameEvents.Any(it => it.Id == eventResponse.EventId));

            authHelper.loginToAccount(playerTwoInGame);

            var submitGameEventResponse = client.UpdateGameEvent(new UpdateGameEventRequest()
            {
                EventId   = eventResponse.EventId,
                EventData = new GameEventRequest()
                {
                    EventData    = ByteString.CopyFromUtf8("NewEventData"),
                    OccursAtTick = 42,
                },
                RoomId = gameId,
            });

            Assert.AreEqual(submitGameEventResponse.Status.IsSuccess, false);
            Assert.AreEqual(submitGameEventResponse.Status.Detail, ResponseType.PERMISSION_DENIED.ToString());
        }
        public void PlayerCanDeleteAnEventThatTheySubmitted()
        {
            SubmitGameEventResponse eventResponse = client.SubmitGameEvent(new SubmitGameEventRequest()
            {
                EventData = new GameEventRequest()
                {
                    EventData    = ByteString.CopyFromUtf8("MyEventData"),
                    OccursAtTick = 42,
                },
                RoomId = gameId,
            });

            Assert.AreEqual(eventResponse.Status.IsSuccess, true);
            Assert.IsTrue(eventResponse.EventId != null);

            GetGameRoomEventsResponse gameEventsBeforeDelete = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId
            });

            Assert.AreEqual(gameEventsBeforeDelete.Status.IsSuccess, true);
            Assert.AreEqual(1, gameEventsBeforeDelete.GameEvents.Count);
            Assert.IsTrue(gameEventsBeforeDelete.GameEvents.Any(it => it.Id == eventResponse.EventId));

            DeleteGameEventResponse deleteResponse = client.DeleteGameEvent(new DeleteGameEventRequest()
            {
                EventId = eventResponse.EventId,
                RoomId  = gameId,
            });

            Assert.AreEqual(deleteResponse.Status.IsSuccess, true);

            GetGameRoomEventsResponse gameEventsAfterDelete = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId
            });

            Assert.AreEqual(gameEventsAfterDelete.Status.IsSuccess, true);
            Assert.AreEqual(0, gameEventsAfterDelete.GameEvents.Count);
        }
        public void PlayerCanUpdateAGameEvent()
        {
            SubmitGameEventResponse eventResponse = client.SubmitGameEvent(new SubmitGameEventRequest()
            {
                EventData = new GameEventRequest()
                {
                    EventData    = ByteString.CopyFromUtf8("MyEventData"),
                    OccursAtTick = 42,
                },
                RoomId = gameId,
            });

            Assert.AreEqual(eventResponse.Status.IsSuccess, true);
            Assert.IsTrue(eventResponse.EventId != null);

            GetGameRoomEventsResponse gameEvents = client.GetGameRoomEvents(new GetGameRoomEventsRequest()
            {
                RoomId = gameId
            });

            Assert.AreEqual(gameEvents.Status.IsSuccess, true);
            Assert.AreEqual(1, gameEvents.GameEvents.Count);
            Assert.IsTrue(gameEvents.GameEvents.Any(it => it.Id == eventResponse.EventId));

            SubmitGameEventResponse updateResponse = client.UpdateGameEvent(new UpdateGameEventRequest()
            {
                EventData = new GameEventRequest()
                {
                    EventData    = ByteString.CopyFromUtf8("MyEventData"),
                    OccursAtTick = 42,
                },
                RoomId  = gameId,
                EventId = eventResponse.EventId,
            });

            Assert.AreEqual(updateResponse.Status.IsSuccess, true);
            Assert.IsTrue(updateResponse.EventId == eventResponse.EventId);
        }