Implement configurable kill rewards with account-wide daily limits, transactional deduplication, and game-to-db retries. Add the approved wide rules and tested isolation tooling. Stage wear40 equipment and persistent extra item metadata with an explicit migration plan and client revision gate. Track all 168 remaining offers; sixteen have staged data/assets, while live acceptance and other systems remain incomplete. This commit is not a production deployment.
97 lines
4.4 KiB
Python
97 lines
4.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Plan/apply the 32->40 wear-slot migration with cores/db stopped and a full backup.
|
|
|
|
The saved plan contains exact row IDs and before/after values. Reapplying that same plan
|
|
is safe after interruption; never regenerate a plan from partially migrated data.
|
|
"""
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import subprocess
|
|
|
|
NAME = 'ava2-shop-wear-40'
|
|
|
|
|
|
def quickslots(blob):
|
|
if not blob:
|
|
return blob
|
|
if len(blob) != 108:
|
|
raise ValueError('Expected 36 three-byte quick slots')
|
|
output = bytearray(blob)
|
|
for index in range(0, len(blob), 3):
|
|
kind, lo, hi = blob[index:index+3]
|
|
pos = lo | hi << 8
|
|
if kind == 1 and (257 <= pos <= 268 or 287 <= pos <= 302):
|
|
output[index+1:index+3] = (pos+8).to_bytes(2, 'little')
|
|
return bytes(output)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--player', required=True)
|
|
parser.add_argument('--plan', type=Path, required=True)
|
|
parser.add_argument('--apply', action='store_true')
|
|
args = parser.parse_args()
|
|
if not re.fullmatch('[A-Za-z0-9_]+', args.player):
|
|
parser.error('Invalid database name')
|
|
|
|
def sql(query):
|
|
result = subprocess.run(['mysql', '-N', '-B', args.player], input=query,
|
|
text=True, capture_output=True, check=True)
|
|
return [line.split('\t') for line in result.stdout.splitlines()]
|
|
|
|
sql('CREATE TABLE IF NOT EXISTS mt_migration(name VARCHAR(64) PRIMARY KEY,done DATETIME);')
|
|
if sql("SELECT name FROM mt_migration WHERE name='%s';" % NAME):
|
|
print(NAME + ': already applied')
|
|
return
|
|
if args.plan.exists():
|
|
plan = json.loads(args.plan.read_text())
|
|
if plan['schema'] != args.player:
|
|
raise RuntimeError('Plan belongs to another database')
|
|
else:
|
|
# Equipped DS cells are relative; belt cells in INVENTORY are absolute.
|
|
bad = sql("SELECT i.id FROM item i LEFT JOIN item_proto p ON i.vnum=p.vnum "
|
|
"WHERE i.window='EQUIPMENT' AND i.pos>=32 AND "
|
|
"(i.pos>43 OR p.type IS NULL OR p.type NOT IN (29,30));")
|
|
if bad:
|
|
raise RuntimeError('Unexpected occupied extended equipment cells; inspect before migration')
|
|
items = sql("SELECT id,pos FROM item WHERE (window='EQUIPMENT' AND pos BETWEEN 32 AND 43) "
|
|
"OR (window='INVENTORY' AND pos BETWEEN 287 AND 302) ORDER BY pos DESC;")
|
|
plan = {'schema': args.player, 'items': [[int(i), int(p), int(p)+8] for i, p in items],
|
|
'quickslots': []}
|
|
for table in ('player', 'player_deleted'):
|
|
for pid, value in sql('SELECT id,HEX(quickslot) FROM `%s`;' % table):
|
|
old = bytes.fromhex(value)
|
|
new = quickslots(old)
|
|
if old != new:
|
|
plan['quickslots'].append([table, int(pid), old.hex(), new.hex()])
|
|
args.plan.parent.mkdir(parents=True, exist_ok=True)
|
|
args.plan.write_text(json.dumps(plan, indent=2)+'\n')
|
|
args.plan.chmod(0o600)
|
|
print('planned items=%d quickslots=%d' % (len(plan['items']), len(plan['quickslots'])))
|
|
if not args.apply:
|
|
return
|
|
# Every update is conditional on its original value, allowing safe saved-plan replay.
|
|
for item, old, new in plan['items']:
|
|
row = sql('SELECT pos FROM item WHERE id=%d;' % item)
|
|
if row not in ([[str(old)]], [[str(new)]]):
|
|
raise RuntimeError('Item changed since planning; cores/db must stay stopped')
|
|
sql('UPDATE item SET pos=%d WHERE id=%d AND pos=%d;' % (new, item, old))
|
|
for table, pid, old, new in plan['quickslots']:
|
|
row = sql('SELECT HEX(quickslot) FROM `%s` WHERE id=%d;' % (table, pid))
|
|
if not row or row[0][0].lower() not in (old, new):
|
|
raise RuntimeError('Quickslot changed since planning')
|
|
sql("UPDATE `%s` SET quickslot=X'%s' WHERE id=%d AND HEX(quickslot)='%s';" %
|
|
(table, new, pid, old.upper()))
|
|
columns = sql("SELECT COLUMN_NAME FROM information_schema.columns WHERE TABLE_SCHEMA=DATABASE() "
|
|
"AND TABLE_NAME='item' AND COLUMN_NAME='port_extra';")
|
|
if not columns:
|
|
sql("ALTER TABLE item ADD port_extra VARBINARY(69) NOT NULL DEFAULT ''; ")
|
|
sql("INSERT INTO mt_migration(name,done) VALUES('%s',NOW());" % NAME)
|
|
print(NAME + ': applied; retain the saved plan for recovery')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|