Skip to content

Commit

Permalink
[Main][Feature] add multiple items functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
airfortech authored Apr 19, 2024
2 parents 2d7bcc0 + e99ccb2 commit d9411b8
Show file tree
Hide file tree
Showing 20 changed files with 345 additions and 45 deletions.
3 changes: 3 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cors from "cors";
import cookieParser from "cookie-parser";
import { ScheduledTask } from "node-cron";
import "express-async-errors";
import dayjs from "dayjs";
import { enemiesRouter } from "./routes/enemies";
import { authRouter } from "./routes/auth";
import { backupsRouter } from "./routes/backups";
Expand All @@ -29,6 +30,8 @@ export const shedules: {
};

(async () => {
console.log("-------------------------------");
console.log(dayjs().format("YYYY-MM-DD HH:mm:ss"));
await connectToDB();
await createInitialSettings();
})();
Expand Down
104 changes: 104 additions & 0 deletions controllers/items/addItems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { Status, messages } from "../../types/responseMessages";
import { Request } from "../../types/Request";
import { ItemsAddFormRequest } 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 { addShield } from "./shield/addShield";

export const addItems = async (req: Request, res: Response) => {
try {
const { items, task } = req.body as ItemsAddFormRequest;
let message = "";
const unifiedItems = items
.map(item =>
item.type === ItemTypes.weapon
? addWeapon(item, ItemTypes.weapon, item.short)
: item.type === ItemTypes.armor
? addArmor(item, ItemTypes.armor, item.short)
: item.type === ItemTypes.shield
? addShield(item, ItemTypes.shield, item.short)
: null
)
.filter(Boolean);

// INFO: add many items in one operation with db
const addedItems = await Item.insertMany(unifiedItems, {
// INFO: skip existing items
ordered: false,
});

if (task === "addNew") {
// message = messages[req.lang].items.itemAdded;
message = messages[req.lang].items.multipleItemsAdded(addedItems.length);
} else {
const operations = unifiedItems
.filter(
item => !addedItems.find(addedItem => addedItem.short === item.short)
)
.map(item => {
let itemToUpdate = null;
if (task === "updateAll") itemToUpdate = item;
else {
const {
short,
weaponSlashingDamage,
weaponPiercingDamage,
weaponBluntDamage,
weaponEffectiveness,
weaponBalance,
armorSlashingRes,
armorPiercingRes,
armorBluntRes,
shieldParry,
...restValues
} = item;
if (task === "updateInfosOnly") {
console.log("restValues", { ...restValues });
itemToUpdate = {
short,
...restValues,
};
console.log("itemToUpdate", itemToUpdate);
} else if (task === "updateValuesOnly")
itemToUpdate = {
short,
weaponSlashingDamage,
weaponPiercingDamage,
weaponBluntDamage,
weaponEffectiveness,
weaponBalance,
armorSlashingRes,
armorPiercingRes,
armorBluntRes,
shieldParry,
};
}
return {
updateOne: {
filter: { short: item.short },
update: itemToUpdate,
// INFO: if true, and no documents found, insert a new document, bulkWrite can provide conflict with true
upsert: false,
},
};
});
// INFO: Sends multiple insertOne, updateOne, updateMany, replaceOne, deleteOne, and/or deleteMany operations to the MongoDB server in one command. This is faster than sending multiple independent operations (e.g. if you use create()) because with bulkWrite() there is only one round trip to MongoDB.
const updatedItems = await Item.bulkWrite(operations);
message = messages[req.lang].items.multipleItemsAddedAndUpdated(
addedItems.length,
updatedItems.modifiedCount
);
}

return res.status(200).json({
status: Status.success,
message: message,
});
} catch (e) {
console.log(e);
throw e;
}
};
2 changes: 2 additions & 0 deletions controllers/items/getItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const getItem = async (req: Request, res: Response) => {
occurrence,
cost,
vendorCost,
npcPurchasePrice,
description,
comment,
} = item;
Expand Down Expand Up @@ -87,6 +88,7 @@ export const getItem = async (req: Request, res: Response) => {
occurrence,
cost,
vendorCost,
npcPurchasePrice,
description,
comment,
};
Expand Down
2 changes: 2 additions & 0 deletions controllers/items/getItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const getItems = async (req: Request, res: Response) => {
occurrence,
cost,
vendorCost,
npcPurchasePrice,
description,
comment,
}) => {
Expand Down Expand Up @@ -106,6 +107,7 @@ export const getItems = async (req: Request, res: Response) => {
occurrence,
cost,
vendorCost,
npcPurchasePrice,
description,
comment,
};
Expand Down
19 changes: 11 additions & 8 deletions controllers/keyGiverDrops/getEditableKeyGiverDrops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,17 @@ export const getEditableKeyGiverDrops = async (req: Request, res: Response) => {
short,
domain,
respawnTime,
locations: locations.map(({ id, locationId, name, domain }) => {
return {
id,
locationId,
name,
domain,
};
}),
locations: locations.map(
({ id, locationId, internalId, name, domain }) => {
return {
id,
locationId,
internalId,
name,
domain,
};
}
),
},
drop: drop ? { id: drop.id, name: drop.name } : null,
magicDrops: magicDrops.map(({ id, name, short }) => {
Expand Down
19 changes: 11 additions & 8 deletions controllers/keyGiverDrops/getKeyGiverDrops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,17 @@ export const getKeyGiverDrops = async (
short,
domain,
respawnTime,
locations: locations.map(({ id, locationId, name, domain }) => {
return {
id,
locationId,
name,
domain,
};
}),
locations: locations.map(
({ id, locationId, internalId, name, domain }) => {
return {
id,
locationId,
internalId,
name,
domain,
};
}
),
},
drop: drop ? { id: drop.id, name: drop.name } : null,
magicDrops: magicDrops.map(({ id, name, short }) => {
Expand Down
19 changes: 11 additions & 8 deletions controllers/keyGiverDrops/getNewestKeyGiverDrops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,17 @@ export const getNewestKeyGiverDrops = async (req: Request, res: Response) => {
short,
domain,
respawnTime,
locations: locations.map(({ id, locationId, name, domain }) => {
return {
id,
locationId,
name,
domain,
};
}),
locations: locations.map(
({ id, locationId, internalId, name, domain }) => {
return {
id,
locationId,
internalId,
name,
domain,
};
}
),
},
drop: drop ? { id: drop.id, name: drop.name } : null,
magicDrops: magicDrops?.map(({ id, name, short }) => {
Expand Down
19 changes: 11 additions & 8 deletions controllers/keyGivers/getKeyGiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ export const getKeyGiver = async (req: Request, res: Response) => {
domain,
playersToComplete,
comment,
locations: locations.map(({ id, locationId, name, domain }) => {
return {
id,
locationId,
name,
domain,
};
}),
locations: locations.map(
({ id, locationId, internalId, name, domain }) => {
return {
id,
locationId,
internalId,
name,
domain,
};
}
),
};
return res.status(200).json({
status: Status.success,
Expand Down
19 changes: 11 additions & 8 deletions controllers/keyGivers/getKeyGivers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ export const getKeyGivers = async (req: Request, res: Response) => {
domain,
playersToComplete,
comment,
locations: locations.map(({ id, locationId, name, domain }) => {
return {
id,
locationId,
name,
domain,
};
}),
locations: locations.map(
({ id, locationId, internalId, name, domain }) => {
return {
id,
locationId,
internalId,
name,
domain,
};
}
),
};
return data;
}
Expand Down
12 changes: 11 additions & 1 deletion controllers/locations/getLocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ export const getLocations = async (req: Request, res: Response) => {
status: Status.success,
data: {
locations: locations.map(
({ id, locationId, name, domain, description, comment, binds }) => {
({
id,
locationId,
internalId,
name,
domain,
description,
comment,
binds,
}) => {
const data: LocationResponse = {
id,
locationId,
internalId,
name,
domain,
description,
Expand Down
26 changes: 25 additions & 1 deletion db/models/Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ const itemSchema = new Schema<IItem>(
shieldParry: {
type: Number,
min: [1, messages[config.lang].items.shieldParryTooLow],
max: [12, messages[config.lang].items.shieldParryTooHigh],
max: [14, messages[config.lang].items.shieldParryTooHigh],
default: null,
validate: {
validator: (value: number | null) => {
Expand Down Expand Up @@ -239,12 +239,36 @@ const itemSchema = new Schema<IItem>(
min: [0, messages[config.lang].items.itemCostTooLow],
max: [10000, messages[config.lang].items.itemCostTooHigh],
default: null,
set: (value: number) => {
if (value) {
return parseFloat(value.toFixed(2));
}
return null;
},
},
vendorCost: {
type: Number,
min: [0, messages[config.lang].items.itemVendorCostTooLow],
max: [10000, messages[config.lang].items.itemVendorCostTooHigh],
default: null,
set: (value: number) => {
if (value) {
return parseFloat(value.toFixed(2));
}
return null;
},
},
npcPurchasePrice: {
type: Number,
min: [0, messages[config.lang].items.itemNpcPurchasePriceTooLow],
max: [10000, messages[config.lang].items.itemNpcPurchasePriceTooHigh],
default: null,
set: (value: number) => {
if (value) {
return parseFloat(value.toFixed(2));
}
return null;
},
},
description: {
type: String,
Expand Down
5 changes: 5 additions & 0 deletions db/models/Location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ const locationSchema = new Schema<ILocation>({
max: [99999, messages[config.lang].location.locationNumberNotInRange],
cast: messages[config.lang].location.locationNotANumber,
},
internalId: {
type: String,
maxLength: [10, messages[config.lang].location.internalIdTooLong],
},
name: {
type: String,
maxLength: [50, messages[config.lang].location.nameTooLong],
default: "",
},
domain: {
type: String,
Expand Down
2 changes: 2 additions & 0 deletions db/tools/createDb/data/locations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Domain } from "../../../../types/Domain";
import { Location } from "../../../../types/Location";
import { randomUUID } from "crypto";
import { locationNames } from "./generatorData/locationNames";
import { randomFromArray } from "./generatorUtils/randomFromArray";
import { randomFromOptions } from "./generatorUtils/randomFromOptions";
Expand All @@ -13,6 +14,7 @@ export const locations: Location[] = randomNames(
).map(name => {
return {
locationId: Math.floor(Math.random() * 100000),
internalId: randomUUID().slice(-10),
name: name,
domain: randomFromOptions(
Domain.unknown,
Expand Down
Loading

0 comments on commit d9411b8

Please sign in to comment.