/
main.py
262 lines (208 loc) · 9.66 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
import tkinter as tk
from PIL import ImageTk, Image
import codicefiscale
from datetime import datetime
import numpy as np
import os.path
import webbrowser
campi = ('Codice Fiscale', 'Modalità', 'Numeri', 'Ruota', 'Puntata', 'Risultato')
entrate_utente = {}
valori_utente = ['0', '0', '0', '0', '0', '0']
numero_campi = 6
modalita = ["ESTRATTO", "AMBO", "TERNO", "QUATERNA", "CINQUINA", "ESTRATTO SECCO", "AMBO SECCO", "TERNO SECCO",
"QUATERNA SECCA", "CINQUINA SECCA"]
nomi_ruote = ["TORINO", "MILANO", "VENEZIA", "GENOVA", "FIRENZE", "ROMA", "NAPOLI", "BARI", "PALERMO", "CAGLIARI",
"NAZIONALE"]
vincite = [5, 25, 450, 12000, 600000, 55, 250, 4500, 120000, 6000000]
def calcoloCodiceFiscale(cod_fisc): # Controlla che che il codice fiscale inserito sia corretto:
if cod_fisc == '0':
return "Inserire un codice fiscale!"
elif not codicefiscale.isvalid(cod_fisc):
return "CF errato"
else:
if verificaEta(cod_fisc):
return "OK"
else:
return "Sei Minorenne!"
def verificaEta(cod_fisc): # Viene verificato se l'utente ha almeno 18 anni:
dataNascita = getDataNascita(cod_fisc)
dataNascita[2] = convertiAnno(dataNascita[2])
eta = calcolaEta(int(dataNascita[0]), int(dataNascita[1]), int(dataNascita[2]))
if eta >= 18:
return 1
else:
print("Mi dispiace non hai un'età sufficiente per giocare!")
return 0
def getDataNascita(codice): # Restituisce la data di nascita:
data = (codicefiscale.get_birthday(codice)) # Dal codice inserito ricava la data di nascita.
return data.split('-') # Divide la data di nascita in un array di tre elementi [giorno, mese, anno].
def convertiAnno(anno): # Converte l'anno del codice fiscale in un anno con 4 cifre:
year = datetime.now().strftime(
"%y") # Assegna alla varibiale "year" solo le ultime due cifre dell'anno corrente (2021 --> 21).
if int(anno) < int(year):
return int(anno) + 2000
else:
return int(anno) + 1900
def calcolaEta(giorno, mese, anno): # Ritorna l'età in base al codice fiscale inserito:
today = datetime.today()
return today.year - anno - ((today.month, today.day) < (mese, giorno))
def salvaEstrazione(): # Viene salvata la matrice su un file con la data del giorno corrente:
filename = fileGiornoOdierno()
if not os.path.exists(filename):
num_estratti = generaNumeriRuote()
file = open(filename, 'w')
np.savetxt(file, num_estratti, fmt='%i', delimiter=';', newline='\n')
file.close()
def fileGiornoOdierno(): # Ritorna il nome del file dei numeri vincenti giornaliero:
now = datetime.now()
nome_file = now.strftime("NumeriVincenti_%d-%m-%Y")
nome_file = f'{nome_file}.txt'
return nome_file
def generaNumeriRuote(): # Genera 5 numeri compresi tra 0 e 90 diversi per ogni riga, per ogni ruota:
mat = np.zeros((11, 5))
for i in range(len(mat)):
mat[i] = np.random.choice(89, 5, replace=False) + 1
return mat
def controllaModalita(mod): # L'utente sceglie la modalità con la quale vuole fare la sua puntata:
if mod == '0':
return "Inserire una modalità!"
elif mod in modalita:
return "OK"
else:
return "Modalita non esistente!"
def controllaRuota(ruota, mod): # L'utente sceglie la ruota du cui vuole fare la sua puntata:
if ruota in nomi_ruote or modalita.index(mod) < 6:
return "OK"
elif ruota == '0':
return "Inserire una ruota!"
else:
return "Ruota non esistente!"
def splitNumeri(numeri): # Viene creato un array dividendo la stringa:
return numeri.split(' ')
def controllaNumeri(numeri_da_giocare,
mod): # Chiede all'utente di inserire i numeri per effetturare la giocata in base alla modalità scelta:
numeri_scelti = splitNumeri(numeri_da_giocare)
numeri_da_giocare = modalita.index(mod) + 1
if numeri_da_giocare > 5:
numeri_da_giocare -= 5
if len(numeri_scelti) < numeri_da_giocare:
return "Numeri inseriti non sufficienti!"
elif len(numeri_scelti) > numeri_da_giocare:
return "Sono stati inseriti troppi numeri!"
else:
for i in range(numeri_da_giocare):
if (numeri_scelti[i] < '1') or (numeri_scelti[i] > '90') or not (numeriDoppi(numeri_scelti, i)):
return "Numeri non Validi!"
else:
return 'OK'
def numeriDoppi(numeri, i): # Ritorna 0 se trova due numeri uguali, oppure se i numeri sono tutti diversi ritorna 1:
for j in range(i):
if numeri[j] == numeri[i]:
return 0
return 1
def leggiFile(): # Legge i numeri estratti dal file dei numeri vincenti del giorno corrente:
filename = fileGiornoOdierno()
file = open(filename, 'r')
matrice = np.loadtxt(file, dtype=int, delimiter=';', usecols=(0, 1, 2, 3, 4))
return matrice
def controlloVincite(num_estratti, mod_scelta, num_scelti, r_scelta,
punt_scelta): # Vengono confrontati i numeri inseriti dall'utente con quelli dell'estrazione:
num_split = splitNumeri(num_scelti)
m = modalita.index(mod_scelta) + 1
if m < 6:
indovinati = controlloVinciteEstrazioni(num_split, m, num_estratti)
else:
r = nomi_ruote.index(r_scelta)
indovinati = controlloVinciteEstrazioniSecche(num_split, num_estratti[r])
if indovinati > 0:
return calcoloVincita(indovinati, m, int(punt_scelta))
else:
return "Non hai trovato nessuno numero!"
def controlloVinciteEstrazioni(num_utente, mod,
num_vincenti): # Controlla se i numeri dell'utenti sono presenti in quelli estratti, su tutte le ruote:
trovati = 0
for i in range(10):
trovati_interni = 0
for j in range(mod):
if int(num_utente[j]) in num_vincenti[i]:
trovati_interni += 1
if trovati_interni >= mod:
trovati += 1
return trovati
def controlloVinciteEstrazioniSecche(num_utente,
num_vincenti): # Controlla se i numeri dell'utenti sono presenti in quelli estratti, sulla ruota scelta dall'utente:
for i in range(len(num_utente)):
if not int(num_utente[i]) in num_vincenti:
return 0
return 1
def calcoloVincita(trovati, mod_scelta, punt_scelta): # Viene calcolata la vincita in base all'importo giocato:
return vincite[mod_scelta - 1] * trovati * punt_scelta
def aggiornaFileGiocatori(cod_fisc, num_utente,
ammontare): # Viene aggiunto al file dei giocatori, il codice fiscale dell'utente con la rispettiva vincita:
if ammontare == "Non hai trovato nessuno numero!":
ammontare = 0
now = datetime.now()
data = now.strftime("%d-%m-%Y")
filename = f"{data}_{cod_fisc}.txt"
if os.path.exists(filename):
file_utenti = open(filename, "a")
else:
file_utenti = open(filename, "w")
file_utenti.write(f"{cod_fisc}, {num_utente}, {ammontare}\n")
file_utenti.close()
def gioca(): # Controlla il gioco:
for i in range(numero_campi - 1):
valori_utente[i] = entrate_utente[campi[i]].get()
valori_utente[i] = valori_utente[i].upper()
salvaEstrazione()
if int(valori_utente[4]) > 0:
test = calcoloCodiceFiscale(valori_utente[0])
if test == 'OK':
test = controllaModalita(valori_utente[1])
if test == 'OK':
test = controllaRuota(valori_utente[3], valori_utente[1])
if test == 'OK':
test = controllaNumeri(valori_utente[2], valori_utente[1])
if test == 'OK':
numeri_estratti = leggiFile()
test = controlloVincite(numeri_estratti, valori_utente[1], valori_utente[2], valori_utente[3],
valori_utente[4])
aggiornaFileGiocatori(valori_utente[0], valori_utente[2], test)
else:
test = "Somma scommessa non valida!"
entrate_utente[campi[numero_campi - 1]].delete(0, tk.END)
entrate_utente[campi[numero_campi - 1]].insert(0, test)
def rgbtohex(r,g,b):
return f'#{r:02x}{g:02x}{b:02x}'
def disegnaFinestra(root, fields): # Disegna la schermata di gioco:
root.iconphoto(False, tk.PhotoImage(file="logo.png"))
root.winfo_toplevel().title("Gioco del Lotto")
img = ImageTk.PhotoImage(Image.open("logolotto.png"))
panel = tk.Label(root, image=img, bg='white')
panel.image = img
panel.pack(side="top", fill="both", expand="yes")
for field in fields:
row = tk.Frame(root)
fg = rgbtohex(r=0, g=72, b=157)
lab = tk.Label(row, width=15, text=field + ": ", anchor='w', font=("Arial", 20), fg='black', bg='white')
ent = tk.Entry(row, width=30, font=("Arial", 20))
ent.insert(0, '0')
row.pack(side=tk.TOP, fill=tk.X, padx=10, pady=10)
lab.pack(side=tk.LEFT)
ent.pack(side=tk.RIGHT, expand=tk.YES, fill=tk.X)
entrate_utente[field] = ent
return entrate_utente
def openfile():
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'help/manuale.html')
webbrowser.open(filename)
finestra = tk.Tk()
entrate_utente = disegnaFinestra(finestra, campi)
finestra.configure(bg='white')
b1 = tk.Button(finestra, text='Esci', font=("Arial", 20), command=finestra.quit, borderwidth=4, relief="ridge")
b1.pack(side=tk.RIGHT, padx=7, pady=7)
b2 = tk.Button(finestra, text='Gioca', font=("Arial", 20), command=lambda: gioca(), borderwidth=4, relief="ridge")
b2.pack(side=tk.RIGHT, padx=7, pady=7)
b3 = tk.Button(finestra, text='Help', font=("Arial", 20), command=openfile, borderwidth=4, relief="ridge")
b3.pack(side=tk.LEFT, padx=7, pady=7)
finestra.mainloop()