-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
267 lines (253 loc) · 11.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
from dotenv import load_dotenv
from os.path import join, dirname, isdir
from os import environ
import mysql.connector
from Juego import *
from Registro import *
from Jugador import *
import hashlib
class Main():
def __init__(self):
self.connection, self.cursor = self.conectar()
self.__jugador1 = None
self.__jugador2 = None
def conectar(self):
dotenv_path = join(dirname(__file__), ".env")
load_dotenv(dotenv_path)
dbname=environ.get("db_name")
host=environ.get("db_host")
username=environ.get("db_username")
password=environ.get("db_password")
connection = mysql.connector.connect(database = dbname,
host = host,
username = username,
password = password)
cursor = connection.cursor()
return connection, cursor
def registrar(self):
test = False
while test == False:
name = input("Introduce tu nombre de usuario que desees\n")
password = input("Introduce tu contrasenia\n")
repassword = input("Vuelve a introducir tu contrasenia\n")
if password != repassword:
print("Tus contrasenias no concuerdan, hazlo de nuevo")
else:
try:
nuevo_registro = Registro(self.connection, self.cursor, name, password)
nuevo_registro.registrar()
except:
print("Usuario ya existente, intenta de nuevo")
else:
test = True
print("Felicidades, te has registrado exitosamente")
def encriptar(self, string):
sha_signature = hashlib.sha256(string.encode()).hexdigest()
return sha_signature
def acceder(self, Invitado = ""):
fetch = []
if Invitado == "":
repeticion = True
while repeticion == True:
acceso = input("Introduce tu usuario\n")
acc = input("Introduce tu contrasenia\n")
acc_pass = self.encriptar(acc)
string_acceso = "SELECT nombre,pk_id_jugador FROM Jugador WHERE nombre='" + acceso + "' AND password='"+acc_pass+"'"
self.cursor.execute(string_acceso)
fetch = self.cursor.fetchall()
self.connection.commit()
#print(fetch)
if fetch == []:
valor = input("El usuario no existe, estas registrado(n)? Si se equivoco en contrasenia o usuario, ingrese lo que sea e intente de nuevo\n").lower()
if valor == "n":
self.registrar()
else:
jugador = Jugador(self.cursor, self.connection,fetch[0][0],fetch[0][1])
repeticion = False
else:
string_invitado = "SELECT pk_id_jugador FROM Jugador WHERE nombre='invitado'"
self.cursor.execute(string_invitado)
fetch = self.cursor.fetchall()
self.connection.commit()
jugador = Jugador(self.cursor, self.connection, "invitado",fetch[0][0])
#print(jugador.getNombre())
return jugador;
def cargas(self):
self.__jugador1 = self.acceder()
string_carga = "SELECT * FROM Partida WHERE fk_id_creador = " + str(self.__jugador1.identificador) + " AND resultado IS NULL"
self.cursor.execute(string_carga)
lista = self.cursor.fetchall()
#imprime = ""
if lista == []:
print("Ups, parece que no tienes partidas guardadas con este usuario")
else:
numeros = []
for list in lista:
print(list)
numeros.append(list[0])
repite = True
while repite == True:
try:
seleccion = int(input("Selecciona el numero de la partida que desees cargar(el que esta mas a la izquierda)\nIntroduce -1 para salir\n"))
if seleccion == -1:
repite = False
return []
elif seleccion in numeros:
string_cargarPartida = "SELECT * FROM Partida WHERE pk_id_partida = "+ str(seleccion)
self.cursor.execute(string_cargarPartida)
cargar = self.cursor.fetchall()
repite = False
return cargar
else:
print("No es una partida valida, intenta de nuevo")
except:
print("No es opcion valida")
return []
def switch(self, argument):
if argument == 1:
print("Elegiste iniciar una nueva partida")
self.__jugador1 = self.acceder()
self.__jugador1.setColor(Fore.RED)
repite = True
#Color del jugador que crea la partida es rojo, el del oponente es amarillo
#print(self.__jugador1.identificador)
while repite == True:
try:
eleccion = int(input("Elige contra quien vas a jugar\n1)Otro jugador registrado\n2)Invitado\n"))
if eleccion==1:
self.__jugador2 = self.acceder()
repite=False
elif eleccion == 2:
self.__jugador2 = self.acceder("invitado")
repite= False
else:
pass
except:
#print(e)
print("Entrada no valida")
self.__jugador2.setColor(Fore.YELLOW)
juego = Juego(self.__jugador1,self.__jugador2,"",self.cursor,self.connection)
juego.jugar()
#print(self.__jugador2.getNombre())
#jugar = Juego(self.__jugador1,self.__jugador2,[],self.cursor,self.connection)
#jugar.jugar()
#print("Partida nueva")
elif argument == 2:
print("Elegiste registrarte")
self.registrar()
#print("Registrarse")
elif argument == 3:
print("Elegiste cargar partida")
#carga = int(input("Introduce el identificador de la partida que estabas jugando"))
lista = self.cargas()
if lista == []:
pass
else:
self.__jugador2 = self.obtenerJugadorpk(lista[0][4])
self.__jugador1.setColor(Fore.RED)
self.__jugador2.setColor(Fore.YELLOW)
#jugador_turno = self.obtenerJugadorpk(lista[0][2])
if self.obtenerJugadorpk(lista[0][2]) == self.__jugador1:
#aqui ya existe self.__jugador1
juego = Juego(self.__jugador1,self.__jugador2,lista[0][1],self.cursor, self.connection,self.__jugador1,lista[0][0])
else:
juego = Juego(self.__jugador1,self.__jugador2,lista[0][1],self.cursor, self.connection,self.__jugador2,lista[0][0])
juego.jugar()
#print("Cargar partida"
elif argument == 4:
print("Elegiste estadisticas")
self.estadisticas()
else:
print("Adios")
def estadisticas(self):
self.__jugador1 = self.acceder()
string_carga = "SELECT * FROM Partida WHERE fk_id_creador = " + str(self.__jugador1.identificador)
self.cursor.execute(string_carga)
lista_creador = self.cursor.fetchall()
string_carga0 = "SELECT * FROM Partida WHERE fk_id_oponente = " + str(self.__jugador1.identificador)
self.cursor.execute(string_carga0)
lista_oponente = self.cursor.fetchall()
#imprime = ""
#print(lista_creador)
#print(lista_oponente)
if lista_creador == [] and lista_oponente == []:
print("Ups, parece que no tienes partidas terminadas con este usuario")
else:
gana_crea = []
pierde_crea = []
empate = []
gana_opo = []
pierde_opo = []
if lista_creador != []:
for list in lista_creador:
if list[5] == "gana":
gana_crea.append(list)
elif list[5] == "pierde":
pierde_crea.append(list)
elif list[5] == "empate":
empate.append(list)
#new_list = [gana, pierde, empate]
if lista_oponente != []:
for list in lista_oponente:
if list[5] == "pierde":
gana_opo.append(list)
elif list[5] == "gana":
pierde_opo.append(list)
gana = gana_opo + gana_crea
pierde = pierde_opo + pierde_crea
if gana != []:
print(Fore.GREEN + "Partidas ganadas" + Fore.RESET)
for k in gana:
if self.__jugador1.identificador == k[4]:#el usuario es el oponente
jugador = self.obtenerJugadorpk(k[3])
print("Ganaste contra "+jugador.getNombre())
else:#el usuario es el creador
jugador = self.obtenerJugadorpk(k[4])
print("Ganaste contra " + jugador.getNombre())
#jugador = self.obtenerJugadorpk(k[4])
#print("Ganaste contra " + jugador.getNombre())
if pierde != []:
print(Fore.RED + "Partidas perdidas" + Fore.RESET)
for k in pierde:
if self.__jugador1.identificador == k[4]:#el usuario es el oponente
jugador = self.obtenerJugadorpk(k[3])
print("Perdiste contra "+jugador.getNombre())
else:#el usuario es el creador
jugador = self.obtenerJugadorpk(k[4])
print("Perdiste contra " + jugador.getNombre())
if empate != []:
print(Fore.YELLOW + "Partidas empatadas" + Fore.RESET)
for k in empate:
jugador = self.obtenerJugadorpk(k[4])
print("Empataste contra " + jugador.getNombre())
#if new_list == [[],[],[]]:
# print("Ups, parece que aun no has acabado ninguna partida!")
def obtenerJugadorpk(self,pk_jugador):
string_obtener = "SELECT pk_id_jugador, nombre FROM Jugador WHERE pk_id_jugador = " + str(pk_jugador)
self.cursor.execute(string_obtener)
lista = self.cursor.fetchall()
jugador = Jugador(self.cursor, self.connection,lista[0][1],pk_jugador)
return jugador
def main(self):
print("Bienvenido al juego de conecta 4")
prueba = False
while prueba == False:
print("Elige una opcion")
print("1) Partida nueva")
print("2) Registrarse")
print("3) Cargar partida")
print("4) Estadisticas")
print("0) Salir")
try:
argumento = int(input())
self.switch(argumento)
if argumento == 0:
prueba = True
except Exception as e:
#print(e)
print("No es entrada valida, intenta de nuevo")
#except:
# print("No es un argumento valido, intenta de nuevo ")
if __name__ == '__main__':
partida = Main()
partida.main()