def getMeal(uid, price, nick): user = api.getUser(uid) nearby = api.search(uid, nick) price = int(10000 * float(price) / (100 + float(user["tip"]))) / 100.0 / 1.0875 # return json.dumps(nearby) choices_outer = [] for rest in nearby: if rest["mino"] <= price and rest["is_delivering"] == 1: rest_data = api.getDetails(rest["id"]) choices = [] addons = [] if api.cuisineCheck(rest_data["cuisine"], uid): for category in rest_data["menu"]: for listing in category["children"]: if float(listing["price"]) <= price: listing["rest"] = rest["id"] foodItem = meal(listing) if foodItem.price < 4.85 or foodItem.price < 0.6 * price or not api.valid( listing): base = addons else: base = choices if "children" in listing: for option in listing["children"]: if api.valid(option): title = option["name"] flex = api.checkFlexibility(title) if "children" in option: for op in option["children"]: optionItem = meal(op) foodItem.addOption( title, optionItem, flex) base.append(foodItem) choices_outer.append([choices, addons]) meals = [] spent = 0 score = 0 while score < random.random(): rest_index = random.randrange(len(choices_outer)) rest_pick = choices_outer[rest_index] if len(rest_pick[0]) != 0: meal_index = random.randrange(len(rest_pick[0])) meal_pick = rest_pick[0][meal_index] final_meal, total_price = meal_pick.pickOptions(price) if total_price <= price - spent: meals.append(final_meal.toDict()) spent = spent + total_price elif spent < total_price: meals = final_meal.toDict() spent = total_price score = (float(spent) / price)**2 + score else: score = score - 0.01 # print "swap" return json.dumps({"core": meals, "total": spent})
def getMeal(uid, price, nick): user = api.getUser(uid) nearby = api.search(uid, nick) price = int(10000 * float(price) / (100 + float(user["tip"]))) / 100.0 / 1.0875 # return json.dumps(nearby) choices_outer = [] for rest in nearby: if rest["mino"] <= price and rest["is_delivering"] == 1: rest_data = api.getDetails(rest["id"]) choices = [] addons = [] if api.cuisineCheck(rest_data["cuisine"], uid): for category in rest_data["menu"]: for listing in category["children"]: if float(listing["price"]) <= price: listing["rest"] = rest["id"] foodItem = meal(listing) if foodItem.price < 4.85 or foodItem.price < 0.6 * price or not api.valid(listing): base = addons else: base = choices if "children" in listing: for option in listing["children"]: if api.valid(option): title = option["name"] flex = api.checkFlexibility(title) if "children" in option: for op in option["children"]: optionItem = meal(op) foodItem.addOption(title, optionItem, flex) base.append(foodItem) choices_outer.append([choices, addons]) meals = [] spent = 0 score = 0 while score < random.random(): rest_index = random.randrange(len(choices_outer)) rest_pick = choices_outer[rest_index] if len(rest_pick[0]) != 0: meal_index = random.randrange(len(rest_pick[0])) meal_pick = rest_pick[0][meal_index] final_meal, total_price = meal_pick.pickOptions(price) if total_price <= price - spent: meals.append(final_meal.toDict()) spent = spent + total_price elif spent < total_price: meals = final_meal.toDict() spent = total_price score = (float(spent) / price) ** 2 + score else: score = score - 0.01 # print "swap" return json.dumps({"core": meals, "total": spent})
def toDict(self): res = {"id": self.id, "name": self.name, "price": self.price, "desc": self.desc} if self.rest != -1: rest = api.getDetails(self.rest) res["rest"] = {"id": self.rest, "name": rest["name"], "phone": rest["cs_contact_phone"]} if len(self.options) > 0: res["options"] = {} for key in self.options: option = self.options[key] if key not in res["options"]: res["options"][key] = [] for item in option["data"]: res["options"][key].append(item.toDict()) return res
def toDict(self): res = { "id": self.id, "name": self.name, "price": self.price, "desc": self.desc, } if self.rest != -1: rest = api.getDetails(self.rest) res["rest"] = { "id": self.rest, "name": rest["name"], "phone": rest["cs_contact_phone"] } if len(self.options) > 0: res["options"] = {} for key in self.options: option = self.options[key] if key not in res["options"]: res["options"][key] = [] for item in option["data"]: res["options"][key].append(item.toDict()) return res