typescript hates me

This commit is contained in:
Ordis 2023-11-22 16:09:32 +01:00
parent 125d03d03a
commit e7245bbb68
6 changed files with 172 additions and 3 deletions

View File

@ -28,6 +28,7 @@ const inventoryController: RequestHandler = async (request: Request, response: R
if (config.testMission) inventoryResponse.Missions = testMissions; if (config.testMission) inventoryResponse.Missions = testMissions;
if (config.testQuestKey) inventoryResponse.QuestKeys = testQuestKeys; if (config.testQuestKey) inventoryResponse.QuestKeys = testQuestKeys;
inventoryResponse.DuviriInfo = { Seed: -5049874987509758080, NumCompletions: 0 };
response.json(inventoryResponse); response.json(inventoryResponse);
}; };

View File

@ -1,13 +1,57 @@
import { Inventory } from "@/src/models/inventoryModel"; import { Inventory } from "@/src/models/inventoryModel";
import { RequestHandler } from "express"; import { RequestHandler } from "express";
import util from "util"; import util from "util";
import { EquipmentCategories, ISaveLoadoutRequest } from "@/src/types/saveLoadoutTypes";
import { isObject } from "@/src/helpers/general";
import { ISuitResponse } from "@/src/types/inventoryTypes/SuitTypes";
export const isObjectEmpty = (obj: Record<string, unknown>) => {
return obj && Object.keys(obj).length === 0 && obj.constructor === Object;
};
export const handleInventoryItemConfigChange = (equipmentChanges: ISaveLoadoutRequest) => {
for (const [equipmentName, equipment] of Object.entries(equipmentChanges)) {
//console.log(equipmentName);
if (!isObjectEmpty(equipment)) {
// non-empty is a change in loadout(or suit...)
switch (equipmentName) {
case "LoadOuts": {
console.log("loadout received");
for (const [loadoutName, loadout] of Object.entries(equipment)) {
console.log(loadoutName, loadout);
//if (!isObjectEmpty(loadout))
}
break;
}
default:
console.log("category not implemented", equipmentName);
}
// Object.keys(value).forEach(element => {
// console.log("name of inner objects keys", element);
// });
// for (const innerValue of Object.values(value)) {
// console.log(innerValue);
// }
}
// console.log(innerObjects);
// if (isObjectEmpty(innerObjects)) {
// console.log(innerObjects, "is empty");
// }
}
};
// eslint-disable-next-line @typescript-eslint/no-misused-promises // eslint-disable-next-line @typescript-eslint/no-misused-promises
const saveLoadoutController: RequestHandler = async (req, res) => { const saveLoadoutController: RequestHandler = async (req, res) => {
const body = JSON.parse(req.body); //validate here
console.log(util.inspect(body, { showHidden: false, depth: null, colors: true })); const body: ISaveLoadoutRequest = JSON.parse(req.body as string) as ISaveLoadoutRequest;
// console.log(util.inspect(body, { showHidden: false, depth: null, colors: true }));
res.sendStatus(200); const { UpgradeVer, ...equipmentChanges } = body;
handleInventoryItemConfigChange(body);
res.status(200).end();
}; };
export { saveLoadoutController }; export { saveLoadoutController };

View File

@ -55,4 +55,12 @@ const parseBoolean = (booleanCandidate: unknown): boolean => {
return booleanCandidate; return booleanCandidate;
}; };
export const isObject = (objectCandidate: unknown): objectCandidate is Record<string, unknown> => {
return (
(typeof objectCandidate === "object" || objectCandidate instanceof Object) &&
objectCandidate !== null &&
!Array.isArray(objectCandidate)
);
};
export { isString, isNumber, parseString, parseNumber, parseDateNumber, parseBoolean, parseEmail }; export { isString, isNumber, parseString, parseNumber, parseDateNumber, parseBoolean, parseEmail };

View File

