· 6 years ago · Jul 08, 2019, 11:34 PM
1Database g_dDatabase;
2
3char g_szAuth[MAXPLAYERS + 1][32];
4
5int g_iMoney[MAXPLAYERS + 1];
6
7public void OnPluginStart()
8{
9 HookEvent("player_spawn", Event_PlayerSpawn);
10 HookEvent("item_purchase", Event_OnBuy);
11
12 SQL_StartConnection();
13}
14
15void SQL_StartConnection()
16{
17 if (g_dDatabase != null)
18 delete g_dDatabase;
19
20 char szError[512];
21 g_dDatabase = SQL_Connect("savemoney", true, szError, sizeof(szError));
22 if (g_dDatabase == null)
23 SetFailState("Cannot connect to database: %s.", szError);
24 // יוצר טבלה בדטאבייס
25 g_dDatabase.Query(SQL_CheckForErrors, "CREATE TABLE IF NOT EXISTS `savemoney` (`steamid` VARCHAR(32) NOT NULL PRIMARY KEY DEFAULT '', `name` VARCHAR(64) NOT NULL, `money` INT NOT NULL DEFAULT 0)");
26 for (int i = 1; i <= MaxClients; i++)if (IsClientInGame(i))OnClientPostAdminCheck(i);
27}
28
29public void OnClientPostAdminCheck(int client)
30{
31 if (!GetClientAuthId(client, AuthId_Steam2, g_szAuth[client], sizeof(g_szAuth)))
32 {
33 KickClient(client, "Verification problem, please reconnect.");
34 }
35
36 if (!IsFakeClient(client))
37 {
38 // מכניס את השחקן לדטאבייס
39 SQL_CheckUser(client);
40 }
41}
42
43public void OnClientDisconnect(int client)
44{
45 if (!IsFakeClient(client))
46 {
47 // מעדכן את הכסף שהיה לו בדטאבייס
48 char szQuery[512];
49 FormatEx(szQuery, sizeof(szQuery), "UPDATE `savemoney` SET `money` = %i WHERE `steamid` = '%s'", g_iMoney[client], g_szAuth[client]);
50 g_dDatabase.Query(SQL_CheckForErrors, szQuery);
51 }
52}
53
54void SQL_CheckUser(int client)
55{
56 char szQuery[512];
57 FormatEx(szQuery, sizeof(szQuery), "SELECT `money` FROM `savemoney` WHERE `steamid` = '%s'", g_szAuth[client]);
58 g_dDatabase.Query(SQL_CheckUser_CB, szQuery, GetClientSerial(client));
59}
60
61public void SQL_CheckUser_CB(Database DB, DBResultSet results, const char[] error, any data)
62{
63 int client = GetClientFromSerial(data);
64 if (results == null)
65 {
66 LogError("Failed to query, error: %s", error);
67 return;
68 }
69 if (IsFakeClient(client) || !IsClientInGame(client) || IsClientSourceTV(client))
70 return;
71
72 char szName[MAX_NAME_LENGTH];
73 GetClientName(client, szName, sizeof(szName));
74
75 int len = strlen(szName) * 2 + 1;
76 char[] szEscapedName = new char[len];
77 g_dDatabase.Escape(szName, szEscapedName, len);
78
79 char szQuery[512];
80 if (results.FetchRow())
81 {
82 g_iMoney[client] = results.FetchInt(0);
83 FormatEx(szQuery, sizeof(szQuery), "UPDATE `savemoney` SET `playername` = '%s', `money` = %i WHERE `steamid` = '%s'", szEscapedName, g_iMoney[client], g_szAuth[client]);
84 }
85 else
86 {
87 g_iMoney[client] = 0;
88 FormatEx(szQuery, sizeof(szQuery), "INSERT INTO `savemoney` (`steamid`, `playername`, `money`) VALUES ('%s', '%s', 0)", g_szAuth[client], szEscapedName, g_iMoney[client]);
89 }
90 g_dDatabase.Query(SQL_CheckForErrors, szQuery);
91}
92
93public void SQL_CheckForErrors(Database DB, DBResultSet results, const char[] error, any data)
94{
95 if (results == null)
96 {
97 LogError("Failed to query, error: %s", error);
98 return;
99 }
100}
101
102public Action Event_PlayerSpawn(Event event, const char[] name, bool dB)
103{
104 int client = GetClientOfUserId(event.GetInt("userid"));
105 // בודק אם השחקן במשחק
106 if (IsClientInGame(client) && !IsFakeClient(client))
107 {
108 // אם לשחקן אין כסף
109 if (g_iMoney[client] == 0)
110 {
111 }
112 // אם יש לו כסף
113 else
114 {
115 // זה לוקח מהדטאבייס תכסף ושם לו
116 SetEntProp(client, Prop_Data, "m_iAccount", g_iMoney[client]);
117 }
118 }
119}
120
121public Action Event_OnBuy(Event event, const char[] name, bool dB)
122{
123 int client = GetClientOfUserId(event.GetInt("userid"));
124
125 if (IsClientInGame(client) && !IsFakeClient(client))
126 {
127 // לוקח את כמות הכסף שיש לשחקן לאחר שהוא קונה
128 int money = GetEntProp(client, Prop_Data, "m_iAccount");
129 // מעדכן בדטאבייס
130 UpdateMoney(client, money);
131 }
132}
133
134public void UpdateMoney(int client, int money)
135{
136 char szQuery[512];
137 FormatEx(szQuery, sizeof(szQuery), "UPDATE `basebuilder` SET `money` = %i WHERE `steamid` = '%s'", money, g_szAuth[client]);
138 g_dDatabase.Query(SQL_CheckForErrors, szQuery);
139}