Burgershot
  • Home
  • Members
  • Team
  • Help
  • Search
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search
Burgershot SA-MP Pawn Scripting [Pawn] How does one make a top5?

 
  • 0 Vote(s) - 0 Average
Pawn How does one make a top5?
Z3fRaN
Offline

Burgershot Member
Posts: 9
Threads: 5
Joined: Feb 2021
Reputation: 0
Location: Egypt
#1
2021-02-26, 01:37 AM (This post was last modified: 2021-02-26, 02:23 AM by Z3fRaN.)
I don't have a good idea on how to make a top5 all time it, only thing I can think of is for(i=0;i<MAX_PLAYERS;i++) but don't know how to continue the rest.
Virsenas
Offline

Burgershot Member
Posts: 47
Threads: 0
Joined: Feb 2021
Reputation: 6
#2
2021-02-26, 02:18 AM
You need to be more specific about what you want to do. Is it an all time top or just for the online players?
Z3fRaN
Offline

Burgershot Member
Posts: 9
Threads: 5
Joined: Feb 2021
Reputation: 0
Location: Egypt
#3
2021-02-26, 02:22 AM (This post was last modified: 2021-02-26, 02:23 AM by Z3fRaN.)
more like top 5 killers of all time, i'm using yini so..
Virsenas
Offline

Burgershot Member
Posts: 47
Threads: 0
Joined: Feb 2021
Reputation: 6
#4
2021-02-26, 04:24 AM
(2021-02-26, 02:22 AM)Z3fRaN Wrote: more like top 5 killers of all time, i'm using yini so..

There is no need for loops. All you need is already there for you to use:

https://open.mp/docs/scripting/callbacks/OnPlayerDisconnect
https://open.mp/docs/scripting/callbacks/OnPlayerDeath

You make an enum that holds top5 players information (player names, number of kills, maybe time when they moved up in the top and so on...). On OnPlayerDeath callback you just make the checks if the player has more kills than top5 player or top4 player and so on. On that callback you do the checks and would be a good idea to save the top list after the top changes. Of course you will need to use Y_INI to load the top5 on OnGameModeInit also.
Kwarde
Offline

Burgershot Member
Posts: 99
Threads: 2
Joined: Sep 2020
Reputation: 8
Location: The Netherlands
#5
2021-02-26, 05:22 AM (This post was last modified: 2021-02-26, 05:26 AM by Kwarde.)
If you're using MySQL you can very easily select a top 5 based on certain data. For example, let's say we have this users table:\
Code:
+----+-----------+-------+--------+-------+
| id | name      | kills | deaths | score |
+----+-----------+-------+--------+-------+
|  1 | Kwarde    |    16 |     25 |     4 |
|  2 | Playa     |     2 |     19 |     1 |
|  3 | Z3fRaN    |    28 |      1 |   100 |
|  4 | Narf3z    |     1 |     28 |     0 |
|  5 | MathPi    |    99 |      0 |    50 |
|  6 | Slayer    |    66 |     13 |    25 |
|  7 | JimmyPage |     0 |      2 |     0 |
+----+-----------+-------+--------+-------+
Using this query:
Code:
SELECT name, kills FROM users ORDER BY kills DESC LIMIT 5;
This would select columns "name" and "kills" from the table "users" and order by kills, descending (DESC -- ASC would order ascending, and thus beginning with the lowest values). LIMIT 5 says: Limit to 5 results. The query itself should be pretty clear.
This would give this output:
Code:
+--------+-------+
| name   | kills |
+--------+-------+
| MathPi |    99 |
| Slayer |    66 |
| Z3fRaN |    28 |
| Kwarde |    16 |
| Playa  |     2 |
+--------+-------+
Virsenas
Offline

Burgershot Member
Posts: 47
Threads: 0
Joined: Feb 2021
Reputation: 6
#6
2021-02-26, 11:54 AM
(2021-02-26, 05:22 AM)Kwarde Wrote: If you're using MySQL you can very easily select a top 5 based on certain data. For example, let's say we have this users table:\
Code:
+----+-----------+-------+--------+-------+
| id | name      | kills | deaths | score |
+----+-----------+-------+--------+-------+
|  1 | Kwarde    |    16 |    25 |    4 |
|  2 | Playa    |    2 |    19 |    1 |
|  3 | Z3fRaN    |    28 |      1 |  100 |
|  4 | Narf3z    |    1 |    28 |    0 |
|  5 | MathPi    |    99 |      0 |    50 |
|  6 | Slayer    |    66 |    13 |    25 |
|  7 | JimmyPage |    0 |      2 |    0 |
+----+-----------+-------+--------+-------+
Using this query:
Code:
SELECT name, kills FROM users ORDER BY kills DESC LIMIT 5;
This would select columns "name" and "kills" from the table "users" and order by kills, descending (DESC -- ASC would order ascending, and thus beginning with the lowest values). LIMIT 5 says: Limit to 5 results. The query itself should be pretty clear.
This would give this output:
Code:
+--------+-------+
| name  | kills |
+--------+-------+
| MathPi |    99 |
| Slayer |    66 |
| Z3fRaN |    28 |
| Kwarde |    16 |
| Playa  |    2 |
+--------+-------+

