Skip to content

Commit 7702fe5

Browse files
committed
refactor: Added logic for buying
1 parent 88ceccb commit 7702fe5

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

src/routes/bazaar/ammo.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,46 @@ router.post(
4646
{
4747
var price = + AMMUNITIONS[caliber]['price'] * 10
4848
var quantity = - 10
49+
50+
// We need to check there is enough ammo to sell
51+
if (req.satchel.ammo[caliber] < Math.abs(quantity)) {
52+
const msg = `Satchel.id:${req.satchel.id} has not enough ${caliber} (${req.satchel.ammo[caliber]})`
53+
logger.verbose(msg);
54+
return res.status(200).json({
55+
success: false,
56+
msg: msg,
57+
payload: {
58+
satchel: req.satchel
59+
}
60+
});
61+
}
62+
63+
var ret_msg = `Ammo:${caliber} sold successfully (${quantity} for ${Math.abs(price)})`
4964
} else if ( action == 'buy') {
5065
var price = - AMMUNITIONS[caliber]['price'] * 10
5166
var quantity = 10
67+
68+
// We need to check there is enough currency to buy
69+
const curr = req.satchel.currency.banana
70+
if (curr < Math.abs(price)) {
71+
const msg = `Satchel.id:${req.satchel.id} has not enough currency to buy (${curr})`
72+
logger.verbose(msg);
73+
return res.status(200).json({
74+
success: false,
75+
msg: msg,
76+
payload: {
77+
satchel: req.satchel
78+
}
79+
});
80+
}
81+
82+
var ret_msg = `Ammo:${caliber} bought successfully (${quantity} for ${Math.abs(price)})`
5283
} else {
5384
const msg = `Price:${caliber} not found`
5485
logger.warn(msg);
5586
return res.status(200).json({ success: false, msg: msg, payload: null });
5687
}
5788

58-
// We need to check there is enough ammo to sell
59-
if (req.satchel.ammo[caliber] < Math.abs(quantity)) {
60-
const msg = `Satchel.id:${req.satchel.id} has not enough ${caliber} (${req.satchel.ammo[caliber]})`
61-
logger.verbose(msg);
62-
return res.status(200).json({
63-
success: false,
64-
msg: msg,
65-
payload: {
66-
satchel: req.satchel
67-
}
68-
});
69-
}
70-
7189
// We update Satchel
7290
try {
7391
req.satchel.ammo[caliber] += quantity
@@ -80,11 +98,10 @@ router.post(
8098
return res.status(200).json({ success: false, msg: msg, payload: null });
8199
}
82100

83-
const msg = `Ammo:${caliber} bought successfully (${quantity} for ${Math.abs(price)})`
84-
logger.verbose(msg);
101+
logger.verbose(ret_msg);
85102
return res.status(200).json({
86103
success: true,
87-
msg: msg,
104+
msg: ret_msg,
88105
payload: {
89106
satchel: req.satchel
90107
}

0 commit comments

Comments
 (0)