def phase_three(dog_id, level, quiet_length, fail_max, treat_frequency_min, treat_frequency_max=-1): if treat_frequency_max == -1: treat_frequency_max = treat_frequency_min quiet_timer = 0 treat_timer = 0 treat_dispense_time = 0 fail_count = 0 # Reset quiet status NoiseUtil.reset_bark_status() while quiet_timer < quiet_length and fail_count < fail_max: # Wait 1 second time.sleep(1) if not Config.RUN_FLAG: Logger.data(dog_id, 3, level, "cancelled") return False if treat_timer == 0: treat_dispense_time = random.randint(treat_frequency_min, treat_frequency_max) if NoiseUtil.has_dog_barked: Logger.info("Dog just barked, incrementing fail count.") Logger.data(dog_id, 3, level, "dog_bark") fail_count += 1 quiet_timer = 0 NoiseUtil.reset_bark_status() continue else: quiet_timer += 1 treat_timer += 1 # Check if a treat should be dispensed if treat_timer == treat_dispense_time: PetTutorUtil.dispense_treat() treat_timer = 0 fail_count = 0 if quiet_timer >= quiet_length: # Dog passed the Challenge. return True if fail_count >= fail_max: # Dog has failed the challenge return False Logger.error("[Quiet Challenge] CODE SHOULD NEVER GET HERE!")
def dog_standing(dog_id, level=1): Logger.info("Phase Five: Level %s - Starting" % level) Logger.data(dog_id, 5, level, "starting") stand_time = 0 treat_timer = 1 treat_dispense_time = 0 if level == 2: stand_time = levels['1']['duration'] + 1 """ DOG IS STANDING """ while True: # If the program is calling for cancellation, just cancel whatever we are doing if not Config.RUN_FLAG: return False # If the dog has laid down, move to #D levels. if MoveUtil.is_dog_down: Logger.data(dog_id, 5, 1 if stand_time <= levels['1']['duration'] else 2, "dog_down") return dog_down(dog_id, '1D') # If the dog has barked, reset to level 1 if NoiseUtil.has_dog_barked: stand_time = 0 treat_timer = 1 continue # If the dog has completed Level 1, announce and save if stand_time == levels['1']['duration']: FileIOUtil.save(dog_id, 5, 1) Logger.info("Phase Five: Level 1 - Complete") Logger.data(dog_id, 5, 1, "complete") FileIOUtil.save(dog_id, 5, 1) Logger.info("Phase Five: Level 2 - Starting") Logger.data(dog_id, 5, 2, "starting") # If we just gave a treat, figure out the next time we should dispense one if treat_timer == 1: treat_dispense_time = random.randint( levels['1']['min'] if stand_time <= levels['1']['duration'] else levels['2']['min'], levels['1']['max'] if stand_time <= levels['1']['duration'] else levels['2']['max']) # Check if a treat should be dispensed if treat_timer == treat_dispense_time: dispense_treat() treat_timer = 0 # End stuff time.sleep(1) treat_timer += 1 stand_time += 1
def level_one(dog_id): Logger.info("Phase One: Level 1 - Starting") Logger.data(dog_id, 1, 1, "starting") scheduler.schedule(5, 150, dispense_treat) if not Config.RUN_FLAG: Logger.data(dog_id, 1, 1, "cancelled") return False FileIOUtil.save(dog_id, 1, 1) Logger.info("Phase One: Level 1 - Complete") Logger.data(dog_id, 1, 1, "complete") return True
def level_four(dog_id): Logger.info("Phase Two: Level 4 - Starting") Logger.data(dog_id, 3, 4, "starting") scheduler.schedule(12, 60, dispense_treat) if not Config.RUN_FLAG: Logger.data(dog_id, 2, 4, "cancelled") return False FileIOUtil.save(dog_id, 2, 4) Logger.info("Phase Two: Level 4 - Complete") Logger.data(dog_id, 3, 4, "complete") return True
def level_three(dog_id): Logger.info("Phase Two: Level 3 - Starting") Logger.data(dog_id, 2, 3, "starting") scheduler.schedule(8, 64, dispense_treat) if not Config.RUN_FLAG: Logger.data(dog_id, 2, 3, "cancelled") return False FileIOUtil.save(dog_id, 2, 3) Logger.info("Phase Two: Level 3 - Complete") Logger.data(dog_id, 2, 3, "complete") return level_four(dog_id)
def level_five(dog_id): Logger.info("Phase Three: Level 5 - Starting") Logger.data(dog_id, 3, 5, "starting") if Challenge.phase_three(dog_id=dog_id, level=5, treat_frequency_min=12, quiet_length=60, fail_max=3): FileIOUtil.save(dog_id, 3, 5) Logger.info("Phase Three: Level 5 - Complete") Logger.data(dog_id, 3, 5, "complete") return level_six(dog_id) else: if not Config.RUN_FLAG: Logger.info("Phase Three: Level 5 - Cancelled") return False Logger.info("Phase Three: Level 5 - Failed, regressing to Level 4") Logger.data(dog_id, 3, 5, "failed") return level_four(dog_id)
def level_two(dog_id): Logger.info("Phase Three: Level 2 - Starting") Logger.data(dog_id, 3, 2, "starting") if Challenge.phase_three(dog_id=dog_id, level=2, treat_frequency_min=3, quiet_length=30, fail_max=3): FileIOUtil.save(dog_id, 3, 2) Logger.info("Phase Three: Level 2 - Complete") Logger.data(dog_id, 3, 2, "complete") return level_three(dog_id) else: if not Config.RUN_FLAG: Logger.info("Phase Three: Level 2 - Cancelled") return False Logger.info("Phase Three: Level 2 - Failed, regressing to Level 2") Logger.data(dog_id, 3, 2, "failed") return level_two(dog_id)
def dog_down(dog_id, level="1D"): # How long the dog is laying down down_time = 0 # What time interval we will give the treat on treat_timer = 1 treat_dispense_time = 0 fail_count = 0 stand_timer = 0 level = int(level[0]) # Grace periods grace_down_timer = 0 grace_bark_timer = 0 dog_stand_state = False Logger.info("Phase Five: Level %sD - Starting" % level) Logger.data(dog_id, 5, "%sD" % level, "starting") while True: if not Config.RUN_FLAG: return False ''' Setup ''' if down_time >= levels["%sD" % level]['duration']: Logger.info("Phase Five: Level %sD - Complete" % level) Logger.data(dog_id, 5, "%sD" % level, "complete") FileIOUtil.save(dog_id, 5, "%sD" % level) if level >= 9: Logger.info("Phase Five: Complete.") return # Dog has passed Phase 5 level += 1 # Increase the level the dog is on Logger.info("Phase Five: Level %sD - Starting" % level) Logger.data(dog_id, 5, "%sD" % level, "starting") down_time = 0 # Start timer over for new level treat_timer = 1 # Calculate new treat time based on new level if treat_timer == 1: treat_dispense_time = random.randint(levels["%sD" % level]['min'], levels["%sD" % level]['max']) ''' Log what happened in the last second ''' if not MoveUtil.is_dog_down and not dog_stand_state: dog_stand_state = True Logger.data(dog_id, 5, "%sD" % level, "dog_stand") if MoveUtil.is_dog_down and dog_stand_state: dog_stand_state = False Logger.data(dog_id, 5, "%sD" % level, "dog_down") if NoiseUtil.has_dog_barked: Logger.data(dog_id, 5, "%sD" % level, "dog_bark") ''' manage stand timer ''' if not MoveUtil.is_dog_down: stand_timer += 1 else: stand_timer = 0 ''' Punishments for bad behavior ''' if grace_down_timer <= 0 and not MoveUtil.is_dog_down: NoiseUtil.reset_bark_status() if level < 8 or (level >= 8 and stand_timer > 10): Logger.info("Dog just stood, incrementing fail count.") fail_count += 1 grace_down_timer = 20 # Grace period down_time = 0 elif grace_bark_timer <= 0 and NoiseUtil.has_dog_barked: NoiseUtil.reset_bark_status() Logger.info("Dog just barked, incrementing fail count.") fail_count += 1 grace_bark_timer = 5 # Grace period down_time = 0 if fail_count >= 6: # Dog failed 2 levels in a row, go to level 1 Logger.info( "Phase Five: Level %sD and %sD - Failed, regressing to Level 1" % (level + 1, level)) Logger.data(dog_id, 5, level, "failed_restart") return dog_standing(dog_id, 1) if fail_count == 3: # Maximum number of fails before regressing level Logger.info( "Phase Five: Level %sD - Failed, regressing to Level %sD" % (level, level - 1 if level - 1 >= 1 else 1)) Logger.data(dog_id, 5, "%sD" % level, "failed") if level > 1: level -= 1 Logger.info("Phase Five: Level %sD - Starting" % level) Logger.data(dog_id, 5, "%sD" % level, "starting") down_time = 0 # Treat reset treat_timer = 1 treat_dispense_time = 0 # Grace periods reset grace_bark_timer = 0 grace_down_timer = 0 continue ''' Reset grace period once dog lays back down ''' if grace_down_timer > 0 and MoveUtil.is_dog_down: grace_down_timer = 0 down_time = 0 if treat_timer == treat_dispense_time: PetTutorUtil.dispense_treat() treat_timer = 0 fail_count = 0 # End stuff time.sleep(1) treat_timer += 1 down_time += 1 if grace_down_timer > 0: grace_down_timer -= 1 if grace_bark_timer > 0: grace_bark_timer -= 1
def phase_four(dog_id, level, still_length, fail_max, treat_frequency_min, treat_frequency_max=-1): if treat_frequency_max == -1: treat_frequency_max = treat_frequency_min still_timer = 0 treat_timer = 0 treat_dispense_time = 0 fail_count = 0 # Reset quiet and move status NoiseUtil.reset_bark_status() MoveUtil.reset_move_status() while still_timer < still_length and fail_count < fail_max: # Wait 1 second time.sleep(1) if not Config.RUN_FLAG: Logger.data(dog_id, 4, level, "cancelled") return False if treat_timer == 0: treat_dispense_time = random.randint(treat_frequency_min, treat_frequency_max) if NoiseUtil.has_dog_barked or MoveUtil.has_dog_moved: if NoiseUtil.has_dog_barked: Logger.data(dog_id, 4, level, "dog_bark") else: Logger.data(dog_id, 4, level, "dog_move") fail_count += 1 still_timer = 0 NoiseUtil.reset_bark_status() MoveUtil.reset_move_status() # Check if the dog has failed too many times if fail_count >= fail_max: break """ Wait for 10 seconds to allow the dog to stop moving. """ sleep_time = 0 for i in range(1, Config.MOVE_PAUSE + 1): time.sleep(1) Logger.debug("Letting the dog stop moving... (%s seconds)" % i) if not MoveUtil.has_dog_moved: sleep_time += 1 else: sleep_time = 0 MoveUtil.reset_move_status() if sleep_time >= 2: break continue # Continue loop (timer has been reset, don't reward dog, etc) else: still_timer += 1 treat_timer += 1 # Check if a treat should be dispensed if treat_timer == treat_dispense_time: PetTutorUtil.dispense_treat() treat_timer = 0 fail_count = 0 """ Wait for 5 seconds to allow the dog to eat the treat. """ sleep_time = 0 MoveUtil.reset_move_status( ) # This is just a precaution, it should already be reset for i in range(1, Config.TREAT_PAUSE + 1): time.sleep(1) if not MoveUtil.has_dog_moved: sleep_time += 1 # Increment the amount of time the dog has not moved. else: sleep_time = 0 # The dog moved, reset sleep time MoveUtil.reset_move_status() if sleep_time >= 2: # Once dog has not moved for X seconds, continue training. break Logger.debug("Letting the dog eat... (%s seconds)" % i) if still_timer >= still_length: # Dog passed the Challenge. return True if fail_count >= fail_max: # Dog has failed the challenge return False Logger.error("[Still Challenge] CODE SHOULD NEVER GET HERE!")
def handler(signum, frame): global force_quit if not force_quit: Logger.data(-1, -1, -1, "force_quit") Logger.error("The program was force quit unexpectedly") force_quit = True