Question

please help me with this :

The scenario: building a system (Backend :Mysql , scripting lang : Javascript/ PHP) where the user perform some actions in order to gain some points. let's say 1 action = 1 point.

My problem : Now i want to limit users to gain atmax 5 points per day. Mean between the interval of 24 hours user can do those actions any number of times but the the points increments will top after reaching "5"

Please suggest me a efficient way of doing it. my DB is minimalistic and have only User details, points .

Now i need to add a timestamp column. No problem , But the problem how do i choose to update the time and when. very confused :(

Was it helpful?

Solution

One of the way you can achieve this is to keep track of the points total, points in a given day and the timestamp of the last point update. Then your logic would look like this:

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

Essentially, it will allow no more than 5 points in any calendar day. You need to be careful though if your system is to be used from multiple time zones, because in that case "today" means different things to different people. For example, it may be Tuesday 2am in London (i.e. the day changed), but it's still Monday 6pm in San Francisco (i.e the day didn't change)

OTHER TIPS

I don't know if this is the most efficient way, but you can have a field in your database for each user that keeps track of how many points they've gained that day. So when they reach 5 points in your example, then they don't gain anymore.

Then, you can have a cron job or something, that resets it to 0 for all the users at a specified time, say midnight for example.

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top