Skip to content

Commit 0ce9e48

Browse files
committed
SEND_GAMESTATE API
1 parent aa2a0a6 commit 0ce9e48

File tree

4 files changed

+63
-20
lines changed

4 files changed

+63
-20
lines changed

client.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,43 @@
22
# WIP
33

44
import socket
5+
import sys
6+
import signal
7+
8+
def signal_handler(signal, frame):
9+
sys.exit(0)
10+
11+
def is_socket_closed(sock: socket.socket):
12+
try:
13+
data = sock.recv(16, socket.MSG_DONTWAIT | socket.MSG_PEEK)
14+
if len(data) == 0:
15+
return True
16+
except BlockingIOError:
17+
return False
18+
except ConnectionResetError:
19+
return True
20+
except Exception as e:
21+
return False
22+
return False
523

624
if __name__ == "__main__":
25+
signal.signal(signal.SIGINT, signal_handler)
26+
727
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
828
client_socket.settimeout(1.0)
929

1030
addr = ("127.0.0.1", 12345)
1131

32+
client_socket.sendto(b'SEND_GAMESTATE', addr)
33+
34+
running = True
35+
while running:
36+
37+
try:
38+
data, addr = client_socket.recvfrom(1024)
39+
print(data.decode())
40+
except:
41+
print('Request Timed Out')
42+
43+
running = False
44+

src/api.lua

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ function BalatrobotAPI.update(dt)
3737
data, msg_or_ip, port_or_nil = BalatrobotAPI.socket:receivefrom()
3838
if data then
3939

40-
local _action = Utils.parseaction(data)
41-
42-
if _action and #_action > 1 and #_action > Bot.ACTIONPARAMS[_action[1]].num_args then
43-
BalatrobotAPI.respond("Error: Incorrect number of params for action " .. action)
44-
elseif not _action then
45-
BalatrobotAPI.respond("Error: Incorrect message format. Should be ACTION|arg1|arg2")
40+
if data == 'SEND_GAMESTATE' then
41+
sendDebugMessage("SEND_GAMESTATE")
42+
BalatrobotAPI.notifyapiclient()
4643
else
47-
BalatrobotAPI.queueaction(_action)
44+
local _action = Utils.parseaction(data)
45+
46+
if _action and #_action > 1 and #_action > Bot.ACTIONPARAMS[_action[1]].num_args then
47+
BalatrobotAPI.respond("Error: Incorrect number of params for action " .. action)
48+
elseif not _action then
49+
BalatrobotAPI.respond("Error: Incorrect message format. Should be ACTION|arg1|arg2")
50+
else
51+
BalatrobotAPI.queueaction(_action)
52+
end
4853
end
4954

5055
elseif msg_or_ip ~= 'timeout' then

src/bot.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ Bot.ACTIONPARAMS[Bot.ACTIONS.START_RUN] = {
102102
func = "start_run"
103103
}
104104

105-
106105
-- CHANGE ME
107106
Bot.SETTINGS = {
108107
stake = 1,
@@ -117,10 +116,10 @@ Bot.SETTINGS = {
117116
action_delay = 0,
118117

119118
-- Replay actions from file?
120-
replay = true,
119+
replay = false,
121120

122121
-- Receive commands from the API?
123-
api = false,
122+
api = true,
124123
}
125124

126125
--- Skips or selects the current blind

src/utils.lua

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ end
1717
function Utils.getHandData()
1818
local _hand = { }
1919

20-
for i = 1, #G.hand.cards do
21-
local _card = Utils.getCardData(G.hand.cards[i])
22-
_hand[i] = _card
20+
if G and G.hand and G.hand.cards then
21+
for i = 1, #G.hand.cards do
22+
local _card = Utils.getCardData(G.hand.cards[i])
23+
_hand[i] = _card
24+
end
2325
end
2426

2527
return _hand
@@ -28,9 +30,11 @@ end
2830
function Utils.getJokersData()
2931
local _jokers = { }
3032

31-
for i = 1, #G.jokers.cards do
32-
local _card = Utils.getCardData(G.jokers.cards[i])
33-
_jokers[i] = _card
33+
if G and G.jokers and G.jokers.cards then
34+
for i = 1, #G.jokers.cards do
35+
local _card = Utils.getCardData(G.jokers.cards[i])
36+
_jokers[i] = _card
37+
end
3438
end
3539

3640
return _jokers
@@ -39,9 +43,11 @@ end
3943
function Utils.getConsumablesData()
4044
local _consumables = { }
4145

42-
for i = 1, #G.consumeables.cards do
43-
local _card = Utils.getCardData(G.consumeables.cards[i])
44-
_consumables[i] = _card
46+
if G and G.consumables and G.consumables.cards then
47+
for i = 1, #G.consumeables.cards do
48+
local _card = Utils.getCardData(G.consumeables.cards[i])
49+
_consumables[i] = _card
50+
end
4551
end
4652

4753
return _consumables
@@ -68,7 +74,7 @@ end
6874

6975
function Utils.getShopData()
7076
local _shop = { }
71-
if not G.shop then return _shop end
77+
if not G or not G.shop then return _shop end
7278

7379
_shop.reroll_cost = G.GAME.current_round.reroll_cost
7480
_shop.cards = { }

0 commit comments

Comments
 (0)