-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbuy.lua
More file actions
287 lines (258 loc) · 10 KB
/
Copy pathbuy.lua
File metadata and controls
287 lines (258 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
-- src/lua/endpoints/buy.lua
-- ==========================================================================
-- Buy Endpoint Params
-- ==========================================================================
---@class Request.Endpoint.Buy.Params
---@field card integer? 0-based index of card to buy
---@field voucher integer? 0-based index of voucher to buy
---@field pack integer? 0-based index of pack to buy
-- ==========================================================================
-- Buy Endpoint
-- ==========================================================================
---@type Endpoint
return {
name = "buy",
description = "Buy a card from the shop",
schema = {
card = {
type = "integer",
required = false,
description = "0-based index of card to buy",
},
voucher = {
type = "integer",
required = false,
description = "0-based index of voucher to buy",
},
pack = {
type = "integer",
required = false,
description = "0-based index of pack to buy",
},
},
requires_state = { G.STATES.SHOP },
---@param args Request.Endpoint.Buy.Params
---@param send_response fun(response: Response.Endpoint)
execute = function(args, send_response)
sendDebugMessage("Init buy()", "BB.ENDPOINTS")
local gamestate = BB_GAMESTATE.get_gamestate()
local area
local pos
local set = 0
if args.card then
area = gamestate.shop
pos = args.card + 1
set = set + 1
end
if args.voucher then
area = gamestate.vouchers
pos = args.voucher + 1
set = set + 1
end
if args.pack then
area = gamestate.packs
pos = args.pack + 1
set = set + 1
end
-- Validate that only one of card, voucher, or pack is provided
if not area then
send_response({
message = "Invalid arguments. You must provide one of: card, voucher, pack",
name = BB_ERROR_NAMES.BAD_REQUEST,
})
return
end
-- Validate that only one of card, voucher, or pack is provided
if set > 1 then
send_response({
message = "Invalid arguments. Cannot provide more than one of: card, voucher, or pack",
name = BB_ERROR_NAMES.BAD_REQUEST,
})
return
end
-- Validate that the area has cards
if #area.cards == 0 then
local msg
if args.card then
msg = "No jokers/consumables/cards in the shop. Reroll to restock the shop"
elseif args.voucher then
msg = "No vouchers to redeem. Defeat boss blind to restock"
elseif args.pack then
msg = "No packs to open"
end
send_response({
message = msg,
name = BB_ERROR_NAMES.BAD_REQUEST,
})
return
end
-- Validate card index is in range
if not area.cards[pos] then
local msg
if args.card then
msg = "Card index out of range. Index: " .. args.card .. ", Available cards: " .. area.count
elseif args.voucher then
msg = "Voucher index out of range. Index: " .. args.voucher .. ", Available: " .. area.count
else
msg = "Pack index out of range. Index: " .. args.pack .. ", Available: " .. area.count
end
send_response({
message = msg,
name = BB_ERROR_NAMES.BAD_REQUEST,
})
return
end
-- Get the card
local card = area.cards[pos]
-- Check if the card can be afforded (accounting for Credit Card joker via bankrupt_at)
local available_money = G.GAME.dollars - G.GAME.bankrupt_at
if card.cost.buy > 0 and card.cost.buy > available_money then
send_response({
message = "Card is not affordable. Cost: " .. card.cost.buy .. ", Available money: " .. available_money,
name = BB_ERROR_NAMES.BAD_REQUEST,
})
return
end
-- Ensure there is space in joker area
if card.set == "JOKER" then
if gamestate.jokers.count >= gamestate.jokers.limit then
send_response({
message = "Cannot purchase joker card, joker slots are full. Current: "
.. gamestate.jokers.count
.. ", Limit: "
.. gamestate.jokers.limit,
name = BB_ERROR_NAMES.BAD_REQUEST,
})
return
end
end
-- Ensure there is space in consumable area
if card.set == "PLANET" or card.set == "SPECTRAL" or card.set == "TAROT" then
if gamestate.consumables.count >= gamestate.consumables.limit then
send_response({
message = "Cannot purchase consumable card, consumable slots are full. Current: "
.. gamestate.consumables.count
.. ", Limit: "
.. gamestate.consumables.limit,
name = BB_ERROR_NAMES.BAD_REQUEST,
})
return
end
end
local initial_shop_count = 0
local initial_dest_count = 0
local initial_money = gamestate.money
if args.card then
initial_shop_count = gamestate.shop.count
initial_dest_count = gamestate.jokers.count
+ gamestate.consumables.count
+ (G.deck and G.deck.config and G.deck.config.card_count or 0)
elseif args.voucher then
initial_shop_count = gamestate.vouchers.count
initial_dest_count = 0
for _ in pairs(gamestate.used_vouchers) do
initial_dest_count = initial_dest_count + 1
end
end
-- Get the buy button from the card
local btn
if args.card then
btn = G.shop_jokers.cards[pos].children.buy_button.definition
elseif args.voucher then
btn = G.shop_vouchers.cards[pos].children.buy_button.definition
elseif args.pack then
btn = G.shop_booster.cards[pos].children.buy_button.definition
end
if not btn then
send_response({
message = "No buy button found for card",
name = BB_ERROR_NAMES.NOT_ALLOWED,
})
return
end
-- Log what we're buying
local item_name = card.name or (card.ability and card.ability.name) or card.label or "Unknown"
local item_type = args.voucher and "Voucher" or args.pack and "Booster" or card.set or "item"
sendDebugMessage(string.format("Buying %s '%s' for $%d", item_type, item_name, card.cost.buy), "BB.ENDPOINTS")
-- Use appropriate function: use_card for vouchers, buy_from_shop for others
if args.voucher or args.pack then
G.FUNCS.use_card(btn)
else
G.FUNCS.buy_from_shop(btn)
end
-- Wait for buy completion with comprehensive verification
G.E_MANAGER:add_event(Event({
trigger = "condition",
blocking = false,
func = function()
local done = false
if args.card then
local shop_count = (G.shop_jokers and G.shop_jokers.config and G.shop_jokers.config.card_count or 0)
local dest_count = (G.jokers and G.jokers.config and G.jokers.config.card_count or 0)
+ (G.consumeables and G.consumeables.config and G.consumeables.config.card_count or 0)
+ (G.deck and G.deck.config and G.deck.config.card_count or 0)
local shop_decreased = (shop_count == initial_shop_count - 1)
local dest_increased = (dest_count == initial_dest_count + 1)
local money_deducted = (G.GAME.dollars == initial_money - card.cost.buy)
if shop_decreased and dest_increased and money_deducted and G.STATE == G.STATES.SHOP then
done = true
end
elseif args.voucher then
local shop_count = (G.shop_vouchers and G.shop_vouchers.config and G.shop_vouchers.config.card_count or 0)
local dest_count = 0
if G.GAME.used_vouchers then
for _ in pairs(G.GAME.used_vouchers) do
dest_count = dest_count + 1
end
end
local shop_decreased = (shop_count == initial_shop_count - 1)
local dest_increased = (dest_count == initial_dest_count + 1)
local money_deducted = (G.GAME.dollars == initial_money - card.cost.buy)
if shop_decreased and dest_increased and money_deducted and G.STATE == G.STATES.SHOP then
done = true
end
elseif args.pack then
local money_deducted = (G.GAME.dollars == initial_money - card.cost.buy)
local has_pack = G.pack_cards and not G.pack_cards.REMOVED and G.pack_cards.cards and G.pack_cards.cards[1]
local state_ok = G.STATE_COMPLETE and G.STATE == G.STATES.SMODS_BOOSTER_OPENED
local pack_ready = has_pack and state_ok
if money_deducted and pack_ready then
-- Check if this pack type needs hand (Arcana/Spectral packs deal hand cards)
-- Don't infer pack type from the first card's set — Black Hole is
-- set=Spectral but appears in Celestial packs, causing a false match.
local needs_hand = G.hand and G.hand.cards and #G.hand.cards > 0
if needs_hand then
-- Wait for hand to be fully loaded and positioned
local hand_limit = G.hand and G.hand.config and G.hand.config.card_limit or 8
local deck_size = G.deck and G.deck.config and G.deck.config.card_count or 52
local expected = math.min(deck_size, hand_limit)
local hand_count = G.hand and G.hand.cards and #G.hand.cards or 0
local hand_ready = G.hand
and not G.hand.REMOVED
and G.hand.cards
and hand_count >= expected
and G.hand.T
and G.hand.T.x
local cards_positioned = hand_ready and G.hand.cards[1] and G.hand.cards[1].T and G.hand.cards[1].T.x
if not done and money_deducted then
sendDebugMessage(string.format(
"buy(pack) hand wait: count=%d expected=%d limit=%d deck=%d positioned=%s",
hand_count, expected, hand_limit, deck_size, tostring(cards_positioned ~= nil)
), "BB.ENDPOINTS")
end
done = hand_ready and cards_positioned
else
done = true
end
end
end
if done then
sendDebugMessage("Return buy()", "BB.ENDPOINTS")
send_response(BB_GAMESTATE.get_gamestate())
return true
end
return false
end,
}))
end,
}