Advertisement
crywolfy

[ZE] Escape Coins System

Dec 16th, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.09 KB | Source Code | 0 0
  1. #include <zombie_escape>
  2.  
  3. // Static (Change it if you need)
  4. new const g_szVaultName[] = "Escape_Coins"
  5. new const g_szLogFile[] = "Escape-Coins.log" // MySQL Errors log file
  6.  
  7. // MySQL Table
  8. new const g_szTable[] =
  9. " \
  10.     CREATE TABLE IF NOT EXISTS `zombie_escape` \
  11.     ( \
  12.         `SteamID` varchar(34) NOT NULL, \
  13.         `EC` int(16) NOT NULL, \
  14.         PRIMARY KEY (`SteamID`) \
  15.     ); \
  16. "
  17.  
  18. // Variables
  19. new g_iMaxClients,
  20.     g_iVaultHandle,
  21.     g_iEscapeCoins[33],
  22.     Float:g_flDamage[33],
  23.     Handle:g_hTuple
  24.  
  25. // Cvars
  26. new g_pCvarEscapeSuccess,
  27.     g_pCvarHumanInfected,
  28.     g_pCvarDamage,
  29.     g_pCvarDamageCoins,
  30.     g_pCvarStartCoins,
  31.     g_pCvarMaxCoins,
  32.     g_pCvarEarnChatNotice,
  33.     g_pCvarSaveType,
  34.     g_pCvarDBInfo[4]
  35.  
  36. // Database
  37. enum
  38. {
  39.     Host = 0,
  40.     User,
  41.     Pass,
  42.     DB
  43. }
  44.  
  45. // Natives
  46. public plugin_natives()
  47. {
  48.     register_native("ze_get_escape_coins", "native_ze_get_escape_coins", 1)
  49.     register_native("ze_set_escape_coins", "native_ze_set_escape_coins", 1)
  50. }
  51.  
  52. public plugin_init()
  53. {
  54.     register_plugin("[ZE] Escape Coins System", ZE_VERSION, AUTHORS)
  55.    
  56.     // Hook Chains
  57.     RegisterHookChain(RG_CBasePlayer_TakeDamage, "Fw_TakeDamage_Post", 1)
  58.    
  59.     // Commands
  60.     register_clcmd("say /EC", "Coins_Info")
  61.     register_clcmd("say_team /EC", "Coins_Info")
  62.    
  63.     // Static Values
  64.     g_iMaxClients = get_member_game(m_nMaxPlayers)
  65.    
  66.     // Cvars
  67.     g_pCvarSaveType = register_cvar("ze_coins_save_type", "0")
  68.     g_pCvarEscapeSuccess = register_cvar("ze_escape_success_coins", "15")
  69.     g_pCvarHumanInfected = register_cvar("ze_human_infected_coins", "5")
  70.     g_pCvarDamage = register_cvar("ze_damage_required", "300")
  71.     g_pCvarDamageCoins = register_cvar("ze_damage_coins", "4")
  72.     g_pCvarStartCoins = register_cvar("ze_start_coins", "50")
  73.     g_pCvarMaxCoins = register_cvar("ze_max_coins", "200000")
  74.     g_pCvarEarnChatNotice = register_cvar("ze_earn_chat_notice", "1")
  75.    
  76.     g_pCvarDBInfo[Host] = register_cvar("ze_ec_host", "localhost")
  77.     g_pCvarDBInfo[User] = register_cvar("ze_ec_user", "user")
  78.     g_pCvarDBInfo[Pass] = register_cvar("ze_ec_pass", "pass")
  79.     g_pCvarDBInfo[DB] = register_cvar("ze_ec_dbname", "dbname")
  80.    
  81.     // Initialize MySQL - Delay 0.1 second required so we make sure that our zombie_escape.cfg already executed and cvars values loaded from it
  82.     set_task(0.1, "Delay_MySQL_Init")
  83. }
  84.  
  85. public Coins_Info(id)
  86. {
  87.     ze_colored_print(id, "%L", LANG_PLAYER, "COINS_INFO", g_iEscapeCoins[id])
  88. }
  89.  
  90. public Delay_MySQL_Init()
  91. {
  92.     MySQL_Init()
  93. }
  94.  
  95. public MySQL_Init()
  96. {
  97.     if (!get_pcvar_num(g_pCvarSaveType))
  98.         return
  99.    
  100.     new szHost[64], szUser[32], szPass[32], szDB[128]
  101.    
  102.     get_pcvar_string(g_pCvarDBInfo[Host], szHost, charsmax(szHost))
  103.     get_pcvar_string(g_pCvarDBInfo[User], szUser, charsmax(szUser))
  104.     get_pcvar_string(g_pCvarDBInfo[Pass], szPass, charsmax(szPass))
  105.     get_pcvar_string(g_pCvarDBInfo[DB], szDB, charsmax(szDB))
  106.    
  107.     g_hTuple = SQL_MakeDbTuple(szHost, szUser, szPass, szDB)
  108.    
  109.     // Let's ensure that the g_hTuple will be valid, we will access the database to make sure
  110.     new iErrorCode, szError[512], Handle:hSQLConnection
  111.    
  112.     hSQLConnection = SQL_Connect(g_hTuple, iErrorCode, szError, charsmax(szError))
  113.    
  114.     if(hSQLConnection != Empty_Handle)
  115.     {
  116.         log_amx("[MySQL] Successfully connected to host: %s (ALL IS OK).", szHost)
  117.         SQL_FreeHandle(hSQLConnection)
  118.     }
  119.     else
  120.     {
  121.         // Disable plugin, and display the error
  122.         set_fail_state("Failed to connect to MySQL database: %s", szError)
  123.     }
  124.    
  125.     // Create our table
  126.     SQL_ThreadQuery(g_hTuple, "QueryCreateTable", g_szTable)
  127. }
  128.  
  129. public QueryCreateTable(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
  130. {
  131.     SQL_IsFail(iFailState, iError, szError, g_szLogFile)
  132. }
  133.  
  134. public client_authorized(id)
  135. {
  136.     if( !is_user_hltv(id) && !is_user_bot(id) )
  137.     {
  138.         LoadCoins(id)
  139.     }
  140. }
  141.  
  142. public plugin_end()
  143. {
  144.     if (get_pcvar_num(g_pCvarSaveType))
  145.     {
  146.         if (g_hTuple != Empty_Handle)
  147.         {
  148.             SQL_FreeHandle(g_hTuple)
  149.         }
  150.     }
  151.     else
  152.     {
  153.         nvault_close(g_iVaultHandle)
  154.     }
  155. }
  156.  
  157. public ze_roundend(WinTeam)
  158. {
  159.     if (WinTeam == ZE_TEAM_HUMAN)
  160.     {
  161.         for(new id = 1; id <= g_iMaxClients; id++)
  162.         {
  163.             g_flDamage[id] = 0.0
  164.            
  165.             if (!is_user_alive(id) || ze_is_user_zombie(id))
  166.                 continue
  167.            
  168.             g_iEscapeCoins[id] += get_pcvar_num(g_pCvarEscapeSuccess)
  169.            
  170.             SaveCoins(id)
  171.            
  172.             if (get_pcvar_num(g_pCvarEarnChatNotice))
  173.             {
  174.                 ze_colored_print(id, "%L", LANG_PLAYER, "ESCAPE_SUCCESS_COINS", get_pcvar_num(g_pCvarEscapeSuccess))
  175.             }
  176.         }
  177.     }
  178. }
  179.  
  180. public ze_user_infected(iVictim, iInfector)
  181. {
  182.     if (iInfector == 0) // Server ID
  183.         return
  184.  
  185.     g_iEscapeCoins[iInfector] += get_pcvar_num(g_pCvarHumanInfected)
  186.    
  187.     SaveCoins(iInfector)
  188.    
  189.     if (get_pcvar_num(g_pCvarEarnChatNotice))
  190.     {
  191.         ze_colored_print(iInfector, "%L", LANG_PLAYER, "HUMAN_INFECTED_COINS", get_pcvar_num(g_pCvarHumanInfected))
  192.     }
  193. }
  194.  
  195. public Fw_TakeDamage_Post(iVictim, iInflictor, iAttacker, Float:fDamage, bitsDamageType)
  196. {
  197.     // Player Damage Himself
  198.     if (iVictim == iAttacker)
  199.         return HC_CONTINUE
  200.    
  201.     // Two Players From one Team
  202.     if (get_member(iAttacker, m_iTeam) == get_member(iVictim, m_iTeam))
  203.         return HC_CONTINUE
  204.    
  205.     // iVictim or iAttacker Not Alive
  206.     if (!is_user_alive(iVictim) || !is_user_alive(iAttacker))
  207.         return HC_CONTINUE
  208.    
  209.     // Attacker is Zombie
  210.     if (get_member(iAttacker, m_iTeam) == TEAM_TERRORIST)
  211.         return HC_CONTINUE
  212.    
  213.     // Store Damage For every Player
  214.     g_flDamage[iAttacker] += fDamage
  215.    
  216.     // Damage Calculator Equal or Higher than needed damage
  217.     while (g_flDamage[iAttacker] >= get_pcvar_float(g_pCvarDamage))
  218.     {
  219.         g_iEscapeCoins[iAttacker] += (get_pcvar_num(g_pCvarDamageCoins))
  220.         g_flDamage[iAttacker] -= get_pcvar_float(g_pCvarDamage)
  221.     }
  222.        
  223.     SaveCoins(iAttacker)
  224.  
  225.     return HC_CONTINUE
  226. }
  227.  
  228. LoadCoins(id)
  229. {
  230.     new szAuthID[35]
  231.     get_user_authid(id, szAuthID, charsmax(szAuthID))
  232.    
  233.     if (!get_pcvar_num(g_pCvarSaveType))
  234.     {
  235.         // Open the Vault
  236.         g_iVaultHandle = nvault_open(g_szVaultName)
  237.        
  238.         // Get coins
  239.         new szCoins[16], iExists, iTimestamp;
  240.         iExists = nvault_lookup(g_iVaultHandle, szAuthID, szCoins, charsmax(szCoins), iTimestamp);
  241.        
  242.         // Close Vault
  243.         nvault_close(g_iVaultHandle)
  244.        
  245.         if (!iExists)
  246.         {
  247.             // Player exist? Load start value then save
  248.             g_iEscapeCoins[id] = get_pcvar_num(g_pCvarStartCoins)
  249.             SaveCoins(id)
  250.         }
  251.         else
  252.         {
  253.             g_iEscapeCoins[id] = str_to_num(szCoins)
  254.         }
  255.     }
  256.     else
  257.     {
  258.         new szQuery[128], szData[5]
  259.         formatex(szQuery, charsmax(szQuery), "SELECT `EC` FROM `zombie_escape` WHERE ( `SteamID` = '%s' );", szAuthID)
  260.      
  261.         num_to_str(id, szData, charsmax(szData))
  262.         SQL_ThreadQuery(g_hTuple, "QuerySelectData", szQuery, szData, charsmax(szData))
  263.     }
  264. }
  265.  
  266. public QuerySelectData(iFailState, Handle:hQuery, szError[], iError, szData[])
  267. {
  268.     if(SQL_IsFail(iFailState, iError, szError, g_szLogFile))
  269.         return
  270.    
  271.     new id = str_to_num(szData)
  272.    
  273.     // No results for this query means that player not saved before
  274.     if(!SQL_NumResults(hQuery))
  275.     {
  276.         // This is new player
  277.         g_iEscapeCoins[id] = get_pcvar_num(g_pCvarStartCoins)
  278.        
  279.         // Get user steamid
  280.         new szAuthID[35]
  281.         get_user_authid(id, szAuthID, charsmax(szAuthID))
  282.        
  283.         // Insert his data to our database
  284.         new szQuery[128]
  285.        
  286.         formatex(szQuery, charsmax(szQuery), "INSERT INTO `zombie_escape` (`SteamID`, `EC`) VALUES ('%s', '%d');", szAuthID, g_iEscapeCoins[id])
  287.         SQL_ThreadQuery(g_hTuple, "QueryInsertData", szQuery)
  288.        
  289.         return
  290.     }
  291.    
  292.     // Get the "EC" column number (It's 2, always i don't like to hardcode :p)
  293.     new iEC_Column = SQL_FieldNameToNum(hQuery, "EC")
  294.    
  295.     // Read the coins of this player
  296.     g_iEscapeCoins[id] = SQL_ReadResult(hQuery, iEC_Column)
  297. }
  298.  
  299. public QueryInsertData(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
  300. {
  301.     SQL_IsFail(iFailState, iError, szError, g_szLogFile)
  302. }
  303.  
  304. SaveCoins(id)
  305. {
  306.     new szAuthID[35], iMaxValue
  307.     iMaxValue = get_pcvar_num(g_pCvarMaxCoins)
  308.     get_user_authid(id, szAuthID, charsmax(szAuthID))
  309.    
  310.     // Set Him to max if he Higher than Max Value
  311.     if (g_iEscapeCoins[id] > iMaxValue)
  312.     {
  313.         g_iEscapeCoins[id] = iMaxValue
  314.     }
  315.  
  316.     new szData[16]
  317.     num_to_str(g_iEscapeCoins[id], szData, charsmax(szData))
  318.  
  319.     if (!get_pcvar_num(g_pCvarSaveType))
  320.     {
  321.         // Open the Vault
  322.         g_iVaultHandle = nvault_open(g_szVaultName)
  323.  
  324.         // Save His SteamID, Escape Coins
  325.         nvault_set(g_iVaultHandle, szAuthID, szData)
  326.        
  327.         // Close Vault
  328.         nvault_close(g_iVaultHandle)
  329.     }
  330.     else
  331.     {
  332.         new szQuery[128]
  333.         formatex(szQuery, charsmax(szQuery), "UPDATE `zombie_escape` SET `EC` = '%d' WHERE `SteamID` = '%s';", g_iEscapeCoins[id], szAuthID)
  334.         SQL_ThreadQuery(g_hTuple, "QueryUpdateData", szQuery)
  335.     }
  336. }
  337.  
  338. public QueryUpdateData(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
  339. {
  340.     SQL_IsFail(iFailState, iError, szError, g_szLogFile)
  341. }
  342.  
  343. // Natives
  344. public native_ze_get_escape_coins(id)
  345. {
  346.     if (!is_user_connected(id))
  347.     {
  348.         log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player id (%d)", id)
  349.         return false;
  350.     }
  351.    
  352.     return g_iEscapeCoins[id]
  353. }
  354.  
  355. public native_ze_set_escape_coins(id, iAmount)
  356. {
  357.     if (!is_user_connected(id))
  358.     {
  359.         log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player id (%d)", id)
  360.         return false;
  361.     }
  362.    
  363.     g_iEscapeCoins[id] = iAmount
  364.    
  365.     SaveCoins(id)
  366.     return true;
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement