24 lines
1.4 KiB
Python
24 lines
1.4 KiB
Python
"""Publish only eligible live intentions to the native recovery guard."""
|
|
import json
|
|
import time
|
|
import urllib.request
|
|
|
|
|
|
def publish_recovery_intents(state, cfg):
|
|
def literal(value):
|
|
return '"'+''.join('\\%03d'%c for c in value.encode())+'"'
|
|
rows=[]
|
|
for bot in state.get('bots',[]):
|
|
identity=bot.get('autonomy') or {};world=bot.get('world') or {}
|
|
action=(bot.get('lastDecision') or {}).get('action')
|
|
if bot.get('status')!='online' or not identity.get('managed') or action not in ('quest','explore'):continue
|
|
if time.time()-world.get('at',0)>120:continue
|
|
guid=identity.get('characterGuid');account=identity.get('accountId')
|
|
if type(guid) is not int or type(account) is not int or guid<=0 or account<=0:continue
|
|
rows.append('{guid=%d,account=%d,name=%s,action=%s}'%(guid,account,literal(bot['name']),literal(action)))
|
|
source='if not AzerothAISetRecoveryIntents then error("Recovery guard not loaded") end; return AzerothAISetRecoveryIntents({'+','.join(rows)+'})'
|
|
req=urllib.request.Request('http://127.0.0.1:8787/api/ale/operator',data=json.dumps({'kind':'raw','source':source}).encode(),headers={'Content-Type':'application/json','Authorization':'Bearer '+cfg['ALE_OPERATOR_TOKEN']})
|
|
with urllib.request.urlopen(req,timeout=10) as r:
|
|
if r.status not in (200,202):raise RuntimeError('Recovery publication failed')
|
|
return len(rows)
|