Steam, no sql.
PHP Code:
/* Formatright © 2010, ConnorMcLeod
This plugin is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this plugin; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <amxmodx>
#define VERSION "0.1.2"
#define PLUGIN "Rate that map"
#define NOT_VOTED_YET -1
enum _:Votes {
Terrible,
Bad,
Not_Bad,
Good,
Awesome
}
new g_iVotes[Votes]
new Trie:g_tPlayerVotes
new g_szStoreFile[64], g_szMapName[32]
new g_pCvarMenuDelay
public plugin_init()
{
register_plugin(PLUGIN, VERSION, "ConnorMcLeod")
g_pCvarMenuDelay = register_cvar("amx_ratemap_delay", "30.0")
get_localinfo("amxx_datadir", g_szStoreFile, charsmax(g_szStoreFile))
add(g_szStoreFile, charsmax(g_szStoreFile), "/maprates")
if( !dir_exists(g_szStoreFile) )
{
mkdir(g_szStoreFile)
}
get_mapname(g_szMapName, charsmax(g_szMapName))
strtolower(g_szMapName)
format(g_szStoreFile, charsmax(g_szStoreFile), "%s/%s.dat", g_szStoreFile, g_szMapName)
g_tPlayerVotes = TrieCreate()
new fp = fopen(g_szStoreFile, "rt")
if( fp )
{
new szBuffer[128], szAuthid[32], szVote[2], c
new szName[32], iVote
while( !feof(fp) )
{
fgets(fp, szBuffer, charsmax(szBuffer))
trim(szBuffer)
c = szBuffer[0]
if( !c || c == '/' || c == ';' || c == '#' )
{
continue
}
parse(szBuffer, szAuthid, charsmax(szAuthid), szVote, charsmax(szVote), szName, charsmax(szName))
g_iVotes[ (iVote = szVote[0] - '0') ]++
TrieSetCell(g_tPlayerVotes, szAuthid, iVote)
}
fclose(fp)
}
else
{
fp = fopen(g_szStoreFile, "wt")
fprintf(fp, ";^n^n")
fclose(fp)
}
register_concmd("amx_show_maprates", "ConCmd_ShowRates", ADMIN_MAP, "[MapName optional] Show map rates")
}
public ConCmd_ShowRates( id )
{
new szMapName[32]
if( read_argv(1, szMapName, charsmax(szMapName)) && !equali(szMapName, g_szMapName) && is_map_valid(szMapName) )
{
strtolower(szMapName)
new szFile[64]
get_localinfo("amxx_datadir", szFile, charsmax(szFile))
format(szFile, charsmax(szFile), "%s/maprates/%s.dat", szFile, szMapName)
if( file_exists(szFile) )
{
new szBuffer[256], iLen
read_file(szFile, 0, szBuffer, charsmax(szBuffer), iLen)
if( iLen > 2 )
{
console_print(id, szBuffer[1])
return PLUGIN_HANDLED
}
}
console_print(id, "No votes for map %s")
return PLUGIN_HANDLED
}
new iTotalVotes
for(new i=Terrible; i<=Awesome; i++)
{
iTotalVotes += g_iVotes[i]
}
if( !iTotalVotes )
{
console_print(id, "Map Rates : No Votes Yet !!")
return PLUGIN_HANDLED
}
console_print(id, "^nMap Rates :")
console_print(id, "%s : %d/%d %3d%%%", "Awesome", g_iVotes[Awesome], iTotalVotes, g_iVotes[Awesome]*100/iTotalVotes)
console_print(id, "%s : %d/%d %3d%%%", "Good", g_iVotes[Good], iTotalVotes, g_iVotes[Good]*100/iTotalVotes)
console_print(id, "%s : %d/%d %3d%%%", "Not Bad", g_iVotes[Not_Bad], iTotalVotes, g_iVotes[Not_Bad]*100/iTotalVotes)
console_print(id, "%s : %d/%d %3d%%%", "Bad", g_iVotes[Bad], iTotalVotes, g_iVotes[Bad]*100/iTotalVotes)
console_print(id, "%s : %d/%d %3d%%%^n", "Terrible", g_iVotes[Terrible], iTotalVotes, g_iVotes[Terrible]*100/iTotalVotes)
return PLUGIN_HANDLED
}
public plugin_end()
{
new iTotalVotes
for(new i=Terrible; i<=Awesome; i++)
{
iTotalVotes += g_iVotes[i]
}
if( iTotalVotes )
{
new szBuffer[256]
formatex(szBuffer, charsmax(szBuffer), ";Awesome %d (%d%%) | Good %d (%d%%) | Not Bad %d (%d%%) | Bad %d (%d%%) | Terrible %d (%d%%) | TOTAL %d",
g_iVotes[Awesome], g_iVotes[Awesome]*100/iTotalVotes,
g_iVotes[Good], g_iVotes[Good]*100/iTotalVotes,
g_iVotes[Not_Bad], g_iVotes[Not_Bad]*100/iTotalVotes,
g_iVotes[Bad], g_iVotes[Bad]*100/iTotalVotes,
g_iVotes[Terrible], g_iVotes[Terrible]*100/iTotalVotes,
iTotalVotes)
write_file(g_szStoreFile, szBuffer, 0)
}
}
public client_putinserver( id )
{
remove_task(id)
if( !is_user_bot(id) && !is_user_hltv(id) )
{
set_task(get_pcvar_float(g_pCvarMenuDelay), "Show_Rate_Menu", id)
}
}
public client_disconnect( id )
{
remove_task(id)
}
public Show_Rate_Menu( id )
{
if( !is_user_connected(id) )
{
return
}
new iCrap
if( player_menu_info(id, iCrap, iCrap) )
{
set_task(15.0, "Show_Rate_Menu", id)
return
}
new szAuthid[32]
get_user_authid(id, szAuthid, charsmax(szAuthid))
new iPreviousVote = NOT_VOTED_YET
TrieGetCell(g_tPlayerVotes, szAuthid, iPreviousVote)
new const szVotes[][] = {"Terrible", "Bad", "Not Bad", "Good", "Awesome"}
new iMenu = menu_create("\yHow would you rate that map ?", "MapRate_Menu_Handled")
menu_setprop(iMenu, MPROP_NUMBER_COLOR, "\y")
for(new i=Terrible; i<=Awesome; i++)
{
if( iPreviousVote == i )
{
new szText[32]
formatex(szText, charsmax(szText), "\d%d. \w%s\w", i+1, szVotes[i])
menu_addtext(iMenu, szText)
}
else
{
menu_additem(iMenu, szVotes[i])
}
}
menu_display(id, iMenu)
}
public MapRate_Menu_Handled(id, iMenu, iItem)
{
menu_destroy(iMenu)
if( iItem >= 0 )
{
new szName[32]
new szAuthid[32]
new iPreviousVote
get_user_authid(id, szAuthid, charsmax(szAuthid))
new bool:bAlreadyVoted = TrieGetCell(g_tPlayerVotes, szAuthid, iPreviousVote)
get_user_name(id, szName, charsmax(szName))
if( bAlreadyVoted )
{
g_iVotes[ iPreviousVote ]--
g_iVotes[ iItem ]++
}
TrieSetCell(g_tPlayerVotes, szAuthid, iItem)
WriteVote(szAuthid, szName, iItem, bAlreadyVoted)
}
}
WriteVote(const szAuthid[32], const szName[], const iVote, bool:bAlreadyVoted)
{
if( !bAlreadyVoted )
{
new fp = fopen(g_szStoreFile, "at")
fprintf(fp, "%s %d %s^n", szAuthid, iVote, szName)
fclose(fp)
}
else
{
new iLine = -1, szSteamId[32], szBuffer[128]
new fp = fopen(g_szStoreFile, "rt")
while( !feof(fp) )
{
iLine++
fgets(fp, szBuffer, charsmax(szBuffer))
trim(szBuffer)
parse(szBuffer, szSteamId, charsmax(szSteamId))
if( equal(szSteamId, szAuthid) )
{
break
}
}
fclose(fp)
formatex(szBuffer, charsmax(szBuffer), "%s %d %s", szAuthid, iVote, szName)
write_file(g_szStoreFile, szBuffer, iLine)
}
}