Going through all user files is really unnecessary. This would need only 5 rows as the checking who has more kills would still be checked on the server live. There is no need to go through all user files.
Kwarde
Offline

Burgershot Member
Posts: 99
Threads: 2
Joined: Sep 2020
Reputation: 8
Location: The Netherlands
#7
2021-02-27, 02:26 PM (This post was last modified: 2021-02-27, 02:29 PM by Kwarde.)
Going through "user files"? Is unnecessary? Is an issue somehow? Do you know how MySQL works?

You found the secret text!
If your database (and server, and system, eventually) is set up properly there's no issue at all.
An old InnoDB users table from some server backup, 11.140 rows, 7.2MiB. 97 columns (this database wasn't set up properly). Using the query from above, it takes about 0.0155-0.020 seconds. Selecting all data (*) would take about 0.065-0.07 seconds.
It's on so many levels different than "going through user files" (as in ini-based includes).

Also keep in mind: MySQL is a relational database management system. It's designed (atleast one of the reasons) to be able to perform eventually complex queries to find data in seperate tables,eventually databases. Such a simple query as above is nothing
Virsenas
Offline

Burgershot Member
Posts: 47
Threads: 0
Joined: Feb 2021
Reputation: 6
#8
2021-02-27, 08:43 PM
(2021-02-27, 02:26 PM)Kwarde Wrote: Going through "user files"? Is unnecessary? Is an issue somehow? Do you know how MySQL works?

You found the secret text!
If your database (and server, and system, eventually) is set up properly there's no issue at all.
An old InnoDB users table from some server backup, 11.140 rows, 7.2MiB. 97 columns (this database wasn't set up properly). Using the query from above, it takes about 0.0155-0.020 seconds. Selecting all data (*) would take about 0.065-0.07 seconds.
It's on so many levels different than "going through user files" (as in ini-based includes).

Also keep in mind: MySQL is a relational database management system. It's designed (atleast one of the reasons) to be able to perform eventually complex queries to find data in seperate tables,eventually databases. Such a simple query as above is nothing

You can nag about the non-essential things, but I think you don't get my point and my point is:

5 rows < hundreds or thousands of rows / 1 file < hundreds or thousands of files / loading and saving data only for the top5 (5 players) instead of comparing data to hundreds or thousands of players. I know the time difference is not that great, but if I can make things smarter and better then I'm going to do it that way. If you want to do things another way and it works for you then nobody is stopping you. Go ahead. I just showed OP at least how I would have done it.
arber
Offline

Burgershot Member
Posts: 11
Threads: 0
Joined: Aug 2020
Reputation: 1
Location: Kosovo
#9
2021-02-27, 11:30 PM
(2021-02-27, 08:43 PM)Virsenas Wrote:
(2021-02-27, 02:26 PM)Kwarde Wrote: Going through "user files"? Is unnecessary? Is an issue somehow? Do you know how MySQL works?

You found the secret text!
If your database (and server, and system, eventually) is set up properly there's no issue at all.
An old InnoDB users table from some server backup, 11.140 rows, 7.2MiB. 97 columns (this database wasn't set up properly). Using the query from above, it takes about 0.0155-0.020 seconds. Selecting all data (*) would take about 0.065-0.07 seconds.
It's on so many levels different than "going through user files" (as in ini-based includes).

Also keep in mind: MySQL is a relational database management system. It's designed (atleast one of the reasons) to be able to perform eventually complex queries to find data in seperate tables,eventually databases. Such a simple query as above is nothing

You can nag about the non-essential things, but I think you don't get my point and my point is:

5 rows < hundreds or thousands of rows / 1 file < hundreds or thousands of files / loading and saving data only for the top5 (5 players) instead of comparing data to hundreds or thousands of players. I know the time difference is not that great, but if I can make things smarter and better then I'm going to do it that way. If you want to do things another way and it works for you then nobody is stopping you. Go ahead. I just showed OP at least how I would have done it.

He isnt saving data for only top 5 he is using the DESC command that is used to sort data in descending order and specifying the number of records he wants to return 5, 10, 50 [font=Verdana, sans-serif](keyword LIMIT).[/font]
Radical
Offline

Burgershot Member
Posts: 148
Threads: 21
Joined: Dec 2020
Reputation: 16
#10
2021-02-27, 11:45 PM (This post was last modified: 2021-02-27, 11:46 PM by Radical.)
(2021-02-26, 01:37 AM)Z3fRaN Wrote: I don't have a good idea on how to make a top5 all time it, only thing I can think of is for(i=0;i<MAX_PLAYERS;i++) but don't know how to continue the rest.


I can give you a code that gets top 5 online players, if you want it.
But to get the best players all the time you have to use MySQL:

(2021-02-26, 05:22 AM)Kwarde Wrote: If you're using MySQL you can very easily select a top 5 based on certain data. For example, let's say we have this users table:\

Code:
+----+-----------+-------+--------+-------+

| id | name      | kills | deaths | score |

+----+-----------+-------+--------+-------+

|  1 | Kwarde    |    16 |    25 |    4 |

|  2 | Playa    |    2 |    19 |    1 |

|  3 | Z3fRaN    |    28 |      1 |  100 |

|  4 | Narf3z    |    1 |    28 |    0 |

|  5 | MathPi    |    99 |      0 |    50 |

|  6 | Slayer    |    66 |    13 |    25 |

|  7 | JimmyPage |    0 |      2 |    0 |

+----+-----------+-------+--------+-------+

Using this query:

Code:
SELECT name, kills FROM users ORDER BY kills DESC LIMIT 5;

This would select columns "name" and "kills" from the table "users" and order by kills, descending (DESC -- ASC would order ascending, and thus beginning with the lowest values). LIMIT 5 says: Limit to 5 results. The query itself should be pretty clear.

This would give this output:

Code:
+--------+-------+

| name  | kills |

+--------+-------+

| MathPi |    99 |

| Slayer |    66 |

| Z3fRaN |    28 |

| Kwarde |    16 |

| Playa  |    2 |

+--------+-------+
Z3fRaN
Offline

Burgershot Member
Posts: 9
Threads: 5
Joined: Feb 2021
Reputation: 0
Location: Egypt
#11
2021-02-27, 11:55 PM
Thank you all for help.

(2021-02-27, 11:45 PM)Radical Wrote:
(2021-02-26, 01:37 AM)Z3fRaN Wrote: I don't have a good idea on how to make a top5 all time it, only thing I can think of is for(i=0;i<MAX_PLAYERS;i++) but don't know how to continue the rest.


I can give you a code that gets top 5 online players, if you want it.
But to get the best players all the time you have to use MySQL:

(2021-02-26, 05:22 AM)Kwarde Wrote: If you're using MySQL you can very easily select a top 5 based on certain data. For example, let's say we have this users table:\

Code:
+----+-----------+-------+--------+-------+

| id | name      | kills | deaths | score |

+----+-----------+-------+--------+-------+

|  1 | Kwarde    |    16 |    25 |    4 |

|  2 | Playa    |    2 |    19 |    1 |

|  3 | Z3fRaN    |    28 |      1 |  100 |

|  4 | Narf3z    |    1 |    28 |    0 |

|  5 | MathPi    |    99 |      0 |    50 |

|  6 | Slayer    |    66 |    13 |    25 |

|  7 | JimmyPage |    0 |      2 |    0 |

+----+-----------+-------+--------+-------+

Using this query:

Code:
SELECT name, kills FROM users ORDER BY kills DESC LIMIT 5;

This would select columns "name" and "kills" from the table "users" and order by kills, descending (DESC -- ASC would order ascending, and thus beginning with the lowest values). LIMIT 5 says: Limit to 5 results. The query itself should be pretty clear.

This would give this output:

Code:
+--------+-------+

| name  | kills |

+--------+-------+

| MathPi |    99 |

| Slayer |    66 |

| Z3fRaN |    28 |

| Kwarde |    16 |

| Playa  |    2 |

+--------+-------+

It's alright, I've done it with Y_INI, just need to test it for now.
« Next Oldest | Next Newest »



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Burgershot - Powered by our Community and MyBB Original Theme by Emerald

Linear Mode
Threaded Mode