Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Me trying build a cash(/points/money) system for CS 1.6
- Compatible with AMXX 1.8.2+ (But I recommand to use AMXX 1.8.3+)
- Untested on lower versions
- */
- #include <amxmodx>
- #include <amxmisc>
- #include <fakemeta>
- #include <hamsandwich>
- #include <nvault>
- #define semicolon 1
- // Settings
- /* Max Cash */
- #define MAX_CASH 2147483647 // 2^31 - 1
- /* Admin flag for commands, see ADMIN_* const */
- #define ADMIN_FLAG ADMIN_BAN
- /* Min & Max for getcash command */
- #define MIN_GETCASH 50
- #define MAX_GETCASH 1337
- /* Min & Max for nextcash command */
- #define MIN_NEXTCASH 50
- #define MAX_NEXTCASH 1337
- /* Time to wait between nextcash command (seconds) */
- #define TIMEWAIT_NEXTCASH 360.0
- // End of settings
- /* Toggle debug mod (for testing purpse only) */
- #define DEBUG
- #if defined DEBUG
- new g_szLogfile[32];
- #endif
- /* Backward Compatibility, DON'T TOUCH */
- #if AMXX_VERSION_NUM < 183
- #define OLD_VERSION
- #define MAX_PLAYERS 32
- #define MAX_NAME_LENGTH 32
- #endif
- #define PLUGIN_VERSION "v1.0"
- new g_szNextCashEntName[] = "ent_nextcash";
- new g_szVault[] = "CashSys";
- new g_hVault;
- new g_iCash[MAX_PLAYERS + 1];
- new bool:g_bCanGetCash[MAX_PLAYERS + 1];
- new bool:g_bCanNextCash[MAX_PLAYERS + 1];
- new g_iNextCashEntity[MAX_PLAYERS + 1];
- public plugin_init() {
- register_plugin("Cash System + API",PLUGIN_VERSION,"Hyuna");
- #if defined OLD_VERSION
- register_concmd("amx_addcash","cmdAddCash",ADMIN_FLAG,"<name or #userid> <cash> - Adds amount of cash to the player.",1);
- register_concmd("amx_removecash","cmdRemoveCash",ADMIN_FLAG,"<name or #userid> <cash> - Removes amount of cash from the player.",1);
- #else
- register_concmd("amx_addcash","cmdAddCash",ADMIN_FLAG,"<name or #userid> <cash> - Adds amount of cash to the player.",1,false);
- register_concmd("amx_removecash","cmdRemoveCash",ADMIN_FLAG,"<name or #userid> <cash> - Removes amount of cash from the player.",1,false);
- #endif
- register_clcmd("say","ActionSayHandler");
- register_clcmd("say_team","ActionSayHandler");
- register_logevent("EventRoundStart",2,"1=Round_Start");
- RegisterHam(Ham_Think,"info_target","fw_NextCashEntityThinkPost",1);
- g_hVault = nvault_open(g_szVault);
- if (g_hVault == INVALID_HANDLE)
- {
- debug_message("CRITICAL ERROR: Falied to open nvault %s, PLUGIN ENDED RUNNING.",g_szVault);
- set_fail_state("Error in creating nVault, See debug log if available.");
- }
- }
- #if defined DEBUG
- public plugin_cfg() {
- get_time("%d-%m-%Y",g_szLogfile,charsmax(g_szLogfile));
- format(g_szLogfile,charsmax(g_szLogfile),"cashsysdebug_%s.txt",g_szLogfile);
- debug_message("----------------------------------");
- debug_message("* Plugin name: Cash System + API");
- debug_message("* Plugin version: %s",PLUGIN_VERSION);
- debug_message("* Plugin author: Hyuna");
- debug_message("* Debug mod: Active");
- debug_message("** Have a nice debug! ;) **");
- debug_message("----------------------------------");
- }
- #endif
- public plugin_end() {
- debug_message("Closing nVault %d",g_hVault);
- nvault_close(g_hVault);
- debug_message("Plugin end running");
- }
- public plugin_natives() {
- register_library("cashsystem");
- register_native("set_user_cash","native_set_user_cash",0);
- register_native("get_user_cash","native_get_user_cash",0);
- }
- public native_set_user_cash(pluginid,params) {
- static client,iCash;
- client = get_param(1);
- if(!isClientValid(client,false))
- {
- log_error(AMX_ERR_NATIVE,"Player ID %d not found!",client);
- debug_message("NATIVE CALL ERROR: set_user_cash - Player ID %d not found; PluginID: %d;",client,pluginid);
- return 0;
- }
- iCash = clamp(get_param(2),0,MAX_CASH);
- debug_message("NATIVE CALL: set_user_cash - Player ID %d; New cash: %d; PluginID: %d;",client,iCash,pluginid);
- g_iCash[client] = iCash;
- SaveData(client);
- return 1;
- }
- public native_get_user_cash(pluginid,params) {
- static client;
- client = get_param(1);
- if(!isClientValid(client,false))
- {
- log_error(AMX_ERR_NATIVE,"Player ID %d not found!",client);
- debug_message("NATIVE CALL ERROR: get_user_cash - Player ID %d not found; PluginID: %d;",client,pluginid);
- return -1;
- }
- debug_message("NATIVE CALL: get_user_cash - Player ID %d; Cash: %d; PluginID: %d;",client,g_iCash[client],pluginid);
- return g_iCash[client];
- }
- public fw_NextCashEntityThinkPost(ent) {
- static iOwner,szClassname[32];
- if (!pev_valid(ent))
- return;
- pev(ent,pev_classname,szClassname,charsmax(szClassname));
- if (!equal(szClassname,g_szNextCashEntName))
- return;
- iOwner = pev(ent,pev_owner);
- g_bCanNextCash[iOwner] = true;
- debug_message("Time for player %d (EntID %d) have been passed, releasing him...",iOwner,ent);
- }
- public cmdAddCash(client,level,cid) {
- static szArg1[8],szArg2[12],
- szAuthID[24],szAuthID2[24],
- szName[MAX_NAME_LENGTH],szName2[MAX_NAME_LENGTH],
- plr,iCash;
- if (!cmd_access(client,level,cid,3))
- return PLUGIN_HANDLED;
- read_argv(1,szArg1,charsmax(szArg1));
- plr = cmd_target(client,szArg1,(CMDTARGET_OBEY_IMMUNITY | CMDTARGET_ALLOW_SELF | CMDTARGET_NO_BOTS));
- if (!plr)
- return PLUGIN_HANDLED;
- read_argv(2,szArg2,charsmax(szArg2));
- iCash = str_to_num(szArg2);
- if (iCash < 1)
- {
- console_print(client,"[AMXX] You've can't use negative value!");
- return PLUGIN_HANDLED;
- }
- g_iCash[plr] = clamp((g_iCash[plr] + iCash),0,MAX_CASH);
- SaveData(plr);
- get_user_authid(client,szAuthID,charsmax(szAuthID));
- get_user_name(client,szName,charsmax(szName));
- get_user_authid(plr,szAuthID2,charsmax(szAuthID2));
- get_user_name(plr,szName2,charsmax(szName2));
- log_amx("Cmd: ^"%s<%d><%s><>^" added %d cash to ^"%s<%d><%s><>^"", szName,get_user_userid(client),szAuthID,iCash,szName2,get_user_userid(plr),szAuthID2);
- debug_message("ADMIN %s added %d cash to PLAYER %s.",szName,iCash,szName2);
- show_activity(client,szName,"added %d cash to %s.",iCash,szName2);
- console_print(client,"[AMXX] You've added %d cash to ^"%s^".",iCash,szName);
- return PLUGIN_HANDLED;
- }
- public cmdRemoveCash(client,level,cid) {
- static szArg1[8],szArg2[12],
- szAuthID[24],szAuthID2[24],
- szName[MAX_NAME_LENGTH],szName2[MAX_NAME_LENGTH],
- plr,iCash;
- if (!cmd_access(client,level,cid,3))
- return PLUGIN_HANDLED;
- read_argv(1,szArg1,charsmax(szArg1));
- plr = cmd_target(client,szArg1,(CMDTARGET_OBEY_IMMUNITY | CMDTARGET_ALLOW_SELF | CMDTARGET_NO_BOTS));
- if (!plr)
- return PLUGIN_HANDLED;
- read_argv(2,szArg2,charsmax(szArg2));
- iCash = str_to_num(szArg2);
- if (iCash < 1)
- {
- console_print(client,"[AMXX] You've can't use negative value!");
- return PLUGIN_HANDLED;
- }
- g_iCash[plr] = clamp((g_iCash[plr] - iCash),0,MAX_CASH);
- SaveData(plr);
- get_user_authid(client,szAuthID,charsmax(szAuthID));
- get_user_name(client,szName,charsmax(szName));
- get_user_authid(plr,szAuthID2,charsmax(szAuthID2));
- get_user_name(plr,szName2,charsmax(szName2));
- log_amx("Cmd: ^"%s<%d><%s><>^" removed %d cash from ^"%s<%d><%s><>^"", szName,get_user_userid(client),szAuthID,iCash,szName2, get_user_userid(plr),szAuthID2);
- show_activity(client,szName,"removed %d cash from %s.",iCash,szName2);
- debug_message("ADMIN %s removed %d cash from PLAYER %s.",szName,iCash,szName2);
- console_print(client,"[AMXX] You've removed %d cash from ^"%s^".",iCash,szName);
- return PLUGIN_HANDLED;
- }
- public ActionSayHandler(client) {
- new szMsg[32];
- new szArg1[12],szArg2[12],szArg3[12];
- read_argv(1,szMsg,charsmax(szMsg));
- parse(szMsg,szArg1,charsmax(szArg1),szArg2,charsmax(szArg2),szArg3,charsmax(szArg3));
- if (equal(szArg1,"/cash"))
- return ActionShowCash(client,szArg2);
- else if (equal(szArg1,"/transfer") || equal(szArg1,"/send") || equal(szArg1,"/give"))
- return ActionTransfer(client,szArg2,szArg3);
- else if (equal(szArg1,"/gamble"))
- return ActionGamble(client,szArg2);
- else if (equal(szArg1,"/getcash"))
- return ActionGetCash(client);
- else if (equal(szArg1,"/nextcash"))
- return ActionNextCash(client);
- return PLUGIN_CONTINUE;
- }
- public ActionShowCash(client,plrname[]) {
- static szName[MAX_NAME_LENGTH],plr;
- if (!plrname[0])
- client_print(client,print_chat,"[AMXX] Your cash: %d",g_iCash[client]);
- else
- {
- plr = cmd_target(client,plrname,(CMDTARGET_NO_BOTS | CMDTARGET_ALLOW_SELF));
- if (plr == client)
- client_print(client,print_chat,"[AMXX] Your cash: %d",g_iCash[client]);
- else if(!is_user_connected(plr))
- client_print(client,print_chat,"[AMXX] Player %s not found.",plrname);
- else
- {
- get_user_name(plr,szName,charsmax(szName));
- client_print(client,print_chat,"[AMXX] %s cash: %d",szName,g_iCash[plr]);
- }
- }
- return PLUGIN_HANDLED;
- }
- public ActionTransfer(client,plrname[],cashamount[12]) {
- static szName[MAX_NAME_LENGTH],szName2[MAX_NAME_LENGTH],plr,iCash;
- if (!plrname[0])
- client_print(client,print_chat,"[AMXX] Syntex: /send <name> <amount>");
- else
- {
- plr = cmd_target(client,plrname,(CMDTARGET_NO_BOTS));
- if (!is_user_connected(plr))
- {
- client_print(client,print_chat,"[AMXX] Player %s not found.",plrname);
- return PLUGIN_HANDLED;
- }
- else if (plr == client)
- {
- client_print(client,print_chat,"[AMXX] You can't send to yourself cash!");
- return PLUGIN_HANDLED;
- }
- else if (!is_str_num(cashamount))
- {
- client_print(client,print_chat,"[AMXX] You must enter numbers only!");
- return PLUGIN_HANDLED;
- }
- iCash = str_to_num(cashamount);
- if (iCash < 1)
- client_print(client,print_chat,"[AMXX] You must send at least 1 cash!");
- else if (g_iCash[client] < iCash)
- client_print(client,print_chat,"[AMXX] You don't have that amount of cash!");
- else
- {
- g_iCash[client]-=iCash;
- g_iCash[plr]+=iCash;
- get_user_name(client,szName,charsmax(szName));
- get_user_name(plr,szName2,charsmax(szName2));
- client_print(client,print_chat,"[AMXX] You got %d cash from %s.",iCash,szName);
- client_print(client,print_chat,"[AMXX] You have send %d cash to %s.",iCash,szName);
- debug_message("SEND: PLAYER %s send %d cash to PLAYER %s.",szName,iCash,szName2);
- }
- }
- return PLUGIN_HANDLED;
- }
- public ActionGamble(client,cashamount[12]) {
- static szName[MAX_NAME_LENGTH],iCash;
- if (!cashamount[0])
- client_print(client,print_chat,"Syntex: /gamble <amount>");
- else
- {
- if (!is_str_num(cashamount))
- {
- if (equal(cashamount,"all"))
- num_to_str(g_iCash[client],cashamount,charsmax(cashamount));
- else
- {
- client_print(client,print_chat,"[AMXX] You must enter numbers only!");
- return PLUGIN_HANDLED;
- }
- }
- iCash = str_to_num(cashamount);
- if (iCash < 1)
- client_print(client,print_chat,"[AMXX] You must gamble at least 1 cash!");
- else if (g_iCash[client] < iCash)
- client_print(client,print_chat,"[AMXX] You don't have that amount of cash!");
- else
- {
- get_user_name(client,szName,charsmax(szName));
- switch(random(2))
- {
- case 0:
- {
- g_iCash[client] = clamp((g_iCash[client] + iCash),0,MAX_CASH);
- client_print(0,print_chat,"[AMXX] %s gambled on %d cash and won!",szName,iCash);
- debug_message("GAMBLE: PLAYER %s gambled on %d cash and won.",szName,iCash);
- }
- case 1:
- {
- g_iCash[client] = clamp((g_iCash[client] - iCash),0,MAX_CASH);
- client_print(0,print_chat,"[AMXX] %s gambled on %d cash and lost!",szName,iCash);
- debug_message("GAMBLE: PLAYER %s gambled on %d cash and lost.",szName,iCash);
- }
- }
- }
- }
- return PLUGIN_HANDLED;
- }
- public ActionGetCash(client) {
- static iCash;
- if (!g_bCanGetCash[client])
- client_print(client,print_chat,"[AMXX] You can use this command only next round!");
- else
- {
- g_bCanGetCash[client] = false;
- iCash = random_num(MIN_GETCASH,MAX_GETCASH);
- g_iCash[client] = clamp((g_iCash[client] + iCash),0,MAX_CASH);
- debug_message("GETCASH: Player ID %d got %d cash.",client,iCash);
- client_print(client,print_chat,"[AMXX] You got %d cash, enjoy!",iCash);
- }
- return PLUGIN_HANDLED;
- }
- public ActionNextCash(client) {
- static iCash;
- if (!g_bCanNextCash[client])
- client_print(client,print_chat,"[AMXX] You need to wait more %.1f seconds to use this command!",(pev(g_iNextCashEntity[client],pev_nextthink) - get_gametime()));
- else
- {
- g_bCanNextCash[client] = false;
- iCash = random_num(MIN_NEXTCASH,MAX_NEXTCASH);
- g_iCash[client] = clamp((g_iCash[client] + iCash),0,MAX_CASH);
- set_pev(g_iNextCashEntity[client],pev_nextthink,(get_gametime() + TIMEWAIT_NEXTCASH));
- debug_message("NEXTCASH: Player ID %d got %d cash.",client,iCash);
- client_print(client,print_chat,"[AMXX] You got %d cash, enjoy!",iCash);
- }
- return PLUGIN_HANDLED;
- }
- public EventRoundStart() {
- static i;
- for (i = 0; i < (MAX_PLAYERS + 1); i++)
- g_bCanGetCash[i] = true;
- debug_message("New round started, g_bCanGetCash global reset.");
- }
- public client_putinserver(client) {
- LoadData(client);
- CreateEntity(client);
- }
- #if defined OLD_VERSION
- public client_disconnect(client) {
- SaveData(client);
- RemoveEntity(client);
- }
- #else
- public client_disconnected(client,bool:drop,message[],maxlen) {
- SaveData(client);
- RemoveEntity(client);
- }
- #endif
- stock LoadData(client) {
- static some[32],szAuthID[24],t_szCash[12],t_bCanGetCash[2],t_bCanNextCash[2];
- if (!isClientValid(client,false))
- return;
- get_user_authid(client,szAuthID,charsmax(szAuthID));
- if (nvault_get(g_hVault,szAuthID,some,charsmax(some)))
- {
- debug_message("LOAD DATA: AuthID: ^"%s^"; Data: ^"%s^"",szAuthID,some);
- replace_all(some,charsmax(some),"#"," ");
- parse(some,t_szCash,charsmax(t_szCash),t_bCanGetCash,charsmax(t_bCanGetCash),t_bCanNextCash,charsmax(t_bCanNextCash));
- g_iCash[client] = str_to_num(t_szCash);
- g_bCanGetCash[client] = bool:str_to_num(t_bCanGetCash);
- g_bCanNextCash[client] = bool:str_to_num(t_bCanNextCash);
- }
- else
- {
- debug_message("LOAD DATA: AuthID: ^"%s^"; Data: Entry not found!",szAuthID,some);
- g_iCash[client] = 0;
- g_bCanGetCash[client] = true;
- g_bCanNextCash[client] = true;
- }
- }
- stock SaveData(client) {
- static some[32],szAuthID[24];
- if (!isClientValid(client,false))
- return;
- get_user_authid(client,szAuthID,charsmax(szAuthID));
- formatex(some,charsmax(some),"%d#%d#%d",g_iCash[client],g_bCanGetCash[client],g_bCanNextCash[client]);
- debug_message("SAVE DATA: AuthID: ^"%s^"; Data: ^"%s^"",szAuthID,some);
- nvault_set(g_hVault,szAuthID,some);
- }
- stock CreateEntity(client) {
- g_iNextCashEntity[client] = engfunc(EngFunc_CreateNamedEntity,engfunc(EngFunc_AllocString,"info_target"));
- if (!pev_valid(g_iNextCashEntity[client]))
- {
- debug_message("CRITICAL ERROR: Falied to create NextCash entity for player %d; PLUGIN ENDED RUNNING.",client);
- set_fail_state("Error creating entities; See debug log if available.");
- }
- set_pev(g_iNextCashEntity[client],pev_classname,g_szNextCashEntName);
- // So we know which client used nextcash
- set_pev(g_iNextCashEntity[client],pev_owner,client);
- // Dummy think
- set_think(g_iNextCashEntity[client],1.0);
- debug_message("Entity %d for player %d successfully created.",g_iNextCashEntity[client],client);
- }
- stock RemoveEntity(client) {
- if (!pev_valid(g_iNextCashEntity[client]))
- return 0;
- engfunc(EngFunc_RemoveEntity,g_iNextCashEntity[client]);
- debug_message("Entity %d for player %d removed.",g_iNextCashEntity[client],client);
- g_iNextCashEntity[client] = -1;
- return 1;
- }
- stock set_think(entity, Float:nextthink){
- if (pev_valid(entity))
- {
- set_pev(entity,pev_nextthink,get_gametime() + nextthink);
- return 1;
- }
- return 0;
- }
- stock bool:isClientValid(client, bool:bAlive) {
- if (!is_user_connected(client))
- return false;
- if (is_user_hltv(client) || is_user_bot(client))
- return false;
- if (bAlive)
- return bool:is_user_alive(client);
- return true;
- }
- stock debug_message(const msg[], any: ...){
- #if defined DEBUG
- static szMsg[512];
- vformat(szMsg,charsmax(szMsg),msg,2);
- log_to_file(g_szLogfile,szMsg);
- return 1;
- #else
- return 0;
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement