forked from aninhalbuquerque/VotingSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
204 lines (171 loc) · 5.7 KB
/
client.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
from protocol import *
def menu_login():
os.system('clear')
menu = 'Digite a opcao desejada:\n1.Fazer login\n2.Cadastrar usuario\n3.Sair'
print(menu)
choice = raw_input('->')
choice = str(choice)
while choice != '1' and choice != '2' and choice != '3':
erro = 'Entrada invalida, tente novamente.'
print(erro)
choice = raw_input('->')
choice = str(choice)
return choice
def user_login():
message = 'Nome de usuario:'
print(message)
user = raw_input('->')
user = str(user)
message = 'Senha:'
print(message)
password = raw_input('->')
password = str(password)
return user, password
def user_registration():
message = 'Usuario: '
print(message)
user = raw_input('->')
user = str(user)
message = 'Senha: '
print(message)
password = raw_input('->')
password = str(password)
message = 'Repita a senha: '
print(message)
password2 = raw_input('->')
password2 = str(password2)
return user, password, password2
def menu_vote():
os.system('clear')
menu = 'Digite a opcao desejada:\n1.Votar\n2.Checar resultado\n3.Criar sessao de voto\n4.Sair'
print(menu)
choice = raw_input('->')
choice = str(choice)
while choice != '1' and choice != '2' and choice != '3' and choice != '4':
erro = 'Entrada invalida, tente novamente.'
print(erro)
choice = raw_input('->')
choice = str(choice)
return choice
def vote(voting_sections, see):
print('Sessoes de votacao: ')
for x in voting_sections:
print(' - ' + x)
print('Escolha a sessao que voce gostaria de ' + see + ': ')
choice = raw_input('->')
while not choice in voting_sections:
print('Sessao nao existe. Tente novamente')
choice = raw_input('->')
return choice
def vote_options(voting_options):
print('Opcoes: ')
for x in voting_options:
print(' - ' + x)
print('Escolha a opcao que voce gostaria de votar: ')
choice = raw_input('->')
while not choice in voting_options:
print('Opcao nao existe. Tente novamente:')
choice = raw_input('->')
return choice
def create():
message = 'Nome da nova sessao de voto:'
print(message)
nome = raw_input('->')
opcoes = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
message = 'Quantidade de opcoes (1-9):'
x = 0
ret = False
while not ret:
print(message)
qtOpcoes = raw_input('->')
if not qtOpcoes in opcoes:
message = 'Quantidade invalida. Tente novamente:'
else:
x = int(qtOpcoes)
ret = True
options = {}
for i in range(1, x + 1):
opcao = "Opcao " + str(i) + ":"
print(opcao)
opcao = raw_input('->')
while opcao in options:
print('Essa opcao ja existe. Tente novamente:')
opcao = raw_input('->')
options[opcao] = 0
return nome, options
sock = votingsys()
sock.open_client(10000)
secret_key = sock.client_connection()
saiu = False
ret = False
while not ret:
choice = menu_login()
sock.send_hash(choice, sock.sock, secret_key)
os.system('clear')
if choice == '1':
user, password = user_login()
ok = sock.user_login_client(user, password, secret_key)
while not ok:
print('Usuario ou senha invalidos. Tente novamente.')
user, password = user_login()
ok = sock.user_login_client(user, password, secret_key)
ret = True
elif choice == '2':
user, password, password2 = user_registration()
ok, erro = sock.cadastro_client(user, password, password2, secret_key)
while not ok:
print(erro)
user, password, password2 = user_registration()
ok, erro = sock.cadastro_client(user, password, password2, secret_key)
else:
sock.close_connection(sock.sock)
saiu = True
ret = True
if not saiu:
ret = False
while not ret:
choice = menu_vote()
sock.send_hash(choice, sock.sock, secret_key)
os.system('clear')
if choice == '1': #votar
voting_sections = sock.recv_voting_sections(secret_key)
section = vote(voting_sections, 'votar')
voting_options = sock.recv_options(section, secret_key)
option = vote_options(voting_options)
(ok, erro) = sock.client_vote(section, option, secret_key)
if ok:
print('Voto registrado com sucesso!')
else:
print(erro)
print('Espere')
cont = 0
while cont < 10000000:
cont = cont + 1
elif choice == '2': #checar resultado
voting_sections = sock.recv_voting_sections(secret_key)
section = vote(voting_sections, 'ver o resultado')
ok, result = sock.client_results(section, secret_key)
if not ok:
print(result)
else:
result = eval(result)
array = []
for x in result:
array.append((result[x], x))
array = sorted(array, key = lambda x: (-x[0],x[1]))
for (qtVotes, option) in array:
print(str(option) + ' -> ' + str(qtVotes))
print('Espere')
cont = 0
while cont < 10000000:
cont = cont + 1
elif choice == '3': #criar
name, options = create()
ok = sock.client_create_session(name, options, secret_key)
while not ok:
print('Ja existe uma sessao com esse nome. Tente novamente')
name, options = create()
ok = sock.client_create_session(name, options, secret_key)
else:
sock.close_connection(sock.sock)
ret = True