This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def initialize(self): | |
self.browser = await launch(headless=self.headless) | |
self.page = await self.browser.newPage() | |
await self.page.goto(self.game_url, {'waitUntil': 'networkidle2'}) | |
envjs_path = pathlib.Path(__file__).resolve().parent.joinpath('env.js') | |
await self.page.addScriptTag(dict(path=str(envjs_path))) | |
await self.is_ready() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from syncer import sync | |
class SyncGame: | |
def __init__(self, game: Game): | |
self.game = game | |
def __getattr__(self, attr): | |
return sync(getattr(self.game, attr)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def get_state(self): | |
state = await self.page.evaluate('''(includeSnapshot, format, quality) => { | |
return neyboyChallenge.state(includeSnapshot, format, quality); | |
}''', True, 'image/jpeg', 30) | |
base64_string = state['snapshot'] | |
base64_string = re.sub('^data:image/.+;base64,', '', base64_string) | |
imgdata = base64.b64decode(base64_string) | |
bytes_io = io.BytesIO(imgdata) | |
image = Image.open(bytes_io) | |
state['snapshot'] = np.array(image) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def tap_left(self, delay=0): | |
x = self.x + self.width // 4 | |
y = self.y + self.height // 3 | |
await self.page.mouse.click(x, y, {'delay': delay}) | |
async def tap_right(self, delay=0): | |
x = (self.x + self.width) - self.width // 4 | |
y = self.y + self.height // 3 | |
await self.page.mouse.click(x, y, {'delay': delay}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __init__(self): | |
headless = os.environ.get('GYM_NEYBOY_ENV_NON_HEADLESS', None) is None | |
self.game = SyncGame.create(headless=headless) | |
self.state = self.game.get_state() | |
self.viewer = None | |
self.observation_space = spaces.Box(low=0, high=255, shape=self.state.snapshot.shape, dtype=np.uint8) | |
self.action_space = spaces.Discrete(3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def render(self, mode='human', close=False): | |
img = self.state.snapshot | |
if mode == 'rgb_array': | |
return img | |
elif mode == 'human': | |
from gym.envs.classic_control import rendering | |
if self.viewer is None: | |
self.viewer = rendering.SimpleImageViewer() | |
self.viewer.imshow(img) | |
return self.viewer.isopen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def close(self): | |
if self.viewer is not None: | |
self.viewer.close() | |
self.viewer = None | |
self.game.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reset(self): | |
self.game.restart() | |
self.state = self.game.get_state() | |
self.game.pause() | |
return self.state.snapshot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def step(self, action): | |
self.game.resume() | |
if action == ACTION_LEFT: | |
self.game.tap_left() | |
elif action == ACTION_RIGHT: | |
self.game.tap_right() | |
self.state = self.game.get_state() | |
self.game.pause() | |
is_over = self.state.status == GAME_OVER_SCREEN | |
if is_over: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from collections import namedtuple, defaultdict | |
import re | |
pattern = re.compile("cv::(\w+)") | |
deps = defaultdict(list) | |
p = Path('.') | |
for i in p.glob('**/*.*pp'): |