def look_for_players(self, look): for msg in controller.msgQueue: ready = MsgProtocol.is_ready_inc(msg.text) if ready and ready["recipient"] == ant.uuid and ready[ 'sender'] in (mate.uuid for mate in mates): mates.get_mate(ready['sender']).ready = True my_log(msg.text) my_print( ready["sender"], " is ready ! {} / {}".format( len([mate for mate in mates if mate.ready]), len(mates))) #my_print("=> player on tile {}".format(look[0].count("player"))) #my_print("=> look : {}".format(look)) ok = True for mate in mates: if not mate.ready: ok = False break if look[0].count("player") == self.size and ok: my_print("Ready to incant ! ") statemachine.closure = lambda: statemachine.replace( self.replacement_state) else: msg = MsgProtocol.ping_team(ant.uuid) safe_controller.execute( BroadcastTransaction(msg, self.broadcast_team))
def resource_repart(self): full_list = list(itertools.chain(*[[k.value] * v for k, v in requirement[ant.lvl + 1][1].items()])) full_list = split_seq(full_list, len(mates)) full_dict = [dict(Counter(e)) for e in full_list] transaction = PackedTransaction(self.wait_answers) for m, items in zip(mates, full_dict): m.inventory = items msg = MsgProtocol.seek_slave(ant.uuid, m.uuid, items) transaction.addTransaction(BroadcastTransaction(msg, lambda ok=None: None)) safe_controller.execute(transaction, rollback=False)
def popped_over(self): super().popped_over() def callback(): meet = LookTransaction(self.meet_callback) safe_controller.execute(meet, rollback=False) msg = MsgProtocol.seek_end(ant.uuid, ant.queen.uuid) transaction = BroadcastTransaction(msg, callback) safe_controller.execute(transaction, rollback=False)
def wait_enrol_msg(self, *args): for m in controller.msgQueue: enr = MsgProtocol.is_enrolment(m.text) if enr and int(enr['level']) == ant.lvl + 1: my_print("Apply for enrolment : ", enr['sender']) self._status_stack.pop(0) msg = MsgProtocol.apply(ant.uuid, enr['sender']) safe_controller.execute(BroadcastTransaction( msg, self.wait_accept_msg), rollback=False) ant.queen = Mate(enr['sender']) return self.template()
def wait_answers(self, *args): for msg in controller.msgQueue: end = MsgProtocol.is_seek_end(msg.text) if end and end['sender'] in (mate.uuid for mate in mates): mates.get_mate(end['sender']).inventory.clear() end = True for mate in mates: if len(mate.inventory): end = False break if end: msg = MsgProtocol.meet_ants(ant.uuid, [m.uuid for m in mates]) transaction = BroadcastTransaction(msg, self.ping) else: transaction = LookTransaction(self.wait_answers) safe_controller.execute(transaction, rollback=False)
def popped_over(self): super().popped_over() for m in controller.msgQueue: enr = MsgProtocol.is_apply(m.text) if enr and enr['recipient'] == ant.uuid: mates.add_mate(enr['sender']) my_print(enr["sender"], " joined ! {} / {}".format(len(mates), requirement[ant.lvl + 1][0] - 1)) if len(mates) == requirement[ant.lvl + 1][0] - 1: break if len(mates) < requirement[ant.lvl + 1][0] - 1: my_print("Failed to find a team lvl {}".format(ant.lvl + 1)) mates.clear() ant.is_queen = False allow_list = [m.uuid for m in mates] msg = MsgProtocol.allowed_ants(ant.uuid, allow_list) transaction = BroadcastTransaction(msg, lambda: statemachine.pop()) safe_controller.execute(transaction, rollback=False)
def set_requested_items(self): my_print("On queen position") transaction = PackedTransaction(self.wait_others) for k, v in ant.request.items(): sub_transaction = SetTransaction(k, v, lambda ok=None: None, lambda ok=None: None, lambda ok=None: None) transaction.addTransaction(sub_transaction) msg = MsgProtocol.ready_inc(ant.uuid, ant.queen.uuid) transaction.addTransaction(BroadcastTransaction(msg, lambda ok=None: None)) incant = IncantationTransaction( lambda ok=None: None, lambda ok=None: None, lambda ok=None: None, lambda ok=None: None, lambda ok=None: None, write=False ) transaction.addTransaction(incant) safe_controller.execute(transaction, floor=0)
def on_push(self, cli): super().on_push(cli) msg = MsgProtocol.enrolment(ant.uuid, ant.lvl + 1) transaction = BroadcastTransaction(msg, lambda: statemachine.push(WaitAnswerState(70))) safe_controller.execute(transaction, rollback=False)