class CastTargetSpell(Engine): def __init__(self, client, spell, target): Engine.__init__(self, client) self.spell = spell self.target = target self.target_mutex = client.target_mutex self.target_mutex.get_target(self._target_ok, self._target_abort) def _target_ok(self): self._client.send(p.Cast(self.spell)) self.engine = SendTarget(self._client, Target(self.target.serial)) self.engine.deferred.addCallbacks(self._target_sent, self._target_failed) def _target_abort(self): self.engine.abort() self._failure() def _target_sent(self, result): self.target_mutex.put_target() reactor.callLater(2, self._success) def _target_failed(self, fail): self.target_mutex.put_target() self._failure(fail)
class CastTargetSpell(Engine): def __init__(self, client, spell, target): Engine.__init__(self, client) self.spell = spell self.target = target self.target_mutex = client.target_mutex self.target_mutex.get_target(self._target_ok, self._target_abort) def _target_ok(self): self._client.send(p.Cast(self.spell)) self.engine = SendTarget(self._client, Target(serial=self.target.serial)) self.engine.deferred.addCallbacks(self._target_sent, self._target_failed) def _target_abort(self): self.engine.abort() self._failure() def _target_sent(self, result): self.target_mutex.put_target() reactor.callLater(2, self._success) def _target_failed(self, fail): self.target_mutex.put_target() self._failure(fail)
class HerdSheep(Engine): def __init__(self, client): Engine.__init__(self, client) self.target_mutex = client.target_mutex self.sheep = find_sheep(client.world) self._next() def _next(self): if len(self.sheep) == 0: self._success() return client = self._client self.crook = find_crook(client.world) if self.crook is None: print "No crook" self._failure() return self.target, self.sheep = self.sheep[0], self.sheep[1:] self.target_mutex.get_target(self.target_ok, self.target_abort) def target_ok(self): client = self._client client.send(p.Use(self.crook.serial)) self.engine = SendTarget(client, Target(serial=self.target.serial)) d = self.engine.deferred d.addCallbacks(self._target_sent, self._target_failed) def target_abort(self): self.engine.abort() self._failure() def _target_sent(self, result): client = self._client self.engine = SendTarget(self._client, Target(serial=client.world.player.serial)) d = self.engine.deferred d.addCallbacks(self._player_sent, self._target_failed) def _player_sent(self, result): self.target_mutex.put_target() reactor.callLater(1, self._next) def _target_failed(self, fail): self.target_mutex.put_target() self._failure(fail)
class HerdSheep(Engine): def __init__(self, client): Engine.__init__(self, client) self.target_mutex = client.target_mutex self.sheep = find_sheep(client.world) self._next() def _next(self): if len(self.sheep) == 0: self._success() return client = self._client self.crook = find_crook(client.world) if self.crook is None: print "No crook" self._failure() return self.target, self.sheep = self.sheep[0], self.sheep[1:] self.target_mutex.get_target(self.target_ok, self.target_abort) def target_ok(self): client = self._client client.send(p.Use(self.crook.serial)) self.engine = SendTarget(client, Target(serial=self.target.serial)) d = self.engine.deferred d.addCallbacks(self._target_sent, self._target_failed) def target_abort(self): self.engine.abort() self._failure() def _target_sent(self, result): client = self._client self.engine = SendTarget(self._client, Target(serial=client.world.player.serial)) d = self.engine.deferred d.addCallbacks(self._player_sent, self._target_failed) def _player_sent(self, result): self.target_mutex.put_target() reactor.callLater(1, self._next) def _target_failed(self, fail): self.target_mutex.put_target() self._failure(fail)
class UseAndTarget(Engine): def __init__(self, client, item, target): Engine.__init__(self, client) if isinstance(item, Entity): item = item.serial if isinstance(target, Entity): target = Target(serial=target.serial) self.item = item self.target = target self.tries = 2 self.engine = None self.target_mutex = client.target_mutex self.target_mutex.get_target(self.target_ok, self.target_abort) def target_ok(self): self._do() def target_abort(self): self.engine.abort() self._failure(Timeout('Target timeout')) def _do(self): client = self._client client.send(p.Use(self.item)) self.engine = SendTarget(client, self.target) self.engine.deferred.addCallbacks(self._target_sent, self._target_failed) def _target_sent(self, result): self.target_mutex.put_target() self._success(result) def _target_failed(self, fail): self.target_mutex.put_target() self._failure(fail) def on_system_message(self, text): if 'must wait to perform another action' in text and \ self.engine is not None and self.tries > 0: self.tries -= 1 self.engine.abort() reactor.callLater(0.5, self._do)
class UseAndTarget(Engine): def __init__(self, client, item, target): Engine.__init__(self, client) if isinstance(item, Entity): item = item.serial if isinstance(target, Entity): target = Target(serial=target.serial) self.item = item self.target = target self.tries = 2 self.engine = None self.target_mutex = client.target_mutex self.target_mutex.get_target(self.target_ok, self.target_abort) def target_ok(self): self._do() def target_abort(self): self.engine.abort() self._failure(Timeout('Target timeout')) def _do(self): client = self._client client.send(p.Use(self.item)) self.engine = SendTarget(client, self.target) self.engine.deferred.addCallbacks(self._target_sent, self._target_failed) def _target_sent(self, result): self.target_mutex.put_target() self._success(result) def _target_failed(self, fail): self.target_mutex.put_target() self._failure(fail) def on_system_message(self, text): if 'must wait to perform another action' in text and \ self.engine is not None and self.tries > 0: self.tries -= 1 self.engine.abort() reactor.callLater(0.5, self._do)