I've been thinking, and that can lead to nothing good...
You want to optimize to the fewest updates possible to get the job done. Reads are infinitely cheaper than updates. So, the update needs to have some kind of and thing that actually changes the time only when it actually has to.
What I mean is that we truncate the date down to 6 second intervals, and we do mod math to see if we've moved to the next one then to update and return etc, otherwise do nothing, because it doesn't matter. Does that make sense? From a programmatic point of view you'd want something like:
id=6
lock the row for id
update time_table set last_time=now() where id=4 and (mod math on 6 sec intvls goes here) returning <that extract epoch thing from above>
-- that last bit to keep it from actually updating if it doesn't have to. No rows match both id and the mod6 test and nothing happens.
Now, most databases don't have modulo math for dates and timestamps, so we pretty much are stuck with doing the date math in unix epoch integer format, seconds since jan 01 1972 or something like that.
So, we build a new table with a credit column. Note that I've chosen some arbitrary limit on number of turn credits. It's easy enough to just drop if you don't need it. And on MySQL you can ignore them as it doesn't implement check constraints.
smarlowe=# create table time_table (id int, first_time timestamp, last_time timestamp, credit int, primary key (id));NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "time_table_pkey" for table "time_table"
CREATE TABLE
smarlowe=# alter table time_table add constraint max_credit check (credit <= 10);
ALTER TABLE
smarlowe=# alter table time_table add constraint min_credit check (credit >= 0);
ALTER TABLE
smarlowe=# alter table time_table add constraint no_time_warps check (first_time <= last_time);
ALTER TABLE
A test to see if the constraints are working:
smarlowe=# insert into time_table values (1,'2008-01-01 00:00:00','2008-02-07 13:43:34',22);
ERROR: new row for relation "time_table" violates check constraint "max_credit"
smarlowe=# insert into time_table values (1,'2008-01-01 00:00:00','2007-02-07 13:43:34',2);
ERROR: new row for relation "time_table" violates check constraint "no_time_warps"
Now a good row of data.
smarlowe=# insert into time_table values (1,'2008-01-01 00:00:00','2008-02-07 13:43:34',2);
INSERT 0 1
Test to make sure the pk failure happens:
smarlowe=# insert into time_table values (1,'2008-01-01 00:00:00','2008-02-04 18:21:16',2);
ERROR: duplicate key violates unique constraint "time_table_pkey"
smarlowe=# insert into time_table values (2,'2008-01-01 00:00:00','2008-02-04 18:21:16',7);
INSERT 0 1
smarlowe=# select * from time_table ;
id | first_time | last_time | credit
----+---------------------+---------------------+--------
1 | 2008-01-01 00:00:00 | 2008-02-07 13:43:34 | 2
2 | 2008-01-01 00:00:00 | 2008-02-04 18:21:16 | 7
(2 rows)
And the data in the database is ready to work on. So, I'll assume that if it's been more than x amount of time, you give them credits for each 6 minute period up to the max you're allowed to have.
Well, I've got this all worked out on my database but am losing cohesion of thoughts. Will finish it up tomorrow.