+class Server(socketserver.ThreadingTCPServer):
+ """Bind together threaded IO handling server and message queue."""
+
+ def __init__(self, queue, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.queue_out = queue
+ self.daemon_threads = True # Else, server's threads have daemon=False.
+
+
+class IO_Handler(socketserver.BaseRequestHandler):
+
+ def handle(self):
+ """Move messages between network socket and main thread via queues.
+
+ On start, sets up new queue, sends it via self.server.queue_out to
+ main thread, and from then on receives messages to send back from the
+ main thread via that new queue.
+
+ At the same time, loops over socket's recv to get messages from the
+ outside via self.server.queue_out into the main thread. Ends connection
+ once a 'QUIT' message is received from socket, and then also kills its
+ own queue.
+
+ All messages to the main thread are tuples, with the first element a
+ meta command ('ADD_QUEUE' for queue creation, 'KILL_QUEUE' for queue
+ deletion, and 'COMMAND' for everything else), the second element a UUID
+ that uniquely identifies the thread (so that the main thread knows whom
+ to send replies back to), and optionally a third element for further
+ instructions.
+ """
+ import plom_socket_io
+
+ def caught_send(socket, message):
+ """Send message by socket, catch broken socket connection error."""
+ try:
+ plom_socket_io.send(socket, message)
+ except plom_socket_io.BrokenSocketConnection:
+ pass
+
+ def send_queue_messages(socket, queue_in, thread_alive):
+ """Send messages via socket from queue_in while thread_alive[0]."""
+ while thread_alive[0]:
+ try:
+ msg = queue_in.get(timeout=1)
+ except queue.Empty:
+ continue
+ caught_send(socket, msg)
+
+ import uuid
+ print('CONNECTION FROM:', str(self.client_address))
+ connection_id = uuid.uuid4()
+ queue_in = queue.Queue()
+ self.server.queue_out.put(('ADD_QUEUE', connection_id, queue_in))
+ thread_alive = [True]
+ t = threading.Thread(target=send_queue_messages,
+ args=(self.request, queue_in, thread_alive))
+ t.start()
+ for message in plom_socket_io.recv(self.request):
+ if message is None:
+ caught_send(self.request, 'BAD MESSAGE')
+ elif 'QUIT' == message:
+ caught_send(self.request, 'BYE')
+ break
+ else:
+ self.server.queue_out.put(('COMMAND', connection_id, message))
+ self.server.queue_out.put(('KILL_QUEUE', connection_id))
+ thread_alive[0] = False
+ print('CONNECTION CLOSED FROM:', str(self.client_address))
+ self.request.close()
+
+
+class Task:
+
+ def __init__(self, name, args=(), kwargs={}):
+ self.name = name
+ self.args = args
+ self.kwargs = kwargs
+ self.todo = 1
+
+
+class Thing:
+
+ def __init__(self, type_, position):
+ self.type = type_
+ self.position = position
+ self.task = Task('wait')
+
+ def task_wait(self):
+ pass
+
+ def task_move(self, direction):
+ if direction == 'UP':
+ self.position[0] -= 1
+ elif direction == 'DOWN':
+ self.position[0] += 1
+ elif direction == 'RIGHT':
+ self.position[1] += 1
+ elif direction == 'LEFT':
+ self.position[1] -= 1
+
+ def decide_task(self):
+ if self.position[1] > 1:
+ self.set_task('move', 'LEFT')
+ elif self.position[1] < 3:
+ self.set_task('move', 'RIGHT')
+ else:
+ self.set_task('wait')
+
+ def set_task(self, task, *args, **kwargs):
+ self.task = Task(task, args, kwargs)
+
+ def proceed(self, is_AI=True):
+ """Further the thing in its tasks.
+
+ Decrements .task.todo; if it thus falls to <= 0, enacts method whose
+ name is 'task_' + self.task.name and sets .task = None. If is_AI, calls
+ .decide_task to decide a self.task.
+ """
+ self.task.todo -= 1
+ if self.task.todo <= 0:
+ task = getattr(self, 'task_' + self.task.name)
+ task(*self.task.args, **self.task.kwargs)
+ self.task = None
+ if is_AI and self.task is None:
+ self.decide_task()
+
+
+class World:
+
+ def __init__(self):
+ self.turn = 0
+ self.map_size = (5, 5)
+ self.map_ = 'xxxxx\n' +\
+ 'x...x\n' +\
+ 'x.X.x\n' +\
+ 'x...x\n' +\
+ 'xxxxx'
+ self.things = [Thing('human', [3, 3]), Thing('monster', [1, 1])]
+ self.player_i = 0
+ self.player = self.things[self.player_i]
+
+