Skip to content

Commit b713204

Browse files
committed
feat: shop condition based on timing
1 parent 6412506 commit b713204

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

src/lua/api.lua

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -755,11 +755,7 @@ API.functions["shop"] = function(args)
755755
---@type PendingRequest
756756
API.pending_requests["shop"] = {
757757
condition = function()
758-
-- Purchase action is non-atomic, so we need to check dollars
759-
-- TODO: try to use the condition directly
760758
return utils.COMPLETION_CONDITIONS["shop"]["buy_card"]()
761-
and #G.shop_jokers.cards == shop_size_before - 1
762-
and G.GAME.dollars == expected_dollars
763759
end,
764760
action = function()
765761
local game_state = utils.get_game_state()
@@ -793,10 +789,6 @@ API.functions["shop"] = function(args)
793789
API.pending_requests["shop"] = {
794790
condition = function()
795791
return utils.COMPLETION_CONDITIONS["shop"]["reroll"]()
796-
and G.GAME.round_scores
797-
and G.GAME.round_scores.times_rerolled
798-
and G.GAME.round_scores.times_rerolled.amt == times_rerolled_before + 1
799-
and G.GAME.dollars == expected_dollars
800792
end,
801793
action = function()
802794
local game_state = utils.get_game_state()

src/lua/utils.lua

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---Utility functions for game state extraction and data processing
22
utils = {}
33
local json = require("json")
4+
local socket = require("socket")
45

56
-- ==========================================================================
67
-- Game State Extraction
@@ -634,6 +635,9 @@ end
634635
-- heuristic based on empirical testing to ensure smooth gameplay without delays.
635636
local EVENT_QUEUE_THRESHOLD = 3
636637

638+
-- Timestamp storage for delayed conditions
639+
local condition_timestamps = {}
640+
637641
---Completion conditions for different game actions to determine when action execution is complete
638642
---These are shared between API and LOG systems to ensure consistent timing
639643
---@type table<string, table>
@@ -728,13 +732,47 @@ utils.COMPLETION_CONDITIONS = {
728732

729733
shop = {
730734
buy_card = function()
731-
return G.STATE == G.STATES.SHOP and #G.E_MANAGER.queues.base < EVENT_QUEUE_THRESHOLD and G.STATE_COMPLETE
735+
local base_condition = G.STATE == G.STATES.SHOP
736+
and #G.E_MANAGER.queues.base < EVENT_QUEUE_THRESHOLD - 1 -- need to reduve the threshold
737+
and G.STATE_COMPLETE
738+
739+
if not base_condition then
740+
-- Reset timestamp if base condition is not met
741+
condition_timestamps.shop_buy_card = nil
742+
return false
743+
end
744+
745+
-- Base condition is met, start timing
746+
if not condition_timestamps.shop_buy_card then
747+
condition_timestamps.shop_buy_card = socket.gettime()
748+
end
749+
750+
-- Check if 0.1 seconds have passed
751+
local elapsed = socket.gettime() - condition_timestamps.shop_buy_card
752+
return elapsed > 0.1
732753
end,
733754
next_round = function()
734755
return G.STATE == G.STATES.BLIND_SELECT and #G.E_MANAGER.queues.base < EVENT_QUEUE_THRESHOLD and G.STATE_COMPLETE
735756
end,
736757
reroll = function()
737-
return G.STATE == G.STATES.SHOP and #G.E_MANAGER.queues.base < EVENT_QUEUE_THRESHOLD and G.STATE_COMPLETE
758+
local base_condition = G.STATE == G.STATES.SHOP
759+
and #G.E_MANAGER.queues.base < EVENT_QUEUE_THRESHOLD - 1 -- need to reduve the threshold
760+
and G.STATE_COMPLETE
761+
762+
if not base_condition then
763+
-- Reset timestamp if base condition is not met
764+
condition_timestamps.shop_reroll = nil
765+
return false
766+
end
767+
768+
-- Base condition is met, start timing
769+
if not condition_timestamps.shop_reroll then
770+
condition_timestamps.shop_reroll = socket.gettime()
771+
end
772+
773+
-- Check if 0.3 seconds have passed
774+
local elapsed = socket.gettime() - condition_timestamps.shop_reroll
775+
return elapsed > 0.30
738776
end,
739777
},
740778
}

0 commit comments

Comments
 (0)