|
@@ -1,96 +1,100 @@
|
|
|
import json
|
|
|
-import select
|
|
|
import socket
|
|
|
-import sys
|
|
|
-import time
|
|
|
|
|
|
HOST = 'localhost'
|
|
|
PORT = 6546
|
|
|
|
|
|
-ssock = None
|
|
|
+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 listen():
|
|
|
- global ssock
|
|
|
- ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
- #ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
- ssock.connect((HOST, PORT))
|
|
|
- print('connected:', ssock)
|
|
|
- send_msg(set_name('BOT_select_first'))
|
|
|
- while True:
|
|
|
- bmsg = ssock.recv(1024)
|
|
|
- if len(bmsg) <= 0:
|
|
|
- print('read 0; hangup')
|
|
|
- exit(3)
|
|
|
- msg = str(bmsg, 'utf-8').strip()
|
|
|
- jmsg = json.loads(msg)
|
|
|
- msg_type = parse_server_message(jmsg)
|
|
|
- print('parsed msg_type:', msg_type)
|
|
|
- if msg_type == 'query':
|
|
|
- action = get_action(jmsg)
|
|
|
- print('parsed action:', action)
|
|
|
- if action == 'give_card':
|
|
|
- bot_give_card(jmsg)
|
|
|
- elif action == 'select_row':
|
|
|
- bot_select_row(jmsg)
|
|
|
- else:
|
|
|
- print('unknown message action:', action)
|
|
|
- else:
|
|
|
- print('unknown message type:', msg_type)
|
|
|
+ @classmethod
|
|
|
+ def build_action(cls, action, rest = {}):
|
|
|
+ ret = { 'action': action }
|
|
|
+ ret = ret | rest
|
|
|
+ return ret
|
|
|
|
|
|
-def bot_give_card(msg):
|
|
|
- print('about to give card')
|
|
|
- hand = msg.get('hand')
|
|
|
- print(hand, '->', hand[0])
|
|
|
- msg = give_card(hand[0])
|
|
|
- send_msg(msg)
|
|
|
- return hand[0]
|
|
|
+ @classmethod
|
|
|
+ def choose_row(cls, row_index):
|
|
|
+ return cls.build_action('select_row', { 'i': row_index })
|
|
|
|
|
|
-def bot_select_row(msg):
|
|
|
- print('about to select row')
|
|
|
- msg = choose_row(0)
|
|
|
- send_msg(msg)
|
|
|
- return 0
|
|
|
+ @classmethod
|
|
|
+ def give_card(cls, card):
|
|
|
+ return cls.build_action('give_card', { 'card': card })
|
|
|
|
|
|
-def get_action(jmsg):
|
|
|
- try:
|
|
|
- action = jmsg.get('action')
|
|
|
- except:
|
|
|
- print('unable to get action')
|
|
|
- exit(8)
|
|
|
- return action
|
|
|
+ def bot_give_card(self, msg):
|
|
|
+ pass
|
|
|
|
|
|
-def parse_server_message(jmsg):
|
|
|
- try:
|
|
|
- typ = jmsg.get('type')
|
|
|
- except:
|
|
|
- print('unable to get type')
|
|
|
- exit(8)
|
|
|
- return typ
|
|
|
+ def bot_select_row(self, msg):
|
|
|
+ pass
|
|
|
|
|
|
-def send_msg(msg):
|
|
|
- jmsg = json.dumps(msg)
|
|
|
- print('msg to send:', jmsg)
|
|
|
- ssock.send(bytes(jmsg, 'utf-8'))
|
|
|
+ def send_msg(self, msg):
|
|
|
+ jmsg = json.dumps(msg)
|
|
|
+ print('msg to send:', jmsg)
|
|
|
+ self.ssock.send(bytes(jmsg, 'utf-8'))
|
|
|
|
|
|
-def build_action(action, rest = {}):
|
|
|
- ret = { 'action': action}
|
|
|
- ret = ret | rest
|
|
|
- return ret
|
|
|
+ def connect(self, host='localhost', port=6546):
|
|
|
+ def set_name(name):
|
|
|
+ return self.build_action('set_name', { 'name': name })
|
|
|
|
|
|
-def choose_row(row_index):
|
|
|
- return build_action('select_row', { 'i': row_index })
|
|
|
+ self.ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
+ self.ssock.connect((host, port))
|
|
|
+ self.send_msg(set_name(self.name))
|
|
|
|
|
|
-def give_card(card):
|
|
|
- return build_action('give_card', { 'card': card })
|
|
|
+ def play(self, initiate_start=False):
|
|
|
+ def get_msg_type(msg):
|
|
|
+ try:
|
|
|
+ typ = msg.get('type')
|
|
|
+ except:
|
|
|
+ print('unable to get type')
|
|
|
+ exit(8)
|
|
|
+ return typ
|
|
|
|
|
|
-def set_name(name):
|
|
|
- return build_action('set_name', { 'name': name })
|
|
|
+ def get_action(msg):
|
|
|
+ try:
|
|
|
+ action = msg.get('action')
|
|
|
+ except:
|
|
|
+ print('unable to get action')
|
|
|
+ exit(9)
|
|
|
+ return action
|
|
|
|
|
|
-if __name__ == '__main__':
|
|
|
- try:
|
|
|
while True:
|
|
|
- listen()
|
|
|
- except KeyboardInterrupt:
|
|
|
- print('interrupt - exiting')
|
|
|
- #TODO: close
|
|
|
- exit(9)
|
|
|
+ bmsg = self.ssock.recv(1024)
|
|
|
+ if len(bmsg) <= 0:
|
|
|
+ print('read 0; hangup')
|
|
|
+ exit(3)
|
|
|
+ jmsg = str(bmsg, 'utf-8').strip()
|
|
|
+ msg = json.loads(jmsg)
|
|
|
+ print('msg:', msg)
|
|
|
+ msg_type = get_msg_type(msg)
|
|
|
+ #print('parsed msg_type:', msg_type)
|
|
|
+ if msg_type == 'query':
|
|
|
+ action = get_action(msg)
|
|
|
+ print('parsed action:', action)
|
|
|
+ if action == 'give_card':
|
|
|
+ self.bot_give_card(msg)
|
|
|
+ elif action == 'select_row':
|
|
|
+ self.bot_select_row(msg)
|
|
|
+ else:
|
|
|
+ print('unknown message action:', action)
|
|
|
+ elif msg_type == 'info':
|
|
|
+ print('info type is not implemented')
|
|
|
+ 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))
|