-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_gold.py
61 lines (46 loc) · 1.78 KB
/
add_gold.py
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
import sys
import re
from datetime import datetime
import os
import utils
if len(sys.argv) != 4:
print("Usage: add_gold.py <gold_count> <description> <vault_path>")
sys.exit(1)
gold_count = int(sys.argv[1])
description = sys.argv[2]
vault_path = sys.argv[3]
config = utils.get_config_from_db(vault_path)
folder = config.get("folder", "RLRPG")
gold_prefix = config.get("gold_prefix")
main_file_name = config.get("main_file_name")
transaction_file_name = config.get("transaction_file_name")
if not (gold_prefix or main_file_name or transaction_file_name):
print("Make sure database has <gold_prefix>, <main_file_name>, <transaction_file_name>")
sys.exit(1)
main_file = os.path.join(vault_path, folder, main_file_name)
transaction_file = os.path.join(vault_path, folder, transaction_file_name)
def get_gold():
"""Extracts the current gold count from the main file."""
with open(main_file, "r") as f:
content = f.read()
match = re.search(rf"{re.escape(gold_prefix)}\s*`(-?\d+)`", content)
return int(match.group(1)) if match else 0
def update_gold():
"""Updates the gold count in the main file and logs the transaction."""
current_gold = get_gold()
new_gold = current_gold + gold_count
with open(main_file, "r") as f:
content = f.read()
updated_content = re.sub(
rf"({re.escape(gold_prefix)}\s*)`-?\d+`",
rf"\1`{new_gold}`",
content
)
with open(main_file, "w") as f:
f.write(updated_content)
date_today = datetime.today().strftime('%Y-%m-%d')
transaction_entry = f"{date_today}: `{'+' if gold_count >= 0 else ''}{gold_count}` ({description})\n"
with open(transaction_file, "a") as f:
f.write(transaction_entry)
print(f"Updated gold: {current_gold} → {new_gold}")
update_gold()