-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Testing][Feature] items and updated drops functionality
- Loading branch information
Showing
63 changed files
with
2,543 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
Oops, something went wrong.