This file contains hidden or 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
@staticmethod | |
async def create(headless=True) -> 'Game': | |
o = Game(headless) | |
await o.initialize() | |
return o | |
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'}) |
This file contains hidden or 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
echo "DefaultTasksMax=infinity" | sudo tee --append /etc/systemd/system.conf > /dev/null \ | |
echo "DefaultLimitNOFILE=10000000" | sudo tee --append /etc/systemd/system.conf > /dev/null \ | |
echo "UserTasksMax=infinity" | sudo tee --append /etc/systemd/logind.conf > /dev/null | |
echo "* soft nproc unlimited" | sudo tee --append /etc/security/limits.conf > /dev/null \ | |
echo "* hard nproc unlimited" | sudo tee --append /etc/security/limits.conf > /dev/null \ | |
echo "* soft nofile unlimited" | sudo tee --append /etc/security/limits.conf >/dev/null \ | |
echo "* hard nofile unlimited" | sudo tee --append /etc/security/limits.conf > /dev/null \ | |
echo "root soft nofile unlimited" | sudo tee --append /etc/security/limits.conf > /dev/null \ | |
echo "root hard nofile unlimited" | sudo tee --append /etc/security/limits.conf > /dev/null |
This file contains hidden or 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'): |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) |