Im trying to write a plugin that will provide some more in-depth details about the current game that is being played in a server. The plugin is going to update a DB table with the current player information so that a related website can query the DB table in order to show information about the current game being played. I have experimented with gameq, but its functionality is too limited for some of the features i require, so i decided to write this plugin.
This is the only the second plugin ive tried writing and the first SQL plugin.
It compiles ok, with a few tag errors (see bottom) and runs according to the server. Im pretty sure it also runs because every 20 seconds theres a big lag on the server. So its trying to do something, but failing badly :(
Code:
#include <amxmodx>
#include <dbi>
/* playerinfo.sma
*
* Plugin fetches player information and stores this information in a DB table in order to
* allow a web page to query the data.
*
* This essentially allows more detailed, real-time information of what is happening in a
* game to be fetched and displayed by an associated web site
*/
#define CHECK_FREQ 20 // Time in between checks of player info
#define RESULT_NONE 0
#define HOST "************"
#define DB "techfus_gameinfo"
#define USERNAME "************"
#define PASSWORD "************"
new PLUGIN[]="PlayerInfo"
new AUTHOR[]="Damocles"
new VERSION[]="1.00"
new Sql:mysql // DB Handle
public plugin_init()
{
// Register the plugin
register_plugin(PLUGIN, VERSION, AUTHOR)
set_task(float(CHECK_FREQ),"checkPlayers",_,_,_,"b")
init_mysql()
}
/*
* Checks players values and updates the DB accordingly
*/
public checkPlayers()
{
new Players[32]
new playerCount, i, uid
get_players(Players, playerCount, "c")
for (i=0; i<playerCount; i++)
{
uid = Players[i]
new authid[32],name[32],team[32]
new Result:ret = RESULT_NONE
get_user_authid(uid, authid, 31) // SteamID
get_user_name(uid, name, 31) // Get Name
get_user_team(uid, team, 31) // Team
// Kills
new frags = get_user_frags(uid)
// Deaths
new deaths = get_user_deaths(uid)
// Check that the ID in the table exists, if it does update, otherwise insert
new Result:check = dbi_query(mysql, "SELECT authid FROM gameinfo WHERE uid = %d", uid)
if (check < RESULT_NONE)
{
new err[255]
new errNum = dbi_error(mysql, err, 254)
server_print("error2: %s|%d", err, errNum)
return PLUGIN_HANDLED
}
if (!(dbi_num_rows (check) > 0))
{
//Insert player information into the table
ret = dbi_query(mysql, "INSERT INTO gameinfo (uid, authid, name, team, frags, deaths) VALUES (%d, %s, %s, $s, %d, %d)", uid, authid, name, team, frags, deaths)
}
else
{
// Else, uid already exists in table, simply update its user information
ret = dbi_query(mysql, "UPDATE gameinfo SET (authid = '%s', name = '%s', team = '%s', frags = %d, deaths = %d) WHERE uid = %d", authid, name, team, frags, deaths, uid)
}
if (ret < RESULT_NONE) //If the query is less than 0, it failed
{
new err[255]
new errNum = dbi_error(mysql, err, 254)
server_print("error2: %s|%d", err, errNum)
return PLUGIN_HANDLED
}
dbi_free_result(check)
dbi_free_result(ret)
}
return PLUGIN_HANDLED
}
public init_mysql()
{
//Create a connection
mysql = dbi_connect(HOST, USERNAME, PASSWORD, DB)
//If the connection is less than 1, it is bad
if (mysql < SQL_OK)
{
new err[255]
new errNum = dbi_error(mysql, err, 254)
server_print("error1: %s|%d", err, errNum)
return 1
}
else
{
// Try and create the table
dbi_query(mysql, "CREATE TABLE IF NOT EXISTS gameinfo (uid INT NOT NULL, authid VARCHAR(32), name VARCHAR(32), team VARCHAR(32), frags INT, deaths INT, PRIMARY KEY(uid))")
}
return PLUGIN_HANDLED
}
public plugin_end()
{
//Close the connection
dbi_close(mysql)
}
Ive got it setup so it queries the server ever 20 seconds for the player information and updates the table accordingly. I know some basic SQL and what i've got in terms of code is pasted together from some examples i've been using.
2) If i were to create a variable called string[10] and then assign a string value to it, say 'cucumber', then if i were to assign another value to it, say 'john', would string now hold just john, or would it just replace the first four characters of the old value with john, i.e. 'johnmber' ??