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 <VaultView> > RetrieveVault(int accountId) { try { // check if player is ingame if (GameManager.Players.TryGetValue(accountId, out PlayerController controller)) { return(ValidatedView <VaultView> .Valid(controller.Account.Vault)); } AccountVaultModel accountVaultModel = await Model <AccountVaultModel> .AsQueryable() .FirstOrDefault(x => x.AccountID == accountId); if (accountVaultModel == null) { return(ValidatedView <VaultView> .Invalid(ErrorCode.ACCOUNT_VAULT_NOT_FOUND)); } return(ValidatedView <VaultView> .Valid( Mapper <AccountVaultModel> .Map <VaultView>(accountVaultModel))); } catch (Exception e) { GameContext.Logger.LogError(e); } return(ValidatedView <VaultView> .Invalid(ErrorCode.OPERATION_FAILED)); }