Exemple #1
0
        public static async Task <ValidatedView> RejectJoinRequest(int accountId, int targetId)
        {
            try {
                ClanMemberModel clanMemberModel = await Model <ClanMemberModel> .AsQueryable()
                                                  .FirstOrDefault(x => x.AccountID == accountId);

                if (clanMemberModel == null || clanMemberModel.Role < ClanRole.VICE_LEADER)
                {
                    return(ValidatedView.Invalid(ErrorCode.CLAN_ACCEPT_MEMBER_INSUFFICIENT_RIGHTS));
                }

                ClanMemberPendingModel clanMemberPendingModel = await Model <ClanMemberPendingModel> .AsQueryable()
                                                                .FirstOrDefault(x => x.AccountID == targetId && x.ClanID == clanMemberModel.ClanID);

                if (clanMemberPendingModel == null)
                {
                    return(ValidatedView.Invalid(ErrorCode.CLAN_PENDING_DID_NOT_REQUEST));
                }

                await Model <ClanMemberPendingModel> .AsCollection()
                .DeleteOneAsync(x => x.AccountID == targetId && x.ClanID == clanMemberModel.ClanID);

                return(ValidatedView.Valid());
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #2
0
        public static async Task <ValidatedView> Delete(int clanId)
        {
            try {
                await Model <ClanMemberModel> .AsCollection()
                .DeleteManyAsync(x => x.ClanID == clanId);

                await Model <ClanMemberPendingModel> .AsCollection()
                .DeleteManyAsync(x => x.ClanID == clanId);

                await Model <ClanRelationModel> .AsCollection()
                .DeleteManyAsync(x => x.InitiatorID == clanId || x.TargetID == clanId);

                await Model <ClanRelationPendingModel> .AsCollection()
                .DeleteManyAsync(x => x.InitiatorID == clanId || x.TargetID == clanId);

                await Model <ClanLogModel> .AsCollection()
                .DeleteManyAsync(x => x.ClanID == clanId);

                await Model <ClanModel> .AsCollection()
                .DeleteOneAsync(x => x.ID == clanId);

                // Update emulator to remove from cache

                return(ValidatedView.Valid());
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #3
0
        public static async Task <ValidatedView> CreateJoinRequest(int accountId, ClanJoinView clanJoinView)
        {
            if (!clanJoinView.IsValid(out string message))
            {
                return(ValidatedView.Invalid(message));
            }

            try {
                if (await Model <ClanMemberPendingModel> .AsQueryable()
                    .Any(x => x.AccountID == accountId && x.ClanID == clanJoinView.ClanID))
                {
                    return(ValidatedView.Invalid(ErrorCode.CLAN_REQUEST_ALREADY_EXISTS));
                }

                if (!await Model <ClanModel> .AsQueryable()
                    .Any(x => x.ID == clanJoinView.ClanID))
                {
                    return(ValidatedView.Invalid(ErrorCode.CLAN_NOT_FOUND));
                }

                await Model <ClanMemberPendingModel> .AsCollection().InsertOneAsync(new ClanMemberPendingModel {
                    AccountID = accountId,
                    ClanID    = clanJoinView.ClanID,
                    Message   = clanJoinView.Description
                });

                return(ValidatedView.Valid());
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #4
0
        public static async Task <ValidatedView <AccountOverview> > RetrieveAccountOverview(int accountId)
        {
            try {
                // check if player is ingame
                if (GameManager.Players.TryGetValue(accountId, out PlayerController controller))
                {
                    AccountOverview accountOverview = Mapper <AccountView> .Map <AccountOverview>(controller.Account);

                    accountOverview.ActiveShipID = controller.Account.CurrentHangar.ShipID;
                    return(ValidatedView <AccountOverview> .Valid(accountOverview));
                }

                AccountModel accountModel = await Model <AccountModel> .AsQueryable()
                                            .FirstOrDefault(x => x.ID == accountId);

                if (accountModel == null)
                {
                    return(ValidatedView <AccountOverview> .Invalid(ErrorCode.ACCOUNT_NOT_FOUND));
                }

                return(ValidatedView <AccountOverview> .Valid(Mapper <AccountModel> .Map <AccountOverview>(accountModel)));
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView <AccountOverview> .Invalid(ErrorCode.OPERATION_FAILED));
        }
        public static async Task <ValidatedView <List <HangarOverview> > > RetrieveHangarOverviews(int accountId)
        {
            try {
                int?activeShipId = await Model <AccountModel> .AsQueryable()
                                   .Where(x => x.ID == accountId)
                                   .Select(x => (int?)x.ActiveShipID)
                                   .FirstOrDefault();

                if (!activeShipId.HasValue)
                {
                    return(ValidatedView <List <HangarOverview> > .Invalid(ErrorCode.NO_HANGAR_FOUND));
                }

                return(ValidatedView <List <HangarOverview> > .Valid(await Model <HangarModel> .AsQueryable()
                                                                     .Where(x => x.AccountID == accountId)
                                                                     .Select(x => new HangarOverview {
                    ShipID = x.ShipID,
                    IsActive = x.ShipID == activeShipId.Value
                }).ToList()
                                                                     ));
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView <List <HangarOverview> > .Invalid(ErrorCode.OPERATION_FAILED));
        }
        public static async Task <ValidatedView> UpdateHangar(int accountId, HangarView hangarView)
        {
            if (!hangarView.IsValid(out string message))
            {
                return(ValidatedView.Invalid(message));
            }

            try {
                HangarModel hangarModel = Mapper <HangarView> .Map(hangarView, new HangarModel { AccountID = accountId });

                hangarModel.Configuration_1 = Mapper <ConfigurationView> .Map <Configuration>(hangarView.Configuration_1);

                hangarModel.Configuration_2 = Mapper <ConfigurationView> .Map <Configuration>(hangarView.Configuration_2);

                ReplaceOneResult result = await Model <HangarModel> .AsCollection()
                                          .ReplaceOneAsync(x => x.AccountID == accountId && x.ShipID == hangarView.ShipID, hangarModel);

                if (result.IsModifiedCountAvailable && result.ModifiedCount == 1)
                {
                    return(ValidatedView.Valid());
                }
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #7
0
        public static async Task <ValidatedView <ClanOverview> > RetrieveClanOverviewFromMember(int accountId)
        {
            try {
                ClanOverview    clanOverview    = null;
                ClanMemberModel clanMemberModel = await Model <ClanMemberModel>
                                                  .AsQueryable().FirstOrDefault(x => x.AccountID == accountId);

                if (clanMemberModel != null)
                {
                    ClanModel clanModel = await Model <ClanModel> .AsQueryable()
                                          .FirstOrDefault(x => x.ID == clanMemberModel.ClanID);

                    if (clanModel == null)
                    {
                        GameContext.Logger.LogWarning($"Player [id: '{accountId}'] is member of a clan [id: '{clanMemberModel.ClanID}'] which does not even exist");
                    }
                    else
                    {
                        clanOverview = Mapper <ClanModel> .Map <ClanOverview>(clanModel);
                    }
                }

                return(ValidatedView <ClanOverview> .Valid(clanOverview));
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView <ClanOverview> .Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #8
0
        public static async Task <ValidatedView <GlobalRole> > Authenticate(AccountSessionView accountSessionView)
        {
            if (!accountSessionView.IsValid(out string message))
            {
                return(ValidatedView <GlobalRole> .Invalid(message));
            }

            try {
                if (!await Model <AccountSessionModel> .AsQueryable()
                    .Any(x => x.AccountID == accountSessionView.AccountID && x.Token == accountSessionView.Token))
                {
                    return(ValidatedView <GlobalRole> .Invalid(ErrorCode.INVALID_SESSION));
                }

                GlobalRole?role = await Model <AccountModel> .AsQueryable()
                                  .Where(x => x.ID == accountSessionView.AccountID)
                                  .Select(x => (GlobalRole?)x.Role).FirstOrDefault();

                if (role.HasValue)
                {
                    return(ValidatedView <GlobalRole> .Valid(role.Value));
                }
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView <GlobalRole> .Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #9
0
        public static async Task <ValidatedView <string> > RetrieveUsername(int accountId)
        {
            try {
                // check if player is ingame
                if (GameManager.Players.TryGetValue(accountId, out PlayerController controller))
                {
                    return(ValidatedView <string> .Valid(controller.Username));
                }

                string username = await Model <AccountModel> .AsQueryable()
                                  .Where(x => x.ID == accountId)
                                  .Select(x => x.Username)
                                  .FirstOrDefault();

                if (username == null)
                {
                    return(ValidatedView <string> .Invalid(ErrorCode.ACCOUNT_NOT_FOUND));
                }

                return(ValidatedView <string> .Valid(username));
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView <string> .Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #10
0
        public static async Task <ValidatedView <AccountClanView> > RetrieveSelf(int accountId)
        {
            try {
                ClanMemberModel clanMemberModel = await Model <ClanMemberModel>
                                                  .AsQueryable().FirstOrDefault(x => x.AccountID == accountId);

                if (clanMemberModel != null)
                {
                    ClanModel clanModel = await Model <ClanModel> .AsQueryable()
                                          .FirstOrDefault(x => x.ID == clanMemberModel.ClanID);

                    if (clanModel == null)
                    {
                        GameContext.Logger.LogWarning($"Player [id: '{accountId}'] is member of a clan [id: '{clanMemberModel.ClanID}'] which does not even exist");
                    }
                    else
                    {
                        AccountClanView accountClanView = new AccountClanView {
                            Role     = clanMemberModel.Role,
                            JoinDate = clanMemberModel.CreationDate
                        };

                        if (GameManager.Players.TryGetValue(clanMemberModel.AccountID, out PlayerController controller))
                        {
                            accountClanView = Mapper <AccountView> .Map(controller.Account, accountClanView);

                            accountClanView.ActiveShipID = controller.Account.CurrentHangar.ShipID;
                            accountClanView.Online       = true;
                            accountClanView.Map          = controller.HangarAssembly.Map.Name;

                            Position actualPosition = controller.MovementAssembly.ActualPosition();
                            accountClanView.Position = (actualPosition.X, actualPosition.Y);
                        }
                        else
                        {
                            AccountModel accountModel = await Model <AccountModel> .AsQueryable()
                                                        .FirstOrDefault(x => x.ID == clanMemberModel.AccountID);

                            if (accountModel == null)
                            {
                                GameContext.Logger.LogCritical($"Player [id: '{clanMemberModel.AccountID}'] not found!");
                                return(ValidatedView <AccountClanView> .Invalid(ErrorCode.ACCOUNT_NOT_FOUND));
                            }

                            accountClanView = Mapper <AccountModel> .Map(accountModel, accountClanView);
                        }

                        return(ValidatedView <AccountClanView> .Valid(accountClanView));
                    }
                }
                else
                {
                    return(ValidatedView <AccountClanView> .Invalid(ErrorCode.CLAN_NOT_MEMBER));
                }
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView <AccountClanView> .Invalid(ErrorCode.OPERATION_FAILED));
        }
 public async Task <IActionResult> Post([FromBody] HangarDetailView hangarDetailView)
 {
     if (HttpContext.TryGetCurrentSession(out AccountSessionView accountSessionView))
     {
         return(Ok(await HangarService.UpdateHangar(accountSessionView.AccountID, hangarDetailView)));
     }
     return(Ok(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED)));
 }
Exemple #12
0
 public async Task <IActionResult> AssingRole(int id, int role)
 {
     if (HttpContext.TryGetCurrentSession(out AccountSessionView accountSessionView))
     {
         return(Ok(await ClanService.AssignRole(accountSessionView.AccountID, id, (ClanRole)role)));
     }
     return(Ok(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED)));
 }
 public async Task <IActionResult> Get()
 {
     if (HttpContext.TryGetCurrentSession(out AccountSessionView accountSessionView))
     {
         return(Ok(await HangarService.RetrieveHangarOverviews(accountSessionView.AccountID)));
     }
     return(Ok(ValidatedView <List <HangarOverview> > .Invalid(ErrorCode.OPERATION_FAILED)));
 }
Exemple #14
0
 public async Task <IActionResult> Get([FromQuery] string query, [FromQuery] int offset, [FromQuery] int count)
 {
     if (HttpContext.TryGetCurrentSession(out AccountSessionView accountSessionView))
     {
         return(Ok(await ClanService.RetrieveClans(accountSessionView.AccountID, (query ?? "").ToLower(), offset, count)));
     }
     return(Ok(ValidatedView <EnumerableResultView <ClanView> > .Invalid(ErrorCode.OPERATION_FAILED)));
 }
Exemple #15
0
 public async Task <IActionResult> GetPending()
 {
     if (HttpContext.TryGetCurrentSession(out AccountSessionView accountSessionView))
     {
         return(Ok(await ClanService.RetrievePendingFromMember(accountSessionView.AccountID)));
     }
     return(Ok(ValidatedView <List <AccountClanView> > .Invalid(ErrorCode.OPERATION_FAILED)));
 }
 public async Task <IActionResult> Activate(int id)
 {
     if (HttpContext.TryGetCurrentSession(out AccountSessionView accountSessionView))
     {
         return(Ok(await HangarService.ActivateHangar(accountSessionView.AccountID, id)));
     }
     return(Ok(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED)));
 }
Exemple #17
0
 public async Task <IActionResult> Leave()
 {
     if (HttpContext.TryGetCurrentSession(out AccountSessionView accountSessionView))
     {
         return(Ok(await ClanService.Leave(accountSessionView.AccountID)));
     }
     return(Ok(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED)));
 }
Exemple #18
0
 public async Task <IActionResult> Post([FromBody] ClanCreateView clanCreateView)
 {
     if (HttpContext.TryGetCurrentSession(out AccountSessionView accountSessionView))
     {
         return(Ok(await ClanService.CreateClan(accountSessionView.AccountID, clanCreateView)));
     }
     return(Ok(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED)));
 }
Exemple #19
0
 public async Task <IActionResult> RejectJoinRequest(int id)
 {
     if (HttpContext.TryGetCurrentSession(out AccountSessionView accountSessionView))
     {
         return(Ok(await ClanService.RejectJoinRequest(accountSessionView.AccountID, id)));
     }
     return(Ok(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED)));
 }
Exemple #20
0
 public async Task <IActionResult> GetDiplomacies()
 {
     if (HttpContext.TryGetCurrentSession(out AccountSessionView accountSessionView))
     {
         return(Ok(await ClanService.RetrieveDiplomacies(accountSessionView.AccountID)));
     }
     return(Ok(ValidatedView <List <ClanDiplomacyView> > .Invalid(ErrorCode.OPERATION_FAILED)));
 }
Exemple #21
0
        public static async Task <ValidatedView <AccountView> > RetrieveAccount(int accountId)
        {
            try {
                AccountModel accountModel = await Model <AccountModel> .AsQueryable()
                                            .FirstOrDefault(x => x.ID == accountId);

                if (accountModel == null)
                {
                    return(ValidatedView <AccountView> .Invalid(ErrorCode.ACCOUNT_NOT_FOUND));
                }

                AccountView accountView = Mapper <AccountModel> .Map <AccountView>(accountModel);

                accountView.GGRings = 64;

                ValidatedView <HangarView> validatedHangarView = await HangarService.RetrieveHangar(accountModel.ID, accountModel.ActiveShipID);

                if (!validatedHangarView.IsValid)
                {
                    return(ValidatedView <AccountView> .Invalid(validatedHangarView.Message));
                }
                accountView.CurrentHangar = validatedHangarView.Object;

                ValidatedView <VaultView> validatedVaultView = await RetrieveVault(accountId);

                if (!validatedVaultView.IsValid)
                {
                    return(ValidatedView <AccountView> .Invalid(validatedVaultView.Message));
                }
                accountView.Vault = validatedVaultView.Object;

                ValidatedView <CooldownView> validatedCooldownView = await RetrieveCooldown(accountId);

                if (!validatedCooldownView.IsValid)
                {
                    return(ValidatedView <AccountView> .Invalid(validatedCooldownView.Message));
                }
                accountView.Cooldown = validatedCooldownView.Object;

                accountView.Clan = new ClanOverview();
                ValidatedView <ClanOverview> validatedClanOverview = await ClanService.RetrieveClanOverviewFromMember(accountId);

                if (validatedClanOverview.IsValid && validatedClanOverview.Object != null)
                {
                    accountView.Clan = validatedClanOverview.Object;
                }

                return(ValidatedView <AccountView> .Valid(accountView));
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView <AccountView> .Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #22
0
        public static async Task <ValidatedView> UpdateAccount(AccountView accountView)
        {
            try {
                AccountModel accountModel = await Model <AccountModel> .AsQueryable()
                                            .Where(x => x.ID == accountView.ID).FirstOrDefault();

                if (accountModel == null)
                {
                    return(ValidatedView.Invalid(ErrorCode.ACCOUNT_NOT_FOUND));
                }

                bool success = true;

                ReplaceOneResult result = await Model <AccountModel> .AsCollection()
                                          .ReplaceOneAsync(x => x.ID == accountView.ID,
                                                           Mapper <AccountView> .Map(accountView, accountModel));

                success &= result.IsModifiedCountAvailable && result.ModifiedCount == 1;

                ValidatedView validatedHangarResultView = await HangarService
                                                          .UpdateHangar(accountView.ID, accountView.CurrentHangar);

                if (!validatedHangarResultView.IsValid)
                {
                    GameContext.Logger.LogInformation($"Player: '{accountView.ID}': {validatedHangarResultView.Message}");
                }
                success &= validatedHangarResultView.IsValid;

                ValidatedView validatedVaultResultView = await UpdateVault(accountView.ID, accountView.Vault);

                if (!validatedVaultResultView.IsValid)
                {
                    GameContext.Logger.LogInformation($"Player: '{accountView.ID}': {validatedVaultResultView.Message}");
                }
                success &= validatedVaultResultView.IsValid;

                ValidatedView validatedCooldownResultView = await UpdateCooldown(accountView.ID, accountView.Cooldown);

                if (!validatedCooldownResultView.IsValid)
                {
                    GameContext.Logger.LogInformation($"Player: '{accountView.ID}': {validatedCooldownResultView.Message}");
                }
                success &= validatedCooldownResultView.IsValid;

                if (success)
                {
                    return(ValidatedView.Valid());
                }
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #23
0
        public static async Task <ValidatedView> AcceptJoinRequest(int accountId, int targetId)
        {
            try {
                ClanMemberModel clanMemberModel = await Model <ClanMemberModel> .AsQueryable()
                                                  .FirstOrDefault(x => x.AccountID == accountId);

                if (clanMemberModel == null || clanMemberModel.Role < ClanRole.VICE_LEADER)
                {
                    return(ValidatedView.Invalid(ErrorCode.CLAN_ACCEPT_MEMBER_INSUFFICIENT_RIGHTS));
                }

                if (await Model <ClanMemberModel> .AsQueryable().Count(x => x.ClanID == clanMemberModel.ClanID) >= 50)
                {
                    return(ValidatedView.Invalid(ErrorCode.CLAN_FULL));
                }

                ClanMemberPendingModel clanMemberPendingModel = await Model <ClanMemberPendingModel> .AsQueryable()
                                                                .FirstOrDefault(x => x.AccountID == targetId && x.ClanID == clanMemberModel.ClanID);

                if (clanMemberPendingModel == null)
                {
                    return(ValidatedView.Invalid(ErrorCode.CLAN_PENDING_DID_NOT_REQUEST));
                }

                ClanMemberModel newClanMemberModel = new ClanMemberModel {
                    ClanID = clanMemberModel.ClanID, AccountID = targetId
                };
                await Model <ClanMemberPendingModel> .AsCollection().DeleteManyAsync(x => x.AccountID == targetId);

                await Model <ClanMemberModel> .AsCollection().InsertOneAsync(newClanMemberModel);

                if (GameManager.Get(accountId, out PlayerController controller))
                {
                    ValidatedView <ClanOverview> validatedView = await RetrieveClanOverviewFromMember(accountId);

                    if (validatedView.IsValid)
                    {
                        controller.Account.Clan = validatedView.Object ?? new ClanOverview();
                    }

                    ClanChangedCommand clanChangedCommand = PacketBuilder.ClanChangedCommand(controller);
                    controller.Send(clanChangedCommand);
                    controller.EntitesInRange(x => x.Send(clanChangedCommand));
                }

                return(ValidatedView.Valid());
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #24
0
        public static async Task <ValidatedView> CreateClan(int accountId, ClanCreateView clanCreateView)
        {
            if (!clanCreateView.IsValid(out string message))
            {
                return(ValidatedView.Invalid(message));
            }

            try {
                if (await Model <ClanMemberModel> .AsQueryable().Any(x => x.AccountID == accountId))
                {
                    GameContext.Logger.LogCritical($"Player [id: '{accountId}'] tries creating a clan while being member of another!");
                    return(ValidatedView.Invalid(ErrorCode.CLAN_ALREADY_MEMBER));
                }

                if (await Model <ClanModel> .AsQueryable().Any(x => x.Name == clanCreateView.Name))
                {
                    return(ValidatedView.Invalid(ErrorCode.CLAN_NAME_ALREADY_IN_USE));
                }

                if (await Model <ClanModel> .AsQueryable().Any(x => x.Tag == clanCreateView.Tag))
                {
                    return(ValidatedView.Invalid(ErrorCode.CLAN_TAG_ALREADY_IN_USE));
                }

                ClanModel clanModel = Mapper <ClanCreateView> .Map <ClanModel>(clanCreateView);

                ClanMemberModel clanMemberModel = new ClanMemberModel {
                    ClanID = clanModel.ID, AccountID = accountId, Role = ClanRole.LEADER
                };

                await Model <ClanMemberPendingModel> .AsCollection().DeleteManyAsync(x => x.AccountID == accountId);

                await Model <ClanModel> .AsCollection().InsertOneAsync(clanModel);

                await Model <ClanMemberModel> .AsCollection().InsertOneAsync(clanMemberModel);

                if (GameManager.Get(accountId, out PlayerController controller))
                {
                    controller.Account.Clan = Mapper <ClanModel> .Map <ClanOverview>(clanModel);

                    ClanChangedCommand clanChangedCommand = PacketBuilder.ClanChangedCommand(controller);
                    controller.Send(clanChangedCommand);
                    controller.EntitesInRange(x => x.Send(clanChangedCommand));
                }

                return(ValidatedView.Valid());
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #25
0
        public static async Task <ValidatedView <List <ClanDiplomacyView> > > RetrievePendingDiplomacies(int accountId)
        {
            try {
                ClanMemberModel clanMemberModel = await Model <ClanMemberModel>
                                                  .AsQueryable().FirstOrDefault(x => x.AccountID == accountId);

                if (clanMemberModel != null)
                {
                    ClanModel clanModel = await Model <ClanModel> .AsQueryable()
                                          .FirstOrDefault(x => x.ID == clanMemberModel.ClanID);

                    if (clanModel == null)
                    {
                        return(ValidatedView <List <ClanDiplomacyView> > .Invalid(ErrorCode.CLAN_NOT_FOUND));
                    }
                    else
                    {
                        List <ClanDiplomacyView> diplomacies = new List <ClanDiplomacyView>();
                        foreach (ClanRelationPendingModel clanRelationModel in await Model <ClanRelationPendingModel>
                                 .AsQueryable().Where(x => x.InitiatorID == clanModel.ID || x.TargetID == clanModel.ID)
                                 .ToList())
                        {
                            ClanDiplomacyView clanDiplomacyView = Mapper <ClanRelationPendingModel> .Map <ClanDiplomacyView>(clanRelationModel);

                            ValidatedView <ClanView> validatedView = await RetrieveClan(accountId,
                                                                                        (clanRelationModel.InitiatorID == clanModel.ID ? clanRelationModel.TargetID
                                : clanRelationModel.InitiatorID));

                            if (!validatedView.IsValid)
                            {
                                GameContext.Logger.LogWarning($"Failed to load pending diplomacy for {clanModel.ID}!");
                                continue;
                            }

                            clanDiplomacyView.Clan = validatedView.Object;
                            diplomacies.Add(clanDiplomacyView);
                        }

                        return(ValidatedView <List <ClanDiplomacyView> > .Valid(diplomacies));
                    }
                }
                else
                {
                    return(ValidatedView <List <ClanDiplomacyView> > .Invalid(ErrorCode.CLAN_NOT_MEMBER));
                }
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView <List <ClanDiplomacyView> > .Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #26
0
        public static async Task <ValidatedView> Register(AccountRegisterView accountRegisterView)
        {
            if (!accountRegisterView.IsValid(out string message))
            {
                return(ValidatedView.Invalid(message));
            }

            try {
                if (await Model <AccountModel> .AsQueryable()
                    .Any(x => x.Username == accountRegisterView.Username))
                {
                    return(ValidatedView.Invalid(ErrorCode.USERNAME_ALREADY_IN_USE));
                }

                AccountModel accountModel = new AccountModel(accountRegisterView.Username,
                                                             accountRegisterView.Password, accountRegisterView.Email, accountRegisterView.Faction);

                int tries = 5;
                while (await Model <AccountModel> .AsQueryable().Any(x => x.ID == accountModel.ID))
                {
                    accountModel.ID = RandomGenerator.UniqueIdentifier();

                    if (tries-- == 0)
                    {
                        return(ValidatedView.Invalid(ErrorCode.PROBLEM_WHILE_CREATING_ACCOUNT));
                    }
                }

                AccountVaultModel  accountVaultModel = new AccountVaultModel(accountModel.ID);
                List <HangarModel> hangarModels      = accountVaultModel.Ships
                                                       .Select(ship => new HangarModel(accountModel.ID, ship, accountModel.FactionID))
                                                       .ToList();

                await Model <AccountModel> .AsCollection().InsertOneAsync(accountModel);

                await Model <AccountChatModel> .AsCollection().InsertOneAsync(new AccountChatModel(accountModel.ID));

                await Model <AccountCooldownModel> .AsCollection().InsertOneAsync(new AccountCooldownModel(accountModel.ID));

                await Model <AccountVaultModel> .AsCollection().InsertOneAsync(accountVaultModel);

                await Model <HangarModel> .AsCollection().InsertManyAsync(hangarModels);

                return(ValidatedView.Valid());
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED));
        }
        public static async Task <ValidatedView> UpdateHangar(int accountId, HangarDetailView hangarDetailView)
        {
            if (!hangarDetailView.IsValid(out string message))
            {
                return(ValidatedView.Invalid(message));
            }

            try {
                if (GameManager.Get(accountId, out PlayerController controller) && !controller.ZoneAssembly.CanEquip)
                {
                    return(ValidatedView.Invalid(ErrorCode.EQUIPMENT_NOT_POSSIBLE));
                }

                HangarModel hangarModel = await Model <HangarModel> .AsQueryable()
                                          .FirstOrDefault(x => x.AccountID == accountId && x.ShipID == hangarDetailView.ShipID);

                if (hangarModel == null)
                {
                    return(ValidatedView <HangarView> .Invalid(ErrorCode.NO_HANGAR_FOUND));
                }

                if (controller != null && controller.HangarAssembly.Ship.ID == hangarDetailView.ShipID)
                {
                    HangarView hangarView = Mapper <HangarModel> .Map <HangarView>(hangarModel);

                    hangarView.Configuration_1 = hangarDetailView.Configuration_1;
                    hangarView.Configuration_2 = hangarDetailView.Configuration_2;

                    UpdateHangarIngame(accountId, hangarView);
                }
                else
                {
                    hangarModel.Configuration_1 = Mapper <ConfigurationView> .Map <Configuration>(hangarDetailView.Configuration_1);

                    hangarModel.Configuration_2 = Mapper <ConfigurationView> .Map <Configuration>(hangarDetailView.Configuration_2);

                    ReplaceOneResult result = await Model <HangarModel> .AsCollection()
                                              .ReplaceOneAsync(x => x.AccountID == accountId && x.ShipID == hangarDetailView.ShipID, hangarModel);
                }

                return(ValidatedView.Valid());
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #28
0
        public static async Task <ValidatedView <ClanView> > RetrieveClanViewFromMember(int accountId)
        {
            try {
                ClanMemberModel clanMemberModel = await Model <ClanMemberModel>
                                                  .AsQueryable().FirstOrDefault(x => x.AccountID == accountId);

                if (clanMemberModel != null)
                {
                    return(await RetrieveClan(accountId, clanMemberModel.ClanID));
                }

                return(ValidatedView <ClanView> .Valid(null));
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView <ClanView> .Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #29
0
        public static async Task <ValidatedView> RevokeJoinRequest(int accountId, int clanId)
        {
            try {
                var result = await Model <ClanMemberPendingModel>
                             .AsCollection()
                             .DeleteOneAsync(x => x.AccountID == accountId && x.ClanID == clanId);

                if (result.DeletedCount == 0)
                {
                    return(ValidatedView.Invalid(ErrorCode.CLAN_FAILED_TO_REVOKE_REQUEST));
                }

                return(ValidatedView.Valid());
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED));
        }
Exemple #30
0
        public static async Task <ValidatedView> UpdateCooldown(int accountId, CooldownView cooldown)
        {
            try {
                ReplaceOneResult result = await Model <AccountCooldownModel> .AsCollection()
                                          .ReplaceOneAsync(x => x.AccountID == accountId, Mapper <CooldownView>
                                                           .Map(cooldown, new AccountCooldownModel {
                    AccountID = accountId
                }));

                if (result.IsModifiedCountAvailable && result.ModifiedCount == 1)
                {
                    return(ValidatedView.Valid());
                }
            } catch (Exception e) {
                GameContext.Logger.LogError(e);
            }
            return(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED));
        }