1+ // routes/item/catalize.ts
2+
13import express , { Request , Response , NextFunction } from 'express' ;
4+ import { param , validationResult } from 'express-validator' ;
25import logger from '../../logger' ;
3- import Item from '../../models/Item' ;
4- import Satchel from '../../models/Satchel' ;
5- import { consumePA , getPA } from '../../redis/redisUtils' ;
66
7+ import { fetchItemMiddleware , assertItemPresent } from '../../middlewares/fetch-item' ;
8+ import { fetchSatchelMiddleware , assertSatchelPresent } from '../../middlewares/fetch-satchel' ;
9+
10+ import { consumePA , getPA } from '../../redis/redisUtils' ;
711import { ITEM_RARITIES } from '../../globals' ;
812
913const router = express . Router ( ) ;
@@ -14,112 +18,91 @@ function asyncHandler(fn: (req: Request, res: Response, next: NextFunction) => P
1418 } ;
1519}
1620
17- router . post ( '/:uuid/catalyze' , asyncHandler ( async ( req : Request , res : Response ) => {
18- logger . info ( `POST /${ req . params . uuid } /catalyze` ) ;
19-
20- const { uuid } = req . params ;
21+ router . post (
22+ '/:item_uuid/catalyze' ,
23+ [
24+ param ( 'item_uuid' )
25+ . isUUID ( 4 )
26+ . withMessage ( 'Invalid UUID format' ) ,
27+ ] ,
28+ asyncHandler ( fetchItemMiddleware ) ,
29+ asyncHandler ( fetchSatchelMiddleware ) ,
30+ asyncHandler ( async ( req : Request , res : Response ) => {
31+ // Check validation result
32+ const errors = validationResult ( req ) ;
33+ if ( ! errors . isEmpty ( ) ) {
34+ return res . status ( 400 ) . json ( { errors : errors . array ( ) } ) ;
35+ }
2136
22- // We look for the related Item
23- const item = await Item . findById ( uuid ) . exec ( ) ;
24- if ( ! item ) {
25- logger . warn ( `Item.id:${ uuid } not found` ) ;
26- return res . status ( 200 ) . json ( {
27- success : false ,
28- msg : `Item.id:${ uuid } not found` ,
29- payload : null
30- } ) ;
31- } else {
32- logger . debug ( `Item.id:${ uuid } found` ) ;
33- }
37+ const { item_uuid } = req . params ;
38+ logger . info ( `POST /${ item_uuid } /catalyze` ) ;
3439
35- // We look for the related Satchel (Item.bearer)
36- const satchel = await Satchel . findById ( item . bearer ) . exec ( ) ;
37- if ( ! satchel ) {
38- logger . warn ( `Satchel.id:${ uuid } not found` ) ;
39- return res . status ( 200 ) . json ( {
40- success : false ,
41- msg : `Satchel.id:${ uuid } not found` ,
42- payload : null
43- } ) ;
44- } else {
45- logger . debug ( `Satchel.id:${ uuid } found` ) ;
46- }
40+ // We check middleware functions didn't screw up
41+ assertItemPresent ( req )
42+ assertSatchelPresent ( req )
4743
48- // Find index of current item rarity and get next rarity
49- const rarityIndex = ITEM_RARITIES . indexOf ( item . rarity ) ;
50- if ( rarityIndex === - 1 || rarityIndex === ITEM_RARITIES . length - 1 ) {
51- // Current rarity not found or already at max rarity
52- const msg = `Item rarity '${ item . rarity } ' is invalid or at max level.` ;
53- logger . warn ( msg ) ;
54- return res . status ( 200 ) . json ( {
55- success : false ,
56- msg : msg ,
57- payload : null
58- } ) ;
59- }
60- const nextRarity = ITEM_RARITIES [ rarityIndex + 1 ] ;
61- const nextRarityKey = nextRarity . toLowerCase ( ) ;
44+ // Find index of current item rarity and get next rarity
45+ const rarityIndex = ITEM_RARITIES . indexOf ( req . item . rarity ) ;
46+ if ( rarityIndex === - 1 || rarityIndex === ITEM_RARITIES . length - 1 ) {
47+ // Current rarity not found or already at max rarity
48+ const msg = `Item rarity '${ req . item . rarity } ' is invalid or at max level.` ;
49+ logger . warn ( msg ) ;
50+ return res . status ( 200 ) . json ( { success : false , msg : msg , payload : null } ) ;
51+ }
52+ const nextRarity = ITEM_RARITIES [ rarityIndex + 1 ] ;
53+ const nextRarityKey = nextRarity . toLowerCase ( ) ;
6254
63- // We check that there is enough shards
64- if ( satchel . shard [ nextRarityKey ] < 10 ) {
65- logger . warn ( `Satchel.id:${ satchel . id } not enough shard.${ nextRarityKey } ` ) ;
66- return res . status ( 200 ) . json ( {
67- success : false ,
68- msg : `Satchel.id:${ satchel . id } not enough shard.${ nextRarityKey } ` ,
69- payload : null
70- } ) ;
71- } else {
72- logger . debug ( `Satchel.id:${ satchel . id } enough shard.${ nextRarityKey } ` ) ;
73- }
55+ // We check that there is enough shards
56+ if ( req . satchel . shard [ nextRarityKey ] < 10 ) {
57+ const msg = `Satchel.id:${ req . satchel . id } not enough shard.${ nextRarityKey } `
58+ logger . warn ( msg ) ;
59+ return res . status ( 200 ) . json ( { success : false , msg : msg , payload : null } ) ;
60+ } else {
61+ logger . verbose ( `Satchel.id:${ req . satchel . id } enough shard.${ nextRarityKey } ` ) ;
62+ }
7463
75- // Update Item
76- try {
77- item . rarity = nextRarity ;
78- item . updated = new Date ( ) ;
79- await item . save ( ) ;
80- } catch ( err ) {
81- logger . error ( `Item.id:${ uuid } Unable to UPDATE: ${ err } ` )
82- }
64+ // Update Item
65+ try {
66+ req . item . rarity = nextRarity ;
67+ req . item . updated = new Date ( ) ;
68+ await req . item . save ( ) ;
69+ } catch ( err ) {
70+ logger . error ( `Item.id:${ item_uuid } Unable to UPDATE: ${ err } ` )
71+ }
8372
84- // Update Satchel amount
85- try {
86- // Decrement shard count by 10
87- if ( typeof satchel . shard [ nextRarityKey ] === 'number' ) {
88- satchel . shard [ nextRarityKey ] -= 10 ;
73+ // Update Satchel amount
74+ try {
75+ req . satchel . shard [ nextRarityKey ] -= 10 ;
76+ req . satchel . updated = new Date ( ) ;
77+ await req . satchel . save ( ) ;
78+ } catch ( err ) {
79+ logger . error ( `Satchel.id:${ req . satchel . id } Unable to UPDATE: ${ err } ` )
80+ }
8981
90- // Optional: ensure it doesn't go below zero
91- if ( satchel . shard [ nextRarityKey ] < 0 ) {
92- satchel . shard [ nextRarityKey ] = 0 ;
93- }
82+ // We consume 2 🔵 for this action
83+ try {
84+ await consumePA ( {
85+ creatureUUID : req . item . bearer , // The creature UUID
86+ bluepa : 2 , // Number of blue PA to consume
87+ redpa : 0 , // Number of red PA to consume
88+ duration : 3600 // Each PA lasts 1 hours (in seconds)
89+ } ) ;
90+ } catch ( err ) {
91+ logger . error ( `Creature.id:${ req . item . bearer } Unable to consume PA: ${ err } ` )
9492 }
95- satchel . updated = new Date ( ) ;
96- await satchel . save ( ) ;
97- } catch ( err ) {
98- logger . error ( `Satchel.id:${ satchel . id } Unable to UPDATE: ${ err } ` )
99- }
10093
101- // We consume 2 🔵 for this action
102- try {
103- await consumePA ( {
104- creatureUUID : item . bearer , // The creature UUID
105- bluepa : 2 , // Number of blue PA to consume
106- redpa : 0 , // Number of red PA to consume
107- duration : 3600 // Each PA lasts 1 hours (in seconds)
94+ const msg = `Item.id:${ item_uuid } catalyzed`
95+ logger . verbose ( msg ) ;
96+ return res . status ( 200 ) . json ( {
97+ success : true ,
98+ msg : msg ,
99+ payload : {
100+ item : req . item ,
101+ pa : await getPA ( req . item . bearer ) ,
102+ satchel : req . satchel
103+ }
108104 } ) ;
109- } catch ( err ) {
110- logger . error ( `Creature.id:${ item . bearer } Unable to consume PA: ${ err } ` )
111105 }
112-
113- logger . info ( `Item.id:${ uuid } catalyzed` ) ;
114- return res . status ( 200 ) . json ( {
115- success : true ,
116- msg : `Item.id:${ uuid } catalyzed successfully` ,
117- payload : {
118- item : item ,
119- pa : await getPA ( item . bearer ) ,
120- satchel : satchel
121- }
122- } ) ;
123- } ) ) ;
106+ ) ) ;
124107
125108export default router ;
0 commit comments