def set_single_train(self): from yata.gyms import gyms self.single_train = gyms.get( self.gym_id, {"energy": 0})["energy"] == self.energy_used self.save()
def gymImport(request): if request.method == 'POST': try: from yata.gyms import gyms body = json.loads(request.body) if "api" not in body: return JsonResponse({ "message": "api not in payload", "type": -1 }) if "payload" not in body: return JsonResponse({ "message": "api not in payload", "type": -1 }) api = body.get("api") for payload in body.get("payload"): # get direct values train = dict({}) train["req"] = json.dumps(api) train["timestamp"] = tsnow() train["id_key"] = api.get("id_key", "x") train["time_diff"] = api.get("time_diff", 0) train["happy_before"] = payload.get("happy_before", 0) train["happy_after"] = payload.get("happy_after", 0) train["happy_delta"] = train["happy_after"] - train[ "happy_before"] train["energy_used"] = payload.get("energy_used", 0) train["stat_type"] = payload.get("stat_type", "None") train["stat_after"] = float(payload.get("stat_after", 0)) train["stat_delta"] = float(payload.get("stat_gain", 0)) train[ "stat_before"] = train["stat_after"] - train["stat_delta"] train["gym_id"] = payload.get("gym_id", 0) train["gym_dot"] = round( gyms.get(train["gym_id"], dict({})).get(train["stat_type"], 0)) # faction perk for p in api.get("faction_perks", []): reg = '\+ increases {stat} gym gains by \d{{1,3}}\%'.format( stat=train["stat_type"]) if re.match(reg, p.lower()) is not None: bonus = p.replace("%", "").replace( "+", "").strip().split(" ")[-1] bonus = int(bonus) if bonus.isdigit() else -1 train["perks_faction"] = bonus continue # education perks for p in api.get("education_perks", []): # specific gym reg = '\+ \d{{1,3}}\% {stat} gym gains'.format( stat=train["stat_type"]) if re.match(reg, p.lower()) is not None: bonus = p.replace("%", "").replace("+", "").strip().split(" ")[0] bonus = int(bonus) if bonus.isdigit() else -1 train["perks_education_stat"] = bonus continue # all gyms # reg = '\+ \d{1,3}\% gym gains' # if re.match(reg, p.lower()) is not None: if p == "+ 1% Gym gains": bonus = p.replace("%", "").replace("+", "").strip().split(" ")[0] bonus = int(bonus) if bonus.isdigit() else -1 train["perks_education_all"] = bonus continue # property perks for p in api.get("property_perks", []): # specific gym reg = '\+ \d{{1,3}}\% gym gains'.format( stat=train["stat_type"]) if re.match(reg, p.lower()) is not None: bonus = p.replace("%", "").replace("+", "").strip().split(" ")[0] bonus = int(bonus) if bonus.isdigit() else -1 train["perks_property"] = bonus continue # book perks for p in api.get("book_perks", []): # "book_perks": ["+ Increases Speed gym gains by 30% for 31 days"] # specific gym reg = '\+ increases {stat} gym gains by \d{{1,3}}\% for 31 days'.format( stat=train["stat_type"]) if re.match(reg, p.lower()) is not None: bonus = p.replace("%", "").replace("+", "").strip().split(" ")[5] bonus = int(bonus) if bonus.isdigit() else -1 train["perks_gym_book"] = bonus continue # company perks for p in api.get("company_perks", []): # all gym reg = '\+ \d{{1,3}}\% gym gains'.format( stat=train["stat_type"]) if re.match(reg, p.lower()) is not None: bonus = p.replace("%", "").replace("+", "").strip().split(" ")[0] bonus = int(bonus) if bonus.isdigit() else -1 train["perks_company"] = bonus continue # specific gym reg = '\+ \d{{1,3}}\% {stat} gym gains'.format( stat=train["stat_type"]) if re.match(reg, p.lower()) is not None: bonus = p.replace("%", "").replace("+", "").strip().split(" ")[0] bonus = int(bonus) if bonus.isdigit() else -1 train["perks_company"] = bonus continue # happiness reg = '\+ \d{{1,3}}\% reduction of happiness loss in gym'.format( stat=train["stat_type"]) if re.match(reg, p.lower()) is not None: bonus = p.replace("%", "").replace("+", "").strip().split(" ")[0] bonus = int(bonus) if bonus.isdigit() else -1 train["company_perks_happy_red"] = bonus # get single train train["single_train"] = train["energy_used"] == gyms.get( train["gym_id"], {"energy": 0})["energy"] traindb = TrainFull.objects.create(**train) traindb.set_error() return JsonResponse({"message": "All good dude", "type": 1}) except BaseException as e: print(e) return JsonResponse({ "message": "Server error... YATA's been poorly coded: {}".format(e), "type": -1 }) else: return returnError( type=403, msg="You need to post. Don\'t try to be a smart ass.")