-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsi-dota.py
164 lines (143 loc) · 5.33 KB
/
gsi-dota.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
from http.server import BaseHTTPRequestHandler, HTTPServer
from yeelight import *
import sys
import time
import json
import configparser
critical_flash = [
RGBTransition(255, 0, 0, duration=200, brightness=100),
RGBTransition(255, 153, 0, duration=200, brightness=100),
]
warning_flash = [
RGBTransition(255, 0, 0, duration=200, brightness=100),
RGBTransition(0, 0, 255, duration=200, brightness=100),
]
bulbs = []
class MyServer(HTTPServer):
# prepare checked items for http server
def init_state(self):
self.health = None
self.silenced = None
self.stunned = None
self.hexed = None
class MyRequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
# receive payload from game
length = int(self.headers['Content-Length'])
body = self.rfile.read(length).decode('utf-8')
self.parse_payload(json.loads(body))
self.send_header('Content-type', 'text/html')
self.send_response(200)
self.end_headers()
def parse_payload(self, payload):
health = self.get_health(payload)
silenced = self.get_silenced(payload)
stunned = self.get_stunned(payload)
hexed = self.get_hexed(payload)
# compare and check if payload changed
if health != self.server.health:
self.server.health = health
print('health state changed to %s' % health)
hp_g = int(round(health / 100 * 255))
hp_r = int(round((255 * (1 - health / 100))))
change_light(hp_r, hp_g, 0)
if warn_low is True:
if health <= 15:
print('Critical Health!')
flash()
time.sleep(0.8)
def get_health(self, payload):
if 'hero' in payload and 'health_percent' in payload['hero']:
return payload['hero']['health_percent']
else:
return None
def get_silenced(self, payload):
if warn_slcd is True:
if 'hero' in payload and 'silenced' in payload['hero']:
if payload['hero']['silenced'] == True:
warn()
print("Your hero is silenced!")
time.sleep(0.8)
return payload['hero']['silenced']
else:
return None
def get_stunned(self, payload):
if warn_stun is True:
if 'hero' in payload and 'stunned' in payload['hero']:
if payload['hero']['stunned'] == True:
warn()
print("Your hero is stunned!")
time.sleep(0.8)
return payload['hero']['stunned']
else:
return None
def get_hexed(self, payload):
if warn_hex is True:
if 'hero' in payload and 'hexed' in payload['hero']:
if payload['hero']['hexed'] == True:
warn()
print("Your hero is hexed!")
time.sleep(0.8)
return payload['hero']['hexed']
else:
return None
def log_message(self, format, *args):
return
def flash():
# let the bulbs blink 3 times with critical_flash colors
for bulbn in bulbs:
if bulbn != '':
bulb = Bulb(bulbn)
bulb.start_flow(Flow(3, Flow.actions.recover, critical_flash))
def warn():
# let the bulbs blink 3 times with warning_flash colors
for bulbn in bulbs:
if bulbn != '':
bulb = Bulb(bulbn)
bulb.start_flow(Flow(3, Flow.actions.recover, warning_flash))
def change_light(r, g, b):
# loop through bulbs with new rgb value
for bulbn in bulbs:
if bulbn != '':
bulb = Bulb(bulbn)
bulb.set_rgb(r, g, b)
def main():
print('Welcome to yeelight-valve-gsi by davidramiro')
print('Reading config...')
config = configparser.ConfigParser()
config.read('config.ini')
# let the following variables be callable outside main()
global warn_low, warn_stun, warn_slcd, warn_hex
warn_low = config.getboolean('dota settings', 'low hp warning')
warn_stun = config.getboolean('dota settings', 'stunned warning')
warn_slcd = config.getboolean('dota settings', 'silenced warning')
warn_hex = config.getboolean('dota settings', 'hexed warning')
bulb_count = int(config.get('general', 'Lamp Count'))
for n in range(1, (bulb_count + 1)):
bulbs.append(config.get(str(n), 'ip'))
print('Initializing...')
for bulbn in bulbs:
if bulbn != '':
print('Initializing Yeelight at %s' % bulbn)
bulb = Bulb(bulbn)
bulb.turn_on()
# turn on music mode on the yeelights for better latency
bulb.start_music()
bulb.set_rgb(0, 0, 255)
bulb.set_brightness(100)
# start up the listening server
server = MyServer(('localhost', 3000), MyRequestHandler)
server.init_state()
print(time.asctime(), '-', 'yeelight-valve-gsi is running - CTRL+C to stop')
try:
server.serve_forever()
except (KeyboardInterrupt, SystemExit):
pass
server.server_close()
# turn off music mode
for bulbn in bulbs:
if bulbn != '':
bulb = Bulb(bulbn)
bulb.stop_music
print(time.asctime(), '-', 'Listener stopped. Thanks for using yeelight-valve-gsi!')
main()