def write(self, queue): with open(self.queuefile, 'w') as f: for order in queue or []: recipe = Recipe.from_json(order) f.write(str(recipe)) with open(self.jsonqueuefile, 'w') as f: f.write(json.dumps(queue or []))
def RandomDrink(target_weight, drink_name='Random drink'): """Returns a Recipe.""" FILTERED_INGREDIENTS = {} for i in INGREDIENTS_ORDERED: if i not in INGREDIENTS: print "No random config for", i else: FILTERED_INGREDIENTS[i] = INGREDIENTS[i] recipe = None total_weight = None attempts = 0 while total_weight != target_weight: if not recipe or attempts > 100: recipe = Recipe(name=drink_name, ingredients=[]) total_weight = [0, 0, 0, 0] attempts = 0 ingredient = random.choice(FILTERED_INGREDIENTS.keys()) min_gap = 1000 for target, (x, y) in zip(target_weight, zip(INGREDIENTS[ingredient], total_weight)): if x > 0: gap = (target - y) * 1.0 / x min_gap = min(gap, min_gap) if min_gap > 0: if min_gap > 1 and random.random() < 0.3: min_gap /= 2.0 recipe.ingredients.append(Ingredient(Oz(min_gap), ingredient)) total_weight = [ x + y * min_gap for x, y in zip(total_weight, INGREDIENTS[ingredient]) ] attempts += 1 return recipe
def recipe_from_json_object(recipe_obj): """Takes a dict decoded from a JSON recipe and returns a Recipe object.""" recipe = Recipe.from_json(recipe_obj) if not recipe.ingredients: if recipe_obj['drink_name'] == "Random Sour": recipe = RandomSourDrink() recipe.user_name = recipe_obj['user_name'] elif recipe_obj['drink_name'] == "Random Boozy": recipe = RandomSpirituousDrink() recipe.user_name = recipe_obj['user_name'] elif recipe_obj['drink_name'] == "Random Bubbly Boozy": recipe = RandomBubblySpirituousDrink() recipe.user_name = recipe_obj['user_name'] elif recipe_obj['drink_name'] == "Random Bubbly Sour": recipe = RandomBubblySourDrink() recipe.user_name = recipe_obj['user_name'] recipe = water_down_recipe(recipe) return recipe
def run(self): last_drink_id = None while True: try: queue = json.loads(self.get('next_drink')) if not self.controller and queue: json_recipe = queue[0] drink_id = json_recipe['id'] if drink_id == last_drink_id: print "Refusing to remake order %s" % last_drink_id time.sleep(20) # Sleep extra long continue # Don't make the same drink twice last_drink_id = drink_id next_recipe = water_down_recipe(Recipe.from_json(json_recipe)) raw_actions = actions_for_recipe(next_recipe) actions = [] for i, action in enumerate(raw_actions): progress = 10 + 90 * i / len(raw_actions) actions.append(UpdateProgressAction(self, drink_id, progress)) actions.append(action) actions.append(FinishDrinkAction(self, drink_id)) self.controller.EnqueueGroup(actions) else: actions = self.controller.InspectQueue() if actions: logging.info("Current action: %s", actions[0]) if (isinstance(actions[0], (WaitForGlassRemoval, WaitForGlassPlaced)) and isinstance(self.controller.robot, FakeRobot)): logging.info("Placing glass") actions[0].force = True self.write(queue) except urllib2.URLError, e: logging.warning("URLError: %s", e) except ValueError, e: print e