I'm working on a opensource game, aim to connect it to an online database. I have basically finished the thing, just working on the optimisation side of it and have one issue. One of my biggest computation functions is working out players rankings. This is my structure and function at present. The main table with all the players map times is the records table.
Records
-------
MapID
PlayerID
Time
Each map is associated with a difficulty for the map and gives a certain amount of points for completing it, which is represented by the DifficultyToPoints table (The Maps table has a difficulty column):
DifficultyToPoints
---------------
Difficulty
Points
This is where I have a problem. Right now I am only giving people points (and then ranking them) according to which maps they have completed and the difficulty of these maps. But now I want to also give people points for their ranking within each individual map. E.G if you ranked first in map X then you get Y points for being first and Z for finishing it. I have a slight idea how I would go about it, but I feel that there is surely a far more efficient method out there.
Here are my current functions WITHOUT the additional points for map rank (MySQL):
I APOLOGISE for the wall of text, but a lot of it is quite straight forward code. To summarise my two issues is one the efficiency of the function (any suggestions to improve it?), and the second is adding points for map ranking.
This is my first time working on websites so all of this is quite new to me so I would appreciate any help. Thanks
Records
-------
MapID
PlayerID
Time
Each map is associated with a difficulty for the map and gives a certain amount of points for completing it, which is represented by the DifficultyToPoints table (The Maps table has a difficulty column):
DifficultyToPoints
---------------
Difficulty
Points
This is where I have a problem. Right now I am only giving people points (and then ranking them) according to which maps they have completed and the difficulty of these maps. But now I want to also give people points for their ranking within each individual map. E.G if you ranked first in map X then you get Y points for being first and Z for finishing it. I have a slight idea how I would go about it, but I feel that there is surely a far more efficient method out there.
Here are my current functions WITHOUT the additional points for map rank (MySQL):
FUNCTION GetPlayerPoints (Player INT) RETURNS INT (11)
DECLARE TempPoints INT;
TempPoints = 0;
Select SUM(DifficultyToPoints.Points) AS Points INTO TempPoints From DifficultyToPoints, Records, Maps WHERE Records.Map_ID = Maps.MapID AND CAST(Maps.Difficulty AS SIGNED INT) = DifficultyToPoints.Level AND Records.Player_ID = Player GROUP BY Records.Map_ID;
//Gets and sums up the points associated with the map's difficulty for every record a player has... I just added GROUP BY Records.Map_ID now because I realized that people would be getting multiple points for completing the same map. It could be wrong.
RETURN TempPoints ;
FUNCTION `GetRank`(Player INT) RETURNS INT (11)
DECLARE `TempRank` INT;
CREATE TEMPORARY TABLE TempPoints (
Points INT ,
Player_ID INT
);
//Creates a temp table for all the points of the players to be inserted into
INSERT INTO TempPoints
SELECT GetPlayerPoints (AccID),
AccID
FROM Players;
SET @rownum := 0;
SELECT rank INTO `TempRank` FROM (
SELECT @rownum := @rownum + 1 AS rank, Points, Player_ID
FROM TempPoints ORDER BY Points DESC
) as result WHERE Player_ID=Player;
//Ranks it
RETURN `TempRank` ;
I APOLOGISE for the wall of text, but a lot of it is quite straight forward code. To summarise my two issues is one the efficiency of the function (any suggestions to improve it?), and the second is adding points for map ranking.
This is my first time working on websites so all of this is quite new to me so I would appreciate any help. Thanks
Last edited: