Update CS:GO team score gamedata.
2 * vim: set ts=4 sw=4 tw=99 noet:
3 * =============================================================================
4 * SourceMod Client Preferences Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
36 CookieManager g_CookieManager;
38 CookieManager::CookieManager()
40 for (int i=0; i<=SM_MAXPLAYERS; i++)
43 statsLoaded[i] = false;
44 statsPending[i] = false;
47 cookieDataLoadedForward = NULL;
50 CookieManager::~CookieManager(){}
52 void CookieManager::Unload()
54 /* If clients are connected we should try save their data */
55 for (int i = playerhelpers->GetMaxClients()+1; --i > 0;)
58 OnClientDisconnecting(i);
61 /* Find all cookies and delete them */
62 for (SourceHook::List<Cookie *>::iterator _iter = cookieList.begin(); _iter != cookieList.end(); _iter++)
68 Cookie *CookieManager::FindCookie(const char *name)
71 if (!cookieFinder.retrieve(name, &cookie))
76 Cookie *CookieManager::CreateCookie(const char *name, const char *description, CookieAccess access)
78 Cookie *pCookie = FindCookie(name);
80 /* Check if cookie already exists */
83 /* Update data fields to the provided values */
84 UTIL_strncpy(pCookie->description, description, MAX_DESC_LENGTH);
85 pCookie->access = access;
90 /* First time cookie - Create from scratch */
91 pCookie = new Cookie(name, description, access);
93 /* Attempt to insert cookie into the db and get its ID num */
94 TQueryOp *op = new TQueryOp(Query_InsertCookie, pCookie);
95 op->m_params.cookie = pCookie;
97 cookieFinder.insert(name, pCookie);
98 cookieList.push_back(pCookie);
100 g_ClientPrefs.AddQueryToQueue(op);
105 bool CookieManager::GetCookieValue(Cookie *pCookie, int client, char **value)
107 CookieData *data = pCookie->data[client];
109 /* Check if a value has been set before */
112 data = new CookieData("");
113 data->parent = pCookie;
114 clientData[client].push_back(data);
115 pCookie->data[client] = data;
116 data->changed = false;
120 *value = &data->value[0];
125 bool CookieManager::SetCookieValue(Cookie *pCookie, int client, const char *value)
127 CookieData *data = pCookie->data[client];
131 data = new CookieData(value);
132 data->parent = pCookie;
133 clientData[client].push_back(data);
134 pCookie->data[client] = data;
138 UTIL_strncpy(data->value, value, MAX_VALUE_LENGTH);
141 data->changed = true;
142 data->timestamp = time(NULL);
147 void CookieManager::OnClientAuthorized(int client, const char *authstring)
149 IGamePlayer *player = playerhelpers->GetGamePlayer(client);
156 connected[client] = true;
157 statsPending[client] = true;
159 g_ClientPrefs.AttemptReconnection();
161 TQueryOp *op = new TQueryOp(Query_SelectData, player->GetSerial());
162 UTIL_strncpy(op->m_params.steamId, authstring, MAX_NAME_LENGTH);
164 g_ClientPrefs.AddQueryToQueue(op);
167 void CookieManager::OnClientDisconnecting(int client)
169 connected[client] = false;
170 statsLoaded[client] = false;
171 statsPending[client] = false;
173 CookieData *current = NULL;
175 g_ClientPrefs.AttemptReconnection();
177 /* Save this cookie to the database */
178 IGamePlayer *player = playerhelpers->GetGamePlayer(client);
179 const char *pAuth = NULL;
184 pAuth = player->GetAuthString();
185 g_ClientPrefs.ClearQueryCache(player->GetSerial());
188 for (SourceHook::List<CookieData *>::iterator _iter = clientData[client].begin();\
189 _iter != clientData[client].end(); _iter++)
191 current = (CookieData *)*_iter;
192 dbId = current->parent->dbid;
194 if (player == NULL || pAuth == NULL || !current->changed || dbId == -1)
196 current->parent->data[client] = NULL;
201 TQueryOp *op = new TQueryOp(Query_InsertData, client);
203 UTIL_strncpy(op->m_params.steamId, pAuth, MAX_NAME_LENGTH);
204 op->m_params.cookieId = dbId;
205 op->m_params.data = current;
207 g_ClientPrefs.AddQueryToQueue(op);
209 current->parent->data[client] = NULL;
212 clientData[client].clear();
215 void CookieManager::ClientConnectCallback(int serial, IQuery *data)
219 /* Check validity of client */
220 if ((client = playerhelpers->GetClientFromSerial(serial)) == 0)
224 statsPending[client] = false;
227 /* Check validity of results */
228 if (data == NULL || (results = data->GetResultSet()) == NULL)
235 unsigned int timestamp;
238 while (results->MoreRows() && ((row = results->FetchRow()) != NULL))
240 const char *name = "";
241 row->GetString(0, &name, NULL);
243 const char *value = "";
244 row->GetString(1, &value, NULL);
246 pData = new CookieData(value);
247 pData->changed = false;
249 pData->timestamp = (row->GetInt(4, (int *)×tamp) == DBVal_Data) ? timestamp : 0;
251 Cookie *parent = FindCookie(name);
255 const char *desc = "";
256 row->GetString(2, &desc, NULL);
258 access = CookieAccess_Public;
259 row->GetInt(3, (int *)&access);
261 parent = CreateCookie(name, desc, access);
264 pData->parent = parent;
265 parent->data[client] = pData;
266 clientData[client].push_back(pData);
269 statsLoaded[client] = true;
271 cookieDataLoadedForward->PushCell(client);
272 cookieDataLoadedForward->Execute(NULL);
275 void CookieManager::InsertCookieCallback(Cookie *pCookie, int dbId)
279 pCookie->dbid = dbId;
283 TQueryOp *op = new TQueryOp(Query_SelectId, pCookie);
284 /* Put the cookie name into the steamId field to save space - Make sure we remember that it's there */
285 UTIL_strncpy(op->m_params.steamId, pCookie->name, MAX_NAME_LENGTH);
286 g_ClientPrefs.AddQueryToQueue(op);
289 void CookieManager::SelectIdCallback(Cookie *pCookie, IQuery *data)
293 if (data == NULL || (results = data->GetResultSet()) == NULL)
298 IResultRow *row = results->FetchRow();
305 row->GetInt(0, &pCookie->dbid);
308 bool CookieManager::AreClientCookiesCached(int client)
310 return statsLoaded[client];
313 bool CookieManager::AreClientCookiesPending(int client)
315 return statsPending[client];
318 void CookieManager::OnPluginDestroyed(IPlugin *plugin)
320 SourceHook::List<char *> *pList;
322 if (plugin->GetProperty("SettingsMenuItems", (void **)&pList, true))
330 for (SourceHook::List<char *>::iterator p_iter = pList->begin(); p_iter != pList->end(); p_iter++)
332 name = (char *)*p_iter;
333 itemcount = clientMenu->GetItemCount();
334 //remove from this plugins list
335 for (unsigned int i=0; i < itemcount; i++)
337 info = clientMenu->GetItemInfo(i, &draw);
344 if (strcmp(draw.display, name) == 0)
346 data = (AutoMenuData *)strtoul(info, NULL, 16);
348 if (data->handler->forward != NULL)
350 forwards->ReleaseForward(data->handler->forward);
352 delete data->handler;
355 clientMenu->RemoveItem(i);
367 bool CookieManager::GetCookieTime(Cookie *pCookie, int client, time_t *value)
369 CookieData *data = pCookie->data[client];
371 /* Check if a value has been set before */
377 *value = data->timestamp;