Ejemplo n.º 1
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,
            });
        }
Ejemplo n.º 2
0
        public async Task <ResponseStatus> BlockUser(DbUserModel requestingDbUserModel)
        {
            if (requestingDbUserModel.HasClaim(UserClaim.Admin) || HasClaim(UserClaim.Admin))
            {
                return(ResponseFactory.createResponse(ResponseType.PERMISSION_DENIED));
            }

            // Check if a relationship already exists for the two players.
            var relationExists = MongoConnector.GetFriendCollection()
                                 .Find(it => (it.PlayerId == UserModel.Id && it.FriendId == requestingDbUserModel.UserModel.Id))
                                 .CountDocuments() != 0;

            // Remove all pre-existing relations
            await MongoConnector.GetFriendCollection().DeleteManyAsync(it =>
                                                                       (it.FriendId == requestingDbUserModel.UserModel.Id && it.PlayerId == UserModel.Id) ||
                                                                       (it.PlayerId == requestingDbUserModel.UserModel.Id && it.FriendId == UserModel.Id));

            await MongoConnector.GetFriendCollection().InsertOneAsync(new FriendModel
            {
                Id              = Guid.NewGuid().ToString(),
                FriendId        = requestingDbUserModel.UserModel.Id,
                FriendStatus    = FriendStatus.StatusBlocked,
                PlayerId        = UserModel.Id,
                UnixTimeCreated = DateTime.UtcNow.ToFileTimeUtc(),
            });

            return(ResponseFactory.createResponse(ResponseType.SUCCESS));
        }
Ejemplo n.º 3
0
        public async Task <ResponseStatus> UnblockUser(DbUserModel requestingDbUserModel)
        {
            var update = Builders <FriendModel> .Update.Set(it => it.FriendStatus, FriendStatus.StatusNoRelation);

            await MongoConnector.GetFriendCollection().UpdateOneAsync((it => it.FriendId == requestingDbUserModel.UserModel.Id && it.PlayerId == UserModel.Id), update);

            return(ResponseFactory.createResponse(ResponseType.SUCCESS));
        }
Ejemplo n.º 4
0
        public async Task <CreateMessageGroupResponse> CreateMessageGroup(List <String> groupMembers)
        {
            // Ensure all members are in the room.
            if (groupMembers.Except(GameConfiguration.Players.Select(it => it.Id)).Any())
            {
                // A player in the group is not in the game.
                return(new CreateMessageGroupResponse()
                {
                    Status = ResponseFactory.createResponse(ResponseType.INVALID_REQUEST),
                });;
            }

            // Get all group chats for the room
            List <GroupChat> roomGroupChats = (await MongoConnector.GetMessageGroupCollection()
                                               .FindAsync(group => group.RoomId == GameConfiguration.Id))
                                              .ToList()
                                              .Select(group => new GroupChat(this, group.ToProto()))
                                              .ToList();

            foreach (GroupChat groupChat in roomGroupChats)
            {
                if (groupChat.MessageGroup.GroupMembers.Count() == groupMembers.Count())
                {
                    // If the group chats are the same size and removing all the duplicates results in an empty list,
                    // We have a duplicate group
                    if (!groupChat.MessageGroup.GroupMembers.Except(groupMembers).Any())
                    {
                        return(new CreateMessageGroupResponse()
                        {
                            Status = ResponseFactory.createResponse(ResponseType.DUPLICATE),
                        });
                    }
                }
            }

            // Otherwise, create the group
            GroupModel newGroup = new GroupModel();

            newGroup.Id     = Guid.NewGuid().ToString();
            newGroup.RoomId = GameConfiguration.Id;
            foreach (string s in groupMembers)
            {
                DbUserModel model = await DbUserModel.GetUserFromGuid(s);

                if (model != null)
                {
                    newGroup.GroupMembers.Add(model.UserModel.Id);
                }
            }

            await MongoConnector.GetMessageGroupCollection().InsertOneAsync(new GroupModelMapper(newGroup));

            return(new CreateMessageGroupResponse()
            {
                GroupId = newGroup.Id,
                Status = ResponseFactory.createResponse(ResponseType.SUCCESS),
            });
        }
Ejemplo n.º 5
0
        public async Task <ResponseStatus> RemoveFriend(DbUserModel requestingDbUserModel)
        {
            await MongoConnector.GetFriendCollection().DeleteOneAsync(it =>
                                                                      (it.PlayerId == UserModel.Id && it.FriendId == requestingDbUserModel.UserModel.Id) ||
                                                                      (it.FriendId == UserModel.Id && it.PlayerId == requestingDbUserModel.UserModel.Id)
                                                                      );

            return(ResponseFactory.createResponse(ResponseType.SUCCESS));
        }
