Commit 1355d466 authored by Ralf's avatar Ralf
Browse files

Forgot some tabs

parent 220b7e3e
......@@ -4,8 +4,8 @@ from game import Game
game = Game()
try:
game.run()
game.run()
finally:
game.close()
game.close()
from util import *
class Player:
def __init__(self, game, conn):
self.buffer = b""
self.game = game
self.conn = conn
# low-level functions
def send(self, data):
assert type(data) == str
send_async(self.conn, (data+"\n").encode('utf-8'))
def close(self): # only to be called by Game - it has to remove us from its list
self.conn.close()
def read(self, conn, mask):
assert self.conn == conn, "The player's connection changed?"
data = conn.recv(1024) # Should be ready
if data:
self.buffer += data
pos = self.buffer.find(b'\n')
if pos >= 0:
cmd = self.buffer[:pos]
self.buffer = self.buffer[pos+1:]
self.readCmd(cmd.decode('utf-8'))
else:
self.game.removePlayer(self)
# high(er)-level functions
def readCmd(self, cmd):
self.send("Selber "+cmd)
def __init__(self, game, conn):
self.buffer = b""
self.game = game
self.conn = conn
# low-level functions
def send(self, data):
assert type(data) == str
send_async(self.conn, (data+"\n").encode('utf-8'))
def close(self): # only to be called by Game - it has to remove us from its list
self.conn.close()
def read(self, conn, mask):
assert self.conn == conn, "The player's connection changed?"
data = conn.recv(1024) # Should be ready
if data:
self.buffer += data
pos = self.buffer.find(b'\n')
if pos >= 0:
cmd = self.buffer[:pos]
self.buffer = self.buffer[pos+1:]
self.readCmd(cmd.decode('utf-8'))
else:
self.game.removePlayer(self)
# high(er)-level functions
def readCmd(self, cmd):
self.send("Selber "+cmd)
......@@ -2,18 +2,18 @@ import threading, traceback, sys
# run a Python command asynchronously
def fire_and_forget(f):
def _fire_and_forget():
try:
f()
except Exception:
log_stderr("fire_and_forget: Got exception out of callback:\n%s" % traceback.format_exc())
t = threading.Thread(target=_fire_and_forget)
t.start()
def _fire_and_forget():
try:
f()
except Exception:
log_stderr("fire_and_forget: Got exception out of callback:\n%s" % traceback.format_exc())
t = threading.Thread(target=_fire_and_forget)
t.start()
def send_async(conn, data):
fire_and_forget(lambda: conn.send(data))
fire_and_forget(lambda: conn.send(data))
def log_stdout(*args):
print(*args, file=sys.stdout, flush=True)
print(*args, file=sys.stdout, flush=True)
def log_stderr(*args):
print(*args, file=sys.stdout, flush=True)
print(*args, file=sys.stdout, flush=True)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment