· 5 years ago · Jul 31, 2020, 06:16 AM
1/*
2
3 Server Information Fetcher Copyrights 2020
4 Script by Oblivion
5
6
7Notes:
8Script uses MySQL r39-6(also Latest Version) and foreach.
9Please edit the mysql_connect under OnFilterScriptInit
10Tables will be automatically created when script is loaded for first time.
11Load the Script along with the gamemode.(No /rcon loadfs bla bla).
12 Script will print a console message after 10 seconds of server start that "Server Information Has been Updated/Inserted"
13 which means it's succcess.
14
15Issues regarding Script? You can contact me at Discord: Oblivion#6693
16*/
17
18#include <a_samp>
19#include <foreach>
20
21
22#define MySQL_VER_Latest (false) // make this true , if you are using latest mysql version.
23
24#if MySQL_VER_Latest == true
25 #include <a_mysql41>
26#else
27 #include <a_mysql>
28#endif
29
30
31
32new svrinfo, query[150];
33
34enum sinfo
35{
36 hostname[80],
37 mapname[50],
38 gamemodetext[100],
39 version[20],
40 worldtime[10],
41 language[30],
42 max_players,
43 onlineplayers
44}
45new ServerInfo[sinfo];
46
47enum pinfo
48{
49 score,
50 name[MAX_PLAYER_NAME],
51 ping,
52 timer
53}
54new pInfo[MAX_PLAYERS][pinfo];
55public OnFilterScriptInit()
56{
57
58 print("=================================== ");
59 print(" Mumbai Gaming Server Data Fetcher ");
60 print(" by Oblivion - Loaded ");
61 print("=================================== ");
62
63 #if MySQL_VER_Latest == true
64 svrinfo = mysql_connect("localhost", "root", "", "configdb");
65 #else
66 svrinfo = mysql_connect("localhost", "root", "configdb", "");
67 #endif
68
69
70 //Create tables
71 CreateServerTables();
72
73 ServerInfo[onlineplayers] = 0;
74
75 SetTimer("ExecuteQuery", 10000, false); // execute query after 10 seconds (For gameemode to load and get data from it)
76 return 1;
77}
78
79
80
81public OnFilterScriptExit()
82{
83 print("===================================");
84 print(" Mumbai Gaming Server Data Fetcher ");
85 print(" by Oblivion - Unloaded ");
86 print("===================================");
87
88 // Kill the Player Timer
89 foreach(new i : Player) if(IsPlayerConnected(i)) KillTimer(pInfo[i][timer]);
90
91 // Make the Online Players to default 0 and update it
92 ServerInfo[onlineplayers] = 0;
93 UpdatePlayerBase(ServerInfo[onlineplayers]);
94
95
96 // Deleting all the rows in playerinfos is a good choice when script exit.
97 mysql_tquery(svrinfo, "DELETE FROM `playerinfos`", " ","");
98
99 // close connection
100 mysql_close(svrinfo);
101 return 1;
102}
103
104
105// Executes 10 seconds
106forward ExecuteQuery();
107public ExecuteQuery()
108{
109 return mysql_tquery(svrinfo, "SELECT * FROM `serverconfig`", "SaveServerInfo", "" "");
110}
111
112// Insert/Update Player Info into the database
113forward SaveServerInfo();
114public SaveServerInfo()
115{
116
117 GetConsoleVarAsString("hostname", ServerInfo[hostname], sizeof(ServerInfo[hostname]));
118
119 GetConsoleVarAsString("mapname", ServerInfo[mapname], sizeof(ServerInfo[mapname]));
120
121
122 GetConsoleVarAsString("version", ServerInfo[version], sizeof(ServerInfo[version]));
123
124
125 GetConsoleVarAsString("worldtime",ServerInfo[worldtime], sizeof(ServerInfo[worldtime]));
126
127 ServerInfo[max_players] = GetConsoleVarAsInt("maxplayers");
128
129 GetConsoleVarAsString("gamemodetext", ServerInfo[gamemodetext], sizeof(ServerInfo[gamemodetext]));
130
131 GetConsoleVarAsString("language",ServerInfo[language], sizeof(ServerInfo[language]));
132
133 new insertquery[800];
134 if(cache_num_rows() == 0)
135 {
136 format(insertquery, sizeof(insertquery), "INSERT INTO `serverconfig`(`hostname`,`gameodetext`,`playersonline`, `maxplayers`, `map`, `language`, `time`, `version`) VALUES ('%s', '%s' , %d , %d , '%s','%s','%s','%s')", ServerInfo[hostname],ServerInfo[gamemodetext],ServerInfo[onlineplayers],ServerInfo[max_players],
137 ServerInfo[mapname], ServerInfo[language],ServerInfo[worldtime],ServerInfo[version]);
138 }
139 else
140 {
141
142 format(insertquery, sizeof(insertquery), "UPDATE `serverconfig` SET `hostname`='%s',`gameodetext`='%s' ,`playersonline`=%d, `maxplayers`=%d, `map`='%s', `language`='%s', `time`='%s', `version`='%s'",
143 ServerInfo[hostname],ServerInfo[gamemodetext],ServerInfo[onlineplayers],ServerInfo[max_players],
144 ServerInfo[mapname], ServerInfo[language],ServerInfo[worldtime],ServerInfo[version]);
145 }
146 mysql_tquery(svrinfo, insertquery,"", "");
147 printf("Server Information has been Updated/Inserted!");
148 return 1;
149}
150
151
152public OnPlayerConnect(playerid)
153{
154 // Get Player Name will be usefull for searching player name in playerinfos table
155 GetPlayerName(playerid, pInfo[playerid][name], MAX_PLAYER_NAME);
156
157 mysql_format(svrinfo, query, sizeof query, "SELECT * FROM `playerinfos` WHERE `name`='%s'", pInfo[playerid][name]);
158 mysql_tquery(svrinfo, query, "InsertPlayerInfo", "i", playerid);
159 return 1;
160}
161
162
163forward InsertPlayerInfo(playerid);
164public InsertPlayerInfo(playerid)
165{
166
167 if(cache_num_rows() == 0)
168 {
169 ServerInfo[onlineplayers]++;
170 UpdatePlayerBase(ServerInfo[onlineplayers]);
171
172
173 format(query, sizeof(query), "INSERT INTO `playerinfos`(`id`,`name`) VALUES (%i, '%s')", playerid, pInfo[playerid][name]);
174 mysql_tquery(svrinfo, query,"", "");
175
176 // Start Timer each 2 seconds
177 pInfo[playerid][timer] = SetTimerEx("UpdatePlayerInfo", 1000, true, "i", playerid);
178 return 1;
179 }
180 return 1;
181}
182
183
184
185public OnPlayerDisconnect(playerid, reason)
186{
187 // detect if player is connected!
188 if(IsPlayerConnected(playerid))
189 {
190 KillTimer(pInfo[playerid][timer]);
191
192 ServerInfo[onlineplayers]--;
193 UpdatePlayerBase(ServerInfo[onlineplayers]);
194
195 // We don't need that playerid anymore.
196
197 mysql_format(svrinfo, query, sizeof query, "DELETE FROM `playerinfos` WHERE `name`='%s'", pInfo[playerid][name]);
198 mysql_tquery(svrinfo, query, "", "");
199 }
200 return 1;
201}
202
203forward UpdatePlayerInfo(playerid);
204public UpdatePlayerInfo(playerid)
205{
206 // stop execution if player is not connected!
207 if(!IsPlayerConnected(playerid))
208 {
209 return 1;
210 }
211 pInfo[playerid][score] = GetPlayerScore(playerid);
212 pInfo[playerid][ping] = GetPlayerPing(playerid);
213
214
215 mysql_format(svrinfo, query, sizeof query, "UPDATE `playerinfos` SET `score`=%d ,`ping`=%d WHERE `name`='%s'", pInfo[playerid][score], pInfo[playerid][ping], pInfo[playerid][name]);
216 mysql_tquery(svrinfo, query, "", "");
217 return 1;
218}
219
220stock CreateServerTables()
221{
222
223 // Server Config Table
224 new tableconfig[500];
225 strcat(tableconfig, "CREATE TABLE IF NOT EXISTS `serverconfig`");
226 strcat(tableconfig, "( `hostname` VARCHAR(80) NOT NULL , `gameodetext` VARCHAR(100) NOT NULL , `playersonline` INT(11) NOT NULL,");
227 strcat(tableconfig, "`maxplayers` INT(10) NOT NULL , `map` VARCHAR(50) NOT NULL , `language` VARCHAR(30) NOT NULL , `time` VARCHAR(10) NOT NULL ,");
228 strcat(tableconfig, "`version` VARCHAR(20) NOT NULL ) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4;");
229 mysql_tquery(svrinfo, tableconfig, "", "");
230
231 // player info table
232 mysql_tquery(svrinfo,"CREATE TABLE IF NOT EXISTS `playerinfos` (\
233 `id` int(11) DEFAULT 0,\
234 `name` varchar(24) DEFAULT NULL,\
235 `score` int(11) NOT NULL DEFAULT '0',\
236 `ping` int(11) NOT NULL DEFAULT '0'\
237 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;", "","");
238 return 1;
239}
240
241// Updates onlineplayers count in ServerConfigs
242stock UpdatePlayerBase(playerbase)
243{
244 mysql_format(svrinfo, query, sizeof query, "UPDATE `serverconfig` SET `playersonline`=%d", playerbase);
245 mysql_tquery(svrinfo, query, "", "");
246 return 1;
247}