-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecalculate.py
93 lines (79 loc) · 3.01 KB
/
recalculate.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import uuid
import db
import asyncio
from scb import SailCreditBureau
from party import Party, PartyMember, STARTING_SSC
scb = SailCreditBureau()
async def calculate():
"""
This script reset the SSC for all users to their default values, and then
recalculates the SSC for all users.
"""
# Save all the logs into memory.
print("Fetching logs...")
existing_logs = await db.get_sail_credit_logs()
# Reset all user to their starting SSC.
print("Resetting all users to default SSC...")
old_user_ssc = {}
for user in await db.get_all_users():
old_user_ssc[user["discord_id"]] = user["sail_credit"]
await db.set_user(user["discord_id"], STARTING_SSC)
# Wipe the logs table.
print("Wiping the logs table...")
await db.clear_sail_credit_logs()
# Recalculate the SSC for all users.
print("Recalculating SSC for all users...")
print(f"Total logs: {len(existing_logs)}")
for log in existing_logs:
# If this is an admin change, don't recalculate using the algorithm.
# Just find the delta and apply.
if log["source"] == "ADMIN":
user = await db.get_user(log["discord_id"])
old_ssc = user["sail_credit"]
delta = log["new_sail_credit"] - log["prev_sail_credit"]
new_ssc = max(0, old_ssc + delta)
await db.change_and_log_sail_credit(
discord_id=log["discord_id"],
party_size=log["party_size"],
party_created_at=log["party_created_at"],
party_finished_at=log["party_finished_at"],
new_ssc=new_ssc,
old_ssc=old_ssc,
source=log["source"],
timestamp=log["timestamp"],
)
continue
party = Party(
uuid=uuid.uuid4(),
role=0,
owner_id=0,
name="",
created_at=log["party_created_at"],
finished_at=log["party_finished_at"],
max_size=log["party_size"],
members=[PartyMember(0, "", 0) for _ in range(log["party_size"])],
)
if log["new_sail_credit"] - log["prev_sail_credit"] < 0:
await scb.process_flaked_user(
party, log["discord_id"], timestamp=log["timestamp"]
)
else:
await scb.process_party_member(
party, log["discord_id"], timestamp=log["timestamp"]
)
print("Non-zero SSC deltas after recalculation:")
new_user_ssc = {
user["discord_id"]: user["sail_credit"] for user in await db.get_all_users()
}
for user_id in new_user_ssc:
old_ssc = old_user_ssc.get(user_id, 1000)
delta = new_user_ssc[user_id] - old_ssc
if delta != 0:
print(
f"[user-id={user_id}] {old_ssc} SSC -> {new_user_ssc[user_id]} SSC ({'+' if delta > 0 else ''}{delta})"
)
print("Done!")
if __name__ == "__main__":
asyncio.run(db.init())
asyncio.run(calculate())
asyncio.run(db.cleanup())