|
@@ -1,34 +1,38 @@
|
|
|
import json
|
|
|
import socket
|
|
|
|
|
|
-import time # TODO: fix json reads on the socket and remove
|
|
|
+import time # TODO: fix json reads on the socket and remove
|
|
|
|
|
|
HOST = 'localhost'
|
|
|
PORT = 6546
|
|
|
|
|
|
+
|
|
|
class Bot:
|
|
|
def __init__(self, name='BOT', host='localhost', port=PORT, initiate_start=False):
|
|
|
self.name = name
|
|
|
self.ssock = None
|
|
|
self.initiate_start = initiate_start
|
|
|
|
|
|
+ def set_name(self, name):
|
|
|
+ self.name = name
|
|
|
+
|
|
|
@classmethod
|
|
|
- def build_action(cls, action, rest = {}):
|
|
|
- ret = { 'action': action }
|
|
|
+ def build_action(cls, action, rest={}):
|
|
|
+ ret = {'action': action}
|
|
|
ret = ret | rest
|
|
|
return ret
|
|
|
|
|
|
@classmethod
|
|
|
def choose_row(cls, row_index):
|
|
|
- return cls.build_action('select_row', { 'i': row_index })
|
|
|
+ return cls.build_action('select_row', {'i': row_index})
|
|
|
|
|
|
@classmethod
|
|
|
def give_card(cls, card):
|
|
|
- return cls.build_action('give_card', { 'card': card })
|
|
|
+ return cls.build_action('give_card', {'card': card})
|
|
|
|
|
|
@classmethod
|
|
|
def start_game(cls):
|
|
|
- time.sleep(.1)
|
|
|
+ time.sleep(1)
|
|
|
return cls.build_action('start')
|
|
|
|
|
|
def bot_give_card(self, msg):
|
|
@@ -42,13 +46,14 @@ class Bot:
|
|
|
print('msg to send:', jmsg)
|
|
|
self.ssock.send(bytes(jmsg, 'utf-8'))
|
|
|
|
|
|
- def connect(self, host='localhost', port=6546):
|
|
|
- def set_name(name):
|
|
|
- return self.build_action('set_name', { 'name': name })
|
|
|
+ def send_name(self):
|
|
|
+ name_action = self.build_action('set_name', {'name': self.name})
|
|
|
+ self.send_msg(name_action)
|
|
|
|
|
|
+ def connect(self, host='localhost', port=6546):
|
|
|
self.ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
self.ssock.connect((host, port))
|
|
|
- self.send_msg(set_name(self.name))
|
|
|
+ self.send_name()
|
|
|
if self.initiate_start:
|
|
|
self.send_msg(self.start_game())
|
|
|
|
|
@@ -78,7 +83,7 @@ class Bot:
|
|
|
msg = json.loads(jmsg)
|
|
|
print('msg:', msg)
|
|
|
msg_type = get_msg_type(msg)
|
|
|
- #print('parsed msg_type:', msg_type)
|
|
|
+ # print('parsed msg_type:', msg_type)
|
|
|
if msg_type == 'query':
|
|
|
action = get_action(msg)
|
|
|
print('parsed action:', action)
|
|
@@ -93,17 +98,4 @@ class Bot:
|
|
|
else:
|
|
|
print('unknown message type:', msg_type)
|
|
|
|
|
|
- @classmethod
|
|
|
- def calc_penalty(cls, row):
|
|
|
- def card_value(card):
|
|
|
- if card == 55:
|
|
|
- return 7
|
|
|
- elif (card % 11) == 0:
|
|
|
- return 5
|
|
|
- elif (card % 10) == 0:
|
|
|
- return 3
|
|
|
- elif (card % 5) == 0:
|
|
|
- return 2
|
|
|
- return 1
|
|
|
-
|
|
|
- return sum(map(card_value, row))
|
|
|
+
|