I know this code is not perfect...
There is also probably some better ways to do anything that I have done. Any input is appreciated.
But one user did not have a proper steam id to write to the users.ini file. So I added the server_print statements to find out what was going on.
I get this as my output. It doesnt make sense to me why he would have a b as his steam id. I read it in properly and test the value and it is correctly Hawks steam id.
Code:
#define PLUGIN "DB Mod"
#define VERSION "1.0"
#define AUTHOR "[gOf]-Soul"
/*
*
*
*
*
*/
#include <amxmodx>
#include <amxmisc>
#include <dbi>
#define USERID 0
#define USERNAME 1
#define USERFLAGS 2
#define GROUPNAME 0
#define GROUPFLAGS 1
new Groups[10][2][255]
new groupcount = 0
new Admins[250][3][50]
new admincount = 0
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
register_cvar("dbmod_sql_host","127.0.0.1")
register_cvar("dbmod_sql_username","root")
register_cvar("dbmod_sql_password","")
register_cvar("dbmod_sql_database","amx")
//register_cvar("dbmod_sql_prefix","dbmod_")
new loading[255]
format(loading,255,"Loading Plugin: %s <> Created By: %s <> Version: %s",PLUGIN,AUTHOR,VERSION)
ServerOut(loading)
new configDir[64]
get_configsdir(configDir, 63)
server_cmd("exec %s/dbmod.cfg", configDir)
new dbhost[64],dbusername[32],dbpassword[32],dbdatabase[32],dbprefix[32]
get_cvar_string("dbmod_sql_host",dbhost,63)
get_cvar_string("dbmod_sql_username",dbusername,31)
get_cvar_string("dbmod_sql_password",dbpassword,31)
get_cvar_string("dbmod_sql_database",dbdatabase,31)
get_cvar_string("dbmod_sql_database",dbprefix,31)
//Load groups from group.ini file
new fileGroups[64]//,numgroup=0
format(fileGroups,64,"%s/groups.ini",configDir)
new line = 0,linelength = 0
new linetext[64]
new left[64],right[64]
//new pattern[] = "(.+) (.+)"
while(read_file(fileGroups,line,linetext,64,linelength) != 0){
//server_print("Group line loaded: %s",linetext)
parse(linetext,left,64,right,64)
copy(Groups[line][GROUPNAME],255,left)
copy(Groups[line][GROUPFLAGS],255,right)
groupcount++
line++
}
//Open connection to db
new Sql:mysql = dbi_connect(dbhost, dbusername, dbpassword, dbdatabase)
//If the connection is less than 1, it is bad
if (mysql < SQL_OK) {
new err[255]
new errNum = dbi_error(mysql, err, 254)
new error[255]
format(error,255,"Connection Error: %s|%d", err, errNum)
ServerOut(error)
return 1
}
//query group members
new i
new whereclause[250]
new group_flags[200]
//new where[250]
for (i = 0;i<line;i++){
//server_print("Group: %s",Groups[i][GROUPNAME])
//if (i==0){
format(whereclause,250,"(g.group_name = '%s')",Groups[i][GROUPNAME])
new notfound[255]
format(notfound,255,"No rows returned for group: %s",Groups[i][GROUPNAME])
//}
//else{
// format(where, 250, "') or (g.group_name = '%s')",Groups[i][0])
//replace(whereclause,250,"')",where)
//}
//server_print(whereclause)
new query[500]
format(query,500,"select username,user_steamid,g.group_name from phpbb_users as u \
left join phpbb_user_group as ug on u.user_id = ug.user_id \
left join phpbb_groups as g on ug.group_id = g.group_id \
where (%s) and (u.user_steamid like 'STEAM%')\
",whereclause)
//server_print(query)
//Query database with the following query...
new Result:res = dbi_query(mysql, query)
//If the query is greater than 0, you got a handle to the result set
if (res <= RESULT_NONE) {
new err[255]
new errNum = dbi_error(mysql, err, 254)
if (errNum == 0){
ServerOut(notfound)
}
else{
new errout[255]
format(errout,255,"error3: %s|%d", err, errNum)
ServerOut(errout)
continue
}
}
//server_print("Result handle: %d", res)
//Get Users for group
new strUsersFile[64]
format(strUsersFile, 63, "%s/users.out", configDir)
delete_file (strUsersFile)
//Loop through the result set
new adminpos
while (res && dbi_nextrow(res)>0) {
new user_id[50],user_name[50],group_name[255]
//Get the column/field called "keyname" from the result set
dbi_result(res, "user_steamid", user_id, 50)
dbi_result(res, "username", user_name, 50)
dbi_result(res, "group_name", group_name,255)
GetGroupFlags(group_name,group_flags)
server_print("Seeing User: %s %s %s",user_id,user_name,group_flags)
adminpos = GetAdminPosition(user_id)
if (GetAdminPosition(user_id) == -1) {
adminpos = admincount
admincount++
server_print("Adding User: %s %s %s Position: %d",user_id,user_name,group_flags,adminpos)
copy(Admins[adminpos][USERID],50,user_id)
copy(Admins[adminpos][USERNAME],50,user_name)
}
else{
server_print("Found User: %s %s %s Position: %d",user_id,user_name,group_flags,adminpos)
}
server_print(Admins[adminpos][USERID])
strcat(Admins[adminpos][USERFLAGS],group_flags,255)
adminpos = -1
//Get Array Index
//get_vaultdata ( const key[], [ data[] = "", len = 0 ] )
//set_vaultdata ( const key[], [ const data[] = "" ] )
}
//Free the result set
dbi_free_result(res)
}
//Close the connection
dbi_close(mysql)
new strUsersFileNew[64],strUsersFile[64],strLine[255]
format(strUsersFileNew, 63, "%s/users.ini", configDir)
format(strUsersFile, 63, "%s/users.ini", configDir)
delete_file (strUsersFileNew)
for (i = 0;i<admincount;i++){
server_print("%d %s %s %s",i,Admins[i][USERID], Admins[i][USERNAME],Admins[i][USERFLAGS])
CleanFlags(Admins[i][USERFLAGS])
server_print("%d %s %s %s",i,Admins[i][USERID], Admins[i][USERNAME],Admins[i][USERFLAGS])
format(strLine, 255, "%s ^"^" ^"%s^" ^"ce^" ;%s",Admins[i][USERID],Admins[i][USERFLAGS], Admins[i][USERNAME])
write_file(strUsersFileNew,strLine,-1)
}
//new error[250]
if (file_exists(strUsersFileNew))
ServerOut("Users file copied")
//if (file_copy(strUsersFileNew,strUsersFile,error,250,true)){
//}
//else{
// ServerOut("Users file not copied - error occurred")
//}
return PLUGIN_HANDLED
}
public ServerOut(output[255]){
new strout[255]
format(strout,255,"[DB MOD] %s",output)
server_print(strout)
log_amx(strout)
}
public GetGroupFlags(group_name[],flags[]){
for (new i = 0;i<groupcount;i++){
if (equali(Groups[i][GROUPNAME],group_name)){
copy(flags,63,Groups[i][GROUPFLAGS])
return
}
}
copy(flags,63,"")
return
}
public GetAdminPosition(steam_id[]){
for (new i = 0;i<admincount;i++){
if (equali(Admins[i][USERID],steam_id))
return i
}
return -1
}
public CleanFlags(flags[]){
new newflags[40]
if(strfind(flags,"a") != -1)
strcat(newflags,"a",40)
if(strfind(flags,"b") != -1)
strcat(newflags,"b",40)
if(strfind(flags,"c") != -1)
strcat(newflags,"c",40)
if(strfind(flags,"d") != -1)
strcat(newflags,"d",40)
if(strfind(flags,"e") != -1)
strcat(newflags,"e",40)
if(strfind(flags,"f") != -1)
strcat(newflags,"f",40)
if(strfind(flags,"g") != -1)
strcat(newflags,"g",40)
if(strfind(flags,"h") != -1)
strcat(newflags,"h",40)
if(strfind(flags,"i") != -1)
strcat(newflags,"i",40)
if(strfind(flags,"j") != -1)
strcat(newflags,"j",40)
if(strfind(flags,"k") != -1)
strcat(newflags,"k",40)
if(strfind(flags,"l") != -1)
strcat(newflags,"l",40)
if(strfind(flags,"m") != -1)
strcat(newflags,"m",40)
if(strfind(flags,"n") != -1)
strcat(newflags,"n",40)
if(strfind(flags,"o") != -1)
strcat(newflags,"o",40)
if(strfind(flags,"p") != -1)
strcat(newflags,"p",40)
if(strfind(flags,"q") != -1)
strcat(newflags,"q",40)
if(strfind(flags,"r") != -1)
strcat(newflags,"r",40)
if(strfind(flags,"s") != -1)
strcat(newflags,"s",40)
if(strfind(flags,"t") != -1)
strcat(newflags,"t",40)
if(strfind(flags,"u") != -1)
strcat(newflags,"u",40)
copy(flags,40,newflags)
}