Untitled
unknown
plain_text
9 days ago
5.0 kB
4
Indexable
import os
import sqlite3
from game_data import locations
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DB_PATH = os.path.join(BASE_DIR, "game.db")
def run_query(sql, params=(), fetch=False):
conn = sqlite3.connect(DB_PATH)
cur = conn.cursor()
cur.execute(sql, params)
result = None
if fetch:
result = cur.fetchall()
else:
result = None
conn.commit()
conn.close()
return result
def init_db():
run_query('''
CREATE TABLE IF NOT EXISTS players (
player_id TEXT PRIMARY KEY,
current_hp INTEGER,
max_hp INTEGER,
damage INTEGER,
current_location_id INTEGER,
passed_locations TEXT,
current_boss_hp INTEGER
)
''')
run_query('''
CREATE TABLE IF NOT EXISTS locations (
location_id INTEGER PRIMARY KEY AUTOINCREMENT,
location_name TEXT UNIQUE,
boss_name TEXT,
boss_hp INTEGER,
boss_dmg INTEGER,
hp_bonus INTEGER,
dmg_bonus INTEGER
)
''')
for loc in locations:
run_query('''
INSERT OR IGNORE INTO locations
(location_name, boss_name, boss_hp, boss_dmg, hp_bonus, dmg_bonus)
VALUES (?, ?, ?, ?, ?, ?)
''', (loc['name'], loc['boss_name'], loc['boss_hp'], loc['boss_dmg'], loc['hp_bonus'], loc['dmg_bonus']))
def save_player(player_id, hp, max_hp, damage, location_id, passed, boss_hp):
run_query('''INSERT OR REPLACE
INTO players (player_id, current_hp, max_hp, damage, current_location_id, passed_locations, current_boss_hp)
VALUES (?, ?, ?, ?, ?, ?, ?)''',
(player_id, hp, max_hp, damage, location_id, passed, boss_hp))
def load_player(player_id):
rows = run_query('''
SELECT current_hp, max_hp, damage, current_location_id, passed_locations, current_boss_hp
FROM players
WHERE player_id = ?
''', (player_id,), fetch=True)
if not rows:
return None
row = rows[0]
return {
'current_hp': row[0],
'max_hp': row[1],
'damage': row[2],
'current_loc_id': row[3],
'passed_locs': row[4] or '',
'current_boss_hp': row[5]
}
def update_location(player_id, loc_id):
run_query('UPDATE players SET current_location_id = ? WHERE player_id = ?', (loc_id, player_id))
def load_locations(loc_id=None, loc_name=None):
if loc_id is not None:
rows = run_query("SELECT * FROM locations WHERE location_id = ?", (loc_id,), fetch=True)
elif loc_name:
rows = run_query("SELECT * FROM locations WHERE location_name LIKE ?", (f"%{loc_name}%",), fetch=True)
else:
rows = run_query("SELECT * FROM locations", fetch=True)
keys = ['id', 'name', 'boss_name', 'boss_hp', 'boss_dmg', 'hp_bonus', 'dmg_bonus']
return [dict(zip(keys, row)) for row in rows]
def update_hp(player_id, player_hp, boss_hp):
run_query('UPDATE players SET current_hp = ?, current_boss_hp = ? WHERE player_id = ?', (player_hp, boss_hp, player_id))
def pass_location(player_id, loc_id):
rows = run_query("SELECT passed_locations FROM players WHERE player_id = ?", (player_id,), fetch=True)
passed = rows[0][0] if rows and rows[0][0] else ""
passed_list = passed.split(",") if passed else []
if str(loc_id) not in passed_list:
new_passed = passed + ("," if passed else "") + str(loc_id)
run_query('UPDATE players SET passed_locations = ? WHERE player_id = ?', (new_passed, player_id))
def add_bonus_and_restore(player_id, hp_bonus, dmg_bonus):
run_query("""
UPDATE players SET
max_hp = max_hp + ?,
damage = damage + ?,
current_hp = max_hp + ?,
current_boss_hp = 0
WHERE player_id = ?
""", (hp_bonus, dmg_bonus, hp_bonus, player_id))
def check_win(passed_locs):
rows = run_query("SELECT COUNT(*) FROM locations", fetch=True)
total = rows[0][0]
passed_list = passed_locs.split(",") if passed_locs else []
return len(passed_list) >= total
def restore_hp(player_id):
run_query("UPDATE players SET current_hp = max_hp WHERE player_id = ?", (player_id,))
if __name__ == "__main__":
TEST_DB = os.path.join(BASE_DIR, "test_game.db")
if os.path.exists(TEST_DB):
os.remove(TEST_DB)
import database
database.DB_PATH = TEST_DB
init_db()
save_player("test", hp=100, max_hp=100, damage=20, location_id=1, passed="1, 2, 3", boss_hp=0)
print(load_player("test"))
print(load_locations())
print(load_locations(loc_id=3))
print(load_locations(loc_name="Лес"))
update_location("test", 3)
update_hp("test", player_hp=75, boss_hp=120)
Editor is loading...
Leave a Comment