0ad/source/tools/rlclient/python/zero_ad/api.py
wraitii df18102e96 Add an 'Evaluate' RL endpoint, intended for dynamic game balancing
This adds a new endpoint to the RL interface for evaluating custom
JavaScript. When combined with the ability to create arbitrary
modifiers, this provides the required functionality for exploring
quantitative game balancing.

Patch By: irishninja
Fixes #5981

Differential Revision: https://code.wildfiregames.com/D3479
This was SVN commit r24962.
2021-02-28 12:16:32 +00:00

33 lines
1,018 B
Python

import urllib
from urllib import request
import json
class RLAPI():
def __init__(self, url):
self.url = url
def post(self, route, data):
response = request.urlopen(url=f'{self.url}/{route}', data=bytes(data, 'utf8'))
return response.read()
def step(self, commands):
post_data = '\n'.join((f'{player};{json.dumps(action)}' for (player, action) in commands))
return self.post('step', post_data)
def reset(self, scenario_config, player_id, save_replay):
path = 'reset?'
if save_replay:
path += 'saveReplay=1&'
if player_id:
path += f'playerID={player_id}&'
return self.post(path, scenario_config)
def get_templates(self, names):
post_data = '\n'.join(names)
response = self.post('templates', post_data)
return zip(names, response.decode().split('\n'))
def evaluate(self, code):
response = self.post('evaluate', code)
return json.loads(response.decode())