-
Notifications
You must be signed in to change notification settings - Fork 2
/
f3termCurses.py
593 lines (570 loc) · 22.2 KB
/
f3termCurses.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# -*- coding: utf-8 -*-
import curses
import random
import time
import sqlite3
import string
import threading
import pygame.mixer
db_parameters = dict()
forceClose = False
is_db_updating = False
db_updated = False
dbCheckInterval = 2
delayTime = 50
start_time = time.time()
def millis():
return (time.time() - start_time) * 1000.0
def load_str():
with open ('tststr.txt', 'r') as fh:
fullstr = fh.read()
return fullstr
def initCurses():
curses.initscr()
curses.start_color()
curses.init_pair(1, 10, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_BLACK, 10)
curses.noecho()
curses.noraw()
curses.curs_set(2)
def readDBParameters(checkInterval=2):
global db_parameters
global is_db_updating
global forceClose
while True:
if forceClose:
break
if not is_db_updating:
is_db_updating = True
conn = sqlite3.connect(r'ft.db')
req = conn.cursor()
req.execute('SELECT name,value FROM params')
params = req.fetchall()
conn.close()
for data in params:
if data[0] == "msgBody":
val = data[1].split("\n")
else:
if data[1].isdigit():
val = int(data[1])
else:
if data[1].upper() == "YES":
val = True
elif data[1].upper() == "NO":
val = False
else:
val = data[1]
db_parameters.update({data[0]:val})
is_db_updating = False
time.sleep(checkInterval)
def updateDBParameters(parameters):
# Принимает словарь, где ключ - поле в базе, значение ключа - значение, которое нужно записать в базу.
global is_db_updating
while is_db_updating:
pass
try:
is_db_updating = True
conn = sqlite3.connect('ft.db')
req = conn.cursor()
print(parameters)
for par in parameters.keys():
req.execute("UPDATE params SET value = '{1}' WHERE name='{0}'".format(par,parameters[par]))
conn.commit()
conn.close()
except Exception as err:
print(err)
finally:
is_db_updating = False
def loadWords(wordLen): # Считывание словаря, отладочная функция.
words = []
with open('words'+str(wordLen)+'.txt','r') as f:
for word in f:
words.append(word.strip("\n\t "))
count = len(words)
return words, count
def getStrPos(x, y):
if x<32:
yNew = y
xNew = x-8
else:
yNew = y+17
xNew = x-32
return (yNew*12+xNew)
def getStrCoords(strPos):
if strPos<204:
y = int(strPos / 12)
x = strPos%12 + 8
else:
y = int(strPos / 12) - 17
x = strPos%12 + 32
return (x, y)
def checkWordPosition(charIndex, wordStr): # Символ проверим на всякий случай
if not wordStr[charIndex].isalpha():
return ('', -1, -1)
i = charIndex
while wordStr[i].isalpha():
if i == 0:
i = -1
break
i -= 1
startPos = i + 1
i = charIndex
while wordStr[i].isalpha():
if i == len(wordStr)-1:
i = len(wordStr)
break
i += 1
endPos = i - 1
selWord = wordStr[startPos:endPos+1]
return (selWord, startPos, endPos)
def checkCheatPosition(charIndex, wordStr):
leftPar = ['[', '(', '{', '<']
rightPar = [']', ')', '}', '>']
direct = 0
startPos = -1
endPos = -1
if wordStr[charIndex] in leftPar:
direct = 1
startPos = charIndex
controlChar = rightPar[leftPar.index(wordStr[charIndex])]
if wordStr[charIndex] in rightPar:
direct = -1
endPos = charIndex - 1
controlChar = leftPar[rightPar.index(wordStr[charIndex])]
if direct == 0:
return('', -1, -1)
i = charIndex + direct
if i > (len(wordStr)-1) or i < 0:
return('', -1, -1)
startSubStr = int(charIndex/12)*12
endSubStr = startSubStr + 11
i = charIndex
while wordStr[i] != controlChar:
if wordStr[i].isalpha():
return ('', -1, -1)
i += direct
if i <= startSubStr or i > endSubStr:
return ('', -1, -1)
if startPos == -1:
startPos = i
if endPos == -1:
endPos = i - 1
cheatStr = wordStr[startPos:endPos+2]
return(cheatStr, startPos, endPos)
def delFromStr(allStr, startPos, endPos):
newStr = allStr[0:startPos] + '.'*(endPos-startPos) + allStr[endPos:]
return (newStr)
def genString(wordQuan, strLen, dictionary):
# Функция формирует строку для вывода в терминал. Строка представляет собой 'мусорные' символы,
# между которыми вставлены слова для подбора пароля.
password = dictionary[random.randint(0, len(dictionary)-1)]
wordLen = len(dictionary[0])
wordList = wordsSelect(dictionary, password, wordQuan)
screenStr = ""
lenArea = int(strLen / wordQuan)
i = 0
while i < wordQuan:
startPos = random.randint(i * lenArea, i * lenArea + (lenArea - wordLen - 1) )
j = i * lenArea
while j < startPos:
screenStr += random.choice(string.punctuation)
j += 1
screenStr += wordList[i]
screenStr += random.choice(string.punctuation)
j += wordLen + 1
while j < (i + 1) * lenArea:
screenStr += random.choice(string.punctuation)
j += 1
i += 1
i = len(screenStr)
while i < strLen:
screenStr += random.choice(string.punctuation)
i += 1
wordList.remove(password)
return password, wordList, screenStr
def compareWords(fWord, sWord):
i = 0
count = 0
for char in fWord:
if char == sWord[i]:
count += 1
i += 1
return count
def wordsSelect(words, pwd, wordQuan):
wordLen = len(pwd)
wordListMax = [] # Слова, максимально похожие по расположению букв на слово-пароль
wordListZero = [] # Слова, совершенно не имеющие одинаково расположенных букв с паролем
wordListOther = [] # Все прочие слова из списка
wordListSelected = [] # Слова, которые будут использоваться непосредственно в игре
wordDelta = 2
while len(wordListMax) == 0:
i = 0
for word in words:
if word != pwd:
c = compareWords(word, pwd)
if c == 0:
wordListZero.append(word)
elif c == (wordLen - 1):
wordListMax.append(word)
elif c == (wordLen - wordDelta):
wordListMax.append(word)
else:
wordListOther.append(word)
wordDelta += 1
wordListSelected.append(pwd) # Пароль
if len(wordListMax) > 0: # Одно слово, максимально близкое к паролю
wordListSelected.append(wordListMax[random.randint(0, len(wordListMax) - 1)])
if len(wordListZero) > 0: #Одно слово, которое совершенно не похоже на пароль
wordListSelected.append(wordListZero[random.randint(0, len(wordListZero) - 1)])
i = 0
while i < wordQuan - 3: # Добавляем ещё слов из общего списка
word = wordListOther[random.randint(0, len(wordListOther) - 1)]
if word not in wordListSelected:
wordListSelected.append(word)
i += 1
random.shuffle(wordListSelected) #Перемешиваем.
return wordListSelected
def delRandomWord(wordList, allStr):
wordNum = random.randint(0, len(wordList) - 1)
# print (wordNum)
word = wordList[wordNum]
startPos = allStr.index(word)
wordList.remove(word)
allStr = allStr.replace(word, '.'*len(word))
return (startPos, wordList, allStr)
def outScreen(parName, delayAfter=2):
global db_parameters
global delayTime
global prtSnd
fullScreenWin = curses.newwin(24, 80, 0, 0)
fullScreenWin.clear()
fullScreenWin.refresh()
fullScreenWin.nodelay(True)
myDelay = delayTime
x = 0
y = 0
for ch in db_parameters[parName]:
key = fullScreenWin.getch()
if (key == curses.KEY_ENTER or key == ord(' ')) and myDelay == delayTime:
myDelay = delayTime/4
if ch == '\n':
y+=1
x = 0
continue
if db_parameters['isSound']:
prtSnd.play(loops=0, maxtime=int(myDelay))
fullScreenWin.addch(y, x, ch, curses.color_pair(1))
time.sleep(myDelay / 1000)
fullScreenWin.refresh()
x += 1
if delayAfter > 0:
time.sleep(delayAfter)
fullScreenWin.clear()
fullScreenWin.refresh()
def hackScreen():
global db_parameters
global db_updated
global delayTime
global wrdSnd
global prtSnd
(wordDict, lenDict) = loadWords(db_parameters['wordLength']) # отладочная загрузка словаря
(pwd, wList, fullStr) = genString(db_parameters['wordsPrinted'], 408, wordDict)
# fullStr = load_str()
# pwd = 'FRIERSON'
# wList = ['INUKSHUK', 'STARLESS', 'HARTWICK', 'FRIERSON', 'DRUMMING', 'SWIMMING',
# 'DERISIVE', 'MUTATION', 'ADHERENT', 'DRUMLINE', 'WHITMORE', 'PISHOGUE',
# 'DODDERER', 'CAROLINA', 'WILLHITE', 'UNDERJAW']
auxStr = [' '*32, ' '*32, ' '*32, ' '*32, ' '*32, ' '*32, ' '*32, ' '*32, \
' '*32, ' '*32, ' '*32, ' '*32, ' '*32, ' '*32, ' '*32, ' '*32]
x = 0
y = 1
myDelay = delayTime
hackServWin = curses.newwin(7, 80, 0, 0)
hackMainWin = curses.newwin(18, 44, 7, 0)
hackCursorWin = curses.newwin(18, 3, 7, 44)
hackAuxWin = curses.newwin(17, 33, 7, 47)
hackHLWin = curses.newwin(1, 33, 23, 47)
hackServWin.clear()
hackServWin.nodelay(True)
hackMainWin.clear()
hackMainWin.nodelay(True)
triesAst = '* ' * db_parameters['attempts']
numTries = db_parameters['attempts']
for ch in db_parameters['hackHeader'].format(numTries, triesAst):
key = hackServWin.getch()
if (key == curses.KEY_ENTER or key == ord(' ')) and myDelay == delayTime:
myDelay = delayTime/4
if ch == '\n':
y+=1
x = 0
continue
if db_parameters['isSound']:
prtSnd.play(loops=0, maxtime=int(myDelay))
hackServWin.addch(y, x, ch, curses.color_pair(1))
time.sleep(myDelay / 1000)
hackServWin.refresh()
x += 1
startHex = random.randint(0x1A00, 0xFA00)
colStr = 0
while colStr<2:
y = 0
while y < 17:
x = 0
hexOut = '{0:#4X} '.format(startHex + y * 12 + colStr*204)
for ch in hexOut:
key = hackMainWin.getch()
if (key == curses.KEY_ENTER or key == ord(' ')) and myDelay == delayTime:
myDelay = delayTime / 4
if db_parameters['isSound']:
prtSnd.play(loops=0, maxtime=int(myDelay))
hackMainWin.addch(y, (colStr*24)+x, ch, curses.color_pair(1))
time.sleep(myDelay / 1000)
hackMainWin.refresh()
x += 1
i = 0
for ch in fullStr[(y+colStr*17)*12:(y+colStr*17)*12+12]:
key = hackMainWin.getch()
if (key == curses.KEY_ENTER or key == ord(' ')) and myDelay == delayTime:
myDelay = delayTime / 4
if db_parameters['isSound']:
prtSnd.play(loops=0, maxtime=int(myDelay))
hackMainWin.addch(y, (colStr*24)+x, ch, curses.color_pair(1))
time.sleep(myDelay / 1000)
hackMainWin.refresh()
x += 1
i += 10
y += 1
colStr += 1
hackCursorWin.addch(16,1,'>',curses.color_pair(1))
hackCursorWin.refresh()
x = 8
y = 0
hackMainWin.move(y, x)
hackMainWin.nodelay(False)
hackMainWin.keypad(True)
wordFlag = False
cheatFlag = False
mssTime = millis()
while True: # Основной цикл
mscTime = millis()
if (mscTime >= (mssTime + 3000)):
mssTime = mscTime
# Читаем базу
if not db_parameters["isPowerOn"] or db_parameters["isLocked"] or db_parameters["isHacked"]:
return
if db_updated:
db_updated = False
return
f = False
key = hackMainWin.getch()
if key == curses.KEY_LEFT:
f = True
if x == 8:
x = 43
elif x == 32:
x = 19
else:
x -= 1
if key == curses.KEY_RIGHT:
f = True
if x == 19:
x = 32
elif x == 43:
x = 8
else:
x += 1
if key == curses.KEY_UP:
f = True
if y == 0:
y = 16
else:
y -= 1
if key == curses.KEY_DOWN:
f = True
if y == 16:
y = 0
else:
y += 1
if key == 10: # Enter
# Выбор позиции
if wordFlag:
dWord = compareWords(selGroup, pwd)
if dWord < db_parameters['wordLength']:
auxStr.pop(0)
auxStr.append(selGroup + ' ['+str(dWord)+' OF '+str(db_parameters['wordLength'])+']')
yAux = 0
for tStr in auxStr:
hackAuxWin.addstr(yAux, 0, tStr+'\n', curses.color_pair(1))
yAux += 1
hackAuxWin.refresh()
numTries -= 1
if numTries > 0:
triesAst = '* ' * numTries
yS = 1
xS = 0
hackServWin.clear()
for ch in db_parameters['hackHeader'].format(numTries, triesAst):
if ch == '\n':
yS += 1
xS = 0
continue
hackServWin.addch(yS, xS, ch, curses.color_pair(1))
xS += 1
hackServWin.refresh()
hackMainWin.move(y, x)
else: # Блокировка
updateDBParameters({"isLocked":"YES"})
db_parameters["isLocked"] = True
time.sleep(1)
return
else: # Терминал успешно взломан
updateDBParameters({"isHacked":"YES"})
db_parameters["isHacked"] = True
return
elif cheatFlag: # Был найден чит
# print(fullStr)
fullStr = delFromStr(fullStr, startPos+1, endPos+1)
# print('======')
# print(fullStr)
(xSC, ySC) = getStrCoords(startPos+1)
i = 0
hackMainWin.addch(ySC, xSC-1, fullStr[startPos], curses.color_pair(1))
while i<len(selGroup)-1:
hackMainWin.addch(ySC, xSC + i, '.', curses.color_pair(1))
i += 1
r = random.randint(1,10)
if r > 1: # 9 из 10 случаев - удаляем слово
(dPos, wList, fullstr) = delRandomWord(wList, fullStr)
i = dPos
while i < dPos + db_parameters['wordLength']:
(dlX, dlY) = getStrCoords(i)
hackMainWin.addch(dlY, dlX, '.', curses.color_pair(1))
i += 1
auxStr.pop(0)
auxStr.append('DUMMY REMOVED')
yAux = 0
for tStr in auxStr:
hackAuxWin.addstr(yAux, 0, tStr+'\n', curses.color_pair(1))
yAux += 1
hackAuxWin.refresh()
hackMainWin.move(y, x)
else:
numTries = db_parameters['attempts']
triesAst = '* ' * numTries
yS = 1
xS = 0
hackServWin.clear()
for ch in db_parameters['hackHeader'].format(numTries, triesAst):
if ch == '\n':
yS += 1
xS = 0
continue
hackServWin.addch(yS, xS, ch, curses.color_pair(1))
xS += 1
hackServWin.refresh()
auxStr.pop(0)
auxStr.append('ATTEMPTS RESTORED')
yAux = 0
for tStr in auxStr:
hackAuxWin.addstr(yAux, 0, tStr+'\n', curses.color_pair(1))
yAux += 1
hackAuxWin.refresh()
cheatFlag = False
hackMainWin.move(y, x)
if f:
if db_parameters['isSound']:
prtSnd.play(loops=0, maxtime=int(myDelay))
if wordFlag or cheatFlag:
i = startPos
xHL = 0
while i <= endPos:
(hlX, hlY) = getStrCoords(i)
hackMainWin.addch(hlY, hlX, fullStr[i], curses.color_pair(1))
hackHLWin.addstr(0, xHL, ' ', curses.color_pair(1))
i += 1
xHL += 1
cheatFlag = False
wordFlag = False
hackMainWin.refresh()
hackHLWin.refresh()
strPos = getStrPos(x,y)
(selWGroup, startWPos, endWPos) = checkWordPosition(strPos, fullStr)
(selCGroup, startCPos, endCPos) = checkCheatPosition(strPos, fullStr)
# print(key, strPos, selWGroup, selCGroup)
if startWPos >= 0:
wordFlag = True
cheatFlag = False
startPos = startWPos
endPos = endWPos
selGroup = selWGroup
if startCPos >= 0:
cheatFlag = True
wordFlag = False
startPos = startCPos
endPos = endCPos + 1
selGroup = selCGroup
if wordFlag or cheatFlag:
if db_parameters['isSound']:
prtSnd.stop()
wrdSnd.play(loops=0)
i = startPos
while i <= endPos:
(hlX, hlY) = getStrCoords(i)
hackMainWin.addch(hlY, hlX, fullStr[i], curses.color_pair(2))
i += 1
hackHLWin.addstr(0, 0, selGroup, curses.color_pair(1))
hackMainWin.refresh()
hackHLWin.refresh()
hackMainWin.move(y, x)
def startTerminal():
# Основной игровой цикл.
global db_parameters
global db_updated
global forceClose
global prtSnd
global wrdSnd
# Предыдущее состояние терминала. Если не совпадает с текущим - будет выполнена очистка и перерисовка экрана.
# Unpowerd - нет питания. Locked - заблокирован. Hacked - взломан. Normal - запитан, ждет взлома.
previous_state = ""
initCurses()
if db_parameters['isSound']:
pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.mixer.init()
prtSnd = pygame.mixer.Sound('f3termprint.wav')
wrdSnd = pygame.mixer.Sound('f3termprint.wav')
while True:
db_updated = False
if forceClose:
break
while is_db_updating: #Ожидаем, пока обновится состояние из БД.
pass
# Проверяем: 1. Есть ли питание. 2. Не заблокирован ли терминал.
# Если все в порядке, показываем игру. После взлома показываем меню.
if not db_parameters["isPowerOn"]:
if previous_state != "Unpowered":
outScreen('unPowerHeader', 0)
previous_state = "Unpowered"
time.sleep(dbCheckInterval)
elif db_parameters["isLocked"]:
if previous_state != "Locked":
outScreen('lockHeader', 0)
previous_state = "Locked"
elif db_parameters["isHacked"]:
if previous_state != "Hacked":
outScreen('mainHeader', 3)
previous_state = "Hacked"
# Здесь вызываем функцию после взлома
forceClose = True # Закрываем всё
else:
# Взлом.
previous_state = "Normal"
outScreen('startHeader', 3)
hackScreen()
if __name__ == "__main__":
dbThread = threading.Thread(target=readDBParameters, args=(dbCheckInterval,))
dbThread.start()
time.sleep(1)
while is_db_updating:
# Ожидаем, пока обновится состояние из БД
pass
startTerminal()