Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <amxmodx>
- #include <amxmisc>
- #if !defined MAX_PLAYERS
- const MAX_PLAYERS = 32
- #endif
- #if !defined MAX_NAME_LENGTH
- const MAX_NAME_LENGTH = 32
- #endif
- #if !defined MAX_AUTHID_LENGTH
- const MAX_AUTHID_LENGTH = 64
- #endif
- #if !defined MAX_IP_LENGTH
- const MAX_IP_LENGTH = 16
- #endif
- const SECONDS_IN_DAY = 86400
- const SECONDS_IN_WEEK = 604800
- const SECONDS_IN_MONTH = 2628000
- const SECONDS_IN_YEAR = 31536000
- new const LIST_FILE_DESC[] = "# This file is generated automatically. Do not modify anything here as it would have no effect.^n\
- # extra <extra value> -- description [main plugin that contains the extra]^n"
- new const PLUGIN_VERSION[] = "3.0-beta"
- new const TIME_FORMAT[] = "%d.%m.%Y"
- const Float:DELAY_ON_CHANGE = 0.1
- const EXTRAS_PER_PAGE = 20
- const MAX_DATA_LENGTH = 256
- const MAX_FILE_LENGTH = 256
- const MAX_DESC_LENGTH = 128
- const MAX_PLUGIN_NAME = 64
- const MAX_TYPE_LENGTH = 32
- const MAX_EXTRA_LENGTH = 32
- const MAX_DATE_LENGTH = 12
- enum _:Extras
- {
- Date[MAX_DATE_LENGTH],
- Type[MAX_TYPE_LENGTH],
- Info[MAX_AUTHID_LENGTH],
- Extra[MAX_EXTRA_LENGTH],
- ExtraValue[MAX_EXTRA_LENGTH]
- }
- enum _:PlayerData
- {
- PDATA_NAME[MAX_NAME_LENGTH],
- PDATA_IP[MAX_IP_LENGTH],
- PDATA_AUTHID[MAX_AUTHID_LENGTH]
- }
- new Array:g_aExtras
- new Array:g_aUserExtras[MAX_PLAYERS + 1]
- new Trie:g_tUserExtras[MAX_PLAYERS + 1]
- new g_ePlayerData[MAX_PLAYERS + 1][PlayerData]
- new g_szConfigFile[MAX_FILE_LENGTH]
- new g_szListFile[MAX_FILE_LENGTH]
- new g_fwUserExtrasUpdated
- new g_fwConfigFileRead
- new g_iUserExtras[MAX_PLAYERS + 1]
- new g_iListFilePointer
- new g_iTotalExtras
- new g_iToday
- #if defined GetFileTime
- new g_iFileTime
- #endif
- new const g_szComments[][] =
- {
- "#===============================================#",
- "# Player Extras by OciXCrom: Configuration file #",
- "#===============================================#",
- "",
- "# Add each extra on a new line following the format:",
- "# ^"duration^" ^"type^" ^"info^" ^"extra^" ^"extra value [optional]^"",
- "",
- "# The ^"duration^" field should contain the date when the extra will end, following the format ^"day.month.year^", e.g. ^"20.09.2018^"",
- "# You can also type in the amount of days, weeks, months or years for the extra to last, e.g. ^"30 days^" - the date will be converted automatically after mapchange.",
- "# If you want the extra to be permanent, set this field to 0.",
- "",
- "# The ^"type^" field can be one of the following: ^"name^", ^"ip^", ^"steam^"",
- "# The ^"info^" field is the actual player's info according to the type, e.g. if ^"type^" is set to ^"name^", the ^"info^" field should be ^"OciXCrom^".",
- "# The ^"extra^" field is where you actually write the extra's name.",
- "# Some extras accept an extra value that can be entered in the ^"extra value^" field.",
- "",
- "# Players can have only one same extra at a time. If duplicate values are found, the player will receive the one that's listed first in the file.",
- "# For a full list of available extras, take a look at the file PlayerExtrasList.ini in the configs folder.",
- "",
- "# Here are some examples:",
- "# ^"20.09.2018^" ^"name^" ^"OciXCrom^" ^"colorchat^"",
- "# ^"08.10.2020^" ^"ip^" ^"77.29.42.36^" ^"prefix^" ^"{Pro Player}^"",
- "# ^"15 days^" ^"steam^" ^"STEAM_0:0:50153248^" ^"health^" ^"125^"",
- "# ^"0^" ^"steam^" ^"BOT^" ^"prefix^" ^"{I'm a bot}^"",
- "#===============================================#"
- }
- public plugin_init()
- {
- register_plugin("Player Extras", PLUGIN_VERSION, "OciXCrom")
- register_cvar("CRXPlayerExtras", PLUGIN_VERSION, FCVAR_SERVER|FCVAR_SPONLY|FCVAR_UNLOGGED)
- register_dictionary("PlayerExtras.txt")
- register_clcmd( "pex_myextras", "Cmd_MyExtras", ADMIN_ALL, "[starting extra number]")
- register_concmd("pex_add", "Cmd_Add", ADMIN_RCON, "<duration> <type> <info> <extra> [extra value]")
- register_concmd("pex_reload", "Cmd_Reload", ADMIN_RCON)
- SetupVars()
- }
- public plugin_precache()
- {
- SetupFiles()
- }
- public plugin_cfg()
- {
- if(g_iListFilePointer)
- {
- fclose(g_iListFilePointer)
- }
- }
- SetupFiles()
- {
- get_configsdir(g_szConfigFile, charsmax(g_szConfigFile))
- copy(g_szListFile, charsmax(g_szListFile), g_szConfigFile)
- add(g_szConfigFile, charsmax(g_szConfigFile), "/PlayerExtras.ini")
- add(g_szListFile, charsmax(g_szListFile), "/PlayerExtrasList.ini")
- g_iListFilePointer = fopen(g_szListFile, "w")
- fputs(g_iListFilePointer, LIST_FILE_DESC)
- }
- SetupVars()
- {
- g_aExtras = ArrayCreate(Extras)
- g_fwUserExtrasUpdated = CreateMultiForward("pex_user_extras_updated", ET_IGNORE, FP_CELL, FP_CELL)
- g_fwConfigFileRead = CreateMultiForward("pex_config_file_reloaded", ET_IGNORE, FP_CELL)
- for(new i = 1; i <= MAX_PLAYERS; i++)
- {
- g_aUserExtras[i] = ArrayCreate(Extras)
- g_tUserExtras[i] = TrieCreate()
- }
- g_iToday = get_systime()
- ReadFile(true)
- }
- public plugin_end()
- {
- #if defined GetFileTime
- if(g_iFileTime != get_filetime())
- {
- ReadFile(false)
- }
- #else
- ReadFile(false)
- #endif
- WriteFile()
- ArrayDestroy(g_aExtras)
- for(new i = 1; i <= MAX_PLAYERS; i++)
- {
- ArrayDestroy(g_aUserExtras[i])
- TrieDestroy(g_tUserExtras[i])
- }
- }
- WriteFile()
- {
- new iFilePointer = fopen(g_szConfigFile, "w")
- if(iFilePointer)
- {
- for(new i; i < sizeof(g_szComments); i++)
- {
- fputs(iFilePointer, g_szComments[i])
- fputs(iFilePointer, "^n")
- }
- for(new eExtra[Extras], i; i < g_iTotalExtras; i++)
- {
- ArrayGetArray(g_aExtras, i, eExtra)
- fprintf(iFilePointer, "^n^"%s^" ^"%s^" ^"%s^" ^"%s^"", eExtra[Date], eExtra[Type], eExtra[Info], eExtra[Extra])
- if(eExtra[ExtraValue][0])
- {
- fprintf(iFilePointer, " ^"%s^"", eExtra[ExtraValue])
- }
- }
- fclose(iFilePointer)
- }
- }
- ReadFile(bool:bFirstTime)
- {
- new iFilePointer = fopen(g_szConfigFile, "rt")
- if(iFilePointer)
- {
- if(bFirstTime)
- {
- #if defined GetFileTime
- g_iFileTime = get_filetime()
- #endif
- }
- else
- {
- g_iTotalExtras = 0
- ArrayClear(g_aExtras)
- }
- new szData[MAX_DATA_LENGTH], eExtra[Extras]
- while(!feof(iFilePointer))
- {
- fgets(iFilePointer, szData, charsmax(szData))
- trim(szData)
- switch(szData[0])
- {
- case EOS, '#', ';': continue
- default:
- {
- parse
- (
- szData,
- eExtra[Date], charsmax(eExtra[Date]),
- eExtra[Type], charsmax(eExtra[Type]),
- eExtra[Info], charsmax(eExtra[Info]),
- eExtra[Extra], charsmax(eExtra[Extra]),
- eExtra[ExtraValue], charsmax(eExtra[ExtraValue])
- )
- if(convert_date(eExtra[Date], charsmax(eExtra[Date])) < g_iToday)
- {
- continue
- }
- if(!is_valid_type(eExtra[Type]))
- {
- continue
- }
- g_iTotalExtras++
- ArrayPushArray(g_aExtras, eExtra)
- eExtra[ExtraValue][0] = EOS
- }
- }
- }
- fclose(iFilePointer)
- }
- new iReturn
- ExecuteForward(g_fwConfigFileRead, iReturn, bFirstTime)
- }
- public client_putinserver(id)
- {
- get_user_name(id, g_ePlayerData[id][PDATA_NAME], charsmax(g_ePlayerData[][PDATA_NAME]))
- get_user_ip(id, g_ePlayerData[id][PDATA_IP], charsmax(g_ePlayerData[][PDATA_IP]), 1)
- get_user_authid(id, g_ePlayerData[id][PDATA_AUTHID], charsmax(g_ePlayerData[][PDATA_AUTHID]))
- load_player_extras(id, true)
- }
- public client_infochanged(id)
- {
- if(!is_user_connected(id))
- {
- return
- }
- static szNewName[MAX_NAME_LENGTH]
- get_user_info(id, "name", szNewName, charsmax(szNewName))
- if(!equal(szNewName, g_ePlayerData[id][PDATA_NAME]))
- {
- copy(g_ePlayerData[id][PDATA_NAME], charsmax(g_ePlayerData[][PDATA_NAME]), szNewName)
- set_task(DELAY_ON_CHANGE, "reload_player_extras", id)
- }
- }
- public Cmd_MyExtras(id, iLevel, iCid)
- {
- if(!cmd_access(id, iLevel, iCid, 1))
- {
- return PLUGIN_HANDLED
- }
- if(!g_iTotalExtras || !g_iUserExtras[id])
- {
- console_print(id, "%L", id, "PEX_NO_EXTRAS")
- return PLUGIN_HANDLED
- }
- static eExtra[Extras]
- print_separator(id, true)
- console_print(id, "%L", id, "PEX_YOUR_EXTRAS", g_iUserExtras[id])
- print_separator(id, false)
- new szArg[5]
- read_argv(1, szArg, charsmax(szArg))
- new iStart = clamp(str_to_num(szArg) - 1, 0, clamp(g_iUserExtras[id] - EXTRAS_PER_PAGE, 0))
- new iMax = clamp(iStart + EXTRAS_PER_PAGE, .max = g_iUserExtras[id])
- for(new i = iStart; i < iMax; i++)
- {
- ArrayGetArray(g_aUserExtras[id], i, eExtra)
- console_print(id, "%2d %-16.15s %-16.15s %s", i + 1, eExtra[Date], eExtra[Extra], eExtra[ExtraValue])
- }
- if(g_iUserExtras[id] - iStart > EXTRAS_PER_PAGE)
- {
- print_separator(id, false)
- console_print(id, "%L", id, "PEX_SEE_MORE", iStart + EXTRAS_PER_PAGE + 1)
- }
- print_separator(id, true)
- return PLUGIN_HANDLED
- }
- public Cmd_Add(id, iLevel, iCid)
- {
- if(!cmd_access(id, iLevel, iCid, 5))
- {
- return PLUGIN_HANDLED
- }
- static eExtra[Extras]
- eExtra[ExtraValue][0] = EOS
- read_argv(1, eExtra[Date], charsmax(eExtra[Date]))
- if(convert_date(eExtra[Date], charsmax(eExtra[Date])) < g_iToday)
- {
- console_print(id, "%L", id, "PEX_INVALID_DATE", eExtra[Date])
- return PLUGIN_HANDLED
- }
- read_argv(2, eExtra[Type], charsmax(eExtra[Type]))
- if(!is_valid_type(eExtra[Type]))
- {
- console_print(id, "%L", id, "PEX_INVALID_TYPE", eExtra[Type])
- return PLUGIN_HANDLED
- }
- read_argv(3, eExtra[Info], charsmax(eExtra[Info]))
- read_argv(4, eExtra[Extra], charsmax(eExtra[Extra]))
- read_argv(5, eExtra[ExtraValue], charsmax(eExtra[ExtraValue]))
- new iFilePointer = fopen(g_szConfigFile, "a")
- if(!iFilePointer)
- {
- console_print(id, "%L", id, "PEX_CANT_OPEN")
- return PLUGIN_HANDLED
- }
- g_iTotalExtras++
- ArrayPushArray(g_aExtras, eExtra)
- fprintf(iFilePointer, "^n^"%s^" ^"%s^" ^"%s^" ^"%s^"", eExtra[Date], eExtra[Type], eExtra[Info], eExtra[Extra])
- print_separator(id, true)
- console_print(id, "%L", id, "PEX_ADD_SUCCESS")
- print_separator(id, false)
- console_print(id, "%L: %s", id, "PEX_DATE", eExtra[Date])
- console_print(id, "%L: %s", id, "PEX_TYPE", eExtra[Type])
- console_print(id, "%L: %s", id, "PEX_INFO", eExtra[Info])
- console_print(id, "%L: %s", id, "PEX_EXTRA", eExtra[Extra])
- if(eExtra[ExtraValue][0])
- {
- console_print(id, "%L: %s", id, "PEX_EXTRA_VALUE", eExtra[ExtraValue])
- fprintf(iFilePointer, " ^"%s^"", eExtra[ExtraValue])
- }
- fclose(iFilePointer)
- print_separator(id, true)
- return PLUGIN_HANDLED
- }
- public Cmd_Reload(id, iLevel, iCid)
- {
- if(!cmd_access(id, iLevel, iCid, 1))
- {
- return PLUGIN_HANDLED
- }
- ReadFile(false)
- new iPlayers[32], iPnum
- get_players(iPlayers, iPnum)
- for(new i; i < sizeof(g_iTotalExtras); i++)
- {
- load_player_extras(iPlayers[i], false)
- }
- console_print(id, "%L", id, "PEX_RELOAD_SUCCESS", g_iTotalExtras)
- return PLUGIN_HANDLED
- }
- public reload_player_extras(id)
- {
- load_player_extras(id, false)
- }
- load_player_extras(id, bool:bOnConnect)
- {
- if(!is_user_connected(id))
- {
- return
- }
- g_iUserExtras[id] = 0
- ArrayClear(g_aUserExtras[id])
- TrieClear(g_tUserExtras[id])
- static eExtra[Extras]
- for(new i; i < g_iTotalExtras; i++)
- {
- ArrayGetArray(g_aExtras, i, eExtra)
- if(meets_requirements(id, eExtra[Type], eExtra[Info]) && !TrieKeyExists(g_tUserExtras[id], eExtra[Extra]))
- {
- g_iUserExtras[id]++
- ArrayPushArray(g_aUserExtras[id], eExtra)
- TrieSetString(g_tUserExtras[id], eExtra[Extra], eExtra[ExtraValue])
- }
- }
- new iReturn
- ExecuteForward(g_fwUserExtrasUpdated, iReturn, id, bOnConnect)
- }
- print_separator(id, bool:bMain)
- {
- console_print(id, bMain ? "===============================" : "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ")
- }
- convert_date(szDate[], iLen)
- {
- static szAmount[5], szCurrency[2], iTimeStamp
- if(contain(szDate, " ") != -1)
- {
- parse(szDate, szAmount, charsmax(szAmount), szCurrency, charsmax(szCurrency))
- switch(szCurrency[0])
- {
- case 'D', 'd': iTimeStamp = SECONDS_IN_DAY
- case 'W', 'w': iTimeStamp = SECONDS_IN_WEEK
- case 'M', 'm': iTimeStamp = SECONDS_IN_MONTH
- case 'Y', 'y': iTimeStamp = SECONDS_IN_YEAR
- }
- iTimeStamp = clamp(iTimeStamp * str_to_num(szAmount) + g_iToday)
- format_time(szDate, iLen, TIME_FORMAT, iTimeStamp)
- }
- else
- {
- iTimeStamp = parse_time(szDate, TIME_FORMAT)
- }
- return iTimeStamp
- }
- bool:is_valid_type(szType[])
- {
- switch(szType[0])
- {
- case 'N', 'n', 'I', 'i', 'S', 's': return true
- }
- return false
- }
- bool:meets_requirements(id, szType[], szInfo[])
- {
- switch(szType[0])
- {
- case 'N', 'n':
- {
- if(equali(g_ePlayerData[id][PDATA_NAME], szInfo))
- {
- return true
- }
- }
- case 'I', 'i':
- {
- if(equal(g_ePlayerData[id][PDATA_IP], szInfo))
- {
- return true
- }
- }
- case 'S', 's':
- {
- if(equal(g_ePlayerData[id][PDATA_AUTHID], szInfo))
- {
- return true
- }
- }
- }
- return false
- }
- #if defined GetFileTime
- get_filetime()
- {
- return GetFileTime(g_szConfigFile, FileTime_LastChange)
- }
- #endif
- public plugin_natives()
- {
- register_library("pex")
- register_native("pex_get_config_file", "_pex_get_config_file")
- register_native("pex_get_extra_value", "_pex_get_extra_value")
- register_native("pex_get_list_file", "_pex_get_list_file")
- register_native("pex_get_total_extras", "_pex_get_total_extras")
- register_native("pex_get_user_extras", "_pex_get_user_extras")
- register_native("pex_register_extra", "_pex_register_extra")
- register_native("pex_reload_config_file", "_pex_reload_config_file")
- register_native("pex_reload_user_extras", "_pex_reload_user_extras")
- register_native("pex_user_has_extra", "_pex_user_has_extra")
- }
- public _pex_get_config_file(iPlugin, iParams)
- {
- set_string(1, g_szConfigFile, get_param(2))
- }
- public bool:_pex_get_extra_value(iPlugin, iParams)
- {
- static szExtra[MAX_EXTRA_LENGTH], szTrieValue[MAX_EXTRA_LENGTH]
- get_string(2, szExtra, charsmax(szExtra))
- TrieGetString(g_tUserExtras[get_param(1)], szExtra, szTrieValue, charsmax(szTrieValue))
- set_string(3, szTrieValue, get_param(4))
- }
- public _pex_get_list_file(iPlugin, iParams)
- {
- set_string(1, g_szListFile, get_param(2))
- }
- public _pex_get_total_extras(iPlugin, iParams)
- {
- return g_iTotalExtras
- }
- public _pex_get_user_extras(iPlugin, iParams)
- {
- return g_iUserExtras[get_param(1)]
- }
- public _pex_register_extra(iPlugin, iParams)
- {
- if(g_iListFilePointer)
- {
- static szExtra[MAX_EXTRA_LENGTH], szDescription[MAX_DESC_LENGTH], szExtraValue[MAX_EXTRA_LENGTH], szPlugin[MAX_PLUGIN_NAME]
- get_string(1, szExtra, charsmax(szExtra))
- get_string(2, szDescription, charsmax(szDescription))
- get_string(3, szExtraValue, charsmax(szExtraValue))
- get_plugin(iPlugin, szPlugin, charsmax(szPlugin))
- fprintf(g_iListFilePointer, szExtra)
- if(szExtraValue[0])
- {
- fprintf(g_iListFilePointer, " <%s>", szExtraValue)
- }
- fprintf(g_iListFilePointer, " -- %s [%s]^n", szDescription, szPlugin)
- }
- }
- public _pex_reload_config_file(iPlugin, iParams)
- {
- ReadFile(false)
- }
- public _pex_reload_user_extras(iPlugin, iParams)
- {
- load_player_extras(get_param(1), false)
- }
- public bool:_pex_user_has_extra(iPlugin, iParams)
- {
- static szExtra[MAX_EXTRA_LENGTH]
- get_string(2, szExtra, charsmax(szExtra))
- return TrieKeyExists(g_tUserExtras[get_param(1)], szExtra)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement