· 9 years ago · Nov 12, 2016, 09:26 PM
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 CmdFr(const id) {
60 client_print(id, print_chat, "frags = %d", g_Frags[id]);
61 return PLUGIN_HANDLED;
62}
63
64public HamKilledPost(const Victim, const Attacker) {
65 if(0 < Attacker < g_MaxPlayers && Victim != Attacker) {
66 g_Frags[Attacker]++;
67 }
68}
69
70public QueryIngnoredHandle(const FailState, const Handle:Query, const Error[]) {
71 if(FailState != TQUERY_SUCCESS) {
72 log_amx("SQL error (IngnoredHandle): ^"%s^"", Error); return;
73 }
74}
75
76public QueryLoadInfoHandle(const FailState, const Handle:Query, const Error[], const ErrorNum, const Data[]) {
77 if(FailState != TQUERY_SUCCESS) {
78 log_amx("SQL error (LoadInfoHandle): ^"%s^"", Error); return;
79 }
80 new id = Data[0];
81 if(!is_user_connected(id)) {
82 return;
83 }
84 if(SQL_MoreResults(Query)) {
85 g_Frags[id] = SQL_ReadResult(Query, 1);
86 } else {
87 formatex(g_Query, charsmax(g_Query), "INSERT INTO `%s` (authid, frags) VALUES ('%s', '%d')", SQL_TABLE, g_AuthId[id], g_Frags[id]);
88 SQL_ThreadQuery(g_Tuple, "QueryInsertInfoHandle", g_Query);
89 }
90}
91
92public QueryInsertInfoHandle(const FailState, const Handle:Query, const Error[]) {
93 if(FailState != TQUERY_SUCCESS) {
94 log_amx("SQL error (InsertInfoHandle): ^"%s^"", Error); return;
95 }
96}