def cache_item_stats(self, session=None): if session is None: session = self.session() api = RLGarageAPI() items = api.get_item_list(0, 10000, override=True)['items'] category_map = { 'car': 1, 'wheels': 2, 'boost': 3, 'topper': 4, 'antenna': 5, 'skin': 6, 'trail': 9, 'goal_explosion': 10, 'banner': 11, 'engine_audio': 12, } ItemStatsWrapper.create_unpainted_stats(session=session, override=True) ItemStatsWrapper.create_unpainted_stats(session=session, counts=True, override=True) for value in category_map.values(): ItemStatsWrapper.create_unpainted_stats(value, session=session, override=True) for key in category_map: ItemStatsWrapper.get_most_used_by_column(key, session=session, override=True) for item in items: id_ = item['ingameid'] if id_ is None: continue print("Item", item) try: ItemStatsWrapper.get_item_usage_over_time(id_, session=session, override=True) except: print("Error")
def create_from_item_id(id_: int, paint_id: int, api: RLGarageAPI): if id_ == 0: return LoadoutItem("None", "", 0, 0) try: item = api.get_item_info(id_, paint_id) except Exception as e: print("Error with loadout", e) return LoadoutItem("Unknown", "", 0, 0) return LoadoutItem(item['name'], item['image'], paint_id, item['rarity'])
def get_car(index: int) -> str: try: return RLGarageAPI().get_item_info(index)['name'] except KeyError: logger.warning(f"Could not find car: {index}.") return "Unknown" except: logger.warning(f"Error getting car for index {index}") return "Unknown"
def get_item_usage_over_time(id_, session, override=False): result = ItemStatsWrapper.get_redis_result_if_exists( "items_get_item_usage_over_time_", id_) if not override and result is not None: return result category_map = { 1: Loadout.car, 2: Loadout.wheels, 3: Loadout.boost, 4: Loadout.topper, 5: Loadout.antenna, 6: Loadout.skin, 9: Loadout.trail, 10: Loadout.goal_explosion, 11: Loadout.banner, 12: Loadout.engine_audio } category = RLGarageAPI().get_item(id_)['category'] loadout_item = category_map[category] date = func.date(Game.match_date) inner = session.query(date.label('date'), Loadout.player, loadout_item) \ .join(Game, Loadout.game == Game.hash) \ .distinct(Loadout.player, date) \ .filter(loadout_item == id_) \ .filter(Loadout.player != Game.primary_player) \ .filter(date > datetime.date(2019, 9, 13)) \ .filter(date < datetime.date.fromtimestamp(datetime.datetime.utcnow().timestamp())) \ .group_by(date, Loadout.player, loadout_item) \ .subquery() inner2 = session.query(date.label('date'), Loadout.player) \ .join(Game, Loadout.game == Game.hash) \ .filter(Loadout.player != Game.primary_player) \ .filter(date > datetime.date(2019, 9, 13)) \ .filter(date < datetime.date.fromtimestamp(datetime.datetime.utcnow().timestamp())) \ .distinct(Loadout.player, date) \ .group_by(date, Loadout.player) \ .subquery() stats = session.query(inner.c.date, func.count(inner.c.player).label('count')) \ .group_by(inner.c.date).subquery() stats2 = session.query(inner2.c.date, func.count(inner2.c.player).label('count')) \ .group_by(inner2.c.date).subquery() final = session.query(stats.c.date, stats.c.count, stats2.c.count) \ .join(stats2, stats.c.date == stats2.c.date).order_by(stats.c.date) data = { 'data': [{ 'date': r[0], 'count': r[1], 'total': r[2] } for r in final.all()] } ItemStatsWrapper.set_redis_result_if_exists( "items_get_item_usage_over_time_", id_, data) return data
def create_from_item_id(id_: int, paint_id: int, api: RLGarageAPI): if id_ == 0: return LoadoutItem("None", "", 0, 0) try: item = api.get_item(id_, paint_id) except Exception as e: logger.debug("Error with loadout: %s", str(e)) return LoadoutItem("Unknown", "", 0, 0) if item is None: return LoadoutItem("Unknown", "", 0, 0) return LoadoutItem(item['name'], item['image'], paint_id, item['rarity'])
def create_item_list(query_params): api = RLGarageAPI() result = ItemStatsWrapper.get_redis_result_if_exists( "api_get_items_list_", query_params['category']) if result is not None: return result order = ItemStatsWrapper.create_unpainted_stats( query_params['category'], counts=True) result = api.get_item_list_by_category( query_params['category'], query_params['page'], query_params['limit'], order=[o['item_id'] for o in order]) result['items'] = [{ 'count': order[i]['count'], **item } for i, item in enumerate(result['items'])] ItemStatsWrapper.set_redis_result_if_exists("api_get_items_list_", query_params['category'], result, ex=60 * 60 * 12) return result
def create_from_player_game(player_game: PlayerGame): api = RLGarageAPI() item_list = [ 'antenna', 'banner', 'boost', 'car', 'engine_audio', 'goal_explosion', 'skin', 'topper', 'trail', 'wheels' ] no_paint = ["engine_audio"] if player_game is not None and len(player_game.loadout_object) > 0: loadout = player_game.loadout_object[0] item_info = { item: LoadoutItem.create_from_item_id( getattr(loadout, item), getattr(loadout, item + "_paint") if item not in no_paint else 0, api) for item in item_list } else: item_info = { item: LoadoutItem.create_from_item_id(0, 0, api) for item in item_list } return Loadout(**item_info)
def api_get_item(query_params=None): api = RLGarageAPI() return better_jsonify(api.get_item(query_params['id']))
def api_get_items_list(query_params=None): if 'category' in query_params: return better_jsonify(ItemStatsWrapper.create_item_list(query_params)) api = RLGarageAPI() return better_jsonify( api.get_item_list(query_params['page'], query_params['limit']))
def cache_items(self): api = RLGarageAPI() api.cache_items()