· 7 years ago · Nov 01, 2018, 06:46 PM
1/* ===========================================================================
2
3 -----------------------
4 -*- [ZP] Stats SQLx -*-
5 -----------------------
6
7 (c) 2018 - Taurus
8 Website: http://8bits-gaming.com
9
10=========================================================================== */
11
12#include < amxmodx >
13#include < sqlx >
14#include < zp50_ammopacks >
15
16#define MAX_PLAYERS 32
17
18#define SQL_HOSTNAME "x"
19#define SQL_USERNAME "x"
20#define SQL_PASSWORD "x"
21#define SQL_DATABASE "x"
22
23new Handle:g_hSQLTuple;
24
25new bool:g_bStatsLoaded[ 33 ];
26
27public plugin_init( )
28{
29 // v1.0.0 - Initial release
30 // v1.0.1 - Optimised the code and removed unnecessary codes
31 // v1.0.2 - Fixed replace runtime error, disabled ammo packs save if they are not yet loaded
32 register_plugin( "[ZP] Stats SQLx", "1.2", "Taurus" );
33
34 SQL_CreateDatabase( );
35}
36
37public plugin_end( )
38{
39 SQL_FreeHandle( g_hSQLTuple );
40}
41
42public client_putinserver( id )
43{
44 g_bStatsLoaded[ id ] = false;
45
46 SQL_InsertPlayer( id );
47}
48
49public client_disconnected( id )
50{
51 if( g_bStatsLoaded[ id ] )
52 {
53 SQL_UpdatePlayer( id );
54
55 g_bStatsLoaded[ id ] = false;
56 }
57}
58
59public SQL_ThreadQuery_Empty( iFailState, Handle:hQuery, szError[ ], iError, szData[ ], iData )
60{
61 if( iFailState != TQUERY_SUCCESS )
62 {
63 set_fail_state( szError );
64 return;
65 }
66
67 SQL_FreeHandle( hQuery );
68}
69
70public SQL_ThreadQuery_Insert( iFailState, Handle:hQuery, szError[ ], iError, szData[ ], iData )
71{
72 if( iFailState != TQUERY_SUCCESS )
73 {
74 set_fail_state( szError );
75 return;
76 }
77
78 static id, iUser;
79 id = szData[ 0 ];
80 iUser = szData[ 1 ];
81
82 if( get_user_userid( id ) != iUser )
83 return;
84
85 static szAuth[ 32 ], szQuery[ 256 ];
86 get_user_authid( id, szAuth, charsmax( szAuth ) );
87
88 formatex( szQuery, charsmax( szQuery ), "SELECT `ammo_packs` FROM `cs_zp_stats` WHERE `auth_id` = '%s';", szAuth );
89 SQL_ThreadQuery( g_hSQLTuple, "SQL_ThreadQuery_Select", szQuery, szData, iData );
90
91 SQL_FreeHandle( hQuery );
92}
93
94public SQL_ThreadQuery_Select( iFailState, Handle:hQuery, szError[ ], iError, szData[ ], iData )
95{
96 if( iFailState != TQUERY_SUCCESS )
97 {
98 set_fail_state( szError );
99 return;
100 }
101
102 static id, iUser;
103 id = szData[ 0 ];
104 iUser = szData[ 1 ];
105
106 if( get_user_userid( id ) != iUser )
107 return;
108
109 // Set player's ammo packs
110 zp_ammopacks_set( id, SQL_ReadResult( hQuery, SQL_FieldNameToNum( hQuery, "ammo_packs" ) ) );
111
112 // Set that the data has been loaded
113 g_bStatsLoaded[ id ] = true;
114
115 SQL_FreeHandle( hQuery );
116}
117
118SQL_CreateDatabase( )
119{
120 g_hSQLTuple = SQL_MakeDbTuple( SQL_HOSTNAME, SQL_USERNAME, SQL_PASSWORD, SQL_DATABASE );
121
122 if( !g_hSQLTuple )
123 {
124 set_fail_state( "Could not make database tuple" );
125 return;
126 }
127
128 static szQuery[ 256 ];
129
130 formatex( szQuery, charsmax( szQuery ), "CREATE TABLE IF NOT EXISTS `cs_zp_stats` ( `auth_id` VARCHAR( 32 ), `player_name` VARCHAR( 32 ), `ammo_packs` INT( 11 ) NOT NULL DEFAULT 0, UNIQUE( `auth_id` ) );" );
131 SQL_ThreadQuery( g_hSQLTuple, "SQL_ThreadQuery_Empty", szQuery );
132}
133
134SQL_InsertPlayer( id )
135{
136 static szAuth[ 32 ], szName[ 64 ];
137 get_user_authid( id, szAuth, charsmax( szAuth ) );
138 get_user_name( id, szName, charsmax( szName ) );
139
140 replace_all( szName, charsmax( szName ), "'", "''" );
141
142 static szData[ 2 ], szQuery[ 256 ];
143 szData[ 0 ] = id;
144 szData[ 1 ] = get_user_userid( id );
145
146 formatex( szQuery, charsmax( szQuery ), "INSERT INTO `cs_zp_stats` ( `auth_id`, `player_name` ) VALUES ( '%s', '%s' ) ON DUPLICATE KEY UPDATE `player_name` = '%s';", szAuth, szName, szName );
147 SQL_ThreadQuery( g_hSQLTuple, "SQL_ThreadQuery_Insert", szQuery, szData, sizeof( szData ) );
148}
149
150SQL_UpdatePlayer( id )
151{
152 static szAuth[ 32 ], szName[ 64 ], szQuery[ 256 ];
153 get_user_authid( id, szAuth, charsmax( szAuth ) );
154 get_user_name( id, szName, charsmax( szName ) );
155
156 replace_all( szName, charsmax( szName ), "'", "''" );
157
158 formatex( szQuery, charsmax( szQuery ), "UPDATE `cs_zp_stats` SET `player_name` = '%s', `ammo_packs` = %d WHERE `auth_id` = '%s';", szName, zp_ammopacks_get( id ), szAuth );
159 SQL_ThreadQuery( g_hSQLTuple, "SQL_ThreadQuery_Empty", szQuery );
160}