-
Notifications
You must be signed in to change notification settings - Fork 280
/
gamectrl.py
259 lines (218 loc) · 9.39 KB
/
gamectrl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# -*- coding: utf-8 -*-
import math
import re
import time
import json
class Generic2048Control(object):
def __init__(self, ctrl):
self.ctrl = ctrl
self.setup()
def setup():
raise NotImplementedError()
def execute(self, cmd):
return self.ctrl.execute(cmd)
def get_status(self):
''' Check if the game is in an unusual state. '''
return self.execute('''
var messageContainer = document.querySelector(".game-message");
if(messageContainer.className.search(/game-over/) !== -1) {"ended"}
else if(messageContainer.className.search(/game-won/) !== -1) {"won"}
else {"running"}
''')
def restart_game(self):
self.send_key_event('keydown', 82)
time.sleep(0.1)
self.send_key_event('keyup', 82)
self.send_key_event('keydown', 32)
time.sleep(0.1)
self.send_key_event('keyup', 32)
def continue_game(self):
''' Continue the game. Only works if the game is in the 'won' state. '''
self.execute('document.querySelector(".keep-playing-button").click();')
def send_key_event(self, action, key):
# Use generic events for compatibility with Chrome, which (for inexplicable reasons) doesn't support setting keyCode on KeyboardEvent objects.
# See http://stackoverflow.com/questions/8942678/keyboardevent-in-chrome-keycode-is-0.
return self.execute('''
var keyboardEvent = document.createEventObject ? document.createEventObject() : document.createEvent("Events");
if(keyboardEvent.initEvent)
keyboardEvent.initEvent("%(action)s", true, true);
keyboardEvent.keyCode = %(key)s;
keyboardEvent.which = %(key)s;
var element = document.body || document;
element.dispatchEvent ? element.dispatchEvent(keyboardEvent) : element.fireEvent("on%(action)s", keyboardEvent);
''' % locals())
class Fast2048Control(Generic2048Control):
''' Control 2048 by hooking the GameManager and executing its move() function.
This is both safer and faster than the keyboard approach, but it is less compatible with clones. '''
def setup(self):
# Obtain the GameManager instance by triggering a fake restart.
self.ctrl.execute(
'''
var _func_tmp = GameManager.prototype.isGameTerminated;
GameManager.prototype.isGameTerminated = function() {
GameManager._instance = this;
return true;
};
''')
# Send an "up" event, which will trigger our replaced isGameTerminated function
self.send_key_event('keydown', 38)
time.sleep(0.1)
self.send_key_event('keyup', 38)
self.execute('GameManager.prototype.isGameTerminated = _func_tmp;')
def get_status(self):
''' Check if the game is in an unusual state. '''
return self.execute('''
if(GameManager._instance.over) {"ended"}
else if(GameManager._instance.won && !GameManager._instance.keepPlaying) {"won"}
else {"running"}
''')
def get_score(self):
return self.execute('GameManager._instance.score')
def get_board(self):
# Chrome refuses to serialize the Grid object directly through the debugger.
grid = json.loads(self.execute('JSON.stringify(GameManager._instance.grid)'))
board = [[0]*4 for _ in range(4)]
for row in grid['cells']:
for cell in row:
if cell is None:
continue
pos = cell['x'], cell['y']
tval = cell['value']
board[pos[1]][pos[0]] = int(round(math.log(tval, 2)))
return board
def execute_move(self, move):
# We use UDLR ordering; 2048 uses URDL ordering
move = [0, 2, 3, 1][move]
self.execute('GameManager._instance.move(%d)' % move)
class Keyboard2048Control(Generic2048Control):
''' Control 2048 by accessing the DOM and using key events.
This is relatively slow, and may be prone to race conditions if your
browser is slow. However, it is more generally compatible with various
clones of 2048. '''
def setup(self):
self.execute(
'''
var elems = document.getElementsByTagName('div');
for(var i in elems)
if(elems[i].className == 'tile-container') {
tileContainer = elems[i];
break;
}
''')
def get_score(self):
score = self.execute('''
var scoreContainer = document.querySelector(".score-container");
var scoreText = '';
var scoreChildren = scoreContainer.childNodes;
for(var i = 0; i < scoreChildren.length; ++i) {
if(scoreChildren[i].nodeType == Node.TEXT_NODE) {
scoreText += scoreChildren[i].textContent;
}
}
scoreText;
''')
return int(score)
def get_board(self):
res = self.execute(
'''
var res = [];
var tiles = tileContainer.children;
for(var i=0; i<tiles.length; i++)
res.push(tiles[i].className);
res
''')
board = [[0]*4 for _ in range(4)]
for tile in res:
tval = pos = None
for k in tile.split():
m = re.match(r'^tile-(\d+)$', k)
if m:
tval = int(m.group(1))
m = re.match(r'^tile-position-(\d+)-(\d+)$', k)
if m:
pos = int(m.group(1)), int(m.group(2))
board[pos[1]-1][pos[0]-1] = int(round(math.log(tval, 2)))
return board
def execute_move(self, move):
key = [38, 40, 37, 39][move]
self.send_key_event('keydown', key)
time.sleep(0.01)
self.send_key_event('keyup', key)
time.sleep(0.05)
class Hybrid2048Control(Fast2048Control, Keyboard2048Control):
''' Control 2048 by hooking the GameManager and using keyboard inputs.
This is safe and fast, and correctly generates keyboard events for compatibility.
'''
setup = Fast2048Control.setup
get_status = Keyboard2048Control.get_status
get_score = Fast2048Control.get_score
get_board = Fast2048Control.get_board
execute_move = Keyboard2048Control.execute_move
class Play2048CoControl(object):
""" Controller for Play2048.co """
def __init__(self, ctrl):
self.ctrl = ctrl
self.setup()
def setup(self):
# Get a reference to the game manager object
self.ctrl.execute(
"""
(async function() {
if(window._2048ai_manager)
return;
// look for the main module
for(let tag of document.querySelectorAll('script[type="module"]')) {
let module = await import(tag.src);
let manager = null;
// look for the game manager
for(let key of Object.keys(module)) {
if(module[key].subscribe && module[key].move && module[key].sanitize) {
manager = module[key];
break;
}
}
if(manager) {
window._2048ai_module = module;
window._2048ai_manager = manager;
// subscribe to the game and sanitize to read the game state
window._2048ai_manager.subscribe((game) => window._2048ai_game = game);
window._2048ai_manager.sanitize();
break;
}
}
})();
"""
)
time.sleep(0.5)
def get_status(self):
''' Check if the game is in an unusual state. '''
return self.ctrl.execute('''
if(window._2048ai_game.state == "fresh" || window._2048ai_game.state == "playing") { "running" }
else if(window._2048ai_game.state == "gameOver") { "ended" }
else if(window._2048ai_game.state == "gameWon") { "won" }
// TODO is this right? how to handle selecting?
else { "running" }
''')
def restart_game(self):
return self.ctrl.execute("window._2048ai_manager.reset()")
def continue_game(self):
''' Continue the game. Only works if the game is in the 'won' state. '''
return self.ctrl.execute("window._2048ai_manager.continueAfterWin()")
def get_score(self):
return self.ctrl.execute("window._2048ai_game.score")
def get_board(self):
# Chrome refuses to serialize the Grid object directly through the debugger.
grid = json.loads(self.ctrl.execute('JSON.stringify(window._2048ai_game.board)'))
board = [[0]*4 for _ in range(4)]
for row in grid:
for cell in row:
if cell is None:
continue
pos = cell["position"]["x"], cell["position"]["y"]
tval = cell['value']
board[pos[1]][pos[0]] = int(round(math.log(tval, 2)))
return board
def execute_move(self, move):
# We use UDLR ordering; 2048 uses URDL ordering
movename = ["up", "down", "left", "right"][move]
self.ctrl.execute("window._2048ai_manager.move('%s')" % movename)