@ -9,6 +9,7 @@ export interface ISuitDocument extends Document, ISuitResponse {
export interface ISuitResponse extends ISuitDatabase { export interface ISuitResponse extends ISuitDatabase {
ItemId: IOid; ItemId: IOid;
//should omit _id which is not present in response
} }
export interface ISuitDatabase { export interface ISuitDatabase {

View File

@ -14,6 +14,7 @@ export interface IInventoryDatabase extends Omit<IInventoryResponse, "TrainingDa
export interface IInventoryResponseDocument extends IInventoryResponse, Document {} export interface IInventoryResponseDocument extends IInventoryResponse, Document {}
export interface IInventoryResponse { export interface IInventoryResponse {
DuviriInfo: { Seed: number; NumCompletions: number };
SubscribedToEmails: number; SubscribedToEmails: number;
Created: IMongoDate; Created: IMongoDate;
RewardSeed: number; RewardSeed: number;

View File

@ -0,0 +1,114 @@
export interface ISaveLoadoutRequest {
LoadOuts: { [key: string]: LoadOut };
LongGuns: { [key: string]: Config };
OperatorAmps: { [key: string]: Config };
Pistols: { [key: string]: Config };
Suits: { [key: string]: Config };
Melee: {};
Sentinels: {};
SentinelWeapons: {};
KubrowPets: {};
SpaceSuits: {};
SpaceGuns: {};
SpaceMelee: {};
Scoops: {};
SpecialItems: {};
MoaPets: {};
Hoverboards: {};
DataKnives: {};
MechSuits: {};
CrewShipHarnesses: {};
Horses: {};
DrifterMelee: {};
UpgradeVer: number;
OperatorLoadOuts: {};
AdultOperatorLoadOuts: {};
KahlLoadOuts: {};
CrewShips: {};
}
export interface Config {
Upgrades: any[];
PvpUpgrades: any[];
Skins: string[];
pricol: Pricol;
attcol: Pricol;
sigcol: Sigcol;
eyecol: Pricol;
facial: Pricol;
cloth: Pricol;
syancol: Pricol;
Songs: any[];
}
export interface Pricol {
t0: number;
t1: number;
t2: number;
t3: number;
m0: number;
m1: number;
en: number;
}
export interface Sigcol {
t0: number;
t1: number;
m0: number;
en: number;
}
export interface Col {
t0: number;
t1: number;
t2: number;
t3: number;
m0?: number;
m1?: number;
en: number;
e1?: number;
}
export interface LoadOuts {
NORMAL: LoadOut;
SENTINEL: LoadOut;
ARCHWING: LoadOut;
NORMAL_PVP: LoadOut;
LUNARO: LoadOut;
OPERATOR: LoadOut;
KDRIVE: LoadOut;
DATAKNIFE: LoadOut;
MECH: LoadOut;
OPERATOR_ADULT: LoadOut;
DRIFTER: LoadOut;
}
type LoadOut = {
Upgrades: Config[];
PvpUpgrades: Config[];
Skins: string[];
pricol: { [key: string]: number };
attcol: Config;
sigcol: Config;
eyecol: Config;
facial: Config;
cloth: Config;
syancol: Config;
Songs: Config[];
};
export type EquipmentCategories =
| { LoadOuts: { [key in keyof LoadOuts]: LoadOut } }
| { LongGuns: Config }
| { OperatorAmps: Config } // Replace 'any' with the actual type
| { Pistols: Config } // Replace 'any' with the actual type
| { Suits: { [key: string]: Config } }
| { Melee: Config } // Replace 'any' with the actual type
| { Sentinels: Config } // Replace 'any' with the actual type
| { SentinelWeapons: Config } // Replace 'any' with the actual type
// Add other categories based on your needs
| { UpgradeVer: number }
| { OperatorLoadOuts: Config } // Replace 'any' with the actual type
| { AdultOperatorLoadOuts: Config } // Replace 'any' with the actual type
| { KahlLoadOuts: Config } // Replace 'any' with the actual type
| { CrewShips: Config }; // Replace 'any' with the actual type