Skip to content

Commit

Permalink
[Testing][Feature] items and updated drops functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
airfortech authored Apr 1, 2024
2 parents 719d833 + 4a21929 commit f2dfe2f
Show file tree
Hide file tree
Showing 63 changed files with 2,543 additions and 74 deletions.
2 changes: 2 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { createInitialSettings } from "./db/tools/createInitialSettings";
import { languageDetector } from "./utils/languageDetector";
import { config } from "./config/config";
import { settingsRouter } from "./routes/settings";
import { itemsRouter } from "./routes/items";

export const shedules: {
backupSchedule: ScheduledTask | null;
Expand Down Expand Up @@ -51,6 +52,7 @@ app.use("/api/locations", locationsRouter);
app.use("/api/keygivers/drops", keyGiverDropsRouter);
app.use("/api/keygivers", keyGiversRouter);
app.use("/api/keys", keysRouter);
app.use("/api/items", itemsRouter);
app.use("/api/privileges", privilegesRouter);
app.use("/api/backups", backupsRouter);
app.use("/api/users", usersRouter);
Expand Down
27 changes: 27 additions & 0 deletions config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ export const config: Config = {
keyGiverDrops: 500,
keyGiverDropsMonthsBack: 50,
keyGiverDropsNoDropPercentage: 50,
keyGiverDropsNoMagicItemsDropPercentage: 50,
keyGiverDropsNoNextDatePercentage: 20,
itemsCommentPercentage: 20,
itemsDescriptionPercentage: 20,
weightPercentage: 30,
volumePercentage: 30,
costPercentage: 30,
vendorCostPercentage: 30,
weapons: 80,
magicWeaponsPercentage: 20,
weaponHandSpecifiedPercentage: 30,
weaponDamageTypeSpecifiedPercentage: 30,
weaponStatsPercentage: 30,
silverWeaponsPercentage: 20,
armors: 40,
magicArmorsPercentage: 30,
armorClassSpecifiedPercentage: 40,
armorStatsPercentage: 40,
armorBodyPartSpecifiedPercentage: 50,
shields: 40,
magicShieldsPercentage: 30,
shieldStatsPercentage: 40,
shieldParrySpecifiedPercentage: 50,
clothes: 40,
magicClothesPercentage: 20,
jewellery: 40,
magicJewelleryPercentage: 40,
itemSlotsPercentage: 30,
},
};
49 changes: 49 additions & 0 deletions controllers/items/addItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { messages, Status } from "../../types/responseMessages";
import { Request } from "../../types/Request";
import {
ItemAddArmorRequest,
ItemAddRequest,
ItemAddShieldRequest,
ItemAddWeaponRequest,
} from "../../types/Item";
import { ItemTypes } from "../../types/ItemTypes";
import { Response } from "express";
import { Item } from "../../db/models/Item";
import { addWeapon } from "./weapon/addWeapon";
import { addArmor } from "./armor/addArmor";
import { addOtherItem } from "./other/addOtherItem";
import { addShield } from "./shield/addShield";
import { CustomError } from "../../utils/customError";

export const addItem = async (req: Request, res: Response, type: ItemTypes) => {
try {
const data = req.body as
| ItemAddRequest
| ItemAddArmorRequest
| ItemAddWeaponRequest
| ItemAddShieldRequest;
const short = data.short.trim().toLowerCase();
const item = await Item.findOne({ short });
if (item)
throw new CustomError(
messages[req.lang].items.itemShortExists(short),
400,
Status.error
);
const args =
type === ItemTypes.weapon
? addWeapon(data as ItemAddWeaponRequest, type, short)
: type === ItemTypes.armor
? addArmor(data as ItemAddWeaponRequest, type, short)
: type === ItemTypes.shield
? addShield(data as ItemAddShieldRequest, type, short)
: addOtherItem(data as ItemAddRequest, type, short);
await new Item({ ...args }).save();
return res.status(200).json({
status: Status.success,
message: messages[req.lang].items.itemAdded(short),
});
} catch (e) {
throw e;
}
};
23 changes: 23 additions & 0 deletions controllers/items/armor/addArmor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ItemAddArmorRequest } from "../../../types/Item";
import { ItemTypes } from "../../../types/ItemTypes";

export const addArmor = (
data: ItemAddArmorRequest,
type: ItemTypes,
short: string
) => {
return {
...data,
short,
type,
weaponType: null as null,
weaponHand: null as null,
weaponSlashingDamage: null as null,
weaponPiercingDamage: null as null,
weaponBluntDamage: null as null,
weaponEffectiveness: null as null,
weaponBalance: null as null,
isWeaponSilver: null as null,
shieldParry: null as null,
};
};
18 changes: 18 additions & 0 deletions controllers/items/armor/updateArmor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ItemUpdateRequest } from "../../../types/Item";
import { ItemTypes } from "../../../types/ItemTypes";

export const updateArmor = (data: ItemUpdateRequest, type: ItemTypes) => {
return {
...data,
type,
weaponType: null as null,
weaponHand: null as null,
weaponSlashingDamage: null as null,
weaponPiercingDamage: null as null,
weaponBluntDamage: null as null,
weaponEffectiveness: null as null,
weaponBalance: null as null,
isWeaponSilver: null as null,
shieldParry: null as null,
};
};
26 changes: 26 additions & 0 deletions controllers/items/deleteItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { messages, Status } from "../../types/responseMessages";
import { Request } from "../../types/Request";
import { Item } from "../../db/models/Item";
import { Response } from "express";
import { CustomError } from "../../utils/customError";
import { isIdValid } from "../../db/validators/universalValidators";

