-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
61 lines (54 loc) · 1.97 KB
/
Copy pathdatabase.py
File metadata and controls
61 lines (54 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# ==============================================================================
# Developed By Ben Timothy
# NTrade Simulator - Professional Retro-Cyber Financial Dashboard & Game Engine
# ==============================================================================
import sqlite3
import json
import os
DB_FILE = "ntrade_simulator.db"
def init_db():
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS saves (
slot_id INTEGER PRIMARY KEY,
slot_name TEXT NOT NULL,
state_json TEXT NOT NULL,
is_hardcore INTEGER DEFAULT 0,
is_dead INTEGER DEFAULT 0
)
""")
conn.commit()
conn.close()
def save_game(slot_id, slot_name, state_dict, is_hardcore=0, is_dead=0):
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
state_json = json.dumps(state_dict)
cursor.execute("""
INSERT OR REPLACE INTO saves (slot_id, slot_name, state_json, is_hardcore, is_dead)
VALUES (?, ?, ?, ?, ?)
""", (slot_id, slot_name, state_json, int(is_hardcore), int(is_dead)))
conn.commit()
conn.close()
def load_game(slot_id):
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute("SELECT state_json, is_hardcore, is_dead FROM saves WHERE slot_id = ?", (slot_id,))
row = cursor.fetchone()
conn.close()
if row:
return json.loads(row[0]), bool(row[1]), bool(row[2])
return None, False, False
def delete_save_file(slot_id):
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute("DELETE FROM saves WHERE slot_id = ?", (slot_id,))
conn.commit()
conn.close()
def get_slots():
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute("SELECT slot_id, slot_name, is_hardcore, is_dead FROM saves ORDER BY slot_id ASC")
rows = cursor.fetchall()
conn.close()
return rows