· 6 years ago · Aug 18, 2019, 09:24 PM
1#pragma semicolon 1
2
3#define PLUGIN_AUTHOR "DeadSworn"
4#define PLUGIN_VERSION "1.01"
5
6#include <sourcemod>
7#include <clients>
8#include <dbi>
9#include <morecolors>
10
11#pragma newdecls required
12
13public Plugin myinfo =
14{
15 name = "Top Speed",
16 author = PLUGIN_AUTHOR,
17 description = "Records top speeds for dodgeball",
18 version = PLUGIN_VERSION,
19 url = ""
20};
21
22Menu g_MenuTop = null;
23
24
25ConVar g_CVPopMax;
26int g_iPopMax;
27
28ConVar g_CVMPH;
29bool g_bDisplayMph;
30
31ConVar g_CVAllowBots;
32bool g_bAllowBots;
33
34
35Database g_Database;
36DBStatement g_DBSMenuTop = null;
37
38int PlayerSpeeds[MAXPLAYERS];
39
40char gPluginTag[] = "\x07900c3f[\x01TopSpeed\x07900c3f]\x01";
41char gPluginCleanTag[] = "[TopSpeed]";
42
43public void OnPluginStart() {
44 if(!SQL_CheckConfig("top_speed")) {
45 LogError("%s Invalid database config", gPluginCleanTag);
46 }
47
48 char error[255];
49 g_Database = SQL_Connect("top_speed", true, error, sizeof(error));
50
51 if(g_Database == null) {
52 LogError("%s Could not connect: %s", gPluginCleanTag, error);
53 }
54
55 SQL_CreateTable();
56
57 g_CVPopMax = CreateConVar("sm_topspeed_population_max", "10", "Amount of players shown on the menu", _, true, 0.0);
58 g_iPopMax = GetConVarInt(g_CVPopMax);
59
60 g_CVMPH = CreateConVar("sm_topspeed_mph", "1", "Display in MPH", _, true, 0.0, true, 1.0);
61 g_bDisplayMph = GetConVarBool(g_CVMPH);
62
63 g_CVAllowBots = CreateConVar("sm_topspeed_allowbots", "1", "Allow top speeds against bots to be recorded", _, true, 0.0, true, 1.0);
64 g_bAllowBots = GetConVarBool(g_CVAllowBots);
65
66 // reg menu commands
67
68 RegAdminCmd("sm_topspeedlog", Command_LogSpeed, ADMFLAG_ROOT);
69 RegConsoleCmd("sm_ts", Command_PersonalTopSpeed, "Get personal top speed");
70 RegConsoleCmd("sm_tc", Command_TopSpeed, "Get Top Speeds");
71}
72
73// Hooks
74public void OnMapStart() {
75 GetTopSpeeds();
76}
77
78
79public void OnMapEnd() {
80 delete g_MenuTop;
81}
82
83public void OnConVarChanged(ConVar convar, const char[] oldValue, const char[] newValue) {
84 g_iPopMax = GetConVarInt(g_CVPopMax);
85 g_bDisplayMph = GetConVarBool(g_CVMPH);
86 g_bAllowBots = GetConVarBool(g_CVAllowBots);
87}
88
89/* on player join get data? */
90public void OnClientPostAdminCheck(int client) {
91 if(!IsValidClient(client)) {
92 return;
93 }
94
95 char auth[32];
96 GetClientAuthId(client, AuthId_Steam2, auth, sizeof(auth));
97
98 // define query
99 char query[512];
100 Format(query, sizeof(query), "SELECT `top_speed` FROM `ts_players` WHERE `steam_id` = '%s'", auth);
101
102 // query
103 DBResultSet data = SQL_Query(g_Database, query);
104
105 char error[255];
106 if(SQL_GetError(g_Database, error, sizeof(error))) {
107 LogError("%s Error getting player data: %s", gPluginCleanTag, error);
108 }
109
110 char buffer[MAX_NAME_LENGTH];
111 GetClientName(client, buffer, sizeof(buffer));
112
113 char escaped_username[MAX_NAME_LENGTH*2+1];
114 SQL_EscapeString(g_Database, buffer, escaped_username, sizeof(escaped_username));
115
116 bool GotData = SQL_FetchRow(data);
117 if(data.RowCount == 0) {
118 SQL_FastQuery(g_Database, "SET NAMES \'utf8\'");
119 Format(query, sizeof(query), "INSERT INTO `ts_players` (`steam_id`, `username`) VALUES ('%s', '%s')", auth, escaped_username);
120 SQL_FastQuery(g_Database, query);
121 }
122
123 if(GotData) {
124 PlayerSpeeds[client] = SQL_FetchInt(data, 0);
125 }
126
127 CloseHandle(data);
128}
129
130public void OnClientDisconnect(int client) {
131 PlayerSpeeds[client] = 0;
132}
133
134public Action Command_LogSpeed(int server, int args) {
135 if(server != 0 ) {
136 return Plugin_Handled;
137 }
138
139 char buffer[32];
140 int client;
141 int rocket_target;
142 int speed;
143
144 GetCmdArg(1, buffer, sizeof(buffer));
145 client = StringToInt(buffer);
146
147 GetCmdArg(2, buffer, sizeof(buffer));
148 rocket_target = StringToInt(buffer);
149
150 if(!IsValidClient(client)) {
151 return Plugin_Handled;
152 }
153
154 if(!g_bAllowBots){
155 if(!IsValidClient(rocket_target)) {
156 return Plugin_Handled;
157 }
158 }
159
160 GetCmdArg(3, buffer, sizeof(buffer));
161 speed = RoundFloat(StringToFloat(buffer));
162
163 if(speed < PlayerSpeeds[client]) {
164 return Plugin_Handled;
165 } else {
166 PlayerSpeeds[client] = speed;
167 }
168
169 char auth[32];
170 GetClientAuthId(client, AuthId_Steam2, auth, sizeof(auth));
171
172 char query[512];
173 Format(query, sizeof(query), "UPDATE `ts_players` SET `top_speed` = '%d' WHERE `steam_id` = '%s' AND '%d' > `top_speed`", speed, auth, speed);
174 bool success = SQL_FastQuery(g_Database, query);
175
176 if(success) {
177 if(g_bDisplayMph) {
178 PrintToChat(client, "%s New personal record: %d", gPluginTag, HammerToMPH(speed));
179 } else {
180 PrintToChat(client, "%s New personal record: %d", gPluginTag, speed);
181 }
182 }
183
184 return Plugin_Handled;
185}
186
187// Commands
188public Action Command_PersonalTopSpeed(int client, int args) {
189 CPrintToChatAll("{FULLRED}%N's {DEFAULT}Highest speed is {TURQUOISE}%d {DEFAULT}mph!", client, PlayerSpeeds[client]);
190}
191
192public Action Command_TopSpeed(int client, int args) {
193 if(g_MenuTop == null) {
194 GetTopSpeeds();
195 }
196
197 g_MenuTop.Display(client, 10);
198
199 return Plugin_Handled;
200}
201
202
203// Menu
204Menu BuildTopMenu(Handle query) {
205 Menu menu = new Menu(Menu_Top);
206
207 int buffer;
208 char info[512];
209 char name[MAX_NAME_LENGTH];
210
211 while(SQL_FetchRow(query)) {
212 SQL_FetchString(query, 0, name, sizeof(name));
213 buffer = SQL_FetchInt(query, 1);
214 Format(info, sizeof(info), "%d;%s", buffer, name);
215 menu.AddItem(info, name);
216 }
217
218 menu.SetTitle("Top Speeds");
219
220 return menu;
221}
222
223public int Menu_Top(Menu menu, MenuAction action, int param1, int param2) {
224 if(action == MenuAction_Select) {
225 char info[32];
226
227 /* Get item info */
228 bool found = menu.GetItem(param2, info, sizeof(info));
229
230 int top_speed;
231 char buffer[32];
232 char name[MAX_NAME_LENGTH];
233
234 int index = SplitString(info, ";", buffer, sizeof(buffer));
235 top_speed = StringToInt(buffer);
236
237 Format(name, sizeof(name), "%s", info[index]);
238
239 Menu_DisplayPlayer(param1, name, sizeof(name), top_speed);
240
241 //PrintToChatAll("%s;%s", top_speed, name);
242
243 }
244}
245
246public int Menu_Handler(Menu menu, MenuAction action, int param1, int param2) {
247 if(action == MenuAction_End){
248 delete menu;
249 } else if(action == MenuAction_VoteEnd) {
250
251 } else if(action == MenuAction_DisplayItem) {
252
253 } else if(action == MenuAction_Display) {
254
255 }
256}
257
258void Menu_DisplayPlayer(int client, char[] name, int maxlen, int top_speed) {
259 Menu menu = new Menu(Menu_Handler);
260 menu.SetTitle("%s", name);
261 char displayString[64];
262 bool mph = GetConVarBool(g_CVMPH);
263
264 if(mph) {
265 Format(displayString, sizeof(displayString), "TopSpeed: %.1fmph", HammerToMPH(top_speed));
266 } else {
267 Format(displayString, sizeof(displayString), "TopSpeed: %d", top_speed);
268 }
269 menu.AddItem("", displayString);
270 menu.Display(client, 60);
271 menu.ExitButton = true;
272 return;
273}
274
275
276// SQL
277void GetTopSpeeds() {
278 /* Check if we haven't already created the statement */
279 if(g_DBSMenuTop == null) {
280 char error[255];
281 SQL_FastQuery(g_Database, "SET NAMES \'utf8\'");
282 g_DBSMenuTop = SQL_PrepareQuery(g_Database, "SELECT `username`, `top_speed` FROM ts_players WHERE `display` = 1 ORDER BY `top_speed` DESC LIMIT ?", error, sizeof(error));
283 if(g_DBSMenuTop == null) {
284 LogError("%s %s", gPluginCleanTag, error);
285 return;
286 }
287 }
288
289 int populationMax = GetConVarInt(g_CVPopMax);
290 SQL_BindParamInt(g_DBSMenuTop, 0, populationMax, false);
291
292 if(!SQL_Execute(g_DBSMenuTop)) {
293 return;
294 }
295 g_MenuTop = BuildTopMenu(g_DBSMenuTop);
296}
297
298void SQL_CreateTable() {
299 char query[1028];
300 Format(query, sizeof(query), "CREATE TABLE IF NOT EXISTS `ts_players` ( \
301 `id` int(32) NOT NULL AUTO_INCREMENT PRIMARY KEY, \
302 `steam_id` varchar(32) NOT NULL, \
303 `username` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, \
304 `top_speed` int(32) NOT NULL DEFAULT '0', \
305 `display` tinyint(1) NOT NULL DEFAULT '1', \
306 UNIQUE KEY `steam_id` (`steam_id`) \
307 );");
308 SQL_FastQuery(g_Database, query);
309
310 char error[255];
311 if(SQL_GetError(g_Database, error, sizeof(error))) {
312 LogError("[TopSpeed] Error in creating table: %s", error);
313 }
314}
315
316/*
317CREATE TABLE IF NOT EXISTS ts_players (
318 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
319 `steam_id` varchar(32) NOT NULL,
320 `username` varchar(128) NOT NULL,
321 `top_speed` int(32) NOT NULL DEFAULT '0',
322 `display` tinyint(1) NOT NULL DEFAULT '1',
323); */
324
325stock bool IsValidClient(int client) {
326 if(client <= 0 || client > MaxClients || !IsClientInGame(client) || IsFakeClient(client)) {
327 return false;
328 }
329
330 return true;
331}
332
333stock int HammerToMPH(int speed) {
334 return RoundFloat(speed * 0.0426496); // * 1.904) * 0.0224
335}