How to save game progress in MySQL database

I am developing a website that is a kind of game. User progress is stored in the MySQL database.

I want to do this if the table is saved while maintaining the column (ID) and moving the column where the progress has a data type text. When the user starts, progress is set (for example, "0"). If he goes to level 1, progress is set to "0 # 1", the second level makes him "0 # 1 # 2". The order of the levels is free, and I want to keep it. So progress can be "0 # 4 # 2 # 15", etc.

Is this a good way to do this? I have no experience with SQL, and I do not want to do something incredibly stupid. I read so confusing information about tables, foreign keys, and something else ...

I want to thank you for reading this, and I look forward to hearing.

Ryan

+5
source share
3 answers

The answer to your question 1

I would not approach your problem this way. I would create 3 tables: a table Levels(primary key "levelKey"), a table Users(primary key "userKey") and a table User_Levelswith a compound key "levelKey" and "userKey" ". When the user completes the level, just insert it into the table User_Levels. Then, To find out if a user has completed a level, simply select:

SELECT 'a' FROM User_Levels WHERE userKey = ? AND levelKey = ?

If the number of rows> 0, the user completed the level

+2
source

2, , . , , . "" , .

watcher . , User_Levels, - 1 # 3 # 4 # 9

, , ajax, . jQuery $.post method, . , , URLRequest.

+2

About your first question, if the game / levels are non-linear personally, I would take a different approach; I would just add a table that contains a column for the user ID and a column for the completed level. Therefore, if user 1 completed levels 0, 4, and 7, my table will have 3 rows:

UID  levels_completed
1    0
1    4
1    7

As for other issues, you can use javascript events and ajax to detect page closures, but I would not rely on this; I would just run queries when necessary. And if your session is destroyed, you are too late ...

+1
source

All Articles