2023-06-02 00:20:49 -03:00
|
|
|
import { Account } from "@/src/models/loginModel";
|
2023-06-04 03:06:22 +02:00
|
|
|
import { createInventory } from "@/src/services/inventoryService";
|
2024-12-23 00:40:35 +01:00
|
|
|
import { IDatabaseAccount, IDatabaseAccountJson } from "@/src/types/loginTypes";
|
2023-06-05 04:16:49 +08:00
|
|
|
import { createShip } from "./shipService";
|
2024-12-23 22:44:01 +01:00
|
|
|
import { Document, Types } from "mongoose";
|
2024-02-18 13:58:43 +01:00
|
|
|
import { Loadout } from "@/src/models/inventoryModels/loadoutModel";
|
|
|
|
import { PersonalRooms } from "@/src/models/personalRoomsModel";
|
2024-05-28 13:45:06 +02:00
|
|
|
import { Request } from "express";
|
2024-12-23 22:44:01 +01:00
|
|
|
import { config } from "@/src/services/configService";
|
2025-02-06 04:42:59 -08:00
|
|
|
import { createStats } from "@/src/services/statsService";
|
2023-05-19 15:22:48 -03:00
|
|
|
|
2024-05-28 13:45:06 +02:00
|
|
|
export const isCorrectPassword = (requestPassword: string, databasePassword: string): boolean => {
|
2023-05-23 20:42:06 -04:00
|
|
|
return requestPassword === databasePassword;
|
2023-05-19 15:22:48 -03:00
|
|
|
};
|
|
|
|
|
2024-12-23 22:44:01 +01:00
|
|
|
export const isNameTaken = async (name: string): Promise<boolean> => {
|
|
|
|
return !!(await Account.findOne({ DisplayName: name }));
|
|
|
|
};
|
|
|
|
|
2024-12-23 00:40:35 +01:00
|
|
|
export const createAccount = async (accountData: IDatabaseAccount): Promise<IDatabaseAccountJson> => {
|
2023-05-23 20:42:06 -04:00
|
|
|
const account = new Account(accountData);
|
|
|
|
try {
|
|
|
|
await account.save();
|
2023-12-14 17:34:15 +01:00
|
|
|
const loadoutId = await createLoadout(account._id);
|
2024-02-18 13:58:43 +01:00
|
|
|
const shipId = await createShip(account._id);
|
|
|
|
await createInventory(account._id, { loadOutPresetId: loadoutId, ship: shipId });
|
|
|
|
await createPersonalRooms(account._id, shipId);
|
2025-02-06 04:42:59 -08:00
|
|
|
await createStats(account._id.toString());
|
2023-05-23 20:42:06 -04:00
|
|
|
return account.toJSON();
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
throw new Error(error.message);
|
|
|
|
}
|
|
|
|
throw new Error("error creating account that is not of instance Error");
|
2023-05-19 15:22:48 -03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-12-29 21:47:18 +01:00
|
|
|
export const createLoadout = async (accountId: Types.ObjectId): Promise<Types.ObjectId> => {
|
2024-02-18 13:58:43 +01:00
|
|
|
const loadout = new Loadout({ loadoutOwnerId: accountId });
|
2023-12-14 17:34:15 +01:00
|
|
|
const savedLoadout = await loadout.save();
|
|
|
|
return savedLoadout._id;
|
|
|
|
};
|
2024-02-18 13:58:43 +01:00
|
|
|
|
2024-12-29 21:47:18 +01:00
|
|
|
export const createPersonalRooms = async (accountId: Types.ObjectId, shipId: Types.ObjectId): Promise<void> => {
|
2024-02-18 13:58:43 +01:00
|
|
|
const personalRooms = new PersonalRooms({
|
|
|
|
personalRoomsOwnerId: accountId,
|
|
|
|
activeShipId: shipId
|
|
|
|
});
|
2025-01-20 18:25:50 +01:00
|
|
|
if (config.skipTutorial) {
|
2025-01-24 14:13:21 +01:00
|
|
|
// // Vor's Prize rewards
|
|
|
|
// const defaultFeatures = [
|
|
|
|
// "/Lotus/Types/Items/ShipFeatureItems/EarthNavigationFeatureItem",
|
|
|
|
// "/Lotus/Types/Items/ShipFeatureItems/MercuryNavigationFeatureItem",
|
|
|
|
// "/Lotus/Types/Items/ShipFeatureItems/ArsenalFeatureItem",
|
|
|
|
// "/Lotus/Types/Items/ShipFeatureItems/SocialMenuFeatureItem",
|
|
|
|
// "/Lotus/Types/Items/ShipFeatureItems/FoundryFeatureItem",
|
|
|
|
// "/Lotus/Types/Items/ShipFeatureItems/ModsFeatureItem"
|
|
|
|
// ];
|
|
|
|
// personalRooms.Ship.Features.push(...defaultFeatures);
|
2025-01-20 18:25:50 +01:00
|
|
|
}
|
2024-02-18 13:58:43 +01:00
|
|
|
await personalRooms.save();
|
|
|
|
};
|
2024-05-28 13:45:06 +02:00
|
|
|
|
2024-12-23 22:44:01 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
type TAccountDocument = Document<unknown, {}, IDatabaseAccountJson> &
|
|
|
|
IDatabaseAccountJson & { _id: Types.ObjectId; __v: number };
|
|
|
|
|
|
|
|
export const getAccountForRequest = async (req: Request): Promise<TAccountDocument> => {
|
2024-05-28 13:45:06 +02:00
|
|
|
if (!req.query.accountId) {
|
|
|
|
throw new Error("Request is missing accountId parameter");
|
|
|
|
}
|
|
|
|
if (!req.query.nonce || parseInt(req.query.nonce as string) === 0) {
|
|
|
|
throw new Error("Request is missing nonce parameter");
|
|
|
|
}
|
2024-12-23 22:44:01 +01:00
|
|
|
const account = await Account.findOne({
|
|
|
|
_id: req.query.accountId,
|
|
|
|
Nonce: req.query.nonce
|
|
|
|
});
|
2024-05-28 13:45:06 +02:00
|
|
|
if (!account) {
|
|
|
|
throw new Error("Invalid accountId-nonce pair");
|
|
|
|
}
|
2024-12-22 00:44:49 +01:00
|
|
|
return account;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getAccountIdForRequest = async (req: Request): Promise<string> => {
|
2025-01-19 01:58:35 +01:00
|
|
|
if (!req.query.accountId) {
|
|
|
|
throw new Error("Request is missing accountId parameter");
|
|
|
|
}
|
|
|
|
if (!req.query.nonce || parseInt(req.query.nonce as string) === 0) {
|
|
|
|
throw new Error("Request is missing nonce parameter");
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
!(await Account.exists({
|
|
|
|
_id: req.query.accountId,
|
|
|
|
Nonce: req.query.nonce
|
|
|
|
}))
|
|
|
|
) {
|
|
|
|
throw new Error("Invalid accountId-nonce pair");
|
|
|
|
}
|
|
|
|
return req.query.accountId as string;
|
2024-05-28 13:45:06 +02:00
|
|
|
};
|
2024-12-23 22:44:01 +01:00
|
|
|
|
|
|
|
export const isAdministrator = (account: TAccountDocument): boolean => {
|
2024-12-29 23:34:26 +01:00
|
|
|
if (!config.administratorNames) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (typeof config.administratorNames == "string") {
|
|
|
|
return config.administratorNames == account.DisplayName;
|
|
|
|
}
|
|
|
|
return !!config.administratorNames.find(x => x == account.DisplayName);
|
2024-12-23 22:44:01 +01:00
|
|
|
};
|