1 from plomrogue.tasks import Task_WAIT, Task_MOVE, Task_WRITE
2 from plomrogue.errors import GameError
3 from plomrogue.commands import cmd_ALL, cmd_LOGIN, cmd_QUERY, cmd_PING
4 from plomrogue.io import GameIO
5 from plomrogue.misc import quote
6 from plomrogue.things import Thing, ThingPlayer
7 from plomrogue.mapping import YX, MapGeometrySquare, Map
18 def get_thing(self, id_, create_unfound):
19 # No default for create_unfound because every call to get_thing
20 # should be accompanied by serious consideration whether to use it.
21 for thing in self.things:
25 t = self.thing_type(self, id_)
34 def __init__(self, *args, **kwargs):
35 super().__init__(*args, **kwargs)
37 self.io = GameIO(self)
38 self.tasks = {'WAIT': Task_WAIT,
41 self.map_geometry = MapGeometrySquare(YX(24, 40))
42 self.commands = {'QUERY': cmd_QUERY,
46 self.thing_type = Thing
47 self.thing_types = {'player': ThingPlayer}
49 self.map = Map(self.map_geometry.size)
51 def get_string_options(self, string_option_type):
53 if string_option_type == 'direction':
54 return self.map_geometry.get_directions()
55 if string_option_type == 'char':
57 string.digits + string.ascii_letters + string.punctuation]
60 def send_gamestate(self, connection_id=None):
61 """Send out game state data relevant to clients."""
63 def send_thing(thing):
64 self.io.send('THING_POS %s %s' % (thing.id_, t.position))
66 self.io.send('TURN ' + str(self.turn))
69 self.io.send('MAP %s %s' % (self.map_geometry.size, quote(self.map.terrain)))
70 self.io.send('GAME_STATE_COMPLETE')
74 for connection_id in self.sessions:
75 if not connection_id in self.io.server.clients:
76 t = self.get_thing(self.sessions[connection_id], create_unfound=False)
78 to_delete += [connection_id]
79 for connection_id in to_delete:
80 del self.sessions[connection_id]
82 for t in [t for t in self.things]:
86 except GameError as e:
87 for connection_id in [c_id for c_id in self.sessions
88 if self.sessions[c_id] == t.id_]:
89 self.io.send('GAME_ERROR ' + quote(str(e)), connection_id)
95 def get_command(self, command_name):
97 def partial_with_attrs(f, *args, **kwargs):
98 from functools import partial
99 p = partial(f, *args, **kwargs)
100 p.__dict__.update(f.__dict__)
103 def cmd_TASK_colon(task_name, game, *args, connection_id):
104 if connection_id not in game.sessions:
105 raise GameError('Not registered as player.')
106 t = game.get_thing(game.sessions[connection_id], create_unfound=False)
107 t.set_next_task(task_name, args)
109 def task_prefixed(command_name, task_prefix, task_command):
110 if command_name.startswith(task_prefix):
111 task_name = command_name[len(task_prefix):]
112 if task_name in self.tasks:
113 f = partial_with_attrs(task_command, task_name, self)
114 task = self.tasks[task_name]
115 f.argtypes = task.argtypes
119 command = task_prefixed(command_name, 'TASK:', cmd_TASK_colon)
122 if command_name in self.commands:
123 f = partial_with_attrs(self.commands[command_name], self)
127 def new_thing_id(self):
128 if len(self.things) == 0:
130 # DANGEROUS – if anywhere we append a thing to the list of lower
131 # ID than the highest-value ID, this might lead to re-using an
132 # already active ID. This condition /should/ not be fulfilled
133 # anywhere in the code, but if it does, trouble here is one of
134 # the more obvious indicators that it does – that's why there's
135 # no safeguard here against this.
136 return self.things[-1].id_ + 1