Updated silver and item collection
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from ..environment import discovery_log_path
|
||||
from . import items as itemdb
|
||||
|
||||
|
||||
_lock = threading.Lock()
|
||||
_initialized = False
|
||||
|
||||
|
||||
def enabled() -> bool:
|
||||
return bool(discovery_log_path())
|
||||
|
||||
|
||||
def log(record_type: str, **fields):
|
||||
path = discovery_log_path()
|
||||
if not path:
|
||||
return
|
||||
|
||||
global _initialized
|
||||
with _lock:
|
||||
log_dir = os.path.dirname(path)
|
||||
if log_dir:
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
mode = 'a' if _initialized else 'w'
|
||||
with open(path, mode, encoding='utf-8') as out:
|
||||
if not _initialized:
|
||||
out.write(json.dumps({
|
||||
'type': 'session_start',
|
||||
'timestamp': _timestamp(),
|
||||
'format': 'aostats-discovery-jsonl-v1',
|
||||
}, sort_keys=True) + '\n')
|
||||
_initialized = True
|
||||
out.write(json.dumps({
|
||||
'type': record_type,
|
||||
'timestamp': _timestamp(),
|
||||
**_clean(fields),
|
||||
}, sort_keys=True) + '\n')
|
||||
|
||||
|
||||
def message_record(message) -> Dict[str, Any]:
|
||||
params = getattr(message, 'params', {}) or {}
|
||||
return {
|
||||
'kind': getattr(message, 'kind', None),
|
||||
'code': getattr(message, 'code', None),
|
||||
'return_code': getattr(message, 'return_code', None),
|
||||
'debug_message': _clean(getattr(message, 'debug_message', None)),
|
||||
'params': _clean(params),
|
||||
'item_id_candidates': item_candidates(params),
|
||||
}
|
||||
|
||||
|
||||
def game_message_record(game_message) -> Dict[str, Any]:
|
||||
params = getattr(game_message, 'params', {}) or {}
|
||||
return {
|
||||
'name': getattr(game_message, 'name', None),
|
||||
'code': getattr(game_message, 'code', None),
|
||||
'params': _clean(params),
|
||||
'item_id_candidates': item_candidates(params),
|
||||
}
|
||||
|
||||
|
||||
def item_candidates(params: Dict[Any, Any]) -> List[Dict[str, Any]]:
|
||||
db = itemdb.get_db()
|
||||
found: List[Dict[str, Any]] = []
|
||||
for key, value in (params or {}).items():
|
||||
for path, number in _walk_numbers(value, str(key)):
|
||||
if number in db:
|
||||
found.append({
|
||||
'path': path,
|
||||
'id': number,
|
||||
'unique_name': db[number],
|
||||
})
|
||||
return found
|
||||
|
||||
|
||||
def _timestamp() -> str:
|
||||
return datetime.now(timezone.utc).isoformat(timespec='milliseconds')
|
||||
|
||||
|
||||
def _clean(value):
|
||||
if isinstance(value, bytes):
|
||||
return {'bytes_hex': value.hex(), 'len': len(value)}
|
||||
if isinstance(value, dict):
|
||||
return {str(k): _clean(v) for k, v in value.items()}
|
||||
if isinstance(value, (list, tuple)):
|
||||
return [_clean(v) for v in value]
|
||||
return value
|
||||
|
||||
|
||||
def _walk_numbers(value, path: str):
|
||||
if isinstance(value, bool):
|
||||
return
|
||||
if isinstance(value, int):
|
||||
yield path, value
|
||||
return
|
||||
if isinstance(value, float) and value.is_integer():
|
||||
yield path, int(value)
|
||||
return
|
||||
if isinstance(value, dict):
|
||||
for key, item in value.items():
|
||||
yield from _walk_numbers(item, f'{path}.{key}')
|
||||
return
|
||||
if isinstance(value, (list, tuple)):
|
||||
for i, item in enumerate(value):
|
||||
yield from _walk_numbers(item, f'{path}[{i}]')
|
||||
Reference in New Issue
Block a user