+ def stringify_yx(self, tuple_):
+ """Transform tuple (y,x) into string 'Y:'+str(y)+',X:'+str(x)."""
+ return 'Y:' + str(tuple_[0]) + ',X:' + str(tuple_[1])
+
+ def quoted(self, string):
+ """Quote and escape string so client interprets it as single token."""
+ quoted = []
+ quoted += ['"']
+ for c in string:
+ if c in {'"', '\\'}:
+ quoted += ['\\']
+ quoted += [c]
+ quoted += ['"']
+ return ''.join(quoted)
+
+ def proceed_to_next_player_turn(self, connection_id):
+ """Run game world turns until player can decide their next step.
+
+ Sends a 'TURN_FINISHED' message, then iterates through all non-player
+ things, on each step furthering them in their tasks (and letting them
+ decide new ones if they finish). The iteration order is: first all
+ things that come after the player in the world things list, then (after
+ incrementing the world turn) all that come before the player; then the
+ player's .proceed() is run, and if it does not finish his task, the
+ loop starts at the beginning. Once the player's task is finished, the
+ loop breaks, and client-relevant game data is sent.
+ """
+ self.send_all('TURN_FINISHED ' + str(self.world.turn))
+ while True:
+ for thing in self.world.things[self.world.player_i+1:]:
+ thing.proceed()
+ self.world.turn += 1
+ for thing in self.world.things[:self.world.player_i]:
+ thing.proceed()
+ self.world.player.proceed(is_AI=False)
+ if self.world.player.task is None:
+ break
+ self.send_all('NEW_TURN ' + str(self.world.turn))
+ self.send_all('MAP_SIZE ' + self.stringify_yx(self.world.map_size))
+ self.send_all('TERRAIN\n' + self.quoted(self.world.map_))
+ for thing in self.world.things:
+ self.send_all('THING TYPE:' + thing.type + ' '
+ + self.stringify_yx(thing.position))
+