-
Notifications
You must be signed in to change notification settings - Fork 3
/
twitch-baduk.py
141 lines (125 loc) · 4.93 KB
/
twitch-baduk.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
#!/usr/bin/env python
import keyboard # List of keyboard keys at https://github.com/boppreh/keyboard/blob/master/keyboard/_canonical_names.py
from subprocess import call
import time
import os
from threading import Thread
from twitch_bot import getTwitchBot
from go_game import Game
from util import trace, settings
class ProgramManager:
def __init__(self):
self.parseKeys()
self.communicationActive = True
self.usingKeyboard = False
self.gameState = Game()
self.useSabaki = settings["use_sabaki"]
self.twitchBot = getTwitchBot(self.gameState)
self.twitchBot.start()
if self.useSabaki:
from game_capture import getScreenshotDaemon
from sabaki_com import startSabakiCommunication
self.comThread = startSabakiCommunication()
self.comThread.start()
self.daemonThread = getScreenshotDaemon(self.gameState, self.twitchBot)
self.daemonThread.start()
th = Thread(target=self.keyboardHookThread)
th.start()
def keyboardHookThread(self):
""" The keyboard hook seems to die sometimes, so we recreate it periodically """
while self.twitchBot is not None:
while self.usingKeyboard:
time.sleep(0.1)
self.resetKeyboardHook()
time.sleep(1)
def resetKeyboardHook(self):
try:
keyboard.unhook(self.keyCheck)
except:
pass
keyboard.hook(self.keyCheck)
def parseKeys(self):
self.ctrl = False
self.shift = False
self.alt = False
self.keyMap = {}
for method, key in settings["keys"].iteritems():
modMask = 4 if "ctrl" in key else 0
modMask = modMask | 2 if "shift" in key else modMask
modMask = modMask | 2 if "alt" in key else modMask
if modMask != 0:
key = key.rsplit("+", 1)[1]
self.keyMap[key] = (getattr(self, method), modMask)
def modKeyCheck(self, modMask):
if modMask == 0:
return True
ctrl = False if modMask & 4 and not self.ctrl else True
shift = False if modMask & 2 and not self.shift else True
alt = False if modMask & 1 and not self.alt else True
return ctrl and shift and alt
def keyCheck(self, kbEvent):
self.usingKeyboard = True
if kbEvent.event_type == keyboard.KEY_DOWN:
if kbEvent.name in ("ctrl", "left ctrl", "right ctrl"):
self.ctrl = True
trace("ctrl pressed", 3)
elif kbEvent.name in ("shift", "left shift", "right shift"):
self.shift = True
trace("shift pressed", 3)
elif kbEvent.name in ("alt", "left alt", "alt gr"):
self.alt = True
trace("alt pressed", 3)
else:
trace("Received keyboard event %s - %s" % (kbEvent.name, kbEvent.scan_code), 3)
try:
method, modifiers = self.keyMap[kbEvent.name]
if self.modKeyCheck(modifiers):
method()
except KeyError:
pass
else:
if kbEvent.name in ("ctrl", "left ctrl", "right ctrl"):
self.ctrl = False
trace("ctrl released", 3)
elif kbEvent.name in ("shift", "left shift", "right shift"):
self.shift = False
trace("shift released", 3)
elif kbEvent.name in ("alt", "left alt", "alt gr"):
self.alt = False
trace("alt released", 3)
self.usingKeyboard = False
def endProgram(self):
trace("Ending program", 0)
self.twitchBot.stop()
del self.twitchBot
self.twitchBot = None
if self.useSabaki:
self.comThread.closeSabaki()
self.daemonThread.stop()
del self.daemonThread
self.daemonThread = None
self.comThread.stop()
del self.comThread
self.comThread = None
def toggleCommunication(self):
if not self.useSabaki:
return
if self.communicationActive:
trace("Pausing updates to sabaki", 0)
self.comThread.pauseComms()
self.communicationActive = False
else:
trace("Resuming updates to sabaki", 0)
self.comThread.resumeComms()
self.communicationActive = True
if __name__ == "__main__":
mgr = ProgramManager()
if mgr.useSabaki:
time.sleep(1)
sabakiDir = os.path.join(os.getcwd(), "Sabaki-master")
electronPath = os.path.join("node_modules", "electron", "dist", "electron.exe")
os.system("cd %s && %s ./" % (sabakiDir, electronPath) )
else:
mgr.twitchBot.join()
keyboard.unhook_all()
del mgr