-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathberobot_db_winrate_catcher.sp
284 lines (232 loc) · 11.4 KB
/
berobot_db_winrate_catcher.sp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <tf2>
#include <tf2_stocks>
#include <morecolors>
#include <sm_logger>
#include <berobot_constants>
#include <berobot>
#include <berobot_core>
#define PLUGIN_VERSION "0.1"
Database hDatabase = null;
char g_map_name[256] = "";
char g_ServerName[64] = "";
// int g_Map_Timestamp_ID;
char g_time[64];
bool g_random_robot[MAXPLAYERS + 1] = {false,...};
public void OnMapStart()
{
//Generate Timestamp here, each map has it's own timestamp
// char g_time[64];
int Map_Timestamp_ID = GetTime();
FormatTime(g_time, sizeof(g_time), "%Y-%m-%d %H:%M:%S", Map_Timestamp_ID);
GetCurrentMap(g_map_name, sizeof(g_map_name)); // Get map name. (itemtest, ctf_2fort)
GetMapDisplayName(g_map_name, g_map_name, sizeof(g_map_name));
Handle convar = FindConVar("hostname");
GetConVarString(convar, g_ServerName, sizeof(g_ServerName));
Database.Connect(GotDatabase, "mm_winrates");
}
public void OnPluginStart()
{
HookEvent("teamplay_round_win", Event_teamplay_robot_win_table, EventHookMode_Post);
// HookEvent("teamplay_round_win", Event_teamplay_human_comp, EventHookMode_Post);
}
public void GotDatabase(Database db, const char[] error, any data)
{
PrintToServer("Database connection %s", db ? "successful" : "failed");
if (db == null)
{
LogError("Database failure: %s", error);
}
else
{
hDatabase = db;
}
InitializeTablesFirstRun(db);
}
public void InitializeTablesFirstRun(Database db){
if (db) {
// If the connection is successful, create the table
SQL_FastQuery(db, "CREATE TABLE IF NOT EXISTS mm_robot_win (id INT AUTO_INCREMENT PRIMARY KEY, game_id_timestamp TIMESTAMP, win_team VARCHAR(3), total_gametime BIGINT, map VARCHAR(255), player_count INT, robot_count INT, human_count INT, robot_team_win TINYINT, server VARCHAR(128))");
SQL_FastQuery(db, "CREATE TABLE IF NOT EXISTS mm_robot_individual_win (id INT AUTO_INCREMENT PRIMARY KEY, game_id_timestamp TIMESTAMP, client_steamId VARCHAR(30), robot_name VARCHAR(30), total_gametime BIGINT, robot_team_win TINYINT)");
SQL_FastQuery(db, "CREATE TABLE IF NOT EXISTS mm_robot_pickrate (id INT AUTO_INCREMENT PRIMARY KEY, game_id_timestamp TIMESTAMP, robot_name VARCHAR(30), map VARCHAR(255), team VARCHAR(3), random TINYINT, pick_time BIGINT, client_id VARCHAR(20), server VARCHAR(128))");
SQL_FastQuery(db, "CREATE TABLE IF NOT EXISTS mm_robot_human_comp (id INT AUTO_INCREMENT PRIMARY KEY, game_id_timestamp TIMESTAMP, client_steamId VARCHAR(30), class TINYINT, total_gametime BIGINT)");
SQL_FastQuery(db, "CREATE TABLE IF NOT EXISTS classes (class_id INT PRIMARY KEY, class_name VARCHAR(20))");
SQL_FastQuery(db, "INSERT INTO classes (class_id, class_name) VALUES \
(0, 'Unknown'), \
(1, 'Scout'), \
(2, 'Sniper'), \
(3, 'Soldier'), \
(4, 'DemoMan'), \
(5, 'Medic'), \
(6, 'Heavy'), \
(7, 'Pyro'), \
(8, 'Spy'), \
(9, 'Engineer')");
}
}
public Action Event_teamplay_robot_win_table(Event event, char[] name, bool dontBroadcast)
{
// 2 = red
// 3 = blu
int team = GetEventInt(event, "team");
int RobotTeam = GetRobotTeam();
char win_team_str[4];
if (team == TFTeam_Blue) {
win_team_str = "BLU";
} else if (team == TFTeam_Red) {
win_team_str = "RED";
}
else
{
win_team_str = "ERR";
}
// PrintToChatAll("Time: %s",time);
int total_gametime = RoundToNearest(GetGameTime());
int robot_team_win = (team == RobotTeam) ? 1 : 0;
int CurrentRobots = GetCurrentRobotCount();
int CurrentHumans = GetCurrentHumanCount();
int player_count = CurrentRobots+CurrentHumans;
// g_Map_Timestamp_ID = g_Map_Timestamp_ID;
// PrintToChatAll("Round Won: Timestamp: %s, Robot team was %i, Gametime was: %i Robot win? %i, ROBOT STR %s, MAP WAS %s", g_time, RobotTeam, total_gametime, robot_team_win, win_team_str,g_map_name);
// timestamp, , total_gametime, g_map_name. player_count, CurrentRobots, CurrentHumans,robot_team_win
// SQL_FastQuery(hDatabase, "INSERT INTO mm_robot_win (timestamp, team, total_gametime, map, player_count, robot_team_win) VALUES (NOW(), 'RED', 600, 'cp_dustbowl', 12, 1)");
// Create the SQL query string with formatted variables
// PrintToChatAll("%s",g_ServerName);
char query[1024];
hDatabase.Format(query, sizeof(query), "INSERT INTO mm_robot_win (game_id_timestamp, win_team, total_gametime, map, player_count, robot_count, human_count, robot_team_win, server) VALUES ('%s', '%s', %i, '%s', %i, %i, %i, %i, '%s')", g_time, win_team_str, total_gametime, g_map_name, player_count, CurrentRobots, CurrentHumans, robot_team_win, g_ServerName);
SQL_FastQuery(hDatabase, query);
// char query[1024];
// Format(query, sizeof(query), "INSERT INTO mm_robot_win (game_id_timestamp, win_team, total_gametime, map, player_count, robot_count, human_count, robot_team_win, server) VALUES ('%s', '%s', %i, '%s', %i, %i, %i, %i, '%s')", g_time, win_team_str, total_gametime, g_map_name, player_count, CurrentRobots, CurrentHumans, robot_team_win, g_ServerName);
// Execute the SQL query in a threaded manner
// hDatabase.Query(T_DBInsert, query);
// Execute the SQL query
//Code for individual players
for(int i = 1; i <= MaxClients; i++)
{
if (IsValidClient(i))
{
char steamID[45];
GetClientAuthId(i, AuthId_SteamID64, steamID, sizeof(steamID));
if(StrEqual(steamID, "STEAM_ID_STOP_IGNORING_RETVALS")) {
steamID = "BOT";
}
// if (!IsAnyRobot(i))
// {
// // char query[1024];
// // Get the player's current class
// int playerClass = TF2_GetPlayerClass(i);
// hDatabase.Format(query, sizeof(query), "INSERT INTO mm_robot_human_comp (game_id_timestamp, client_steamId, class, total_gametime) VALUES ('%s', '%s', %i, %i)", g_time, steamID, playerClass, total_gametime);
// SQL_FastQuery(hDatabase, query);
// }
// if (!IsAnyRobot(i))
// {
// // Get the player's current class
// int playerClass = TF2_GetPlayerClass(i);
char query[512];
if (!IsAnyRobot(i))
{
int playerClass = TF2_GetPlayerClass(i);
Format(query, sizeof(query), "INSERT INTO mm_robot_human_comp (game_id_timestamp, client_steamId, class, total_gametime) VALUES ('%s', '%s', %d, %d)", g_time, steamID, playerClass, total_gametime);
// Execute the SQL query in a threaded manner
hDatabase.Query(T_DBInsert, query);
}
// }
// if (IsAnyRobot(i))
// {
// char robotName[NAMELENGTH];
// Robot robot;
// GetRobot(i, robotName, NAMELENGTH);
// GetRobotDefinition(robotName, robot);
// // PrintToChatAll("Robot name was %s", robotName);
// hDatabase.Format(query, sizeof(query), "INSERT INTO mm_robot_individual_win (game_id_timestamp, client_steamId, robot_name, total_gametime, robot_team_win) VALUES ('%s', '%s', '%s', %i, %i)", g_time, steamID, robotName, total_gametime, robot_team_win);
// SQL_FastQuery(hDatabase, query);
// }
if (IsAnyRobot(i))
{
char robotName[NAMELENGTH];
Robot robot;
GetRobot(i, robotName, NAMELENGTH);
GetRobotDefinition(robotName, robot);
// PrintToChatAll("Robot name was %s", robotName);
// hDatabase.Format(query, sizeof(query), "INSERT INTO mm_robot_individual_win (game_id_timestamp, client_steamId, robot_name, total_gametime, robot_team_win) VALUES ('%s', '%s', '%s', %i, %i)", g_time, steamID, robotName, total_gametime, robot_team_win);
// SQL_FastQuery(hDatabase, query);
Format(query, sizeof(query), "INSERT INTO mm_robot_individual_win (game_id_timestamp, client_steamId, robot_name, total_gametime, robot_team_win) VALUES ('%s', '%s', '%s', %i, %i)", g_time, steamID, robotName, total_gametime, robot_team_win);
hDatabase.Query(T_DBInsert, query);
}
// if (IsAnyRobot(i))
// {
// char robotName[NAMELENGTH];
// Robot robot;
// GetRobot(i, robotName, NAMELENGTH);
// GetRobotDefinition(robotName, robot);
// char query[512];
// Format(query, sizeof(query), "INSERT INTO mm_robot_individual_win (game_id_timestamp, client_steamId, robot_name, total_gametime, robot_team_win) VALUES ('%s', '%s', '%s', %i, %i)", g_time, steamID, robotName, total_gametime, robot_team_win);
// // Execute the SQL query in a threaded manner
// hDatabase.Query(T_DBInsert, query);
// }
}
}
return Plugin_Continue;
}
public void T_DBInsert(Database db, DBResultSet results, const char[] error, any data)
{
if (db == null || error[0] != '\0')
{
LogError("Threaded query failed: %s", error);
}
else
{
// Query successful, handle the result if needed
}
}
public void MM_PickRobotAndPreviousRobot(int client, int random, const char[] robotName)
{
// MC_PrintToChatAll("Call was for %N with string:%s:", client, robotName);
char steamID[45];
GetClientAuthId(client, AuthId_SteamID64, steamID, sizeof(steamID));
if(StrEqual(steamID, "STEAM_ID_STOP_IGNORING_RETVALS")) {
steamID = "BOT";
}
int team = GetClientTeam(client);
char team_str[4];
if (team == TFTeam_Blue) {
team_str = "BLU";
} else if (team == TFTeam_Red) {
team_str = "RED";
}
else
{
team_str = "ERR";
}
int pick_time = RoundToNearest(GetEngineTime());
char query[1024];
// hDatabase.Format(query, sizeof(query), "INSERT INTO mm_robot_pickrate (game_id_timestamp, robot_name, map, team, random, pick_time, client_id, server) VALUES ('%s', '%s', '%s', '%s', %i, %i, '%s', '%s')",
// g_time, robotName, g_map_name, team_str, g_random_robot[client], pick_time, steamID, g_ServerName);
Format(query, sizeof(query), "INSERT INTO mm_robot_pickrate (game_id_timestamp, robot_name, map, team, random, pick_time, client_id, server) VALUES ('%s', '%s', '%s', '%s', %i, %i, '%s', '%s')",
g_time, robotName, g_map_name, team_str, g_random_robot[client], pick_time, steamID, g_ServerName);
// SQL_FastQuery(hDatabase, query);
hDatabase.Query(T_DBInsert, query);
// SQL_TQuery()
// PrintToConsoleAll(query);
g_random_robot[client] = false;
}
public void MM_WasRandomRobotForward(int client)
{
g_random_robot[client] = true;
}
// public void InsertCallback(Handle db, Handle queryHandle, any data)
// {
// // Check if the query was successful
// if (SQL_FinishQuery(queryHandle))
// {
// PrintToServer("Data insertion successful");
// }
// else
// {
// LogError("Data insertion failed: %s", SQL_GetLastError(db));
// }
// // Release the query handle
// SQL_FreeHandle(queryHandle);
// }