1 from plomrogue.tasks import Task_WAIT, Task_MOVE
2 from plomrogue.errors import GameError
3 from plomrogue.commands import cmd_ALL, cmd_LOGIN, cmd_QUERY
4 from plomrogue.io import GameIO
5 from plomrogue.misc import quote
6 from plomrogue.things import Thing, ThingPlayer
17 def get_thing(self, id_, create_unfound):
18 # No default for create_unfound because every call to get_thing
19 # should be accompanied by serious consideration whether to use it.
20 for thing in self.things:
24 t = self.thing_type(self, id_)
33 def __init__(self, *args, **kwargs):
34 super().__init__(*args, **kwargs)
36 self.io = GameIO(self)
37 self.tasks = {'WAIT': Task_WAIT,
39 self.commands = {'QUERY': cmd_QUERY, 'ALL': cmd_ALL, 'LOGIN': cmd_LOGIN}
40 self.thing_type = Thing
41 self.thing_types = {'player': ThingPlayer}
44 def get_string_options(self, string_option_type):
45 if string_option_type == 'direction':
46 return ['UP', 'DOWN', 'LEFT', 'RIGHT']
49 def send_gamestate(self, connection_id=None):
50 """Send out game state data relevant to clients."""
52 def send_thing(thing):
53 self.io.send('THING_POS %s %s' % (thing.id_, t.position))
55 self.io.send('TURN ' + str(self.turn))
58 self.io.send('GAME_STATE_COMPLETE')
62 for connection_id in self.sessions:
63 if not connection_id in self.io.server.clients:
64 t = self.get_thing(self.sessions[connection_id], create_unfound=False)
66 to_delete += [connection_id]
67 for connection_id in to_delete:
68 del self.sessions[connection_id]
70 for t in [t for t in self.things]:
74 except GameError as e:
75 for connection_id in [c_id for c_id in self.sessions
76 if self.sessions[c_id] == t.id_]:
77 self.io.send('GAME_ERROR ' + quote(str(e)), connection_id)
83 def get_command(self, command_name):
85 def partial_with_attrs(f, *args, **kwargs):
86 from functools import partial
87 p = partial(f, *args, **kwargs)
88 p.__dict__.update(f.__dict__)
91 def cmd_TASK_colon(task_name, game, *args, connection_id):
92 if connection_id not in game.sessions:
93 raise GameError('Not registered as player.')
94 t = game.get_thing(game.sessions[connection_id], create_unfound=False)
95 t.set_next_task(task_name, args)
97 def task_prefixed(command_name, task_prefix, task_command):
98 if command_name.startswith(task_prefix):
99 task_name = command_name[len(task_prefix):]
100 if task_name in self.tasks:
101 f = partial_with_attrs(task_command, task_name, self)
102 task = self.tasks[task_name]
103 f.argtypes = task.argtypes
107 command = task_prefixed(command_name, 'TASK:', cmd_TASK_colon)
110 if command_name in self.commands:
111 f = partial_with_attrs(self.commands[command_name], self)
115 def new_thing_id(self):
116 if len(self.things) == 0:
118 # DANGEROUS – if anywhere we append a thing to the list of lower
119 # ID than the highest-value ID, this might lead to re-using an
120 # already active ID. This condition /should/ not be fulfilled
121 # anywhere in the code, but if it does, trouble here is one of
122 # the more obvious indicators that it does – that's why there's
123 # no safeguard here against this.
124 return self.things[-1].id_ + 1