sistema di punti limite giornaliero [chiusa]
-
26-10-2019 - |
Domanda
please help me con questo:
Lo scenario: la costruzione di un sistema (Backend: Mysql, scripting lang: Javascript / PHP) in cui l'utente esegue alcune azioni al fine di guadagnare qualche punto. Diciamo 1 azione = 1 punto.
Il mio problema: Ora voglio utenti limite per guadagnare atmax 5 punti al giorno. Significare tra l'intervallo di utenza 24 ore può fare quelle azioni qualsiasi numero di volte ma gli incrementi punti sarà superiore dopo aver raggiunto "5"
Vi prego di suggerire un modo efficace di farlo. il mio DB è minimalista e hanno solo Utente dettagli, punti.
Ora ho bisogno di aggiungere una colonna timestamp. Nessun problema, ma il problema come faccio a scegliere di aggiornare il tempo e quando. molto confuso :(
Soluzione
Uno dei modi è possibile raggiungere questo obiettivo è quello di tenere traccia dei punti totali, punti in un dato giorno e la data e ora dell'ultimo aggiornamento punto. Allora la vostra logica sarebbe simile a questa:
if the action is performed, which may cause a point award
if last_point_timestamp < today then
increment total points
set today's points to 1
set last_point_timestamp to current date/time
elseif today's points below 5
increment total points
increment today's points
set last_point_timestamp to current date/time
else
maximum number of points for today is reached - no more points
end if
end if
In sostanza, non sarà più di 5 punti in ciascun giorno di calendario permettere. È necessario essere attenti, però se il sistema deve essere utilizzato da più fusi orari, giacché in tale ipotesi significa "oggi" cose diverse per persone diverse. Per esempio, può essere Martedì 02:00 a Londra (vale a dire il giorno cambiato), ma è ancora Lunedi 06:00 a San Francisco (cioè il giorno non è cambiata)
Altri suggerimenti
Non so se questo è il modo più efficiente, ma è possibile avere un campo nel database per ogni utente che tiene traccia di quanti punti hanno guadagnato quel giorno. Così, quando raggiungono i 5 punti nel tuo esempio, poi non guadagno più.
Quindi, si può avere un lavoro cron o qualcosa, che lo reimposta a 0 per tutti gli utenti in un momento specifico, dicono mezzanotte per esempio.
Have a table called Points with four fields: user points todays_points last_point
To add a point:
SELECT * FROM points WHERE 'user' == $user"
if(last_point < older than today)
resetPoints($user);
addPoint($user);
elseif(todays_points < 5)
addPoint($user)
function resetPoints($user)
UPDATE todays_points to zero;
function addPoint($user)
ADD one to total points
ADD one to todays points
UPDATE last_point to current times
I would personally do this similar to MysticXG however with a trigger to track limits. The reason I would use a cron job is that it centralises the resetting of points and ensures that it doesn't burden the database with processing which can easily be done by just executing "UPDATE users SET limit = 5" each day. For this example I created a table with fields
user INT UNSIGNED AI PK
points INT UNSIGNED
dailyLimit TINYINT UNSIGNED DEFAULT 5
I then added a trigger to fire before updates with the following logic
IF NEW.points > OLD.points THEN
IF NEW.points - OLD.points > OLD.dailyLimit THEN
SET NEW.points = OLD.points + OLD.dailyLimit;
END IF;
SET NEW.dailyLimit = OLD.dailyLimit + OLD.points - NEW.points;
END IF
This means that if a users points increase, it will increase by up to the number of points they have remaining in that day. If they go over that limit it caps out at an increase of how many points they have remaining. If their points decrease, then their limit is unaffected (unsure whether this is desirable or not)
Then to reset the limits, all you have to do is at a set point each day run a script which executes this query
UPDATE users SET dailyLimit = 5
Just FYI here's the full query to create the trigger
CREATE TRIGGER `checkLimit` BEFORE UPDATE ON `points` FOR EACH ROW
IF NEW.points > OLD.points THEN
IF NEW.points - OLD.points > OLD.dailyLimit THEN
SET NEW.points = OLD.points + OLD.dailyLimit;
END IF;
SET NEW.dailyLimit = OLD.dailyLimit + OLD.points - NEW.points;
END IF;