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 cmd_TURN, cmd_MAP_LINE)
5 from plomrogue.io import GameIO
6 from plomrogue.misc import quote
7 from plomrogue.things import Thing, ThingPlayer
8 from plomrogue.mapping import YX, MapGeometrySquare, Map
19 def get_thing(self, id_, create_unfound):
20 # No default for create_unfound because every call to get_thing
21 # should be accompanied by serious consideration whether to use it.
22 for thing in self.things:
26 t = self.thing_type(self, id_)
35 def __init__(self, save_file, *args, **kwargs):
37 super().__init__(*args, **kwargs)
39 self.io = GameIO(self, save_file)
40 self.tasks = {'WAIT': Task_WAIT,
43 self.map_geometry = MapGeometrySquare(YX(24, 40))
44 self.commands = {'QUERY': cmd_QUERY,
48 'MAP_LINE': cmd_MAP_LINE,
50 self.thing_type = Thing
51 self.thing_types = {'player': ThingPlayer}
53 self.map = Map(self.map_geometry.size)
54 if os.path.exists(self.io.save_file):
55 if not os.path.isfile(self.io.save_file):
56 raise GameError('save file path refers to non-file')
58 with open(self.io.save_file, 'r') as f:
60 for i in range(len(lines)):
62 print("FILE INPUT LINE %5s: %s" % (i, line), end='')
63 self.io.handle_input(line)
65 def get_string_options(self, string_option_type):
67 if string_option_type == 'direction':
68 return self.map_geometry.get_directions()
69 if string_option_type == 'char':
71 string.digits + string.ascii_letters + string.punctuation]
74 def send_gamestate(self, connection_id=None):
75 """Send out game state data relevant to clients."""
77 def send_thing(thing):
78 self.io.send('THING_POS %s %s' % (thing.id_, t.position))
80 self.io.send('TURN ' + str(self.turn))
83 self.io.send('MAP %s %s' % (self.map_geometry.size, quote(self.map.terrain)))
84 self.io.send('GAME_STATE_COMPLETE')
88 for connection_id in self.sessions:
89 if not connection_id in self.io.server.clients:
90 t = self.get_thing(self.sessions[connection_id], create_unfound=False)
92 to_delete += [connection_id]
93 for connection_id in to_delete:
94 del self.sessions[connection_id]
96 for t in [t for t in self.things]:
100 except GameError as e:
101 for connection_id in [c_id for c_id in self.sessions
102 if self.sessions[c_id] == t.id_]:
103 self.io.send('GAME_ERROR ' + quote(str(e)), connection_id)
106 self.send_gamestate()
110 def get_command(self, command_name):
112 def partial_with_attrs(f, *args, **kwargs):
113 from functools import partial
114 p = partial(f, *args, **kwargs)
115 p.__dict__.update(f.__dict__)
118 def cmd_TASK_colon(task_name, game, *args, connection_id):
119 if connection_id not in game.sessions:
120 raise GameError('Not registered as player.')
121 t = game.get_thing(game.sessions[connection_id], create_unfound=False)
122 t.set_next_task(task_name, args)
124 def task_prefixed(command_name, task_prefix, task_command):
125 if command_name.startswith(task_prefix):
126 task_name = command_name[len(task_prefix):]
127 if task_name in self.tasks:
128 f = partial_with_attrs(task_command, task_name, self)
129 task = self.tasks[task_name]
130 f.argtypes = task.argtypes
134 command = task_prefixed(command_name, 'TASK:', cmd_TASK_colon)
137 if command_name in self.commands:
138 f = partial_with_attrs(self.commands[command_name], self)
142 def new_thing_id(self):
143 if len(self.things) == 0:
145 # DANGEROUS – if anywhere we append a thing to the list of lower
146 # ID than the highest-value ID, this might lead to re-using an
147 # already active ID. This condition /should/ not be fulfilled
148 # anywhere in the code, but if it does, trouble here is one of
149 # the more obvious indicators that it does – that's why there's
150 # no safeguard here against this.
151 return self.things[-1].id_ + 1
158 with open(self.io.save_file, 'w') as f:
159 write(f, 'TURN %s' % self.turn)
160 for y, line in self.map.lines():
161 write(f, 'MAP_LINE %5s %s' % (y, quote(line)))