开发者

Allow access to a game every few hours using mysql and php without long code?

开发者 https://www.devze.com 2023-03-23 13:26 出处:网络
I have some games I have programmed for members to play. I usually allow members to play games every day, and I have a cronjob resetting the value开发者_开发知识库 at midnight to allow access to the g

I have some games I have programmed for members to play. I usually allow members to play games every day, and I have a cronjob resetting the value开发者_开发知识库 at midnight to allow access to the games again the next day.

I constructed a new game, and I wanted it to allow members to play every 2 hours. I realize I cannot do this with a cronjob, because members will play the game at different times.

I know this probably sounds bad, but I'm not very familiar with timestamps, so I really don't know where to start. I haven't really had any reason to look into it until now. I'm guessing that the time or now function will accomplish this when compared, but again I cannot find the relevant situation in the manual about doing this with mysql and successfully submitting that data in the same format.

I've seen examples of other programmers doing this in a certain way, but it seemed they went to unnecessary lengths to make it work. The example I've seen would add a lot of lines to my code.

If this is a repeat question, I apologize, but I don't know what keywords to search for in this situation. All I have gotten is javascript countdowns.


Well the first thing that you're going to need is a way to determine when each member has played the game. So you will need to create a table with the following information:

MemberID
GameID (In case you want to support more than just 1 game in the future using this model)
DateTimePlayed

So now the first problem you have to solve is "Has player X played game Y in the last 2 hours?" That can be solved with a simple query:

SELECT * FROM MemberGameHistory WHERE MemberID = X and GameID = Y and DateTimePlayed > DATE_SUB(NOW(), INTERVAL 2 HOURS)

If you're happy that they haven't played it and decide to let them in, then you need to insert a row so that the next time you run the query you'll see that they've done it:

INSERT INTO MemberGameHistory (MemberID, GameID, DateTimePlayed) VALUES (X, Y, NOW())

Does that solve your problem?

0

精彩评论

暂无评论...
验证码 换一张
取 消