Ejemplo n.º 6
0
        public async Task <List <GameEventModel> > GetPlayerGameEvents(DbUserModel player)
        {
            List <GameEventModel> events = (await MongoConnector.GetGameEventCollection()
                                            .FindAsync(it => it.IssuedBy == player.UserModel.Id))
                                           .ToList()
                                           .Select(it => it.ToProto())
                                           .ToList();

            events.Sort((a, b) => a.OccursAtTick.CompareTo(b.OccursAtTick));
            return(events);
        }
Ejemplo n.º 7
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),
            });
        }
Ejemplo n.º 8
0
        public async Task <ResponseStatus> AddFriendRequestFrom(DbUserModel requestingDbUserModel)
        {
            await MongoConnector.GetFriendCollection().InsertOneAsync(new FriendModel()
            {
                PlayerId        = UserModel.Id,
                FriendId        = requestingDbUserModel.UserModel.Id,
                FriendStatus    = FriendStatus.StatusPending,
                UnixTimeCreated = DateTime.UtcNow.ToFileTimeUtc(),
            });

            return(ResponseFactory.createResponse(ResponseType.SUCCESS));
        }
Ejemplo n.º 9
0
        public async Task <bool> IsBlocked(DbUserModel otherDbUserModel)
        {
            var blockedPlayer = (await MongoConnector.GetFriendCollection()
                                 .FindAsync(it =>
                                            it.FriendStatus == FriendStatus.StatusBlocked &&
                                            (it.PlayerId == UserModel.Id && it.FriendId == otherDbUserModel.UserModel.Id ||
                                             it.FriendId == UserModel.Id && it.PlayerId == otherDbUserModel.UserModel.Id)
                                            ))
                                .FirstOrDefault();

            return(blockedPlayer != null);
        }
Ejemplo n.º 10
0
        public async Task <List <GroupChat> > GetPlayerGroupChats(DbUserModel dbUserModel)
        {
            // Get all group chats the player is in
            List <GroupChat> groupChatList = (await MongoConnector.GetMessageGroupCollection()
                                              .FindAsync(group =>
                                                         group.RoomId == GameConfiguration.Id &&
                                                         group.GroupMembers.Contains(dbUserModel.UserModel.Id)))
                                             .ToList()
                                             .Select(group => new GroupChat(this, group.ToProto()))
                                             .ToList();

            return(groupChatList);
        }
Ejemplo n.º 11
0
        public async Task <bool> HasFriendRequestFrom(DbUserModel friend)
        {
            FriendModel friendRequest = (await MongoConnector.GetFriendCollection()
                                         .FindAsync(it =>
                                                    it.FriendStatus == FriendStatus.StatusPending &&
                                                    (it.PlayerId == UserModel.Id && it.FriendId == friend.UserModel.Id)))
                                        .ToList()
                                        .FirstOrDefault();

            if (friendRequest == null)
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 12
0
        public async Task <MessageGroup> asMessageGroup(int messagesPagination = 1)
        {
            MessageGroup model = new MessageGroup()
            {
                GroupId = MessageGroup.Id,
            };

            // Convert UserIds to Users.
            foreach (string userId in MessageGroup.GroupMembers)
            {
                model.GroupMembers.Add((await DbUserModel.GetUserFromGuid(userId)).AsUser());
            }

            model.Messages.AddRange(await GetMessages(messagesPagination));
            return(model);
        }
Ejemplo n.º 13
0
        private GameEventModel toGameEventModel(DbUserModel requestor, GameEventRequest request)
        {
            Guid eventId;

            eventId = Guid.NewGuid();

            return(new GameEventModel()
            {
                Id = eventId.ToString(),
                UnixTimeIssued = DateTime.UtcNow.ToFileTimeUtc(),
                IssuedBy = requestor.UserModel.Id,
                OccursAtTick = request.OccursAtTick,
                EventData = request.EventData,
                RoomId = GameConfiguration.Id,
            });
        }
Ejemplo n.º 14
0
        public async Task <ResponseStatus> SendChatMessage(DbUserModel dbUserModel, string message)
        {
            // Set the creation time.
            MessageModel model = new MessageModel()
            {
                Id                = Guid.NewGuid().ToString(),
                RoomId            = Room.GameConfiguration.Id,
                GroupId           = MessageGroup.Id,
                SenderId          = dbUserModel.UserModel.Id,
                Message           = message,
                UnixTimeCreatedAt = DateTime.UtcNow.ToFileTimeUtc(),
            };

            await MongoConnector.GetMessagesCollection().InsertOneAsync(model);

            return(ResponseFactory.createResponse(ResponseType.SUCCESS));
        }
Ejemplo n.º 15
0
        public static async Task <SuperUser> CreateSuperUser()
        {
            String password = Guid.NewGuid().ToString();

            Console.WriteLine($"Password: {password}");
            DbUserModel dbUserModel = new DbUserModel(new UserModel()
            {
                Id            = Guid.NewGuid().ToString(),
                Username      = "******",
                Email         = "SuperUser",
                EmailVerified = true,
                PasswordHash  = JwtManager.HashPassword(password),
                Claims        = { UserClaim.User, UserClaim.Admin, UserClaim.Dev, UserClaim.EmailVerified }
            });
            await dbUserModel.SaveToDatabase();

            return(new SuperUser(dbUserModel, password));
        }
Ejemplo n.º 16
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,
            });
        }
