MC/network/repair-closed-xp-boundary.py
2026-11-27 23:43:05 +00:00

37 lines
3 KiB
Python

"""Guarded repair of a closed vanilla XP bar exactly at its next-level boundary.
Never touches inventory, balance, permissions, or arbitrary invalid profiles.
"""
import pathlib,json,hashlib,sys,re,urllib.parse,datetime
def vanilla_total(level):
if level<=16:return level*level+6*level
if level<=31:return (5*level*level-81*level+720)//2
return (9*level*level-325*level+4440)//2
def replacement(row):
if row['session_open']!=0 or not 0.999999<row['experience_progress']<=1.0 or not isinstance(row['experience_level'],int) or not 0<=row['experience_level']<100000:raise ValueError('Not an exact closed XP boundary')
level=row['experience_level']+1
if row['total_experience']!=vanilla_total(level):raise ValueError('XP total does not establish an exact next-level boundary')
return level,0.0
def main():
import yaml,pymysql
base=pathlib.Path(sys.argv[1]);expected=sys.argv[2] if len(sys.argv)>2 else None
d=yaml.safe_load((base/'plugins/DuneCore/config.yml').read_text())['database'];u=urllib.parse.urlparse(re.sub('^jdbc:','',d['jdbc-url']))
c=pymysql.connect(host=u.hostname,port=u.port,user=d['username'],password=d['password'],database=u.path.strip('/'),cursorclass=pymysql.cursors.DictCursor,autocommit=False)
try:
with c.cursor() as q:
q.execute('SELECT @@hostname AS host');assert q.fetchone()['host']=='ubuntu-4gb-fsn1-1'
# MySQL's FLOAT text protocol can print 0.99999958 as 1. Read full precision.
q.execute('SELECT uuid,experience_level,CAST(experience_progress AS DOUBLE) experience_progress,total_experience,session_open,inventory FROM dune_players WHERE experience_progress>0.999999 AND session_open=0 FOR UPDATE');rows=q.fetchall()
manifest=[]
for row in rows:
level,progress=replacement(row);manifest.append({'uuid':row['uuid'],'oldLevel':row['experience_level'],'level':level,'total':row['total_experience'],'inventoryHash':hashlib.sha256((row['inventory'] or '').encode()).hexdigest()})
fingerprint=hashlib.sha256(json.dumps(manifest,sort_keys=True).encode()).hexdigest()
if expected:
if expected!=fingerprint:raise ValueError('Profile repair plan changed')
for row in manifest:
q.execute('UPDATE dune_players SET experience_level=%s,experience_progress=0 WHERE uuid=%s AND experience_level=%s AND experience_progress>0.999999 AND total_experience=%s AND session_open=0',[row['level'],row['uuid'],row['oldLevel'],row['total']]);assert q.rowcount==1
q.execute('SELECT inventory FROM dune_players WHERE uuid=%s',[row['uuid']]);assert hashlib.sha256((q.fetchone()['inventory'] or '').encode()).hexdigest()==row['inventoryHash']
c.commit();receipt=base/'plugins/JarvisTranslator/xp-boundary-repair.json';receipt.write_text(json.dumps({'completed':datetime.datetime.now(datetime.timezone.utc).isoformat(),'planHash':fingerprint,'repairs':manifest},indent=2));receipt.chmod(0o600)
else:c.rollback()
print(json.dumps({'applied':bool(expected),'planHash':fingerprint,'profiles':[{k:v for k,v in r.items() if k!='uuid'} for r in manifest]}))
finally:c.close()
if __name__=='__main__':main()