-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlayAsOne.py
316 lines (257 loc) · 10.3 KB
/
PlayAsOne.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#! usr/bin/env python2
from flask import Flask, request, flash, url_for, redirect, render_template, abort, send_from_directory, jsonify
from flask.ext.socketio import SocketIO, emit
from json import loads, dumps
import Tkinter as tk
import pyautogui
import threading
import ttk
import time
import sys
if sys.platform == 'win32':
import win32gui
if sys.platform == 'darwin':
pass
class PlayAsOne:
def __init__(self):
self.gui = GUI(self)
self.running = False
def start(self):
if not self.find_game_window():
self.gui.status_label.config(text='Could not locate the window!')
self.running = True
self.gui.start_button.config(text='Stop', command=self.stop)
self.gui.status_label.config(text='Running')
self.gui.titlebar_entry.config(state='disabled')
self.gui.mode_combobox.config(state='disabled')
self.gui.input_mode_combobox.config(state='disabled')
self.gui.interval_entry.config(state='disabled')
threading.Thread(target=socketio.run, args=(app,), kwargs={'host': '18.111.92.199', 'port':3000}).start()
threading.Thread(target=self.regulate_democracy).start()
def stop(self):
self.gui.start_button.config(text='Start', command=self.start)
self.gui.status_label.config(text='Not Running')
self.gui.titlebar_entry.config(state='normal')
self.gui.mode_combobox.config(state='normal')
self.gui.input_mode_combobox.config(state='normal')
self.gui.interval_entry.config(state='normal')
self.running = False
def regulate_democracy(self):
while self.running:
execute_democracy()
time.sleep(self.get_democracy_interval())
def is_running(self):
return self.running
def get_mode(self):
return self.gui.mode_combobox.get()
def get_input_mode(self):
return self.gui.input_mode_combobox.get()
def get_democracy_interval(self):
try:
return_code = int(self.gui.interval_entry.get())
return return_code
except ValueError:
return 0
def find_game_window(self):
if sys.platform == 'win32':
winlist = []
def enum_cb(hwnd, extra):
winlist.append((hwnd, win32gui.GetWindowText(hwnd)))
win32gui.EnumWindows(enum_cb, None)
window = [
(hwnd, title) for hwnd, title in winlist if self.gui.titlebar_entry.get().lower() in title.lower()]
if not window:
return False
window = window[0]
hwnd = window[0]
win32gui.ShowWindow(hwnd, 11)
win32gui.ShowWindow(hwnd, 1)
win32gui.SetForegroundWindow(hwnd)
region = win32gui.GetWindowRect(hwnd)
region = (
region[0],
region[1],
region[2]-region[0],
region[3]-region[1]
)
return region
return False
def send_key(self, key):
if not self.running:
return
self.find_game_window()
try:
wrapdict = eval(self.gui.wrap_entry.get())
if key.lower() in wrapdict:
key = wrapdict[key.lower()]
except ValueError:
pass
pyautogui.press(str(key))
def send_mouse_click(self, x, y, button):
if not self.running:
return
window_region = self.find_game_window()
if x < window_region[0]:
return
if y < window_region[1]:
return
if x > window_region[2]:
return
if y > window_region[2]:
return
pyautogui.click(x=x, y=y, button=button)
class GUI(tk.Tk):
def __init__(self, server):
tk.Tk.__init__(self)
self.server = server
self.mode_frame = tk.Frame(self)
self.mode_frame.grid(row=0, column=0, columnspan=2)
self.mode_label = tk.Label(self.mode_frame, text='Mode: ')
self.mode_label.grid(row=0, column=0, sticky='w')
self.mode_combobox = ttk.Combobox(
self.mode_frame, state='readonly', width=9, values=('Chaos', 'Democracy'))
self.mode_combobox.set('Chaos')
self.mode_combobox.grid(row=0, column=1, sticky='ew')
self.input_mode_label = tk.Label(self.mode_frame, text='Input Mode: ')
self.input_mode_label.grid(row=1, column=0, sticky='w')
self.input_mode_combobox = ttk.Combobox(
self.mode_frame, state='readonly', width=13, values=('NES', 'SNES', 'Full Keyboard'))
self.input_mode_combobox.set('Full Keyboard')
self.input_mode_combobox.grid(row=1, column=1, sticky='ew')
self.titlebar_label = tk.Label(self.mode_frame, text='Title Bar Name: ')
self.titlebar_label.grid(row=2, column=0, sticky='w')
self.titlebar_entry = ttk.Entry(self.mode_frame)
self.titlebar_entry.grid(row=2, column=1, sticky='ew')
self.interval_label = tk.Label(self.mode_frame, text='Democracy Interval: ')
self.interval_label.grid(row=3, column=0, sticky='w')
self.interval_entry = ttk.Entry(self.mode_frame)
self.interval_entry.grid(row=3, column=1, sticky='ew')
self.wrap_label = tk.Label(self.mode_frame, text='Wrap Keys: ')
self.wrap_label.grid(row=4, column=0, sticky='w')
self.wrap_entry = ttk.Entry(self.mode_frame)
self.wrap_entry.grid(row=4, column=1, sticky='ew')
self.status_label = tk.Label(self, text='Not Running')
self.status_label.grid(row=3, column=0, columnspan=2)
self.start_button = ttk.Button(self, text='Start', command=self.server.start)
self.start_button.grid(row=4, column=0, columnspan=2)
def start(self):
if self.deselected_screenshot is None:
pyautogui.alert(
text='You need to set a constant screenshot.', title='Screenshot', button='OK')
return
self.start_button.config(text='Stop', command=self.stop)
self.server.start()
def stop(self):
self.start_button.config(text='Start', command=self.start)
self.server.stop()
def take_screenshot(self):
def func():
self.screenshot_button.config(state='disabled')
for second in reversed(range(4)):
self.screenshot_label.config(
text='Deselect the game window %s' % second)
if second != 0:
time.sleep(1)
region = []
for second in reversed(range(4)):
self.screenshot_label.config(
text='Place the mouse at the top left\nof the game\'s title bar %s' % second)
if second != 0:
time.sleep(1)
constant_top_left = pyautogui.position()
region.extend(constant_top_left)
for second in reversed(range(4)):
self.screenshot_label.config(
text='Place the mouse at the bottom right\nof the game\'s title bar %s' % second)
if second != 0:
time.sleep(1)
constant_bottom_right = pyautogui.position()
region.extend(
(constant_bottom_right[0] - constant_top_left[0],
constant_bottom_right[1] - constant_top_left[1])
)
self.deselected_screenshot = pyautogui.screenshot(region=region)
pyautogui.click()
self.selected_screenshot = pyautogui.screenshot(region=region)
for second in reversed(range(4)):
self.screenshot_label.config(
text='Place mouse at the top left\nof the entire game window %s' % second)
if second != 0:
time.sleep(1)
top_left = pyautogui.position()
for second in reversed(range(4)):
self.screenshot_label.config(
text='Place mouse at the bottom right\nof the entire game window %s' % second)
if second != 0:
time.sleep(1)
bottom_right = pyautogui.position()
self.screen_size = [
constant_top_left[0] - top_left[0],
constant_top_left[1] - top_left[1],
bottom_right[0] - constant_bottom_right[0],
bottom_right[1] - constant_bottom_right[1]
]
self.screenshot_taken = True
self.screenshot_label.config(text='Screenshot Taken')
self.screenshot_button.config(
state='normal', text='Retake Screenshot')
threading.Thread(target=func).start()
app = Flask(__name__)
socketio = SocketIO(app, async_mode='eventlet')
gui_server = PlayAsOne()
user_count = 0
users = {}
democracy = []
@app.route("/")
def hello():
print("User entered")
return render_template('index.html')
@socketio.on('connect', namespace="/")
def test_connect():
print('test ran')
@socketio.on("add user", namespace="/")
def handle_add_user(username):
global user_count
global gui_server
user_count += 1
user = username
print user
users[user] = {'input_count': 0}
emit("initialize", {
'input_type': gui_server.get_input_mode(), 'mode': gui_server.get_mode()})
def handle_chaos(user_input):
gui_server.send_key(user_input)
def handle_democracy(user_input):
global democracy
for demo_input in democracy:
if demo_input[0] == user_input:
print(demo_input)
demo_input[1] += 1
break
democracy.append([user_input, 1])
def execute_democracy():
global democracy
most_votes = ["", 0]
for user_input in democracy:
if user_input[1] > most_votes[1]:
most_votes = user_input
gui_server.send_key(most_votes[0])
democracy = []
@socketio.on('on disconnect', namespace="/")
def handle_disconnect(json):
global user_count
user_count -= 1
user = loads(json)
users.pop(user['username'])
@socketio.on("sendInput", namespace="/")
def handle_input(json):
user_input = json["user_input"]
print(user_input)
if user_input not in users:
users[user_input] = {'input_count': 0}
users[user_input]['input_count'] += 1
if (gui_server.get_mode() == 'Chaos'):
handle_chaos(user_input)
elif gui_server.get_mode() == 'Democracy':
handle_democracy(user_input)
gui_server.gui.mainloop()