-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
159 lines (117 loc) · 5.03 KB
/
main.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
from gevent import monkey, sleep; monkey.patch_all()
from time import time
from capture import screenshot_image, get_windows_bytitle, Screenshot
from ocr import get_stats
from server import Server
from window import Gui
from threading import Thread, Event
def main():
broken = 0
kill_event = Event()
socket_server = Server()
socket_server.start()
gui = Gui('League Stats', kill_event)
gui.start()
gui.set_status_server(True)
# =========================================
# Main Loop
# =========================================
game_open = False
game_started = False
last_results = None
kill_spree = (0,0)
while not kill_event.is_set():
sleep(0.3)
windows = get_windows_bytitle('League of Legends')
# =========================================
# Found Window
# =========================================
if windows:
if not game_open:
game_open = True
socket_server.send({ 'event': 'game_loading' })
gui.set_status_game(True)
window = windows[0]
# =========================================
# Screenshot
# =========================================
try:
screenshot = Screenshot(screenshot_image(window))
except:
#TODO: save screenshot
gui.set_status_stats(False)
gui.set_status_game(False)
gui.set_stats({})
continue
# =========================================
# Stats
# =========================================
try:
screenshot.image.save('testu.bmp')
results = get_stats(screenshot)
except:
#TODO: save screenshot
results = None
if results is None:
gui.set_status_stats(False)
gui.set_stats({})
continue
# =========================================
# Stats Exist!
# =========================================
if not game_started:
game_started = True
socket_server.send({ 'event': 'game_started' })
gui.set_status_stats(True)
gui.set_stats(results)
if results['team_kills'] > 200 or results['team_deaths'] > 200 or results['kills'] > 200:
broken += 1
screenshot.image.save('broken{}.bmp'.format(broken))
# =========================================
# Events
# =========================================
if last_results and results:
diff_cs = results['CS'] - last_results['CS']
diff_kills = results['kills'] - last_results['kills']
diff_deaths = results['deaths'] - last_results['deaths']
diff_team_kills = results['team_kills'] - last_results['team_kills']
diff_team_deaths = results['team_deaths'] - last_results['team_deaths']
if diff_team_kills < 0 or diff_team_deaths < 0 or diff_kills < 0:
broken += 1
screenshot.image.save('broken_before_{}.bmp'.format(broken))
last_screenshot.image.save('broken_after_{}.bmp'.format(broken))
if diff_cs > 0:
socket_server.send({ 'event': 'cs', 'amount': diff_cs, 'total': results['CS'] })
if diff_kills:
time_now = time()
time_since_last = time_now - kill_spree[0]
if time_since_last <= 10.5:
kill_spree = (time_now, kill_spree[1]+diff_kills)
else:
kill_spree = (time_now, 1)
kills = kill_spree[1]
socket_server.send({ 'event': 'kill', 'total': results['kills'], 'streak': kills })
elif diff_team_kills:
socket_server.send({ 'event': 'team_kill', 'amount': diff_team_kills, 'total': results['team_kills'] })
if diff_deaths:
socket_server.send({ 'event': 'death', 'total': results['deaths'] })
elif diff_team_deaths:
socket_server.send({ 'event': 'team_death', 'amount': diff_team_deaths, 'total': results['team_deaths'] })
last_results = results
last_screenshot = screenshot
# =========================================
# Missing Window
# =========================================
else:
if game_started:
game_started = False
last_results = None
socket_server.send({ 'event': 'game_finished' })
if game_open:
game_open = False
gui.set_status_game(False)
gui.set_stats({})
gui.set_status_clients(len(socket_server.clients()))
socket_server.stop()
if __name__ == '__main__':
main()