def __init__(self, device, tasklist): HD.__init__(self, device, tasklist, 'money') self.log=MyLogger('shop', LOG_LEVEL=logging.DEBUG) self.tasklist.addtask(20, f"checking for items to sell", self.image, self.checkItems) self.shoplist=Shoplist(self) self.position=HD.getPos([-15,0]) self.slots=4 self.max_slots=4 self.atshop=False self.temp_shop=self.device.loadTemplates('shop','')
def __init__(self, device, tasklist, crop): HD.__init__(self, device, tasklist, crop) self.switch = [-485, 120] self.scythe = [-190, -80] self.enabled = True self.filelist_full = [] self.filelist_empt = [] self.filelist_trig = [] self.temp_full = [] self.temp_empt = [] self.temp_trig = [] self.tasklist.addProduct(crop, self.addJob, self.getJobTime)
def check(self): self.log.debug('checking board') self.checkImages() skiplist = HD.loadJSON('skip') self.nextcheck = 1 if self.reset_screen(): location = self.device.locate_item(self.base_template, .75, one=True) self.log.debug(location) if len(location) and self.open(location): if not self.checkComplete(): self.log.debug('update board info') for card in self.cards: x, y = card.location self.device.tap(x, y) sleep(.1) products = self.device.check_present( self.product_templates, .93) for product in products: if product in skiplist: self.device.tap(*self.bin) card.reset() break self.log.debug(f'found: {product}') card.add(product) self.nextcheck = 5 self.checkComplete() self.check_cross() self.tasklist.addtask(self.nextcheck, 'board', self.image, self.check)
def checkItems(self): self.atshop=False wait=10 if self.max_slots>self.slots: self.checkShop() self.atshop=True try: json_data=HD.loadJSON('sell') for product,data in data.items(): wished,scheduled=self.tasklist.getWish(product) #check if prodruct needs to be ordered if (scheduled-wished<data['maximum']-self.slots*data['amount']) and data['order']: self.log.debug(f'need more {product}') self.tasklist.addWish(product,data['maximum']-(scheduled-wished)) if -wished>data['minimum']+data['amount']: available=-wished-data['minimum'] stocks=int(available/data['amount']) if stocks<self.slots: self.slots-= stocks else: stocks=self.slots self.slots=0 if stocks: self.log.debug(f'i can sell some') self.shoplist.add(product,stocks, **data) wait=2 except Exception as e: self.log.error(e) finally: self.tasklist.addtask(wait, f"checking for items to sell", self.image, self.checkItems) self.sellItems()
def __init__(self, device, tasklist): HD.__init__(self, device, tasklist, 'board') self.device = device self.tasklist = tasklist self.log = MyLogger('Board', LOG_LEVEL=logging.INFO) self.nextcheck = 0.1 self.image = path.join('images', 'board', 'car_button_C.png') self.base_template = HD.loadTemplates('board', 'base') self.complete_templates = HD.loadTemplates('board', 'check') # self.card_template=HD.loadTemplates('board','pins') self.product_templates = {} self.car = [1335, 775] self.bin = [1175, 780] self.cards = [] for location in [[290, 290], [535, 290], [775, 290], [290, 520], [535, 520], [775, 520], [290, 730], [535, 730], [775, 730]]: self.cards.append(Card(tasklist, location)) self.product_images = [] self.checkImages() self.tasklist.addtask(self.nextcheck, 'board', self.image, self.check)
def updateListData(self): settings = HD.loadJSON('animals') resources = HD.loadJSON('resources') count = {} if len(resources) and "pens" in resources and len(settings): if resources['pens'] != self.data or settings != self.settings: self.data = resources['pens'] self.settings = settings for pen in self.values(): pen.enabled = False for newpen in self.data: animal = newpen['animal'] if animal not in count: count[animal] = 0 count[animal] += 1 name = f"Pen {animal} [{count[animal]}]" if animal in self.settings: data = self.settings[animal] product = data['product'] if name not in self: self[name] = Pen(self.device, self.tasklist, animal, product) data['name'] = name data['log'] = self.log data['enabled'] = True data['position'] = HD.getPos(newpen['location']) data['temp_pen'] = HD.loadTemplates('pens', animal) data['temp_collect'] = HD.loadTemplates( path.join('pens', 'collect'), animal) data['temp_food'] = HD.loadTemplates( path.join('pens', 'food'), animal) data['amount'] = newpen['amount'] data['update'] = self.updateListData for key, value in data.items(): setattr(self[name], key, value)
def updateListData(self): settings = HD.loadJSON('crops') resources = HD.loadJSON('resources') count = {} if len(resources) and "crops" in resources and len(settings): if resources['crops'] != self.data or settings != self.settings: self.log.error("Changes found") self.log.error(settings) self.log.error(self.settings) self.log.error(resources['crops']) self.log.error(self.data) self.data = resources['crops'] self.settings = settings for crop in self.values(): crop.enabled = False for newcrop in self.data: crop = newcrop['crop'] if crop not in count: count[crop] = 0 count[crop] += 1 name = f"Crop {crop} [{count[crop]}]" if crop in self.settings: if name not in self: self[name] = Crop(self.device, self.tasklist, crop) data = self.settings[crop].copy() data.update(newcrop) data['name'] = name data['log'] = self.log data['enabled'] = True self.log.debug(f"location: {newcrop['location']}") data['position'] = self.device.getPos( newcrop['location']) self.log.debug(f"position: {data['position']}") data['temp_switch'] = self.switch_template data['update'] = self.updateListData for key, value in data.items(): setattr(self[name], key, value)
def updateListData(self): self.log.debug('updating data') settings = HD.loadJSON('trees') resources = HD.loadJSON('resources') count = {} if len(resources) and "trees" in resources and len(settings): self.log.debug('data is valid') if resources['trees'] != self.data or settings != self.settings: self.data = resources['trees'] self.settings = settings for fruit in self.values(): fruit.enabled = False for newfruit in self.data: fruit = newfruit['fruit'] self.log.debug(f'fruit: {fruit}') if fruit not in count: count[fruit] = 0 count[fruit] += 1 self.log.debug(f"amount: {newfruit['amount']}") for i in range(newfruit['amount']): name = f"{fruit} [{count[fruit]}-{i}]" self.log.debug(f"name: {name}") if fruit in self.settings: if name not in self: self[name] = Tree(self.device, self.tasklist, fruit) data = self.settings[fruit].copy() data['log'] = self.log data['name'] = name data['enabled'] = True data['position'] = self.device.getPos( newfruit['location']) data['templates'] = HD.loadTemplateMap( 'trees', fruit) data['update'] = self.updateListData for key, value in data.items(): setattr(self[name], key, value)
def __init__(self, device, tasklist, name): HD.__init__(self, device, tasklist, name) self.templates = HD.loadTemplates('visitors', name)
def __init__(self, device, tasklist, product): HD.__init__(self, device, tasklist, product) self.product = product self.enabled = True self.tasklist.addProduct(self.product, self.addJob, self.getJobTime)
def __init__(self, device, tasklist, animal, product): HD.__init__(self, device, tasklist, product) self.animal = animal self.center = [0, 0] self.product = product self.tasklist.addProduct(self.product, self.addJob, self.getJobTime)
class Pen(HD): pencil = HD.loadTemplates('pens', 'pencil') def __init__(self, device, tasklist, animal, product): HD.__init__(self, device, tasklist, product) self.animal = animal self.center = [0, 0] self.product = product self.tasklist.addProduct(self.product, self.addJob, self.getJobTime) # self.tasklist.addtask(0.05, self.animal, self.image, self.collect) def getJobTime(self): if not self.enabled: return False waittime = self.getWaitTime() waittime += self.jobs * self.eattime + 0.1 return waittime def addJob(self): self.jobs += 1 # self.checkFood() self.checkJobs() return self.amount def checkCollected(self): self.log.debug(f"checking if all {self.product} is collected") result = self.device.locate_item(self.temp_collect, self.threshold_collect) if len(result): self.log.debug('not collected') return False return True def checkFed(self): self.log.debug(f"checking if all {self.animal} are fed") result = self.device.locate_item(self.temp_food, self.threshold_food) if len(result): self.log.debug('not fed') return False return True def checkJobs(self): self.log.debug(f"checking jobs for {self.product}") wait = self.getWaitTime() + 0.2 if not self.scheduled: if self.jobs > 0: self.log.debug('adding task') self.jobs += -1 self.setWaittime(wait) self.tasklist.addtask(wait, f"collect: {self.product}", self.image, self.collect) self.tasklist.addWish(self.food, self.amount) self.scheduled = True return # self.tasklist.reset(self.product) def checkFood(self): self.tasklist.resetWish(self.food, self.amount) def exit(self): x, y = self.center self.device.tap(x - 150, y - 80) self.move_from() def locate_pen(self): self.log.debug("move to Pen") if self.reset_screen(): self.move_to() location = self.device.locate_item(self.temp_pen, self.threshold, one=True) if len(location): self.center = np.add(location, self.center_offset) self.device.tap(*self.center) sleep(.3) pencil_loc = self.device.locate_item(self.pencil, 0.8, one=True) if len(pencil_loc): self.center = np.add(pencil_loc, self.pencil_offset) return True return False def loadPath(self): data = self.loadJSON('paths') if not len(data): data = {'default': [[0, 0]]} trace_path = data[ self.animal] if self.animal in data else data['default'] return np.array(trace_path) def createWaypoints(self, delta=[0, 0]): trace_path = self.loadPath() waypoints = np.empty_like(trace_path) for i in range(trace_path.shape[0]): waypoints[i, :] = trace_path[i, :] + self.center + delta return waypoints.tolist() def execute(self, condition, icon, steps=3): r, theta = [0, 0] while (not condition() and r < 15): delta = [cos(theta) * r, sin(theta) * r / 2] waypoints = self.createWaypoints(delta) self.trace(waypoints, *icon) theta += pi / 4 if (theta > pi * 0.9): theta = 0 r += 25 / steps def checkMissingFood(self): wait = 10 if self.check_cross(): self.setWaittime(10) self.log.debug(f"Missing Food. retry in {wait} minutes") self.checkFood() self.tasklist.addtask(wait + .2, f'Try Feeding {self.animal}', self.image, self.feed) return True return False def feed(self, atsite=False): try: self.log.debug('feeding') self.update() if not self.enabled: return False if not (atsite or self.locate_pen()): raise Exception("Could not locate pen") self.setWaittime(10) if atsite: self.execute(self.checkFed, self.icon_feed, steps=1) if self.checkMissingFood(): raise Exception("No Food available") if not self.checkFed(): self.log.debug("Not all animals are fed.") self.execute(self.checkFed, self.icon_feed, steps=3) if self.checkMissingFood(): raise Exception("No Food available") self.checkFood() return True except Exception as e: self.log.error(f"{self.name}") self.log.error(e) return False def collect(self): try: self.log.debug(f'collecting: {self.product}') self.update() if not self.enabled: return False if not self.locate_pen(): raise Exception(f"Could not locate {self.name}") if self.checkCollected(): self.setWaittime(10) self.feed(True) raise Exception(f"Already Collected or not Fed") self.execute(self.checkCollected, self.icon_collect) self.setWaittime(5) if self.check_cross(): raise Exception("Barn is full, need to sell something") self.tasklist.removeSchedule(self.product, self.amount) self.log.debug("Collection succesfull") self.setWaittime(10) if self.feed(True): self.setWaittime(self.eattime) self.scheduled = False self.checkJobs() except Exception as e: self.log.error(f"Fout in {self.name}") self.log.error(e) self.log.debug("retry in 5 minutes") self.tasklist.addtask(5, self.animal, self.image, self.collect) finally: self.exit()