Ejemplo n.º 17
0
        public async Task <bool> IsFriend(DbUserModel friend)
        {
            FriendModel friendModel = (await MongoConnector.GetFriendCollection()
                                       .FindAsync(it =>
                                                  it.FriendStatus == FriendStatus.StatusFriends && (
                                                      (it.PlayerId == this.UserModel.Id && it.FriendId == friend.UserModel.Id) ||
                                                      (it.FriendId == this.UserModel.Id && it.PlayerId == friend.UserModel.Id)
                                                      )
                                                  ))
                                      .ToList()
                                      .FirstOrDefault();

            if (friendModel == null)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 18
0
        public async Task <ResponseStatus> JoinRoom(DbUserModel dbUserModel)
        {
            if (IsRoomFull())
            {
                return(ResponseFactory.createResponse(ResponseType.ROOM_IS_FULL));
            }

            if (IsPlayerInRoom(dbUserModel))
            {
                return(ResponseFactory.createResponse(ResponseType.DUPLICATE));
            }

            if (GameConfiguration.RoomStatus != RoomStatus.Open)
            {
                return(ResponseFactory.createResponse(ResponseType.GAME_ALREADY_STARTED));
            }

            List <DbUserModel> playersInRoom = new List <DbUserModel>();

            foreach (User user in GameConfiguration.Players)
            {
                playersInRoom.Add(await DbUserModel.GetUserFromGuid(user.Id));
            }

            // Check if any players in the room have the same device identifier
            if (playersInRoom.Any(it => it.UserModel.DeviceIdentifier == dbUserModel.UserModel.DeviceIdentifier))
            {
                return(ResponseFactory.createResponse(ResponseType.PERMISSION_DENIED));
            }

            GameConfiguration.Players.Add(dbUserModel.AsUser());
            MongoConnector.GetGameRoomCollection().ReplaceOne((it => it.Id == GameConfiguration.Id), new GameConfigurationMapper(GameConfiguration));

            // Check if the player joining the game is the last player.
            if (GameConfiguration.Players.Count == GameConfiguration.GameSettings.MaxPlayers)
            {
                return(await StartGame());
            }

            return(ResponseFactory.createResponse(ResponseType.SUCCESS));;
        }
Ejemplo n.º 19
0
        public async Task <ResponseStatus> LeaveRoom(DbUserModel dbUserModel)
        {
            if (GameConfiguration.RoomStatus == RoomStatus.Open)
            {
                // Check if the player leaving was the host.
                if (GameConfiguration.Creator.Id == dbUserModel.UserModel.Id)
                {
                    // Delete the game
                    await MongoConnector.GetGameRoomCollection().DeleteOneAsync(it => it.Id == GameConfiguration.Id);

                    return(ResponseFactory.createResponse(ResponseType.SUCCESS));
                }

                // Otherwise, just remove the player from the player list.
                GameConfiguration.Players.Remove(dbUserModel.AsUser());
                await MongoConnector.GetGameRoomCollection().ReplaceOneAsync((it => it.Id == GameConfiguration.Id), new GameConfigurationMapper(GameConfiguration));

                return(ResponseFactory.createResponse(ResponseType.SUCCESS));
            }
            // TODO: Player left the game while ongoing.
            // Create a player leave game event and push to event list

            return(ResponseFactory.createResponse(ResponseType.INVALID_REQUEST));
        }
Ejemplo n.º 20
0
 public SuperUser(DbUserModel dbUserModel, string password)
 {
     this.DbUserModel = dbUserModel;
     this.password    = password;
 }
Ejemplo n.º 21
0
 public Boolean IsPlayerInGroup(DbUserModel dbUserModel)
 {
     return(MessageGroup.GroupMembers.Any(it => it == dbUserModel.UserModel.Id));
 }
Ejemplo n.º 22
0
 public Boolean IsPlayerInRoom(DbUserModel player)
 {
     return(GameConfiguration.Players.Contains(player.AsUser()));
 }