I have written my first plugin based on a map voting one for amx. Since it is the first time, I would appreciate someone looking it over to make sure I didn't do something really stupid that will crash the server, cause a memory leak, etc... I have been running this on my server and it seems to be working great. Here is a webpage that queries the database for the stored results.
Code:
/* AMX Mod X script
* sql_maprate
* by Slurpy [COF]([email protected])
*
* Thanks to:
* devicenull for his SQl tutorial
* p00h map_rate for amx that was the basis for this
* ALOT of other AMXX scripters whose code I looked through to learn how to do this
*
* map vote results saved into a MySQL database.
* tested only on mysql 4.0.xx
* requires sql module enabled!
*
*
* How can I make my website with this?
* basic PHP page to dislplay results will be available when plugin released
*/
#include <amxmodx>
#include <dbi>
new Sql
:dbc
new Result
:result
new state
[3]
new nowstate
[3]
new plnum
new bshow
= true
new Float:total
public plugin_init
() {
register_plugin("SQL Map Rank",
"0.1",
"Slurpy [COF]")
register_menucmd(register_menuid("What do you think about "),
(1<<
0)|
(1<<
1)|
(1<<
2),
"vote_count")
set_task(10.0,
"read_rate",
777,
"",
0,
"b")
set_task(Float:10.0,
"sql_init")
}
public sql_init
() {
new host
[64], username
[32], password
[32], dbname
[32], error
[32]
get_cvar_string("amx_sql_host",host,
64)
get_cvar_string("amx_sql_user",username,
32)
get_cvar_string("amx_sql_pass",password,
32)
get_cvar_string("amx_sql_db",dbname,
32)
dbc
= dbi_connect
("host",
"username",
"password",
"dbname",error,
32)
if (dbc
== SQL_FAILED
)
log_amx("[AMXX] SQL Connection Failed")
else
{
dbi_query
(dbc,
"CREATE TABLE IF NOT EXISTS `maprank` ( `map_name` VARCHAR(32) NOT NULL,`good` INT NOT NULL, `okay` INT NOT NULL, `bad` INT NOT NULL, 'total' INT NOT NULL, PRIMARY KEY(`map_name`))")
}
}
public sql_insert
() {
if (dbc
== SQL_FAILED
) return PLUGIN_CONTINUE
new mapname
[33]
get_mapname(mapname,
32)
new totalint
totalint
= floatround(total
)
//Insert map information into the tables
result
= dbi_query
(dbc,
"INSERT INTO maprank (map_name, good, okay, bad, total) values ('%s',%i,%i,%i,%i) ON DUPLICATE KEY UPDATE map_name=map_name, good=good+%i, okay=okay+%i, bad=bad+%i, total=total+%i",mapname,state
[0],state
[1],state
[2],totalint,state
[0],state
[1],state
[2],totalint
)
dbi_free_result
(result
)
return PLUGIN_CONTINUE
}
public ask_menu
() {
new menu
[256]
new mapname
[33]
get_mapname(mapname,
32)
plnum
= 0
nowstate
[0] = 0
nowstate
[1] = 0
nowstate
[2] = 0
//Display the voting menu
format(menu,
255,
"\yWhat do you think about %s?\w^n^n1. Very Good ! !^n2. Medium .^n3. So Bad . . .",mapname
)
show_menu(0,
(1<<
0)|
(1<<
1)|
(1<<
2),menu,
10)
client_cmd(0,
"spk Gman/Gman_Choose2")
set_task(10.0,
"end_conduct")
client_print(0,print_chat,
"* Conducting a survey of players...")
}
public vote_count
(id,key
) {
new name
[32]
get_user_name(id,name,
31)
switch(key
){
case 0: {
client_print(0,print_chat,
"* %s selected Very Good!!",name
)
state
[0]++
nowstate
[0]++
}
case 1: {
client_print(0,print_chat,
"* %s selected Okay!",name
)
state
[1]++
nowstate
[1]++
}
case 2:{
client_print(0,print_chat,
"* %s selected So Bad...",name
)
state
[2]++
nowstate
[2]++
}
}
plnum
++
}
public end_conduct
() {
new Float:ans
[3]
new Float:tans
[3]
if(nowstate
[0] == 0) {
nowstate
[0] = 0
}else {
ans
[0] = float(nowstate
[0]) / float(plnum
)
}
if(nowstate
[2] == 0) {
nowstate
[2] = 0
}else {
ans
[2] = float(nowstate
[2]) / float(plnum
)
}
ans
[1] = 1.00 - (ans
[0] + ans
[2])
client_print(0,print_chat,
"* Now Rate : Very Good!!!(%.2f) Medium.(%.2f) So Bad...(%.2f)",ans
[0],ans
[1],ans
[2])
total
= float(state
[0] + state
[1] + state
[2])
if(state
[0] == 0) {
tans
[0] = float(0)
}else {
tans
[0] = float(state
[0]) / total
}
if(state
[2] == 0) {
tans
[2] = float(0)
}else {
tans
[2] = float(state
[2]) / total
}
tans
[1] = 1.00 - (tans
[0] + tans
[2])
client_print(0,print_chat,
"* Total Rate : Very Good!!!(%.2f) Medium.(%.2f) So Bad...(%.2f)",tans
[0],tans
[1],tans
[2])
sql_insert
()
return PLUGIN_CONTINUE
}
public read_rate
() {
new timeleft
= get_timeleft()
if(bshow
&&(timeleft>
0)&&(timeleft<
300)) {
bshow
= false
ask_menu
()
}
}