· 5 years ago · Sep 22, 2020, 07:08 PM
1#include <amxmodx>
2#include <sqlx>
3
4//Uncomment 'IM_USING_ZP50' if your server is running ZP50 and above.
5//#define IM_USING_ZP50
6
7#if defined IM_USING_ZP50
8native zp_ammopacks_get(id)
9native zp_ammopacks_set(id, amount)
10#else
11native zp_get_user_ammo_packs(id)
12native zp_set_user_ammo_packs(id, amount)
13#endif
14
15#define MAX_PLAYERS 32
16
17//Max player's ammopack that can be save in vault.
18#define MAX_AMMOPACK 999999
19
20//Max day player's ammopack can be stay in vault, set 0 = permanent.
21#define DAY_PRUNE 0
22
23new const szHost[] = ""
24new const szUser[] = ""
25new const szPass[] = ""
26
27enum QueryTypes
28{
29 qtCreateTable,
30 qtPruneDB,
31 qtLoadAmmo,
32 qtSaveAmmo
33}
34
35new const Query_CreateTable[] = "CREATE TABLE IF NOT EXISTS tblStore (ID INTEGER PRIMARY KEY AUTOINCREMENT, SteamID VARCHAR(34) UNIQUE, Time_Stamp INTEGER, Ammo INTEGER);";
36new const Query_PruneDatabase[] = "DELETE FROM tblStore WHERE Time_Stamp < %d;";
37new const Query_LoadAmmo[] = "SELECT Ammo FROM tblStore WHERE SteamID='%s';";
38new const Query_SaveAmmo[] = "REPLACE INTO tblStore (SteamID, Time_Stamp, Ammo) VALUES ('%s',%d,%d);";
39
40enum _:QueryInfo
41{
42 QueryTypes:qiQueryType,
43 qiPlayerID
44}
45
46new bool:bIsLoaded[MAX_PLAYERS + 1], szSteamid[MAX_PLAYERS + 1][35]
47new Handle:g_SQLTuple, g_szBuffer[256]
48
49public plugin_init()
50{
51 register_plugin("ZP AP Store SQL", "0.0.1", "wbyokomo/bugsy")
52}
53
54public plugin_cfg()
55{
56 SQL_SetAffinity("sqlite")
57 g_SQLTuple = SQL_MakeDbTuple(szHost, szUser, szPass, "ZPAPStore")
58
59 new Handle:SQLConnection, ErrorCode
60 SQLConnection = SQL_Connect(g_SQLTuple, ErrorCode, g_szBuffer, charsmax(g_szBuffer))
61
62 if(SQLConnection == Empty_Handle)
63 set_fail_state(g_szBuffer)
64
65 RunQuery(SQLConnection, Query_CreateTable)
66
67 #if DAY_PRUNE > 0
68 formatex(g_szBuffer, charsmax(g_szBuffer), Query_PruneDatabase, get_systime() - (86400 * DAY_PRUNE))
69 RunQuery(SQLConnection, g_szBuffer)
70 #endif
71}
72
73public plugin_end()
74{
75 SQL_FreeHandle(g_SQLTuple)
76}
77
78public client_putinserver(id)
79{
80 if(!is_user_hltv(id) && !is_user_bot(id))
81 {
82 get_user_authid(id, szSteamid[id], charsmax(szSteamid[]))
83 LoadData(id)
84 }
85}
86
87#if AMXX_VERSION_NUM < 190
88public client_disconnect(id)
89#else
90public client_disconnected(id)
91#endif
92{
93 SaveData(id)
94 bIsLoaded[id] = false
95}
96
97LoadData(id)
98{
99 new QueryData[QueryInfo]
100
101 QueryData[qiQueryType] = qtLoadAmmo
102 QueryData[qiPlayerID] = id
103
104 formatex(g_szBuffer, charsmax(g_szBuffer), Query_LoadAmmo, szSteamid[id])
105 SQL_ThreadQuery(g_SQLTuple, "QueryHandle", g_szBuffer, QueryData, sizeof(QueryData))
106}
107
108SaveData(id)
109{
110 if(bIsLoaded[id])
111 {
112 #if defined IM_USING_ZP50
113 new ap = min(zp_ammopacks_get(id), MAX_AMMOPACK)
114 #else
115 new ap = min(zp_get_user_ammo_packs(id), MAX_AMMOPACK)
116 #endif
117
118 formatex(g_szBuffer, charsmax(g_szBuffer), Query_SaveAmmo, szSteamid[id], get_systime(), ap)
119 SQL_ThreadQuery(g_SQLTuple, "QueryHandle", g_szBuffer)
120 }
121}
122
123RunQuery(Handle:SQLConnection, const szQuery[])
124{
125 new Handle:Query = SQL_PrepareQuery(SQLConnection, szQuery)
126
127 if(!SQL_Execute(Query))
128 {
129 SQL_QueryError(Query, g_szBuffer, charsmax(Query_CreateTable))
130 set_fail_state(g_szBuffer)
131 }
132}
133
134public QueryHandle(FailState, Handle:Query, const Error[], Errcode, const Data[QueryInfo], DataSize)
135{
136 if(FailState == TQUERY_CONNECT_FAILED)
137 {
138 set_fail_state("Could not connect to SQL database.")
139 return
140 }
141 else if(FailState == TQUERY_QUERY_FAILED)
142 {
143 log_amx("Query error: %s", Error)
144 set_fail_state(g_szBuffer)
145 return
146 }
147
148 new iAmmo, id = Data[qiPlayerID]
149
150 switch(Data[qiQueryType])
151 {
152 case qtLoadAmmo:
153 {
154 if (SQL_NumResults(Query))
155 {
156 iAmmo = SQL_ReadResult(Query, 0)
157
158 #if defined IM_USING_ZP50
159 zp_ammopacks_set(id, iAmmo)
160 #else
161 zp_set_user_ammo_packs(id, iAmmo)
162 #endif
163
164 bIsLoaded[id] = true
165 }
166 }
167 }
168}