|
Veteran Member
Join Date: Feb 2013
Location: Rromania
|

08-23-2013
, 09:31
SQL Money saving optimization help
|
#1
|
Okay, so here's the deal: I got a server running which uses "credits" as currency instead of money. You get one credit for each minute you play, but that's pretty irrelevant. What I care about is saving those credits, and I use SQL for that, but since I barely know how to use it, it proved to be a challenge. People are sometimes losing their credits, there's no real evidence of how and why but they do, and it's not just some dumbasses lying to get some free cash. There's almost no latency between the sql host and the server(same machine) and by what I figured out it has something to do with the credits getting saved after they are set to 0. Please help me figure it out.
Since the plugin is not public and many would do lots of things to get it, I can't post the full source(also it's huge, 5k lines), but I will post everything relevant to the cause.
Code:
Okay so the SQL connection and all that stuff is taken from a SQL tutorial off this forum and it's working fine, won't post that.
#define ID_GIVE_CREDIT (taskid - TASK_GIVE_CREDITS)
public Load_MySql(id)
{
new nume[32], szTemp[512]
get_user_name(id, nume, 31)
new Data[1]
Data[0] = id
format(szTemp,charsmax(szTemp),"SELECT * FROM `credit` WHERE (`credit`.`nume` = '%s')", nume)
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
}
public register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
if(FailState == TQUERY_CONNECT_FAILED)
{
log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error)
}
else if(FailState == TQUERY_QUERY_FAILED)
{
log_amx("Load Query failed. [%d] %s", Errcode, Error)
}
new id
id = Data[0]
if(SQL_NumResults(Query) < 1)
{
new nume[32]
get_user_name(id, nume, 31)
if(equal(nume,"Player"))
return PLUGIN_HANDLED
new szTemp[512]
format(szTemp,charsmax(szTemp),"INSERT INTO `credit` ( `nume` , `credite`) VALUES ('%s','0');",nume)
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}
else
{
iCredit[id] = SQL_ReadResult(Query, 1)
}
return PLUGIN_HANDLED
}
public Save_MySql(id)
{
new nume[32], szTemp[512]
get_user_name(id, nume, 31)
format(szTemp,charsmax(szTemp),"UPDATE `credit` SET `credite` = '%i' WHERE `credit`.`nume` = '%s';", iCredit[id], nume)
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}
public client_putinserver(id)
{
if(!is_user_bot(id))
{
set_task(2.0, "setnull", id)
set_task(4.0,"loadcredits", id)
}
}
public loadcredits(id)
{
Load_MySql(id)
set_task(60.0, "Give1Points", id+TASK_GIVE_CREDITS, _, _, "b")
}
public forward_client_userinfochanged(id, buffer) {
if (!is_user_connected(id))
return FMRES_IGNORED
static oldname[32], newname[32]
get_user_name(id, oldname, sizeof oldname - 1)
engfunc(EngFunc_InfoKeyValue, buffer, g_name, newname, sizeof newname - 1)
if (equal(newname, oldname))
return FMRES_IGNORED
msg_name_change(id, oldname, newname)
return FMRES_SUPERCEDE
}
msg_name_change(id, /* const */ oldname[], /* const */ newname[]) {
message_begin(MSG_BROADCAST, g_msgid_saytext)
write_byte(id)
write_string(g_name_change)
write_string(oldname)
write_string(newname)
message_end()
set_task(1.5, "setnull", id)
remove_task(id+TASK_GIVE_CREDITS)
set_task(3.0,"loadnewcredits", id)
}
public loadnewcredits(id)
{
Load_MySql(id)
set_task(60.0, "Give1Points", id+TASK_GIVE_CREDITS, _, _, "b")
}
public client_disconnect(id)
{
remove_task(id+TASK_GIVE_CREDITS)
Save_MySql(id)
set_task(3.0, "setnull", id)
}
public setnull(id)
{
iCredit[id] = 0
}
public Give1Points(taskid)
{
static id
id = ID_GIVE_CREDIT;
if(!is_user_connected(id) || cs_get_user_team(id) == CS_TEAM_SPECTATOR || cs_get_user_team(id) == CS_TEAM_UNASSIGNED)
{
return
}
iCredit[id] = iCredit[id] + 1
Save_MySql(id)
}
This is everything that has save_mysql in it, let me know if I missed something or if you want another piece of code and I'll be sure to post it.
Thanks for reading.
|
|