Add Ship
This commit is contained in:
parent
c6687e3b4d
commit
78f93bd5a7
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@
|
||||
|
||||
/.env
|
||||
/static/data/*
|
||||
yarn.lock
|
||||
|
@ -1,8 +1,22 @@
|
||||
import { Account } from "@/src/models/loginModel";
|
||||
import { Ship } from "@/src/models/shipModel";
|
||||
import { createShip } from "@/src/services/shipService";
|
||||
import { RequestHandler } from "express";
|
||||
import getShip from "@/static/fixed_responses/getShip.json";
|
||||
|
||||
const getShipController: RequestHandler = (_req, res) => {
|
||||
res.json(getShip);
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
const getShipController: RequestHandler = async (_req, res) => {
|
||||
const accountId = _req.query.accountId;
|
||||
const ship = await Ship.findOne({ ShipOwnerId: accountId });
|
||||
if (!ship) {
|
||||
const account = await Account.findOne({ _id: accountId });
|
||||
if (account) {
|
||||
await createShip(account._id);
|
||||
const new_ship = await Ship.findOne({ ShipOwnerId: accountId });
|
||||
res.json(new_ship);
|
||||
return;
|
||||
}
|
||||
}
|
||||
res.json(ship);
|
||||
};
|
||||
|
||||
export { getShipController };
|
||||
|
@ -9,6 +9,7 @@ import { createAccount, isCorrectPassword } from "@/src/services/loginService";
|
||||
import { ILoginResponse } from "@/src/types/loginTypes";
|
||||
import { DTLS, groups, HUB, IRC, Nonce, NRS, platformCDNs } from "@/static/fixed_responses/login_static";
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
const loginController: RequestHandler = async (request, response) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument
|
||||
const body = JSON.parse(request.body); // parse octet stream of json data to json object
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Document, Schema, model } from "mongoose";
|
||||
import { IInventoryDatabase, IInventoryResponse, ISuitDatabase, ISuitDocument, Oid } from "../types/inventoryTypes";
|
||||
import { Schema, model } from "mongoose";
|
||||
import { IInventoryDatabase, ISuitDatabase } from "../types/inventoryTypes";
|
||||
import { Oid } from "../types/commonTypes";
|
||||
|
||||
const polaritySchema = new Schema({
|
||||
Slot: Number,
|
||||
@ -65,7 +66,7 @@ const suitSchema = new Schema({
|
||||
});
|
||||
|
||||
suitSchema.set("toJSON", {
|
||||
transform(_document, returnedObject: ISuitDocument) {
|
||||
transform(_document, returnedObject) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
||||
returnedObject.ItemId = { $oid: returnedObject._id.toString() } satisfies Oid;
|
||||
delete returnedObject._id;
|
||||
@ -246,7 +247,7 @@ const inventorySchema = new Schema({
|
||||
});
|
||||
|
||||
inventorySchema.set("toJSON", {
|
||||
transform(_document, returnedObject: ISuitDocument) {
|
||||
transform(_document, returnedObject) {
|
||||
delete returnedObject._id;
|
||||
delete returnedObject.__v;
|
||||
}
|
||||
|
46
src/models/shipModel.ts
Normal file
46
src/models/shipModel.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { Schema, model } from "mongoose";
|
||||
import { IShipDatabase } from "../types/shipTypes";
|
||||
import { Oid } from "../types/commonTypes";
|
||||
|
||||
const roomSchema = new Schema({
|
||||
Name: String,
|
||||
MaxCapacity: Number
|
||||
});
|
||||
|
||||
roomSchema.set("toJSON", {
|
||||
transform(_document, returnedObject) {
|
||||
delete returnedObject._id;
|
||||
}
|
||||
});
|
||||
|
||||
const shipSchema = new Schema({
|
||||
Rooms: [roomSchema],
|
||||
Features: [Schema.Types.Mixed],
|
||||
ContentUrlSignature: String
|
||||
});
|
||||
|
||||
const apartmentSchema = new Schema({
|
||||
Rooms: [roomSchema],
|
||||
FavouriteLoadouts: [Schema.Types.Mixed]
|
||||
});
|
||||
|
||||
const shipDatabaseSchema = new Schema({
|
||||
ShipOwnerId: String,
|
||||
Ship: shipSchema,
|
||||
Apartment: apartmentSchema
|
||||
});
|
||||
|
||||
shipDatabaseSchema.set("toJSON", {
|
||||
transform(_document, returnedObject) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
||||
returnedObject.Ship.ShipId = { $oid: returnedObject._id.toString() } satisfies Oid;
|
||||
delete returnedObject._id;
|
||||
delete returnedObject.Ship._id;
|
||||
delete returnedObject.Apartment._id;
|
||||
delete returnedObject.__v;
|
||||
}
|
||||
});
|
||||
|
||||
const Ship = model<IShipDatabase>("Ship", shipDatabaseSchema);
|
||||
|
||||
export { Ship };
|
@ -1,6 +1,7 @@
|
||||
import { Account } from "@/src/models/loginModel";
|
||||
import { createInventory } from "@/src/services/inventoryService";
|
||||
import { IDatabaseAccount } from "@/src/types/loginTypes";
|
||||
import { createShip } from "./shipService";
|
||||
|
||||
const isCorrectPassword = (requestPassword: string, databasePassword: string): boolean => {
|
||||
return requestPassword === databasePassword;
|
||||
@ -11,6 +12,7 @@ const createAccount = async (accountData: IDatabaseAccount) => {
|
||||
try {
|
||||
await account.save();
|
||||
await createInventory(account._id);
|
||||
await createShip(account._id);
|
||||
return account.toJSON();
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
|
17
src/services/shipService.ts
Normal file
17
src/services/shipService.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { Ship } from "@/src/models/shipModel";
|
||||
import new_ship from "@/static/fixed_responses/postShip.json";
|
||||
import { Types } from "mongoose";
|
||||
|
||||
const createShip = async (accountOwnerId: Types.ObjectId) => {
|
||||
try {
|
||||
const ship = new Ship({ ...new_ship, ShipOwnerId: accountOwnerId });
|
||||
await ship.save();
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
throw new Error(`error creating inventory" ${error.message}`);
|
||||
}
|
||||
throw new Error("error creating inventory that is not of instance Error");
|
||||
}
|
||||
};
|
||||
|
||||
export { createShip };
|
3
src/types/commonTypes.ts
Normal file
3
src/types/commonTypes.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export interface Oid {
|
||||
$oid: string;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
import { Document, Types } from "mongoose";
|
||||
import { Oid } from "./commonTypes";
|
||||
|
||||
export interface IInventoryDatabase extends IInventoryResponse {
|
||||
accountOwnerId: Types.ObjectId;
|
||||
@ -187,10 +188,6 @@ export interface AdultOperatorLoadOut {
|
||||
ItemId: Oid;
|
||||
}
|
||||
|
||||
export interface Oid {
|
||||
$oid: string;
|
||||
}
|
||||
|
||||
export interface Color {
|
||||
t0?: number;
|
||||
t1?: number;
|
||||
|
29
src/types/shipTypes.ts
Normal file
29
src/types/shipTypes.ts
Normal file
@ -0,0 +1,29 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
import { Types } from "mongoose";
|
||||
import { Oid } from "./inventoryTypes";
|
||||
|
||||
export type IShipDatabase = IShipResponse;
|
||||
|
||||
export interface IShipResponse {
|
||||
ShipOwnerId: Types.ObjectId;
|
||||
Ship: IShipClass;
|
||||
Apartment: IApartmentClass;
|
||||
}
|
||||
|
||||
export interface IShipClass {
|
||||
Rooms: IRoomsClass[];
|
||||
ShipId: Oid;
|
||||
Features: string[];
|
||||
ContentUrlSignature: string;
|
||||
}
|
||||
|
||||
export interface IRoomsClass {
|
||||
Name: string;
|
||||
MaxCapacity: number;
|
||||
}
|
||||
|
||||
export interface IApartmentClass {
|
||||
Rooms: IRoomsClass[];
|
||||
FavouriteLoadouts: string[];
|
||||
}
|
33
static/fixed_responses/postShip.json
Normal file
33
static/fixed_responses/postShip.json
Normal file
@ -0,0 +1,33 @@
|
||||
{
|
||||
"ShipOwnerId": "removed",
|
||||
"Ship": {
|
||||
"Features": [
|
||||
"/Lotus/Types/Items/ShipFeatureItems/EarthNavigationFeatureItem",
|
||||
"/Lotus/Types/Items/ShipFeatureItems/ArsenalFeatureItem",
|
||||
"/Lotus/Types/Items/ShipFeatureItems/SocialMenuFeatureItem",
|
||||
"/Lotus/Types/Items/ShipFeatureItems/ModsFeatureItem",
|
||||
"/Lotus/Types/Items/ShipFeatureItems/FoundryFeatureItem",
|
||||
"/Lotus/Types/Items/ShipFeatureItems/MercuryNavigationFeatureItem"
|
||||
],
|
||||
"ShipId": { "$oid": "removed" },
|
||||
"Rooms": [
|
||||
{ "Name": "AlchemyRoom", "MaxCapacity": 1600 },
|
||||
{ "Name": "BridgeRoom", "MaxCapacity": 1600 },
|
||||
{ "Name": "LisetRoom", "MaxCapacity": 1000 },
|
||||
{ "Name": "OperatorChamberRoom", "MaxCapacity": 1600 },
|
||||
{ "Name": "OutsideRoom", "MaxCapacity": 1600 },
|
||||
{ "Name": "PersonalQuartersRoom", "MaxCapacity": 1600 }
|
||||
],
|
||||
"ContentUrlSignature": "removed"
|
||||
},
|
||||
"Apartment": {
|
||||
"Rooms": [
|
||||
{ "Name": "ElevatorLanding", "MaxCapacity": 1600 },
|
||||
{ "Name": "ApartmentRoomA", "MaxCapacity": 1000 },
|
||||
{ "Name": "ApartmentRoomB", "MaxCapacity": 1600 },
|
||||
{ "Name": "ApartmentRoomC", "MaxCapacity": 1600 },
|
||||
{ "Name": "DuviriHallway", "MaxCapacity": 1600 }
|
||||
],
|
||||
"FavouriteLoadouts": []
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user