i'm trying to develop a simple multiplayer turn base game, i'm start creating the grid map, i have already write the javascript script to display the range movement for the specific unit, but my problem is how to check if the grid position sent to server by the client is a valid position, i think this is a operation to do on server side (i use php) because javascript is easy editable by the user, my first throught is to save in a database each tile and its data (example: is a water tile, a forest tile...), so when the position is send to server i can read the db and check if the position is valid, but i think this method is very slow and require a lot of server resources, so there is a better way to do this? (i don't know if this is the correct site or is a question for StackOverflow)
1 Answer
Your idea is not a bad solution in this case. Because your game is turn based, the amount of time it takes to do a database search shouldn't be noticeable. Because you are checking in a PHP script, which I assume is not constantly running, you can't keep the map data in memory at all times so storing it on the file system is the other solution (unless you want to use a in-memory database like Redis). If you want to ensure that the database lookup is quick...make sure the primary key is the index of the tile. To calculate the index of the tile, use the formula: x + (y*mapWidth)
where mapWidth is the number of tiles in each row and x/y is the position of the tile in the grid. If you don't want to store the tile data per-row (I would recommend against storing each tile in a table row) you can store map data in a text blob as a comma-separated list of all the tiles, then convert that to an array and search by the index.
-
\$\begingroup\$ thanks for the fast reply, i'm using php socket on server side so the script is always running to send message to clients, and there will be multiple map active in the same moment so i think i will try the text blob solution \$\endgroup\$ May 14, 2014 at 17:39