move to async database

This commit is contained in:
Hay1tsme
2024-01-09 14:42:17 -05:00
parent edd3ce8ead
commit 05586df08a
77 changed files with 1925 additions and 1948 deletions

View File

@@ -35,7 +35,7 @@ class CxbBase:
return {"data": []}
async def handle_auth_usercheck_request(self, data: Dict) -> Dict:
profile = self.data.profile.get_profile_index(
profile = await self.data.profile.get_profile_index(
0, data["usercheck"]["authid"], self.version
)
if profile is not None:
@@ -50,7 +50,7 @@ class CxbBase:
return {"token": data["entry"]["authid"], "uid": data["entry"]["authid"]}
async def handle_auth_login_request(self, data: Dict) -> Dict:
profile = self.data.profile.get_profile_index(
profile = await self.data.profile.get_profile_index(
0, data["login"]["authid"], self.version
)
@@ -204,8 +204,8 @@ class CxbBase:
uid = data["loadrange"]["uid"]
self.logger.info(f"Load data for {uid}")
profile = self.data.profile.get_profile(uid, self.version)
songs = self.data.score.get_best_scores(uid)
profile = await self.data.profile.get_profile(uid, self.version)
songs = await self.data.score.get_best_scores(uid)
data1 = []
index = []
@@ -271,7 +271,7 @@ class CxbBase:
thread_ScoreData = Thread(target=CxbBase.task_generateScoreData(song, index, data1))
thread_ScoreData.start()
v_profile = self.data.profile.get_profile_index(0, uid, self.version)
v_profile = await self.data.profile.get_profile_index(0, uid, self.version)
v_profile_data = v_profile["data"]
for _, data in enumerate(profile):
@@ -300,11 +300,11 @@ class CxbBase:
for value in data["saveindex"]["data"]:
if "playedUserId" in value[1]:
self.data.profile.put_profile(
await self.data.profile.put_profile(
data["saveindex"]["uid"], self.version, value[0], value[1]
)
if "mcode" not in value[1]:
self.data.profile.put_profile(
await self.data.profile.put_profile(
data["saveindex"]["uid"], self.version, value[0], value[1]
)
if "shopId" in value:
@@ -335,7 +335,7 @@ class CxbBase:
"index": value[0],
}
)
self.data.score.put_best_score(
await self.data.score.put_best_score(
data["saveindex"]["uid"],
song_json["mcode"],
self.version,
@@ -360,32 +360,32 @@ class CxbBase:
for index, value in enumerate(data["saveindex"]["data"]):
if int(data["saveindex"]["index"][index]) == 101:
self.data.profile.put_profile(
await self.data.profile.put_profile(
aimeId, self.version, data["saveindex"]["index"][index], value
)
if (
int(data["saveindex"]["index"][index]) >= 700000
and int(data["saveindex"]["index"][index]) <= 701000
):
self.data.profile.put_profile(
await self.data.profile.put_profile(
aimeId, self.version, data["saveindex"]["index"][index], value
)
if (
int(data["saveindex"]["index"][index]) >= 500
and int(data["saveindex"]["index"][index]) <= 510
):
self.data.profile.put_profile(
await self.data.profile.put_profile(
aimeId, self.version, data["saveindex"]["index"][index], value
)
if "playedUserId" in value:
self.data.profile.put_profile(
await self.data.profile.put_profile(
aimeId,
self.version,
data["saveindex"]["index"][index],
json.loads(value),
)
if "mcode" not in value and "normalCR" not in value:
self.data.profile.put_profile(
await self.data.profile.put_profile(
aimeId,
self.version,
data["saveindex"]["index"][index],
@@ -437,7 +437,7 @@ class CxbBase:
}
)
self.data.score.put_best_score(
await self.data.score.put_best_score(
aimeId, data1["mcode"], self.version, indexSongList[i], songCode[0]
)
i += 1
@@ -446,7 +446,7 @@ class CxbBase:
async def handle_action_sprankreq_request(self, data: Dict) -> Dict:
uid = data["sprankreq"]["uid"]
self.logger.info(f"Get best rankings for {uid}")
p = self.data.score.get_best_rankings(uid)
p = await self.data.score.get_best_rankings(uid)
rankList: List[Dict[str, Any]] = []
@@ -492,7 +492,7 @@ class CxbBase:
# REV S2
if "clear" in rid:
try:
self.data.score.put_ranking(
await self.data.score.put_ranking(
user_id=uid,
rev_id=int(rid["rid"]),
song_id=int(rid["sc"][1]),
@@ -500,7 +500,7 @@ class CxbBase:
clear=rid["clear"],
)
except Exception:
self.data.score.put_ranking(
await self.data.score.put_ranking(
user_id=uid,
rev_id=int(rid["rid"]),
song_id=0,
@@ -510,7 +510,7 @@ class CxbBase:
# REV
else:
try:
self.data.score.put_ranking(
await self.data.score.put_ranking(
user_id=uid,
rev_id=int(rid["rid"]),
song_id=int(rid["sc"][1]),
@@ -518,7 +518,7 @@ class CxbBase:
clear=0,
)
except Exception:
self.data.score.put_ranking(
await self.data.score.put_ranking(
user_id=uid,
rev_id=int(rid["rid"]),
song_id=0,
@@ -530,12 +530,12 @@ class CxbBase:
async def handle_action_addenergy_request(self, data: Dict) -> Dict:
uid = data["addenergy"]["uid"]
self.logger.info(f"Add energy to user {uid}")
profile = self.data.profile.get_profile_index(0, uid, self.version)
profile = await self.data.profile.get_profile_index(0, uid, self.version)
data1 = profile["data"]
p = self.data.item.get_energy(uid)
p = await self.data.item.get_energy(uid)
if not p:
self.data.item.put_energy(uid, 5)
await self.data.item.put_energy(uid, 5)
return {
"class": data1["myClass"],
@@ -548,7 +548,7 @@ class CxbBase:
energy = p["energy"]
newenergy = int(energy) + 5
self.data.item.put_energy(uid, newenergy)
await self.data.item.put_energy(uid, newenergy)
if int(energy) <= 995:
array.append(

View File

@@ -1,6 +1,5 @@
from typing import Optional, Dict, List
from os import walk, path
import urllib
from typing import Optional
from os import path
import csv
from read import BaseReader
@@ -8,7 +7,6 @@ from core.config import CoreConfig
from titles.cxb.database import CxbData
from titles.cxb.const import CxbConstants
class CxbReader(BaseReader):
def __init__(
self,
@@ -29,17 +27,14 @@ class CxbReader(BaseReader):
self.logger.error(f"Invalid project cxb version {version}")
exit(1)
def read(self) -> None:
pull_bin_ram = True
async def read(self) -> None:
if path.exists(self.bin_dir):
await self.read_csv(self.bin_dir)
else:
self.logger.warn(f"{self.bin_dir} does not exist, nothing to import")
if not path.exists(f"{self.bin_dir}"):
self.logger.warning(f"Couldn't find csv file in {self.bin_dir}, skipping")
pull_bin_ram = False
if pull_bin_ram:
self.read_csv(f"{self.bin_dir}")
def read_csv(self, bin_dir: str) -> None:
async def read_csv(self, bin_dir: str) -> None:
self.logger.info(f"Read csv from {bin_dir}")
try:
@@ -55,7 +50,7 @@ class CxbReader(BaseReader):
if not "N/A" in row["standard"]:
self.logger.info(f"Added song {song_id} chart 0")
self.data.static.put_music(
await self.data.static.put_music(
self.version,
song_id,
index,
@@ -71,7 +66,7 @@ class CxbReader(BaseReader):
)
if not "N/A" in row["hard"]:
self.logger.info(f"Added song {song_id} chart 1")
self.data.static.put_music(
await self.data.static.put_music(
self.version,
song_id,
index,
@@ -83,7 +78,7 @@ class CxbReader(BaseReader):
)
if not "N/A" in row["master"]:
self.logger.info(f"Added song {song_id} chart 2")
self.data.static.put_music(
await self.data.static.put_music(
self.version,
song_id,
index,
@@ -97,7 +92,7 @@ class CxbReader(BaseReader):
)
if not "N/A" in row["unlimited"]:
self.logger.info(f"Added song {song_id} chart 3")
self.data.static.put_music(
await self.data.static.put_music(
self.version,
song_id,
index,
@@ -113,7 +108,7 @@ class CxbReader(BaseReader):
)
if not "N/A" in row["easy"]:
self.logger.info(f"Added song {song_id} chart 4")
self.data.static.put_music(
await self.data.static.put_music(
self.version,
song_id,
index,

View File

@@ -25,7 +25,7 @@ class CxbRev(CxbBase):
score_data = json.loads(data["putlog"]["data"])
userid = score_data["usid"]
self.data.score.put_playlog(
await self.data.score.put_playlog(
userid,
score_data["mcode"],
score_data["difficulty"],

View File

@@ -19,12 +19,12 @@ energy = Table(
class CxbItemData(BaseData):
def put_energy(self, user_id: int, rev_energy: int) -> Optional[int]:
async def put_energy(self, user_id: int, rev_energy: int) -> Optional[int]:
sql = insert(energy).values(user=user_id, energy=rev_energy)
conflict = sql.on_duplicate_key_update(energy=rev_energy)
result = self.execute(conflict)
result = await self.execute(conflict)
if result is None:
self.logger.error(
f"{__name__} failed to insert item! user: {user_id}, energy: {rev_energy}"
@@ -33,10 +33,10 @@ class CxbItemData(BaseData):
return result.lastrowid
def get_energy(self, user_id: int) -> Optional[Dict]:
async def get_energy(self, user_id: int) -> Optional[Dict]:
sql = energy.select(and_(energy.c.user == user_id))
result = self.execute(sql)
result = await self.execute(sql)
if result is None:
return None
return result.fetchone()

View File

@@ -21,7 +21,7 @@ profile = Table(
class CxbProfileData(BaseData):
def put_profile(
async def put_profile(
self, user_id: int, version: int, index: int, data: JSON
) -> Optional[int]:
sql = insert(profile).values(
@@ -30,7 +30,7 @@ class CxbProfileData(BaseData):
conflict = sql.on_duplicate_key_update(index=index, data=data)
result = self.execute(conflict)
result = await self.execute(conflict)
if result is None:
self.logger.error(
f"{__name__} failed to update! user: {user_id}, index: {index}, data: {data}"
@@ -39,7 +39,7 @@ class CxbProfileData(BaseData):
return result.lastrowid
def get_profile(self, aime_id: int, version: int) -> Optional[List[Dict]]:
async def get_profile(self, aime_id: int, version: int) -> Optional[List[Dict]]:
"""
Given a game version and either a profile or aime id, return the profile
"""
@@ -47,12 +47,12 @@ class CxbProfileData(BaseData):
and_(profile.c.version == version, profile.c.user == aime_id)
)
result = self.execute(sql)
result = await self.execute(sql)
if result is None:
return None
return result.fetchall()
def get_profile_index(
async def get_profile_index(
self, index: int, aime_id: int = None, version: int = None
) -> Optional[Dict]:
"""
@@ -72,7 +72,7 @@ class CxbProfileData(BaseData):
)
return None
result = self.execute(sql)
result = await self.execute(sql)
if result is None:
return None
return result.fetchone()

View File

@@ -58,7 +58,7 @@ ranking = Table(
class CxbScoreData(BaseData):
def put_best_score(
async def put_best_score(
self,
user_id: int,
song_mcode: str,
@@ -79,7 +79,7 @@ class CxbScoreData(BaseData):
conflict = sql.on_duplicate_key_update(data=sql.inserted.data)
result = self.execute(conflict)
result = await self.execute(conflict)
if result is None:
self.logger.error(
f"{__name__} failed to insert best score! profile: {user_id}, song: {song_mcode}, data: {data}"
@@ -88,7 +88,7 @@ class CxbScoreData(BaseData):
return result.lastrowid
def put_playlog(
async def put_playlog(
self,
user_id: int,
song_mcode: str,
@@ -125,7 +125,7 @@ class CxbScoreData(BaseData):
combo=combo,
)
result = self.execute(sql)
result = await self.execute(sql)
if result is None:
self.logger.error(
f"{__name__} failed to insert playlog! profile: {user_id}, song: {song_mcode}, chart: {chart_id}"
@@ -134,7 +134,7 @@ class CxbScoreData(BaseData):
return result.lastrowid
def put_ranking(
async def put_ranking(
self, user_id: int, rev_id: int, song_id: int, score: int, clear: int
) -> Optional[int]:
"""
@@ -151,7 +151,7 @@ class CxbScoreData(BaseData):
conflict = sql.on_duplicate_key_update(score=score)
result = self.execute(conflict)
result = await self.execute(conflict)
if result is None:
self.logger.error(
f"{__name__} failed to insert ranking log! profile: {user_id}, score: {score}, clear: {clear}"
@@ -160,28 +160,28 @@ class CxbScoreData(BaseData):
return result.lastrowid
def get_best_score(self, user_id: int, song_mcode: int) -> Optional[Dict]:
async def get_best_score(self, user_id: int, song_mcode: int) -> Optional[Dict]:
sql = score.select(
and_(score.c.user == user_id, score.c.song_mcode == song_mcode)
)
result = self.execute(sql)
result = await self.execute(sql)
if result is None:
return None
return result.fetchone()
def get_best_scores(self, user_id: int) -> Optional[Dict]:
async def get_best_scores(self, user_id: int) -> Optional[Dict]:
sql = score.select(score.c.user == user_id)
result = self.execute(sql)
result = await self.execute(sql)
if result is None:
return None
return result.fetchall()
def get_best_rankings(self, user_id: int) -> Optional[List[Dict]]:
async def get_best_rankings(self, user_id: int) -> Optional[List[Dict]]:
sql = ranking.select(ranking.c.user == user_id)
result = self.execute(sql)
result = await self.execute(sql)
if result is None:
return None
return result.fetchall()

View File

@@ -29,7 +29,7 @@ music = Table(
class CxbStaticData(BaseData):
def put_music(
async def put_music(
self,
version: int,
mcode: str,
@@ -55,12 +55,12 @@ class CxbStaticData(BaseData):
title=title, artist=artist, category=category, level=level
)
result = self.execute(conflict)
result = await self.execute(conflict)
if result is None:
return None
return result.lastrowid
def get_music(
async def get_music(
self, version: int, song_id: Optional[int] = None
) -> Optional[List[Row]]:
if song_id is None:
@@ -73,12 +73,12 @@ class CxbStaticData(BaseData):
)
)
result = self.execute(sql)
result = await self.execute(sql)
if result is None:
return None
return result.fetchall()
def get_music_chart(
async def get_music_chart(
self, version: int, song_id: int, chart_id: int
) -> Optional[List[Row]]:
sql = select(music).where(
@@ -89,7 +89,7 @@ class CxbStaticData(BaseData):
)
)
result = self.execute(sql)
result = await self.execute(sql)
if result is None:
return None
return result.fetchone()