1 from plomrogue.tasks import (Task_WAIT, Task_MOVE, Task_WRITE,
2 Task_FLATTEN_SURROUNDINGS)
3 from plomrogue.errors import GameError
4 from plomrogue.commands import (cmd_ALL, cmd_LOGIN, cmd_QUERY, cmd_PING,
5 cmd_TURN, cmd_MAP_LINE)
6 from plomrogue.io import GameIO
7 from plomrogue.misc import quote
8 from plomrogue.things import Thing, ThingPlayer
9 from plomrogue.mapping import YX, MapGeometrySquare, Map
20 def get_thing(self, id_, create_unfound):
21 # No default for create_unfound because every call to get_thing
22 # should be accompanied by serious consideration whether to use it.
23 for thing in self.things:
27 t = self.thing_type(self, id_)
36 def __init__(self, save_file, *args, **kwargs):
38 super().__init__(*args, **kwargs)
40 self.io = GameIO(self, save_file)
41 self.tasks = {'WAIT': Task_WAIT,
44 'FLATTEN_SURROUNDINGS': Task_FLATTEN_SURROUNDINGS}
45 self.map_geometry = MapGeometrySquare(YX(24, 40))
46 self.commands = {'QUERY': cmd_QUERY,
50 'MAP_LINE': cmd_MAP_LINE,
52 self.thing_type = Thing
53 self.thing_types = {'player': ThingPlayer}
55 self.map = Map(self.map_geometry.size)
56 if os.path.exists(self.io.save_file):
57 if not os.path.isfile(self.io.save_file):
58 raise GameError('save file path refers to non-file')
60 with open(self.io.save_file, 'r') as f:
62 for i in range(len(lines)):
64 print("FILE INPUT LINE %5s: %s" % (i, line), end='')
65 self.io.handle_input(line)
67 def get_string_options(self, string_option_type):
69 if string_option_type == 'direction':
70 return self.map_geometry.get_directions()
71 if string_option_type == 'char':
73 string.digits + string.ascii_letters + string.punctuation]
76 def send_gamestate(self, connection_id=None):
77 """Send out game state data relevant to clients."""
79 def send_thing(thing):
80 self.io.send('THING_POS %s %s' % (thing.id_, t.position))
82 self.io.send('TURN ' + str(self.turn))
85 self.io.send('MAP %s %s' % (self.map_geometry.size, quote(self.map.terrain)))
86 self.io.send('GAME_STATE_COMPLETE')
90 for connection_id in self.sessions:
91 if not connection_id in self.io.server.clients:
92 t = self.get_thing(self.sessions[connection_id], create_unfound=False)
94 to_delete += [connection_id]
95 for connection_id in to_delete:
96 del self.sessions[connection_id]
98 for t in [t for t in self.things]:
102 except GameError as e:
103 for connection_id in [c_id for c_id in self.sessions
104 if self.sessions[c_id] == t.id_]:
105 self.io.send('GAME_ERROR ' + quote(str(e)), connection_id)
108 self.send_gamestate()
112 def get_command(self, command_name):
114 def partial_with_attrs(f, *args, **kwargs):
115 from functools import partial
116 p = partial(f, *args, **kwargs)
117 p.__dict__.update(f.__dict__)
120 def cmd_TASK_colon(task_name, game, *args, connection_id):
121 if connection_id not in game.sessions:
122 raise GameError('Not registered as player.')
123 t = game.get_thing(game.sessions[connection_id], create_unfound=False)
124 t.set_next_task(task_name, args)
126 def task_prefixed(command_name, task_prefix, task_command):
127 if command_name.startswith(task_prefix):
128 task_name = command_name[len(task_prefix):]
129 if task_name in self.tasks:
130 f = partial_with_attrs(task_command, task_name, self)
131 task = self.tasks[task_name]
132 f.argtypes = task.argtypes
136 command = task_prefixed(command_name, 'TASK:', cmd_TASK_colon)
139 if command_name in self.commands:
140 f = partial_with_attrs(self.commands[command_name], self)
144 def new_thing_id(self):
145 if len(self.things) == 0:
147 # DANGEROUS – if anywhere we append a thing to the list of lower
148 # ID than the highest-value ID, this might lead to re-using an
149 # already active ID. This condition /should/ not be fulfilled
150 # anywhere in the code, but if it does, trouble here is one of
151 # the more obvious indicators that it does – that's why there's
152 # no safeguard here against this.
153 return self.things[-1].id_ + 1
160 with open(self.io.save_file, 'w') as f:
161 write(f, 'TURN %s' % self.turn)
162 for y, line in self.map.lines():
163 write(f, 'MAP_LINE %5s %s' % (y, quote(line)))