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 look_end(self, _): self.lap_nbr -= 1 if self.lap_nbr: transaction = LookTransaction(self.look_end) safe_controller.execute(transaction) else: my_print("Checking my mails !") statemachine.closure = lambda: statemachine.pop()
def follow_dir(self, dir): path = Path() real_dir = Vector() if dir.x != 0: real_dir.x = dir.x else: real_dir.y = dir.y path.addPoint(real_dir, EmptyPathTransaction()) safe_controller.execute(PathManipulator(path.generateOrder()[0], self.listen_the_queen))
def castIncantation(self, incantation): incant = IncantationTransaction(self.incantationStart, self.incantationStart, self.incantationEnd, self.incantationEnd, lambda ok=None: None) incantation.addTransaction(incant) for i in range(self.delay): incantation.addTransaction(LookTransaction(lambda ok: None)) safe_controller.execute(incantation)
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 meet_callback(self, _): for m in controller.msgQueue: meet = MsgProtocol.is_meet_ants(m.text) if meet and ant.uuid in meet['recipients'] and meet[ 'sender'] == ant.queen.uuid: statemachine.closure = lambda ok=None: statemachine.replace( FollowQueenState()) return meet = LookTransaction(self.meet_callback) safe_controller.execute(meet, rollback=False)
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 updateAntLook(self, look): ant.look = look found, path = self.findLooksItems(look) if found: save = path.generateOpti(True)[0] my_log(save) self.pathHandler = PathManipulator( save, self.checkEnd) # TODO estimate ? safe_controller.execute(self.pathHandler) else: self.goNextPlace()
def updateAntLook(self, look): ant.look = look found, path = self.findEmptyTile(look) if found: save = path.generateOrder(False)[0] self.pathHandler = PathManipulator( save, lambda ok=None: statemachine.replace(self.replacement_state)) safe_controller.execute(self.pathHandler) else: self.goNextPlace()
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 goNextPlace(self): path = Path() if self.progress == self.surface: left_dist = ant.lvl path.addPoint(Vector(-left_dist, 0), EmptyPathTransaction()) path.addPoint(Vector(-left_dist, 1), LookTransaction(lambda value: None)) self.progress = 0 else: path.addPoint(Vector(0, 1), LookTransaction(lambda value: None)) self.progress += 1 path, look = path.generateOrder(False) self.pathHandler = PathManipulator(path, self.updateAntLook) safe_controller.execute(self.pathHandler)
def find_callback(self, _): for m in controller.msgQueue: seek = MsgProtocol.is_seek_slave(m.text) if seek and seek['recipient'] == ant.uuid and seek[ 'sender'] == ant.queen.uuid: items = { Resources(name): int(value) for name, value in seek['items'].items() } ant.request = dict(items) statemachine.closure = lambda: statemachine.push( SeekItemsState(items)) return find = LookTransaction(self.find_callback) safe_controller.execute(find, rollback=False)
def checkEnd(self, *args): del args check = True for k, v in self.items_dict.items(): if v > 0: check = False if check and (not self.rollback or self.tracker.stayed()): statemachine.closure = lambda: statemachine.pop() elif check and self.rollback: self.rollback = False look, path = self.tracker.returnHome() self.pathHandler = PathManipulator(path, self.checkEnd) safe_controller.execute(self.pathHandler) else: safe_controller.execute(LookTransaction(self.updateAntLook))
def listen_the_queen(self, _=None): save = None for msg in controller.msgQueue: ping = MsgProtocol.is_ping_team(msg.text) if ping and ping['sender'] == ant.queen.uuid: save = msg if save and (save.dir.x != self.last.x or save.dir.y != self.last.y): self.last = save.dir save = None if save and save.dir.x == 0 and save.dir.y == 0: self.set_requested_items() elif save: self.follow_dir(save.dir) self.last = Vector(-100, -100) else: safe_controller.execute(LookTransaction(self.listen_the_queen))
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 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 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) my_print("Waiting for mates...") transaction = LookTransaction(self.look_end) safe_controller.execute(transaction)
def on_push(self, cli): super().on_push(cli) safe_controller.execute(LookTransaction(self.updateAntLook))
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)
def callback(): meet = LookTransaction(self.meet_callback) safe_controller.execute(meet, rollback=False)
def template(self): safe_controller.execute(self._status_stack[0](), rollback=False)
def on_push(self, cli): super().on_push(cli) find = LookTransaction(self.find_callback) safe_controller.execute(find, rollback=False)
def broadcast_team(self): safe_controller.execute(LookTransaction(self.look_for_players))
def on_push(self, cli): super().on_push(cli) safe_controller.execute(LookTransaction(self.listen_the_queen))
def on_push(self, cli): super().on_push(cli) safe_controller.execute(InventoryTransaction(self.aloneLvl))
def on_push(self, cli): super().on_push(cli) transactions = PackedTransaction(self.execute_incantation) transactions.addTransaction(LookTransaction(self.store_look)) transactions.addTransaction(InventoryTransaction(self.store_inventory)) safe_controller.execute(transactions)
def on_push(self, cli): super().on_push(cli) safe_controller.execute(LookTransaction(self.look_for_players))