-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
810 lines (462 loc) · 19.4 KB
/
main.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
from time import sleep
from typing import List
from upgrades import update_blessings
from dungeons import dungeon1
from utils import animated_text, check_email_valid, main_decorator, text_decorator, write_monster_stats, write_stats
import re
from characters import MainCharacter
import mysql.connector
from mysql.connector.cursor_cext import CMySQLCursor
import pwinput
# OBTENDO DADOS DO USUÁRIO
def get_name() -> str:
valid = False
name = ''
while not valid:
valid = True
main_decorator()
name = input('Digite seu nome: ').strip()
invalid_characters = '0123456789!@#$%&*()_-<,.>;:[{}]|+-*/'
for character in invalid_characters:
if character in name:
valid = False
if not valid:
animated_text('\n\033[1;33mNome inválido! ( Digite seu nome real )\033[m\n')
sleep(1.5)
return name.title()
def get_email(name: str = 'usuario') -> str:
valid = False
email = ''
regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,}$'
while not valid:
valid = True
main_decorator()
email = input('Digite seu e-mail: ').strip()
if not (re.search(regex,email)):
valid = False
animated_text(f'\n\033[1;33mDigite um e-mail válido (Ex: {name.lower()}@gmail.com)\033[m\n' )
sleep(1.5)
return email
def get_password() -> str:
valid = False
password = ''
while not valid:
main_decorator()
valid = True
password = pwinput.pwinput(prompt='Digite sua Senha: ')
if len(password) < 8:
valid = False
animated_text('\n\033[1;33mA senha deve conter pelo menos 8 caracteres...\033[m\n')
sleep(1)
continue
if not re.search("[a-z]", password) or not re.search("[A-Z]", password) or not re.search("[0-9]", password) or not re.search("[_@$]", password) or re.search("\s", password):
valid = False
animated_text('\n\033[1;33mA senha deve conter pelo menos 1 letra minuscula, 1 maiuscula, um número e 1 caractere especial...\033[m\n')
sleep(1)
continue
return password
def get_username(query) -> str:
valid = False
username = ''
while not valid:
main_decorator()
valid = True
username = input('Digite seu nome de usuário: ').strip()
if len(username) > 14 or len(username) < 3:
valid = False
print('\nO nome de usuário deve conter entre 3-14 caracteres...\n')
continue
try:
for users in query:
if username == users[4]:
animated_text('\n\033[1;33mNome de Usuário já esta sendo usado!\033[m\n')
valid = False
sleep(2)
continue
except IndexError:
pass
return username
def get_class() -> str:
valid = False
user_class = ''
main_decorator()
print('''Escolha uma classe para iniciar
[1] Guerreiro
[2] Arqueiro
[3] Mago
''')
while not valid:
user_class = input('\033[1;33mEscolha do Usuário:\033[m ').strip().lower()
if user_class == 'guerreiro' or user_class == '1':
user_class = 'guerreiro'
valid = True
elif user_class == 'arqueiro' or user_class == '2':
user_class = 'arqueiro'
valid = True
elif user_class == 'mago' or user_class == '3':
user_class = 'mago'
valid = True
else: print('\nEscolha inválida!\n')
return user_class
def check_email(name, query) -> str:
valid = False
email = ''
while not valid:
valid = True
email = get_email(name)
regex = r'([A-Za-z0-9]+[.-_])*[A-Za-z0-9]+@[A-Za-z0-9-]+(\.[A-Z|a-z]{2,})+'
if not (re.search(regex, email)): valid = False
if not valid:
animated_text('\n\033[1;33mEmail inválido!\033[m\n')
valid = False
sleep(2)
continue
try:
for users in query:
if email == users[2]:
animated_text('\n\033[1;33mEmail existente! Tente fazer login.\033[m\n')
valid = False
sleep(2)
continue
except IndexError:
pass
number_validation = check_email_valid(email)
code = 0
trys = 0
while code != number_validation:
if trys > 0:
print('\nCódigo errado')
sleep(2)
text_decorator('Dungeon of Adventure', 'cian')
print(f'Código de 6 digitos enviado para "\033[1;34m{email}\033[m"\n')
code = input('\033[1;33mDigite o código: \033[m')
trys += 1
if trys >= 3:
animated_text('\nMuitas tentativas falhas... Encerrando\n')
return ''
return email
def register() -> MainCharacter:
valid = False
connection = mysql.connector.connect(host='localhost', database='dbtextrpg', user='root', password='')
cursor = connection.cursor()
cursor.execute('SELECT * FROM users')
query = cursor.fetchall()
name = ''
password = ''
email = ''
username = ''
user_class = ''
while not valid:
valid = True
name = get_name()
email = check_email(name, query)
if email == '': return MainCharacter('', '')
password = get_password()
if len(password) < 8:
valid = False
animated_text('\n\033[1;33mA senha deve conter 8 ou mais caracteres!\033[m\n')
continue
username = get_username(query)
user_class = get_class()
user = MainCharacter(username, user_class)
cursor.execute(f"INSERT INTO users (`name`,`email`,`password`,`username`,`user_class`) VALUES ('{name}','{email}','{password}','{username}','{user_class}')")
connection.commit()
update_blessings(user, [0, 0, 0, 0, 0, 0, 0, 0, 0])
connection.commit()
cursor.close()
connection.close()
return user
def login() -> MainCharacter:
valid = False
query = []
while not valid:
valid = True
email = get_email()
password = pwinput.pwinput(prompt='Digite sua Senha: ')
connection = mysql.connector.connect(host='localhost', database='dbtextrpg', user='root', password='')
cursor = connection.cursor()
cursor.execute(f"SELECT * FROM users WHERE email = '{email}'")
query = cursor.fetchall()
try:
right_password = query[0][3]
except IndexError:
animated_text('\n\033[1;33Email ou senha invalidos!\033[m\n')
valid = False
sleep(2)
continue
if right_password != password:
valid = False
animated_text('\n\033[1;33Email ou senha invalidos!\033[m\n')
continue
cursor.execute(f"SELECT goddesslife_blessing, godwar_blessing, ironheart_blessing, greed_blessing, wisdom_blessing, dodge_blessing, criticalchance_blessing, criticaldamage_blessing, secoundchance_blessing FROM users WHERE email = '{email}'")
result: List[int] = cursor.fetchall()
user = MainCharacter(name = query[0][4], user_class = query[0][5], level = query[0][6],xp = query[0][7], gold = query[0][8], dungeon = query[0][9], floor=query[0][10],deaths = query[0][11])
update_blessings(user, result[0])
connection.commit()
cursor.close()
connection.close()
return user
return MainCharacter(name = query[0][4], user_class = query[0][5], level = query[0][6],xp = query[0][7], gold = query[0][8], dungeon = query[0][9])
# RANKINGS
def level_ranking(cursor: CMySQLCursor, name: str) -> None:
cursor.execute('SELECT username, user_class, user_level FROM users ORDER BY user_xp DESC')
result = cursor.fetchall()
text_decorator('Ranking de Level', 'cian')
# MÉTODO RUIM SE TIVER MUITOS USUÁRIOS
print('TOP 10 JOGADORES\n')
for indice, user in enumerate(result):
if indice < 10:
if user[0] == name:
animated_text(f'\033[1;33m{indice+1}°\033[m - \033[1;33m{user[0]}\033[m / {user[1].title()} / Level {user[2]}')
print()
else:
if indice == 9:
print(f'{indice+1}°- {user[0]} / {user[1].title()} / Level {user[2]}')
print()
else:
print(f'{indice+1}° - {user[0]} / {user[1].title()} / Level {user[2]}')
elif user[0] == name:
animated_text(f'\033[1;33m{indice+1}°\033[m - \033[1;33m{user[0]}\033[m / {user[1].title()} / Level {user[2]}')
print('\n')
animated_text(input('\033[1;33mPressione enter para voltar...\033[m'))
def deaths_ranking(cursor: CMySQLCursor, name: str) -> None:
cursor.execute('SELECT username, user_class, user_level, deaths FROM users ORDER BY deaths DESC, user_dungeon ASC, user_floor ASC')
result = cursor.fetchall()
text_decorator('Ranking de Mortes', 'red')
# MÉTODO RUIM SE TIVER MUITOS USUÁRIOS
print('TOP 10 JOGADORES\n')
for indice, user in enumerate(result):
if indice < 10:
if user[0] == name:
animated_text(f'\033[1;33m{indice+1}°\033[m - \033[1;33m{user[0]}\033[m / {user[1].title()} / Level {user[2]} / Deaths: \033[1;31m{user[3]}\033[m')
print()
else:
if indice == 9:
print(f'{indice+1}°- {user[0]} / {user[1].title()} / Level {user[2]} / Deaths: \033[1;31m{user[3]}\033[m')
print()
else: print(f'{indice+1}° - {user[0]} / {user[1].title()} / Level {user[2]} / Deaths: \033[1;31m{user[3]}\033[m')
elif user[0] == name:
animated_text(f'\033[1;33m{indice+1}°\033[m - \033[1;33m{user[0]}\033[m / {user[1].title()} / Level {user[2]} / Deaths: \033[1;31m{user[3]}\033[m')
print('\n')
animated_text(input('\033[1;33mPressione enter para voltar...\033[m'))
def gold_ranking(cursor: CMySQLCursor, name: str) -> None:
cursor.execute('SELECT username, user_class, user_level, user_gold FROM users ORDER BY user_gold DESC, user_level ASC')
result = cursor.fetchall()
text_decorator('Ranking de Ouro', 'yellow')
# MÉTODO RUIM SE TIVER MUITOS USUÁRIOS
print('TOP 10 JOGADORES\n')
for indice, user in enumerate(result):
if indice < 10:
if user[0] == name:
animated_text(f'\033[1;33m{indice+1}\033[m - \033[1;33m{user[0]}\033[m / {user[1].title()} / Level {user[2]} / Gold: \033[1;33m{user[3]}\033[m')
print()
else:
if indice == 9:
print(f'{indice+1}- {user[0]} / {user[1].title()} / Level {user[2]} / Gold: \033[1;33m{user[3]}\033[m')
print()
else: print(f'{indice+1} - {user[0]} / {user[1].title()} / Level {user[2]} / Gold: \033[1;33m{user[3]}\033[m')
elif user[0] == name:
animated_text(f'\033[1;33m{indice+1}\033[m - \033[1;33m{user[0]}\033[m / {user[1].title()} / Level {user[2]} / Gold: \033[1;33m{user[3]}\033[m')
print('\n')
animated_text(input('\033[1;33mPressione enter para voltar...\033[m'))
def dungeon_ranking(cursor: CMySQLCursor, name: str) -> None:
cursor.execute('SELECT username, user_class, user_level, user_dungeon FROM users ORDER BY user_dungeon DESC, user_floor DESC, user_level ASC, user_xp ASC')
result = cursor.fetchall()
text_decorator('Ranking de Dungeon', 'cian')
# MÉTODO RUIM SE TIVER MUITOS USUÁRIOS
print('TOP 10 JOGADORES\n')
for indice, user in enumerate(result):
if indice < 10:
if user[0] == name:
animated_text(f'\033[1;33m{indice+1}°\033[m - \033[1;33m{user[0]}\033[m / {user[1].title()} / Level {user[2]} / Dungeon: {user[3]}')
print()
else:
if indice == 9:
print(f'{indice+1}°- {user[0]} / {user[1].title()} / Level {user[2]} / Dungeon: {user[3]}')
print()
else: print(f'{indice+1}° - {user[0]} / {user[1].title()} / Level {user[2]} / Dungeon: {user[3]}')
elif user[0] == name:
animated_text(f'\033[1;33m{indice+1}°\033[m - \033[1;33m{user[0]}\033[m / {user[1].title()} / Level {user[2]} / Duugeon: {user[3]}')
print('\n')
break
animated_text(input('\033[1;33mPressione enter para voltar...\033[m'))
def ranking(player: MainCharacter) -> None:
user_choice = ''
connection = mysql.connector.connect(host='localhost', database='dbtextrpg', user='root', password='')
cursor = connection.cursor()
while user_choice != '0':
text_decorator('Ranking', 'cian')
print('''Escolha uma Opção
[0] - Sair
[1] - Ranking de Level
[2] - Ranking de Mortes
[3] - Ranking de Gold
[4] - Ranking de Dungeon
\033[1;33mEscolha do Usuário: \033[m''', end='')
user_choice = input('')
if user_choice == '1':
level_ranking(cursor, player.name)
elif user_choice == '2':
deaths_ranking(cursor, player.name)
elif user_choice == '3':
gold_ranking(cursor, player.name)
elif user_choice == '4':
dungeon_ranking(cursor, player.name)
elif user_choice != '0':
animated_text('\n\033[1;33mOpção Invalida\033[m')
sleep(1)
cursor.close()
connection.close()
# LOJA
def blessing_details(player: MainCharacter, blessing: list, cursor: CMySQLCursor) -> None:
while True:
text_decorator(f'Detalhes de {blessing[0]}', 'cian')
if blessing[1] != '':
print(f'''\033[1;34mNivel Atual\033[m
{blessing[1]}
\033[1;34mProximo Nivel\033[m
{blessing[2]}
Gold: \033[1;33m{blessing[3]}\033[m
''')
else:
print(f'''\033[1;34mNivel Atual\033[m
Não obtido ainda
\033[1;34mProximo Nivel\033[m
{blessing[2]}
Preço: \033[1;33m{blessing[3]}\033[m G''')
if player.gold >= blessing[3]:
print(f'Gold Atual: \033[1;32m{player.gold}\033[m G')
else:
print(f'Gold Atual: \033[1;31m{player.gold}\033[m G')
print('\n[0] - Sair')
print('[1] - Comprar')
user_choice = input('\n\033[1;33mEscolha do Usuário: \033[m')
if user_choice == '0':
return
elif user_choice == '1':
if player.gold < blessing[3]:
animated_text('\n\033[1;33mGold insuficiente!\033[m')
sleep(1)
return
player.gold -= blessing[3]
name_bd = blessing[0].lower().replace(' ', '').replace('of', '') + '_blessing'
cursor.execute(f"UPDATE users SET {name_bd} = {name_bd} + 1 WHERE username = '{player.name}'")
return
else:
continue
def shop(player: MainCharacter) -> None:
while True:
connection = mysql.connector.connect(host='localhost', database='dbtextrpg', user='root', password='')
cursor = connection.cursor()
cursor.execute(f"SELECT goddesslife_blessing, godwar_blessing, ironheart_blessing, greed_blessing, wisdom_blessing, dodge_blessing, criticalchance_blessing, criticaldamage_blessing, secoundchance_blessing FROM users WHERE username = '{player.name}'")
result = cursor.fetchall()
update_blessings(player, result[0])
cursor.execute('SELECT name, description, next_description, gold FROM shop ORDER BY gold ASC')
result = cursor.fetchall()
text_decorator('Loja de Melhorias', 'cian')
print('Escolha um item')
print('\n[0] - Sair')
print()
for index, blessing in enumerate(result):
print(f'[{index+1}] - ', blessing[0])
print()
user_choice = input('\033[1;33mEscolha do Usuário: \033[m')
if user_choice == '0':
break
try:
user_choice = int(user_choice)
blessing_details(player, result[user_choice-1], cursor)
except TypeError:
animated_text('\n\033[1;31mOpção Invalída!\033[m')
sleep(1)
except IndexError:
animated_text('\n\033[1;31mOpção Invalída!\033[m')
sleep(1)
connection.commit()
cursor.close()
connection.close()
player.update_stats()
# MENU PRINCIPAL
def menu(player: MainCharacter) -> bool:
exit = False
user_choice = ''
player.update_stats()
while not exit:
text_decorator('Menu Principal', 'cian')
print('''Escolha uma Opção
[0] - Sair
[1] - Ir para a Dungeon
[2] - Ver o Ranking
[3] - Ver Meu Status
[4] - Mudar de Classe
[5] - Loja
\033[1;33mEscolha do Usuário: \033[m''', end='')
user_choice = input('')
if user_choice == '0':
exit = True
elif user_choice == '1':
exit = True
elif user_choice == '2':
ranking(player)
elif user_choice == '3':
player.show_stats()
animated_text(input('\033[1;33mPresisone enter para voltar ao Menu...\033[m'))
elif user_choice == '4':
player.change_class()
elif user_choice == '5':
shop(player)
elif user_choice != '0':
animated_text('\n\033[1;33mOpção Invalida!\033[m')
sleep(1)
if user_choice == '0':
return False
return True
# CHAMADA DAS DUNGEONS
def history(player: MainCharacter):
while True:
user_choice = menu(player)
if not user_choice: break
if player.dungeon == 1:
result_dungeon = dungeon1(player)
player.check_stats()
if result_dungeon:
player.dungeon += 1
player.floor = 1
player.level_up()
player.update_stats()
if player.dungeon == 2:
break
# INICIALIZADOR
def init():
valid = False
player = MainCharacter('', '')
user_choice = ''
while not valid:
valid = True
text_decorator('Dungeon of Adventure', 'cian')
animated_text('''Escolha uma opção
[0] - Sair
[1] - Login
[2] - Registrar-me
\033[1;33mEscolha do Usuário:\033[m ''')
user_choice = input('')
if user_choice == '0': break
if user_choice == '1': player = login()
elif user_choice == '2': player = register()
else:
valid = False
print()
animated_text('\033[1;31mOpção invalida! Digite "0", "1" ou "2".\033[m')
sleep(2)
continue
if player.name == '': pass
elif user_choice != '0':
write_stats()
write_monster_stats()
player.show_stats()
animated_text('\033[1;33mPressione enter para continuar...\033[m')
input(' ')
history(player)
player.update_stats()
animated_text('\n\033[1;32mObrigado por jogar meu jogo! Volte sempre\033[m\n')
init()