export const deleteItem = async (req: Request, res: Response) => {
try {
const id = req.params.id;
isIdValid(id, messages[req.lang].items.itemNotExists, 404);
const item = await Item.findOneAndRemove({ _id: id });
if (!item)
throw new CustomError(
messages[req.lang].items.itemNotExists,
404,
Status.error
);
return res.status(200).json({
status: Status.success,
message: messages[req.lang].items.itemDeleted(item.short),
});
} catch (e) {
throw e;
}
};
102 changes: 102 additions & 0 deletions controllers/items/getItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Status, messages } from "../../types/responseMessages";
import { Request } from "../../types/Request";
import { ItemResponse } from "../../types/Item";
import { Response } from "express";
import { Item } from "../../db/models/Item";
import { isIdValid } from "../../db/validators/universalValidators";
import { CustomError } from "../../utils/customError";

export const getItem = async (req: Request, res: Response) => {
try {
const id = req.params.id;
isIdValid(id, messages[req.lang].items.itemNotExists, 404);
const item = await Item.findOne({ _id: id });
if (!item)
throw new CustomError(
messages[req.lang].items.itemNotExists,
404,
Status.error
);
const {
name,
short,
isMagic,
type,
slot,
weaponType,
weaponHand,
weaponSlashingDamage,
weaponPiercingDamage,
weaponBluntDamage,
weaponEffectiveness,
weaponBalance,
isWeaponSilver,
armorClass,
armorHead,
armorLeftArm,
armorRightArm,
armorChest,
armorLegs,
armorFoots,
armorHands,
armorPiercingRes,
armorSlashingRes,
armorBluntRes,
shieldParry,
weight,
volume,
durability,
specialBonus,
occurrence,
cost,
vendorCost,
description,
comment,
} = item;
const data: ItemResponse = {
id,
name,
short,
isMagic,
type,
slot,
weaponType,
weaponHand,
weaponSlashingDamage,
weaponPiercingDamage,
weaponBluntDamage,
weaponEffectiveness,
weaponBalance,
isWeaponSilver,
armorClass,
armorHead,
armorLeftArm,
armorRightArm,
armorChest,
armorLegs,
armorFoots,
armorHands,
armorPiercingRes,
armorSlashingRes,
armorBluntRes,
shieldParry,
weight,
volume,
durability,
specialBonus,
occurrence,
cost,
vendorCost,
description,
comment,
};
return res.status(200).json({
status: Status.success,
data: {
item: data,
},
});
} catch (e) {
throw e;
}
};
121 changes: 121 additions & 0 deletions controllers/items/getItems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Status } from "../../types/responseMessages";
import { Request } from "../../types/Request";
import { ItemResponse } from "../../types/Item";
import { ItemTypes } from "../../types/ItemTypes";
import { ItemWeapon } from "../../types/ItemWeapon";
import { ItemArmorClass } from "../../types/ItemArmorClass";
import { Response } from "express";
import { Item } from "../../db/models/Item";

export const getItems = async (req: Request, res: Response) => {
try {
const isMagic =
typeof req.query.isMagic === "string"
? JSON.parse(req.query.isMagic)
: undefined;
const isWeaponSilver =
typeof req.query.isWeaponSilver === "string"
? JSON.parse(req.query.isWeaponSilver)
: undefined;
const type = ItemTypes[req.query.type as keyof typeof ItemTypes];
const weaponType =
ItemWeapon[req.query.weaponType as keyof typeof ItemWeapon];
const armorClass =
ItemArmorClass[req.query.armorClass as keyof typeof ItemArmorClass];

const items = await Item.find({
...(isMagic !== undefined && { isMagic }),
...(isWeaponSilver !== undefined && { isWeaponSilver }),
type,
...(weaponType !== undefined && { weaponType }),
...(armorClass !== undefined && { armorClass }),
});
return res.status(200).json({
status: Status.success,
data: {
items: items
.map(
({
id,
name,
short,
isMagic,
type,
slot,
weaponType,
weaponHand,
weaponSlashingDamage,
weaponPiercingDamage,
weaponBluntDamage,
weaponEffectiveness,
weaponBalance,
isWeaponSilver,
armorClass,
armorHead,
armorLeftArm,
armorRightArm,
armorChest,
armorLegs,
armorFoots,
armorHands,
armorPiercingRes,
armorSlashingRes,
armorBluntRes,
shieldParry,
weight,
volume,
durability,
specialBonus,
occurrence,
cost,
vendorCost,
description,
comment,
}) => {
const data: ItemResponse = {
id,
name,
short,
isMagic,
type,
slot,
weaponType,
weaponHand,
weaponSlashingDamage,
weaponPiercingDamage,
weaponBluntDamage,
weaponEffectiveness,
weaponBalance,
isWeaponSilver,
armorClass,
armorHead,
armorLeftArm,
armorRightArm,
armorChest,
armorLegs,
armorFoots,
armorHands,
armorPiercingRes,
armorSlashingRes,
armorBluntRes,
shieldParry,
weight,
volume,
durability,
specialBonus,
occurrence,
cost,
vendorCost,
description,
comment,
};
return data;
}
)
.sort((a, b) => a.short.localeCompare(b.short)),
},
});
} catch (e) {
throw e;
}
};
Loading

0 comments on commit f2dfe2f

Please sign in to comment.