the quickest, most resourceful, easy way to do this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lizlazloz
Forum Commoner
Posts: 64
Joined: Mon Dec 29, 2003 7:29 am

the quickest, most resourceful, easy way to do this?

Post by lizlazloz »

ok, i'm making a little online game, just for fun, and in it you can 'attack' people. what i want to do is make it so u can only attack a particular person once a day.

how would i do this??? cookies wouldnt work as the user could just delete them, and neither sessions cuz the suer could just close the browser. all i can think of is an absolutely massive MySQL database, sure theres a better way?
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

It won't be that massive if all you have to keep track of is when the user last did an attack.

When a user attacks, store the time in mysql. Then when he tries to attack again, see if the recorded time is older than a day. It will take 2 querys, get the time & check, then update the time.
lizlazloz
Forum Commoner
Posts: 64
Joined: Mon Dec 29, 2003 7:29 am

Post by lizlazloz »

yes, but the thing is, i want the user to be able to attack all the other players in the game, just not the 1 they have already attacked. so if i did it the way you said i would need a field in each entry for every other user... right? and that aint gonna happen :?
ilovetoast
Forum Contributor
Posts: 142
Joined: Thu Jan 15, 2004 7:34 pm

Post by ilovetoast »

Four db fields:

id - primary key
attacker_id - the one attacking, foreign key for a separate user table
defender_id - the one being attacked, foreign key for a separate user table
attack_time - time of attack as timestamp of your choice

One table row per possible attack. The row only needs to exist if the attack actually occured and you can use a housekeeping routine to periodically discard badly out of date entries.

Then, as Sevengraff pointed out, just run two queries.

peace

eat toast all day long
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

make sure you have a cron to delete the tables everynight tho, or 1. your db will get huuuuge and 2. people won't be able to attacks others again
Post Reply