· 10 years ago · Sep 01, 2016, 11:20 AM
1/* ============================
2 * SQL example 0.1 [24.08.2016]
3 * Credits: Mistrick
4 * ========================= */
5
6#include <amxmodx>
7#include <hamsandwich>
8#include <sqlx>
9
10#pragma semicolon 1
11
12const MAX_PLAYERS = 32;
13new const SQL_HOST[] = "127.0.0.1";
14new const SQL_USER[] = "admin";
15new const SQL_PASSWORD[] = "qwerty";
16new const SQL_DATABASE[] = "test_db";
17new const SQL_TABLE[] = "test_table";
18
19new g_MaxPlayers, Handle:g_Tuple, g_Query[256], g_Frags[MAX_PLAYERS + 1], g_AuthId[MAX_PLAYERS + 1][24];
20
21public plugin_init() {
22 register_plugin("SQL example", "0.1", "Subb98");
23 register_clcmd("say /fr", "CmdFr");
24 RegisterHam(Ham_Killed, "player", "HamKilledPost", 1);
25 g_MaxPlayers = get_maxplayers() + 1;
26}
27
28public plugin_cfg() {
29 if((g_Tuple = SQL_MakeDbTuple(SQL_HOST, SQL_USER, SQL_PASSWORD, SQL_DATABASE)) == Empty_Handle) {
30 set_fail_state("Can't create connection tuple");
31 }
32 formatex(g_Query, charsmax(g_Query),
33 "CREATE TABLE IF NOT EXISTS `%s`(\
34 id INT(11) UNSIGNED AUTO_INCREMENT,\
35 authid VARCHAR(24) NOT NULL,\
36 frags INT(11) NOT NULL,\
37 PRIMARY KEY(id))", SQL_TABLE
38 );
39 SQL_ThreadQuery(g_Tuple, "QueryIngnoredHandle", g_Query);
40}
41
42public client_putinserver(id) {
43 g_Frags[id] = 0;
44 if(!is_user_bot(id) && !is_user_hltv(id)) {
45 new Data[1]; Data[0] = id;
46 get_user_authid(id, g_AuthId[id], charsmax(g_AuthId[]));
47 formatex(g_Query, charsmax(g_Query), "SELECT id, frags FROM `%s` WHERE authid='%s'", SQL_TABLE, g_AuthId[id]);
48 SQL_ThreadQuery(g_Tuple, "QueryLoadInfoHandle", g_Query, Data, sizeof Data);
49 }
50}
51
52public client_disconnect(id) {
53 if(!is_user_bot(id) && !is_user_hltv(id)) {
54 formatex(g_Query, charsmax(g_Query), "UPDATE `%s` SET frags='%d' WHERE authid='%s'", SQL_TABLE, g_Frags[id], g_AuthId[id]);
55 SQL_ThreadQuery(g_Tuple, "QueryIngnoredHandle", g_Query);
56 }
57}
58
59public plugin_end() {
60 SQL_FreeHandle(g_Tuple);
61}
62
63public CmdFr(const id) {
64 client_print(id, print_chat, "frags = %d", g_Frags[id]);
65 return PLUGIN_HANDLED;
66}
67
68public HamKilledPost(const Victim, const Attacker) {
69 if(0 < Attacker < g_MaxPlayers && Victim != Attacker) {
70 g_Frags[Attacker]++;
71 }
72}
73
74public QueryIngnoredHandle(const FailState, const Handle:Query, const Error[]) {
75 if(FailState != TQUERY_SUCCESS) {
76 log_amx("SQL error (IngnoredHandle): ^"%s^"", Error); return;
77 }
78}
79
80public QueryLoadInfoHandle(const FailState, const Handle:Query, const Error[], const ErrorNum, const Data[]) {
81 if(FailState != TQUERY_SUCCESS) {
82 log_amx("SQL error (LoadInfoHandle): ^"%s^"", Error); return;
83 }
84 new id = Data[0];
85 if(!is_user_connected(id)) {
86 return;
87 }
88 if(SQL_MoreResults(Query)) {
89 g_Frags[id] = SQL_ReadResult(Query, 1);
90 } else {
91 formatex(g_Query, charsmax(g_Query), "INSERT INTO `%s` (authid, frags) VALUES ('%s', '%d')", SQL_TABLE, g_AuthId[id], g_Frags[id]);
92 SQL_ThreadQuery(g_Tuple, "QueryInsertInfoHandle", g_Query);
93 }
94}
95
96public QueryInsertInfoHandle(const FailState, const Handle:Query, const Error[]) {
97 if(FailState != TQUERY_SUCCESS) {
98 log_amx("SQL error (InsertInfoHandle): ^"%s^"", Error); return;
99 }
100}