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:
|
2009-08-28 15:52:56 -06:00
|
|
|
print(self, '--> %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:
|
2009-09-01 11:23:40 -06:00
|
|
|
print (self, '<-- %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
|
|
|
|
|
|
|
|
|
2009-09-01 11:23:40 -06:00
|
|
|
class IdiotBot(threading.Thread):
|
|
|
|
def __init__(self, team, move):
|
2009-08-26 15:14:09 -06:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.team = team
|
2009-09-01 11:23:40 -06:00
|
|
|
self.move = move
|
|
|
|
|
|
|
|
def get_move(self):
|
|
|
|
return self.move
|
2009-08-26 15:14:09 -06:00
|
|
|
|
|
|
|
def run(self):
|
2009-09-25 13:27:37 -06:00
|
|
|
c = Client(('cfl', 5388))
|
2009-09-01 11:23:40 -06:00
|
|
|
c.debug = False
|
2009-08-26 15:14:09 -06:00
|
|
|
#print('lobby', c.command('^', 'lobby'))
|
|
|
|
c.command('login', self.team, 'furble')
|
|
|
|
while True:
|
2009-09-01 11:23:40 -06:00
|
|
|
move = self.get_move()
|
2009-08-26 15:14:09 -06:00
|
|
|
ret = c.command(move)
|
|
|
|
if ret == ['WIN']:
|
|
|
|
print('%s wins' % self.team)
|
2009-09-25 13:27:37 -06:00
|
|
|
amt = random.uniform(0.1, 2.6)
|
2009-08-28 15:52:56 -06:00
|
|
|
if c.debug:
|
|
|
|
print(c, 'sleep %f' % amt)
|
|
|
|
time.sleep(amt)
|
2009-08-25 18:04:42 -06:00
|
|
|
|
|
|
|
def main():
|
2009-08-26 15:14:09 -06:00
|
|
|
bots = []
|
2009-09-01 11:23:40 -06:00
|
|
|
for team, move in (('rockbot', 'rock'), ('cutbot', 'scissors'), ('paperbot', 'paper')):
|
|
|
|
bots.append(IdiotBot(team, move).start())
|
2009-08-25 18:04:42 -06:00
|
|
|
|
|
|
|
main()
|