2009-08-25 18:04:42 -06:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
|
|
|
import socket
|
|
|
|
import json
|
2009-08-26 15:14:09 -06:00
|
|
|
import random
|
|
|
|
import time
|
|
|
|
import threading
|
2009-08-25 18:04:42 -06:00
|
|
|
|
|
|
|
class Client:
|
|
|
|
rbufsize = -1
|
|
|
|
wbufsize = 0
|
2009-08-26 15:14:09 -06:00
|
|
|
debug = False
|
2009-08-25 18:04:42 -06:00
|
|
|
|
|
|
|
def __init__(self, addr):
|
|
|
|
self.conn = socket.create_connection(addr)
|
|
|
|
self.wfile = self.conn.makefile('wb', self.wbufsize)
|
|
|
|
self.rfile = self.conn.makefile('rb', self.rbufsize)
|
|
|
|
|
|
|
|
def write(self, *val):
|
|
|
|
s = json.dumps(val)
|
2009-08-26 15:14:09 -06:00
|
|
|
if self.debug:
|
|
|
|
print('--> %s' % s)
|
2009-08-25 18:04:42 -06:00
|
|
|
self.wfile.write(s.encode('utf-8') + b'\n')
|
|
|
|
|
|
|
|
def read(self):
|
|
|
|
line = self.rfile.readline().strip().decode('utf-8')
|
|
|
|
if not line:
|
|
|
|
return
|
2009-08-26 15:14:09 -06:00
|
|
|
if self.debug:
|
|
|
|
print ('<-- %s' % line)
|
2009-08-25 18:04:42 -06:00
|
|
|
return json.loads(line)
|
|
|
|
|
|
|
|
def command(self, *val):
|
|
|
|
self.write(*val)
|
|
|
|
ret = self.read()
|
|
|
|
if ret[0] == 'OK':
|
|
|
|
return ret[1]
|
|
|
|
elif ret[0] == 'ERR':
|
|
|
|
raise ValueError(ret[1])
|
|
|
|
else:
|
2009-08-26 15:14:09 -06:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
class RandomBot(threading.Thread):
|
|
|
|
def __init__(self, team):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.team = team
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
c = Client(('localhost', 5388))
|
|
|
|
#print('lobby', c.command('^', 'lobby'))
|
|
|
|
c.command('login', self.team, 'furble')
|
|
|
|
while True:
|
|
|
|
move = random.choice(['rock', 'scissors', 'paper'])
|
|
|
|
ret = c.command(move)
|
|
|
|
if ret == ['WIN']:
|
|
|
|
print('%s wins' % self.team)
|
2009-08-26 17:46:06 -06:00
|
|
|
time.sleep(random.uniform(0.2, 3))
|
2009-08-25 18:04:42 -06:00
|
|
|
|
|
|
|
def main():
|
2009-08-26 15:14:09 -06:00
|
|
|
bots = []
|
|
|
|
for i in ['zebra', 'aardvark', 'wembly']:
|
|
|
|
bots.append(RandomBot(i).start())
|
2009-08-25 18:04:42 -06:00
|
|
|
|
|
|
|
main()
|