Files
2026-01-26 18:48:28 -05:00

321 lines
16 KiB
Python

from database import ddrgp_insert_playlog, getDDRGPPlaylogbyRefid, manage_user_GP
from variables.strings.eacnet import (
log_2
)
from variables.strings.games.ddrgp import (
gpGetHash,
gpMusicList,
ddrGPPlayerData,
ddrGPArcadeData,
ddrGPRecv,
ddrGPmPack,
ddrGPsaveOK,
ddrGPtabooCheck,
gpGetDownloadURLs,
ddrGPMusicPacks,
ddrGPNoteData,
ddrGPmusicData,
ddrGPGetMain
)
from time import time
from base64 import b64decode, b64encode
import re
from kbinxml import KBinXML
import xmltodict
import json
profileKeys = [
"area", "hexseq", "weekly", "weekly_cnt",
"exLamps", "flag1", "flag2", "new_profile_flag",
"flag4", "flag5", "flag6", "flag7",
"weight", "name", "rival"
]
optionKeys = [
"SPEED",
"BOOST",
"APPEARANCE",
"TURN",
"STEP_ZONE",
"SCROLL",
"ARROW_COLOR",
"CUT",
"FREEZE",
"JUMPS",
"ARROW_SKIN",
"FILTER",
"GUIDELINE",
"GAUGE",
"COMBO_POSITION",
"FAST_SLOW",
]
rivalKeys = [
"SLOT_1_ACTIVE",
"SLOT_2_ACTIVE",
"SLOT_3_ACTIVE",
"SLOT_1_DDRCODE",
"SLOT_2_DDRCODE",
"SLOT_3_DDRCODE",
]
lastSessionKeys = [
"session_id",
"rival_id",
"music_id",
"session_count",
"unknown_value_1",
"unknown_value_2",
"status",
"calories_burned_session_1",
"calories_burned_session_2",
"unknown_value_3",
"unknown_value_4",
"unknown_value_5",
"unknown_value_6",
"final_value",
"unique_code",
]
playerCOMMON = 'MSwwLDM3MjkwMGQsMCwwLDAsMCwwLGZmZmZmZmZmZmZmZmZmZmYsMCwwLDAsMCwwLDAs'
playerOPTION = 'MCwzLDAsMCwwLDAsMCwzLDAsMCwwLDAsMSwyLDAsMCwwLDEwLjAwMDAwMCwxMC4wMDAwMDAsMTAuMDAwMDAwLDEwLjAwMDAwMCwwLjAwMDAwMCwwLjAwMDAwMCwwLjAwMDAwMCwwLjAwMDAwMCwsLCwsLCwsLCwsLCw='
playerLAST = 'MSwwLDAsMCwwLDAsMCwwLDAsMCwwLDAsMCwwLDAsMCwwLDAuMDAwMDAwLDAuMDAwMDAwLDAuMDAwMDAwLDAuMDAwMDAwLDAuMDAwMDAwLDAuMDAwMDAwLDAuMDAwMDAwLDAuMDAwMDAwLCwsLCwsLCwsLCwsLA=='
playerRIVAL = 'MCwwLDAsMCwwLDAsMCwwLDAsMCwwLDAsMCwwLDAsMCwwLDAuMDAwMDAwLDAuMDAwMDAwLDAuMDAwMDAwLDAuMDAwMDAwLDAuMDAwMDAwLDAuMDAwMDAwLDAuMDAwMDAwLDAuMDAwMDAwLCwsLCwsLCwsLCwsLA=='
def DDRGPRelay(module, method, data, playerToken):
gameData = data.get("data", {})
refid = gameData.get("refid", {}).get("#text", {})
print(f"refID: {refid}")
print(f"{module} {method}")
match module:
case "tabooword_2":
return ddrGPtabooCheck
case "log_2":
match method:
case "save":
# Game History Save, Not Needed Right Now
return log_2
case _: # Backup Case
return log_2
case "fileinfo_2":
match method:
case "getprev": # Get Preview Music
return gpGetHash
case "getmain": # Get Main Music
#return gpGetHash # Requires Additional Data not available at this current moment, code in theory works perfectly fine.
#allDownloads = xmltodict.parse(KBinXML(gpGetHash).to_text())
#allDownloads = allDownloads["eacnet"]["response"]["fileinfo_2"]["soundinfo"]
allDownloads = json.loads(ddrGPGetMain)
mcode_data = None
for item in allDownloads:
if str(item["mcode"]) == gameData["mcode"]["#text"]:
mcode_data = item
break # Exit the loop once the item is found
if mcode_data is not None:
print("MCODE found. Sending to Client.")
return gpGetDownloadURLs.format(mcode=gameData["mcode"]["#text"],xsb_path=mcode_data["xsb_path"],xsb_url=mcode_data["xsb_url"],xsb_size=mcode_data["xsb_size"],xsb_hash=mcode_data["xsb_hash"],xwb_path=mcode_data["xwb_path"],xwb_url=mcode_data["xwb_url"],xwb_size=mcode_data["xwb_size"],xwb_hash=mcode_data["xwb_hash"],status=0).encode()
else:
print("specific MCODE not found.")
return gpGetDownloadURLs.format(mcode="",xsb_path="",xsb_url="",xsb_size="",xwb_path="",xwb_url="",xwb_size="",status=1).encode()
case "data_2":
match method:
case "getmusic":
return gpMusicList
case "playerdata_2":
match method:
case "usergamedata_advanced":
match gameData["mode"]["#text"]:
case "userload": # int(time())*1000
isNew = 1
gameSessionTime = int(time()) * 1000
musicData = getDDRGPPlaylogbyRefid(refid)
# Count scores, clearkinds and ranks for each notetype
counts = {}
for item in musicData:
mcode = item['musicid']
# Initialize mcode if not already in dictionary
if mcode not in counts:
counts[mcode] = {}
notetype = item['notetype']
# Initialize notetype if not already in mcode dictionary
if notetype not in counts[mcode]:
counts[mcode][notetype] = {}
# Initialize other subcategories if not already present
subcategories = ['score', 'clearkind', 'rank', 'id']
for subcategory in subcategories:
if subcategory not in item:
item[subcategory] = None # or some default value
counts[mcode][notetype][subcategory] = item[subcategory]
print(counts)
music = ''''''
for mcode, note_dict in counts.items():
noteData = ''
for noteid in range(9):
data = note_dict.get(noteid, {})
score = data.get('score', -1)
clearkind = data.get('clearkind', 0)
rank = data.get('rank', 0)
ghostid = data.get('id', -1) if 'id' in data else -1
formatted_string = f"mcode: {mcode}, noteid: {noteid}, score: {score}, clearkind: {clearkind}, rank: {rank}, ghostid: {ghostid}"
print(formatted_string)
noteData += ddrGPNoteData.format(count=0,rank=rank,clear=clearkind,score=score,ghostID=ghostid)
music += ddrGPmusicData.format(mcode=mcode,noteData=noteData)
sampleData = """"""
# TODO: Save Data and Pull down Events at some Point
return ddrGPPlayerData.format(music=music,gameSession=gameSessionTime).encode()
#return sampleData.encode()
case "arcadeload":
print("Attempting To Give fake Arcade Data")
return ddrGPArcadeData
case "mpackload": # Music Pack Data
#return ddrGPmPack
return ddrGPMusicPacks
case "usersave": # Score Save
for note in gameData["note"]:
if int(note["stagenum"]["#text"]) > 0:
ddrgp_insert_playlog(
refid=refid,
stage_no=note["stagenum"]["#text"],
musicid=note["mcode"]["#text"],
notetype=note["notetype"]["#text"],
rank=note["rank"]["#text"],
clearkind=note["clearkind"]["#text"],
score=note["score"]["#text"],
exscore=note["exscore"]["#text"],
maxcombo=note["maxcombo"]["#text"],
life=note["life"]["#text"],
fastcount=note["fastcount"]["#text"],
slowcount=note["slowcount"]["#text"],
judge_marvelous=note["judge_marvelous"]["#text"],
judge_perfect=note["judge_perfect"]["#text"],
judge_great=note["judge_great"]["#text"],
judge_good=note["judge_good"]["#text"],
judge_boo=note["judge_boo"]["#text"],
judge_miss=note["judge_miss"]["#text"],
judge_ok=note["judge_ok"]["#text"],
judge_ng=note["judge_ng"]["#text"],
calorie=note["calorie"]["#text"],
ghostsize=note["ghostsize"]["#text"],
ghost=note["ghost"]["#text"],
timestamp=note["endtime"]["#text"],
playstyle=note["playstyle"]["#text"]
)
return ddrGPsaveOK
case "usernew":
return ddrGPsaveOK
case _:
return b"Bad request"
case "usergamedata_recv":
# Always return the default values to force player registration, if they are not set.
playerData = manage_user_GP(refid,playerToken,None,None,None,None,0)
if playerData is None:
return ddrGPRecv.format(player=playerCOMMON,options=playerOPTION,last=playerLAST,rival=playerRIVAL).encode()
else:
return ddrGPRecv.format(player=playerData.common,options=playerData.option,last=playerData.last,rival=playerData.rival).encode()
case "usergamedata_send": # TODO: Figure out how to save OPTIONS and COMMON type D10 Data
COMMON = None
OPTION = None
LAST = None
RIVAL = None
saveRecords = gameData["record"]["d"]
if isinstance(saveRecords, dict):
# process what is being saved
data = b64decode(saveRecords["#text"]).decode()
if re.search(r".+,(COMMON),", data):
print("COMMON data")
len(data)
if len(data) > 22:
COMMON = b64encode(re.sub(r'(.+?,(COMMON),)', "", re.sub(r"(\d+,)(\d+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.*)(,.*)", r'\1\2,372900d\4\5\6\7\8\9\10\11\12\13\13\13\15', data)).encode()).decode()
elif re.search(r".+,(OPTION),", data):
print("OPTION data")
OPTION = b64encode(re.sub(r".+,(OPTION),", "", data).encode()).decode()
elif re.search(r".+,(LAST),", data):
print("LAST data")
LAST = b64encode(re.sub(r".+,(LAST),", "", data).encode()).decode()
elif re.search(r".+,(RIVAL),", data):
print("RIVAL data")
if len(data) > 22:
RIVAL = b64encode(re.sub(r".+,(RIVAL),", "", data).encode()).decode()
elif isinstance(saveRecords, list):
for item in saveRecords:
data = b64decode(item["#text"]).decode()
if re.search(r".+,(COMMON),", data):
print("COMMON data")
len(data)
print(data)
if data.startswith("D10,COMMON,"):
playerData = manage_user_GP(refid,playerToken,None,None,None,None,0)
COMMONdata = b64decode(playerData.common).decode()
# Remove spaces and commas from the chart string
COMMONdata = COMMONdata.replace(" ", "")
# Split the COMMON data string into different fields
fields = COMMONdata.split(",")
# Convert the fields to the correct data type and append them to the chart data list
commonEdit = []
for field in fields:
if type(field) == str:
commonEdit.append(field) # Keeping the 'd' value as a string
else:
commonEdit.append(int(field))
print(commonEdit)
# Do the same with the data
data = data.replace("D10,COMMON,", "")
commonReplace = []
fields = data.split(",")
for field in fields:
if type(field) == str:
commonReplace.append(field) # Keeping the 'd' value as a string
else:
commonReplace.append(int(field))
# replace certain data from the common data with the new data
commonEdit[3] = commonReplace[0]
commonEdit[5] = commonReplace[1]
commonEdit[8] = commonReplace[2]
commonEdit[10] = commonReplace[3]
commonEdit[11] = commonReplace[4]
# Convert the edited data back into a base64 string
commonEdit = ",".join(commonEdit)
COMMON = b64encode(commonEdit.encode()).decode()
if len(data) > 22:
COMMON = b64encode(re.sub(r'(.+?,(COMMON),)', "", re.sub(r"(\d+,)(\d+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.+)(,.*)(,.*)", r'\1\2,372900d\4\5\6\7\8\9\10\11\12\13\13\13\15', data)).encode()).decode()
if re.search(r".+,(OPTION),", data):
print("OPTION data")
if len(data) > 65:
OPTION = b64encode((re.sub(r",17.000000", ",10.000000", re.sub(r",42.000000", ",10.000000", re.sub(r",0.000000", ",10.000000", re.sub(r",a,", ",7,", re.sub(r".+,(OPTION),", "", data)))))+",1907c71c7bd,18d8cd94ce2,").encode()).decode()
if re.search(r".+,(LAST),", data):
print("LAST data")
print(re.sub(r".+,(LAST),", "", data).encode())
LAST = b64encode(re.sub(r".+,(LAST),", "", data).encode()).decode()
if re.search(r".+,(RIVAL),", data):
print("RIVAL data")
if len(data) > 22:
RIVAL = b64encode(re.sub(r".+,(RIVAL),", "", data).encode()).decode()
print(f'sending values {refid},{playerToken},COMMON {COMMON},OPTION {OPTION},LAST {LAST},RIVAL {RIVAL}')
manage_user_GP(refid,playerToken,COMMON,OPTION,LAST,RIVAL,1)
return ddrGPsaveOK
case _: # Backup Case
return b"NoMatches"