2023-08-22 11:21:42 +02:00
import sqlite3
2022-09-30 14:45:53 +02:00
2023-08-22 11:21:42 +02:00
#Create a new row, for the account [compte] whith [points] points
#SQLITE
2022-09-30 14:45:53 +02:00
def add_row ( compte , points , mycursor , mydb ) :
2023-08-22 11:21:42 +02:00
sql = " INSERT INTO daily (compte, points, date) VALUES (?, ?, date()) "
2022-09-30 14:45:53 +02:00
val = ( compte , points )
mycursor . execute ( sql , val )
mydb . commit ( )
2022-09-30 15:17:29 +02:00
#printf(mycursor.rowcount, "record created.")
2022-09-30 14:45:53 +02:00
2023-08-22 11:21:42 +02:00
#update the ammount of points for the account [compte]
#SQLITE
2022-09-30 14:45:53 +02:00
def update_row ( compte , points , mycursor , mydb ) :
2023-08-22 11:21:42 +02:00
sql = f " UPDATE daily SET points = { points } WHERE compte = ' { compte } ' AND date = date() ; "
2022-09-30 14:45:53 +02:00
mycursor . execute ( sql )
mydb . commit ( )
2022-09-30 15:17:29 +02:00
#printf(mycursor.rowcount, "record(s) updated")
2022-09-30 14:45:53 +02:00
2023-08-22 11:21:42 +02:00
# update the value of last_pts for the table comptes
2022-09-30 14:45:53 +02:00
def update_last ( compte , points , mycursor , mydb ) :
sql = f " UPDATE comptes SET last_pts = { points } WHERE compte = ' { compte } ' ; "
mycursor . execute ( sql )
mydb . commit ( )
2022-09-30 15:17:29 +02:00
#printf(mycursor.rowcount, "record(s) updated")
2022-09-30 14:45:53 +02:00
2023-08-22 11:21:42 +02:00
# if return if there already is a line in the database for the account [compte]. if same_point is enabled, the line must also have the same number of points
# SQLITE
def get_row ( compte , points , mycursor , same_points = True ) :
2022-09-30 14:45:53 +02:00
if same_points :
2023-08-22 11:21:42 +02:00
mycursor . execute ( f " SELECT * FROM daily WHERE points = { points } AND compte = ' { compte } ' AND date = date() ; " )
2022-09-30 14:45:53 +02:00
else :
2023-08-22 11:21:42 +02:00
mycursor . execute ( f " SELECT * FROM daily WHERE compte = ' { compte } ' AND date = date() ; " )
2022-09-30 14:45:53 +02:00
myresult = mycursor . fetchall ( )
return ( len ( myresult ) == 1 )
2023-08-22 11:21:42 +02:00
def add_to_database ( compte , points , save_if_fail = True ) :
2023-02-13 19:29:54 +01:00
if points is None :
pass
else :
2023-08-22 16:32:37 +02:00
mydb = sqlite3 . connect ( " /app/MsRewards-Reborn/MsRewards.db " )
2023-08-22 11:21:42 +02:00
mycursor = mydb . cursor ( )
if get_row ( compte , points , mycursor , True ) : #check if the row exist with the same ammount of points and do nothind if it does
#printf("les points sont deja bon")
#return(0)
pass
elif get_row ( compte , points , mycursor , False ) : #check if the row exist, but without the same ammount of points and update the point account then
update_row ( compte , points , mycursor , mydb )
#printf("row updated")
#return(1)
else : # if the row don't exist, create it with the good ammount of points
add_row ( compte , points , mycursor , mydb )
#return(2) #printf("row added")
if int ( points ) > 10 :
update_last ( compte , points , mycursor , mydb )
mycursor . close ( )
mydb . close ( )
2022-09-30 14:45:53 +02:00
2022-11-16 18:13:36 +01:00
2022-09-30 14:45:53 +02:00