-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn-nato-radio-alphabet.py
executable file
·242 lines (201 loc) · 6.77 KB
/
learn-nato-radio-alphabet.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# cLx 2020 - http://clx.freeshell.org/
# I never managed to learn radio "NATO" alphabet, so i made this... still a WIP !
import sys, os, fnmatch, random, time
try:
# sudo apt-get install python3-pyqt5
#raise("We want to use PyQt4")
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
print("Using PyQt5")
except:
# sudo apt-get install python-qtpy python3-qtpy
from PyQt4.QtGui import *
from PyQt4.QtCore import *
print("Using PyQt4")
class Game():
words = {'a':("Alfa", "Alpha"), 'b':"Bravo", 'c':("Charlie", "Charly"), 'd':"Delta", 'e':"Echo", 'f':("Foxtrot", "Fox-trot"), 'g':"Golf", 'h':"Hotel", 'i':"India",
'j':("Juliett", "Juliet"), 'k':"Kilo", 'l':"Lima", 'm':"Mike", 'n':"November", 'o':"Oscar", 'p':"Papa", 'q':"Quebec", 'r':"Romeo",
's':"Sierra", 't':"Tango", 'u':"Uniform", 'v':"Victor", 'w':("Whiskey", "Whisky"), 'x':"Xray", 'y':"Yankee", 'z':("Zulu", "Zoulou")}
grantedTimeByLevels = [10, 5, 3, 2]
def __init__(self):
self.initial = ""
self.inputstring = ""
self.started = False
self.time_max = float("nan")
self.time_start = float("nan")
self.time_remaining = float("nan")
self.level = 0
def start(self):
self.guessList = list(self.words.keys())
random.shuffle(self.guessList)
self.newWord()
def newWord(self):
if len(self.guessList) == 0:
return False
self.initial = self.guessList[0].upper(); del self.guessList[0]
self.validWords = self.words[self.initial.lower()]
self.inputstring = ""
self.time_max = self.grantedTimeByLevels[self.level]
self.time_remaining = self.time_max
self.time_start = time.time()
self.started = True
return True
def getSolution(self, failed=False):
self.time_max = float("nan")
self.time_start = float("nan")
self.time_remaining = float("nan")
self.started = False
if failed:
self.guessList.insert(3, self.initial)
if len(self.guessList[-1]) > 1 and self.guessList[-1] != self.initial:
self.guessList.insert(-1, self.initial)
if type(self.validWords) is str:
return self.validWords
return self.validWords[0]
def getRemainingTime(self):
self.time_remaining = self.time_start + self.time_max - time.time()
return self.time_remaining, self.time_max
def addCharacter(self, character):
if self.inputstring == "":
if character != self.initial:
self.inputstring += self.initial
self.inputstring += character
def delCharacter(self):
self.inputstring = self.inputstring[:-1]
def checkValidity(self):
if type(self.validWords) is str:
s = [self.validWords]
else:
s = self.validWords
for w in s:
if self.inputstring.lower() == w.lower():
self.started = False
return True
return False
def getWhatToDisplay(self):
if self.inputstring != "":
return self.inputstring
return self.initial
class GUI(QWidget):
def __init__(self):
super(GUI, self).__init__()
self.game = Game()
self.initUI()
def initUI(self):
self.setStyleSheet("\
QWidget { background-color: #000000; color: #ffffff; } \
QLabel { margin: 0px; padding: 0px; } \
QSplitter::handle:vertical { image: none; } \
QSplitter::handle:horizontal { width: 2px; image: none; } \
QPushButton { background-color: #404040; background: #404040; } \
QLabel#label { font-size: 50pt; } \
");
layout = QVBoxLayout(self)
def mkQLabel(objectName, text='', alignment=Qt.AlignLeft):
o = QLabel()
o.setObjectName(objectName)
o.setAlignment(alignment)
o.setText(text)
return o
def mkButton(text, function):
btn = QPushButton(text)
btn.setFocusPolicy(Qt.TabFocus)
if function:
btn.clicked.connect(function)
return btn
self.text = mkQLabel('label', "Press Start", Qt.AlignCenter | Qt.AlignTop)
self.text.setMinimumSize(500, 100)
layout.addWidget(self.text)
self.progressbar = QProgressBar()
self.progressbar.setOrientation(Qt.Horizontal)
self.progressbar.setValue(0)
self.progressbar.setMaximum(100)
self.progressbar.setTextVisible(False)
self.progressbar.setVisible(False)
layout.addWidget(self.progressbar)
self.pushButton = mkButton("Start", self.pushButtonPressed)
layout.addWidget(self.pushButton)
self.refreshTimer = QTimer()
self.refreshTimer.timeout.connect(self.refreshTimerTimeout)
self.startGameTimer = QTimer()
self.startGameTimer.timeout.connect(self.startGameTimerTimeout)
self.pauseTimer = QTimer()
self.pauseTimer.timeout.connect(self.pauseTimerTimeout)
self.setWindowTitle(u"Radio Alphabet")
self.show()
def pushButtonPressed(self):
self.text.setText("LEVEL %d" % (self.game.level+1))
self.pushButton.setDisabled(True)
self.pushButton.setVisible(False)
self.progressbar.setVisible(True)
self.startGameTimer.start(1000)
def startGameTimerTimeout(self):
self.startGameTimer.stop()
self.game.start()
self.text.setText(self.game.getWhatToDisplay())
self.refreshTimer.start(100)
self.blinkingState = False
def pauseTimerTimeout(self):
self.pauseTimer.stop()
self.progressbar.setValue(0)
if self.game.newWord():
self.text.setText(self.game.getWhatToDisplay())
self.refreshTimer.start(100)
self.blinkingState = False
else:
# next level ?
if self.game.level+1 < len(self.game.grantedTimeByLevels):
self.game.level+=1
self.pushButtonPressed()
else: # no more level
self.text.setText("Finished!")
self.pushButton.setDisabled(False)
self.pushButton.setVisible(True)
self.progressbar.setVisible(False)
self.text.setStyleSheet("");
def refreshTimerTimeout(self):
remainingTime, maxTime = self.game.getRemainingTime()
a = int(maxTime*10)
b = int((maxTime-remainingTime)*10)
if a != self.progressbar.maximum():
self.progressbar.setMaximum(a)
self.progressbar.setValue(b)
self.progressbar.repaint()
if remainingTime <= 0:
self.refreshTimer.stop()
self.text.setText(self.game.getSolution(failed=True))
self.pauseTimer.start(2000)
self.text.setStyleSheet("QLabel { color: red; }");
elif remainingTime < 1:
if self.blinkingState:
self.text.setStyleSheet("QLabel { background-color: red; color: black; }");
else:
self.text.setStyleSheet("QLabel { color: red; }");
self.blinkingState = not self.blinkingState
def keyPressEvent(self, event):
if self.game.started:
key = event.key()
if key == Qt.Key_Backspace:
self.game.delCharacter()
elif key >= Qt.Key_A and key <= Qt.Key_Z:
self.game.addCharacter(chr(key))
if self.game.checkValidity():
self.refreshTimer.stop()
self.text.setText(self.game.getSolution())
self.pauseTimer.start(2000)
self.text.setStyleSheet("QLabel { color: green; }");
else:
return
self.text.setText(self.game.getWhatToDisplay())
def main():
global m1
app = QApplication(sys.argv)
m1 = GUI()
app.installEventFilter(m1)
ret = app.exec_()
sys.exit(ret)
if __name__ == '__main__':
main()