def get_stat_data(char_id: int, character_field: str, item_field: str): user_id = session["user_id"] if not check_if_user_has_character(user_id, char_id): if is_admin(): user_id = get_user_id_from_char_id(char_id) if user_id < 0: return jsonify(current_value="Err") query_result = select_query.select_char_fields(user_id, char_id, (character_field, )) if query_result is None: return jsonify(current_value=0, base=0) base_stat = query_result[character_field] characters = select_query.select_character_data(char_id) item_id_list = data_helper.get_character_items_id(characters) stat_additional = 0 for i in item_id_list: item = select_query.select_item_fields(i, (item_field, )) if item is not None: stat_additional += item[item_field] return jsonify(additional=stat_additional, base=base_stat)
def check_if_user_has_character(user_id, char_id): has_char = select_query.select_char_fields(user_id, char_id, ("Character_ID", )) if has_char is not None: return True return False
def get_currency(char_id): field_name = "Character_Currency" user_id = session["user_id"] if not check_if_user_has_character(user_id, char_id): if is_admin(): user_id = get_user_id_from_char_id(char_id) if user_id < 0: return jsonify(current_value="Err") query_result = select_query.select_char_fields(user_id, char_id, (field_name, )) if query_result is None: return jsonify(current_value=0) return jsonify(current_value=query_result[field_name])
def get_items_in_slot(char_id, item_slot): user_id = session["user_id"] if not check_if_user_has_character(user_id, char_id): if is_admin(): user_id = get_user_id_from_char_id(char_id) if user_id < 0: return jsonify(current_value="Err") characters = select_query.select_char_fields(user_id, char_id) if characters is None: return 'NULL' item_id_list = data_helper.get_character_items_id(characters) select_fields = ("Items.Item_ID", "Items.Item_Weight", "Items.Item_Name", "Rarities.Rarities_Color", "Slots.Slots_ID", "Inventory.Amount") where_clause = "WHERE Inventory.Character_ID = ? AND Slots.Slots_Name = ?" joins = ("INNER JOIN Items on Inventory.Item_ID=Items.Item_ID", "INNER JOIN Rarities on Rarities.Rarities_ID=Items.Rarity_ID", "INNER JOIN Slots on Items.Item_Slot=Slots.Slots_ID") query_result = select_query.select(select_fields, "Inventory", True, where_clause, (char_id, item_slot), joins) items = [] for q in query_result: item_fields = { 'Item_ID': q['Item_ID'], 'Item_Weight': q['Item_Weight'], 'Item_Name': escape(q['Item_Name']), 'Rarities_Color': q['Rarities_Color'], 'Amount': q['Amount'], 'Slots_ID': int(q['Slots_ID']), 'Is_Equiped': True if q['Item_ID'] in item_id_list else False } items.append(item_fields) return jsonify(items)
def getItemAmount(char_id, item_id): user_id = session["user_id"] if not check_if_user_has_character(user_id, char_id): if is_admin(): user_id = get_user_id_from_char_id(char_id) if user_id < 0: return jsonify(current_value="Err") characters = select_query.select_char_fields(user_id, char_id) if characters is None: return redirect(url_for('character.character_select')) select_fields = ("Inventory.Item_ID", "Amount", "Items.Item_Slot", "Slots.Slots_Name") joins = ("LEFT JOIN Items ON Inventory.Item_ID = Items.Item_ID", "LEFT JOIN Slots ON Items.Item_Slot = Slots.Slots_ID") where_clause = "WHERE Character_ID = ? AND Inventory.Item_ID = ?" query_result = select_query.select(select_fields, "Inventory", False, where_clause, (char_id, item_id), joins) return jsonify(current_value=query_result['Amount'], item_id=query_result['Item_ID'], slot_name=query_result['Slots_Name'])
def get_current_equiped_items(char_id, item_slot): user_id = session["user_id"] if not check_if_user_has_character(user_id, char_id): if is_admin(): user_id = get_user_id_from_char_id(char_id) if user_id < 0: return jsonify(current_value="Err") characters = select_query.select_char_fields(user_id, char_id) if characters is None: return redirect(url_for('character.character_select')) slot_name = select_query.select_slot_names(item_slot)["Slots_Name"] multiple = {'Weapon': 4, 'Ring': 2, 'Item': 2, 'Trinket': 2} r = 1 slots = [] if slot_name in multiple.keys(): r = int(multiple[slot_name]) for i in range(r): field_name = "Character_" + slot_name + str(i + 1) slots.append(field_name) else: slots.append("Character_" + slot_name) item_ids = select_query.select_char_fields(user_id, char_id, tuple(slots)) field_names = ['Item_ID', 'Item_Name', 'Item_Picture', 'Rarities_Color'] item_list = [] item_defaults = { 'Ring': { 'item_name': 'Ring', 'picture': 'Ring.png' }, 'Item': { 'item_name': 'Item', 'picture': 'Item.png' }, 'Trinket': { 'item_name': 'Trinket', 'picture': 'Trinket.png' } } for i in range(r): temp_dict = {} temp_dict['Item_ID'] = -1 if slot_name == 'Weapon': if i % 2 == 1: temp_dict['Item_Name'] = 'Off Hand ' temp_dict['Item_Picture'] = 'Off_Hand.png' else: temp_dict['Item_Name'] = 'Main Hand ' temp_dict['Item_Picture'] = 'Main_Hand.png' temp_dict['Item_Name'] += str(math.ceil((i + 1) / 2)) else: temp_dict['Item_Name'] = item_defaults[slot_name]['item_name'] temp_dict['Item_Picture'] = item_defaults[slot_name]['picture'] temp_dict['Rarities_Color'] = '#606060' item_list.append(temp_dict) count = 0 for i in item_ids: join_str = "INNER JOIN Rarities ON Rarities.Rarities_ID=Items.Rarity_ID" item_data_result = select_query.select(tuple(field_names), "Items", False, "WHERE Item_ID=?", (i, ), (join_str, )) if item_data_result is None: count += 1 continue temp_data = {} for name in field_names: temp_data[name] = item_data_result[name] item_list[count] = temp_data count += 1 slot_name = select_query.select_slot_names(item_slot)['Slots_Name'] output = {'slot_name': slot_name, 'items': item_list, 'num_of_slots': r} return jsonify(output)