-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgetManager.py
275 lines (232 loc) · 8.85 KB
/
widgetManager.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
import os;
import inspect
from importlib import import_module;
numbers = {
'one': '1',
'two': '2',
'to': '2',
'too': '2',
'three': '3',
'for' : "4",
'four': '4',
'five': '5',
'six': '6',
'seven': '7',
'eight': '8',
'nine': '9',
'ten': '10',
'zero' : '0',
'no': '0'
}
class WidgetManager():
def __init__(self):
self.widgetClass = {
#Fills up upon starting
#Key: Description words
#Value: Reference to class
}
self.widgetName = {
#Fills up upon starting
#Key: Widget Name
#Value: Object
}
self.widgetObject = {
#Fills up upon starting
#Key: Description words
#Value: Object
}
for file in os.listdir(f"bombWidgets"):
if file.endswith(".py") and file != '__init__.py':
widgetName = file.split(".py")[0]
widget = import_module(f"bombWidgets.{widgetName}")
widgetClasses = inspect.getmembers(widget, inspect.isclass)
widgetClasses = [x for x in widgetClasses if x[0] != 'Module']
descriptionWords = getattr(widget, 'descriptionWords')
widgetInternalName = getattr(widget, 'widgetName')
newObject = widgetClasses[0][1]()
for description in descriptionWords:
self.widgetClass[description] = widgetClasses[0][1]
self.widgetObject[description] = newObject
self.widgetName[widgetInternalName] = newObject
self.numberOfBatteries = 0
self.numberOfStrikes = 0
self.serialNumber = ""
self.labels = {
'snd': -1,
'clr': -1,
'car': -1,
'ind': -1,
'frq': -1,
'sig': -1,
'nsa': -1,
'msa': -1,
'trn': -1,
'bob': -1,
'frk': -1
}
def checkKeys(self, rawInput):
for key in self.widgetObject:
if key in rawInput:
for number in numbers:
if number in rawInput:
self.widgetObject[key].setValue(int(numbers[number]))
speechOutput = f"There are {self.widgetObject[key].getValue()} {self.widgetObject[key].getWidgetName()}."
self.numberOfBatteries = 0
for widget in self.widgetObject:
if "battery" in self.widgetObject[widget].getTags():
self.numberOfBatteries += self.widgetObject[widget].getValue()
return [0, speechOutput]
speechOutput = f"Invalid input, please try again."
return [-1, speechOutput]
if "serial" in rawInput or "cereal" in rawInput:
return self.setSerial(rawInput)
elif "battery" in rawInput or "batteries" in rawInput:
return self.setBatteries(rawInput)
elif "label" in rawInput or 'indicator' in rawInput:
return self.setLabels(rawInput)
elif "strike" in rawInput:
return self.setStrikes(rawInput)
return [-2, ""]
def setSerial(self, input):
tempSerial = ""
speechOutput = ""
modifiedInput = input.split(' is ')[-1]
modifiedInput = modifiedInput.split()
if len(modifiedInput) != 6:
speechOutput = "Invalid input, please try again."
return [-1, speechOutput]
for word in modifiedInput:
if word in numbers:
tempSerial += numbers[word] + " "
else:
tempSerial += word[0] + " "
self.serialNumber = tempSerial[0:-1]
speechOutput = f"Serial Number is {tempSerial}"
return [0, speechOutput]
def setBatteries(self, input):
speechOutput = ""
modifiedInput = input.split()
for word in modifiedInput:
if word in numbers:
self.numberOfBatteries = int(numbers[word])
speechOutput = f"There are {self.numberOfBatteries} batteries."
return [0, speechOutput]
speechOutput = f"Invalid input, please try again."
return [-1, speechOutput]
def setStrikes(self, input):
speechOutput = ""
modifiedInput = input.split()
for word in modifiedInput:
if word in numbers:
self.numberOfStrikes = int(numbers[word])
speechOutput = f"There are {self.numberOfStrikes} strikes."
return [0, speechOutput]
speechOutput = f"Invalid input, please try again."
return [-1, speechOutput]
def setLabels(self, input):
speechOutput = ''
labelAdded = ''
labelLight = ''
if 'off' in input or 'zero' in input or 'no light' in input or 'unlit' in input:
if 'f r k' in input or 'f rk' in input:
self.labels['frk'] = 0
labelAdded = 'f r k'
labelLight = 'off'
elif 'car' in input or 'c a r' in input:
self.labels['car'] = 0
labelAdded = 'c a r'
labelLight = 'off'
elif 's n d' in input or 's an d' in input:
self.labels['snd'] = 0
labelAdded = 's n d'
labelLight = 'off'
elif 'c l r' in input:
self.labels['clr'] = 0
labelAdded = 'c l r'
labelLight = 'off'
elif 'i n d' in input or 'i an d' in input:
self.labels['ind'] = 0
labelAdded = 'i an d'
labelLight = 'off'
elif 'f r q' in input:
self.labels['frq'] = 0
labelAdded = 'f r q'
labelLight = 'off'
elif 'n s a' in input or 'an s a' in input:
self.labels['nsa'] = 0
labelAdded = 'n s a'
labelLight = 'off'
elif 'm s a' in input:
self.labels['msa'] = 0
labelAdded = 'm s a'
labelLight = 'off'
elif 't r n' in input or 't r an' in input:
self.labels['trn'] = 0
labelAdded = 't r n'
labelLight = 'off'
elif 'b o b' in input:
self.labels['bob'] = 0
labelAdded = 'b o b'
labelLight = 'off'
else:
speechOutput = f"Invalid input, please try again."
return [-1, speechOutput]
elif 'on' in input or 'one' in input or 'light' in input or 'lit' in input:
if 'f r k' in input:
self.labels['frk'] = 1
labelAdded = 'f r k'
labelLight = 'on'
elif 'car' in input or 'c a r' in input:
self.labels['car'] = 1
labelAdded = 'c a r'
labelLight = 'on'
elif 's n d' in input or 's an d' in input:
self.labels['snd'] = 1
labelAdded = 's n d'
labelLight = 'on'
elif 'c l r' in input:
self.labels['clr'] = 1
labelAdded = 'c l r'
labelLight = 'on'
elif 'i n d' in input or 'i an d' in input:
self.labels['ind'] = 1
labelAdded = 'i an d'
labelLight = 'on'
elif 'f r q' in input:
self.labels['frq'] = 1
labelAdded = 'f r q'
labelLight = 'on'
elif 'n s a' in input or 'an s a' in input:
self.labels['nsa'] = 1
labelAdded = 'n s a'
labelLight = 'on'
elif 'm s a' in input:
self.labels['msa'] = 1
labelAdded = 'm s a'
labelLight = 'on'
elif 't r n' in input or 't r an' in input:
self.labels['trn'] = 1
labelAdded = 't r n'
labelLight = 'on'
elif 'b o b' in input:
self.labels['bob'] = 1
labelAdded = 'b o b'
labelLight = 'on'
else:
speechOutput = f"Invalid input, please try again."
return [-1, speechOutput]
else:
speechOutput = f"Invalid input, please try again."
return [-1, speechOutput]
speechOutput = f"Label {labelAdded} is {labelLight}."
return [0, speechOutput]
def getSerial(self):
return self.serialNumber
def getBatteries(self):
return self.numberOfBatteries
def getStrikes(self):
return self.numberOfStrikes
def getLabels(self):
return self.labels
def getWidget(self, widgetNameInput):
return self.widgetName[widgetNameInput].getValue()