· 9 years ago · Dec 29, 2016, 09:54 PM
1/*
2
3freak fortress 2 status, written by nitros
4
5*/
6
7#pragma semicolon 1
8
9#include <sourcemod>
10#include <freak_fortress_2>
11#include <clientprefs>
12
13#define PLUGIN_VERSION "0.0.1"
14
15#define STATS_COOKIE "ff2stats_onforuser"
16
17
18public Plugin:myinfo=
19{
20 name="Freak Fortress Stats",
21 author="Nitros",
22 description="Boss stats for freak fortress 2",
23 version=PLUGIN_VERSION,
24 url="ben@bensimms.moe"
25};
26
27
28new Handle:g_bossStatsCookie;
29new Handle:db;
30
31public void OnPluginStart()
32{
33 g_bossStatsCookie = RegClientCookie(STATS_COOKIE, "Enable stats for user", CookieAccess_Protected);
34 InitDB(db);
35 HookEvent("teamplay_round_win", OnRoundEnd);
36
37
38 RegConsoleCmd("ff2stats", statsToggleCmd);
39}
40
41public Action:statsToggleCmd(int client, int args)
42{
43 if(!IsValidClient(client))
44 {
45 return Plugin_Continue;
46 }
47
48 statsTogglePanel(client);
49 return Plugin_Handled;
50}
51
52public Action:statsTogglePanel(client)
53{
54 if(!IsValidClient(client))
55 {
56 return Plugin_Continue;
57 }
58
59 new Handle:panel=CreatePanel();
60 SetPanelTitle(panel, "Turn stats for you when you're boss...");
61 DrawPanelItem(panel, "On");
62 DrawPanelItem(panel, "Off");
63 SendPanelToClient(panel, client, statsTogglePanelH, MENU_TIME_FOREVER);
64 CloseHandle(panel);
65 return Plugin_Continue;
66}
67
68public statsTogglePanelH(Handle:menu, MenuAction:action, client, selection)
69{
70 if(IsValidClient(client) && action==MenuAction_Select)
71 {
72 if(selection==2) //Off
73 {
74 setStatsCookie(client, false);
75 }
76 else //On
77 {
78 //If they already have music enabled don't do anything
79 setStatsCookie(client, true);
80 }
81 CPrintToChat(client, "{olive}[FF2stats]{default} %t", "ff2stats", selection==2 ? "off" : "on");
82 }
83}
84
85
86InitDB(&Handle:DBHandle)
87{
88 new String:Error[255];
89 DBHandle = SQL_Connect("stats_db", true, Error, sizeof(Error));
90
91 if(DBHandle == INVALID_HANDLE)
92 {
93 SetFailState(Error);
94 }
95 new String:Query[255];
96 Format(Query, sizeof(Query), "CREATE TABLE IF NOT EXISTS player_stats (steamid INT, bossname TEXT, win INT)");
97 SQL_LockDatabase(DBHandle);
98 SQL_FastQuery(DBHandle, Query);
99 SQL_UnlockDatabase(DBHandle);
100}
101
102
103setStatsCookie(int client, bool val)
104{
105 if(!IsValidClient(client) || IsFakeClient(client) || !AreClientCookiesCached(client))
106 {
107 return;
108 }
109 char cookieVal[8];
110 IntToString(val, cookieVal, sizeof(cookieVal));
111 SetClientCookie(client, g_bossStatsCookie, cookieVal);
112}
113
114
115bool StatsEnabledForClient(int client)
116{
117 if(!AreClientCookiesCached(client)) // not loaded? dont run stats
118 {
119 return false;
120 }
121 decl String:sValue[8];
122 GetClientCookie(client, g_bossStatsCookie, sValue, sizeof(sValue));
123 return (sValue[0] != '\0' && StringToInt(sValue));
124}
125
126void addGameToDB(int steamid, const char[] boss_name, bool win)
127{
128 char query[200];
129
130 /* Create enough space to make sure our string is quoted properly */
131 int buffer_len = strlen(boss_name) * 2 + 1;
132 char[] new_boss_name = new char[buffer_len];
133
134 /* Ask the SQL driver to make sure our string is safely quoted */
135 SQL_EscapeString(db, boss_name, new_boss_name, buffer_len);
136
137 /* Build the query */
138 Format(query, sizeof(query), "INSERT INTO <tablehere> (steamid, bossname, win) VALUES (%d, '%s', %d)", steamid, new_boss_name, win);
139
140 /* Execute the query */
141 SQL_LockDatabase(db);
142 SQL_FastQuery(db, query);
143 SQL_UnlockDatabase(db);
144}
145
146
147public Action:OnRoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
148{
149 if(!FF2_IsFF2Enabled())
150 {
151 return Plugin_Continue;
152 }
153 new bool:bossWin=false;
154 if((GetEventInt(event, "team")==FF2_GetBossTeam()))
155 {
156 bossWin=true; // boss won
157 }
158 decl String:bossName[64];
159 new boss = -1;
160 for(new client; client<MaxClients; client++)
161 {
162 if(!StatsEnabledForClient(client)){continue;} // dont add if not counting stats
163 boss=FF2_GetBossIndex(client);
164 if(!(boss==-1)) // we have a boss
165 {
166 new bossSteamID = GetSteamAccountID(client); // steamid
167 if (bossSteamID==0){continue;} // dont break on invalid steamid
168 FF2_GetBossSpecial(boss, bossName, sizeof(bossName));
169 addGameToDB(bossSteamID, bossName, bossWin);
170 }
171 }
172
173 return Plugin_Continue;
174}
175
176stock bool:IsValidClient(client, bool:replaycheck=true)
177{
178 if(client<=0 || client>MaxClients)
179 {
180 return false;
181 }
182
183 if(!IsClientInGame(client))
184 {
185 return false;
186 }
187
188 if(GetEntProp(client, Prop_Send, "m_bIsCoaching"))
189 {
190 return false;
191 }
192
193 if(replaycheck)
194 {
195 if(IsClientSourceTV(client) || IsClientReplay(client))
196 {
197 return false;
198 }
199 }
200 return true;
201}