Files
2026-11-23 03:28:32 +02:00

35 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""Client-side aliases for Turtle page_text IDs above 65535.
70334 ItemSparse.PageID is u16. Turtle pages above 65535 get the highest free IDs below 65536 (free = no Turtle page
with that ID). The gateway maps the alias to the real ID when the client queries a page, and maps the real IDs
(page and next page) back in the reply. Writes include/gateway/page_text_aliases.hpp and a JSON map for the converter.
"""
import json, subprocess, sys
from pathlib import Path
HERE = Path(__file__).resolve().parent
ROOT = HERE.parents[1]
def rows():
out = subprocess.run([sys.executable, str(HERE / 'tw_world_query.py'), 'select entry from page_text order by entry'],
check=True, capture_output=True, text=True).stdout
return sorted(int(x) for x in out.split() if x.isdigit())
def main():
ids = rows()
high = [i for i in ids if i > 0xffff]
used = {i for i in ids if i <= 0xffff}
free = (i for i in range(0xffff, 0, -1) if i not in used)
alias = {real: next(free) for real in high}
(HERE / 'page-text-aliases.json').write_text(json.dumps({str(k): v for k, v in alias.items()}, indent=1) + '\n')
lines = ['// Generated by ops/claude_itemsparse_773/page_text_aliases.py from tw_world.page_text.', '#pragma once',
'#include <cstdint>', '', 'namespace gateway::page_text_aliases {',
'struct Alias { uint32_t real; uint16_t client; };', 'inline constexpr Alias Aliases[] = {']
lines += [f' {{{k}u, {v}u}},' for k, v in sorted(alias.items())]
lines += ['};', 'inline uint32_t to_source(uint32_t client) {',
' for (const auto& a : Aliases) if (a.client == client) return a.real;', ' return client;', '}',
'inline uint32_t to_client(uint32_t real) {',
' for (const auto& a : Aliases) if (a.real == real) return a.client;', ' return real;', '}',
'} // namespace gateway::page_text_aliases', '']
(ROOT / 'include/gateway/page_text_aliases.hpp').write_text('\n'.join(lines))
print('pages', len(ids), 'aliased', len(alias), 'range', min(alias.values()), max(alias.values()))
if __name__ == '__main__': main()