Mario AR. |
07-10-2014 02:29 |
Re: AMXX Lottery
AMXX Lottery API
Installing the API- To use the API, the first step is to download the source code, edit one line and then compile.
PHP Code:
/*================================================================================================
Uncomment the next line if you want to use the API. Read the .inc file for instructions. Using the API will disable the following cvars: lottery_options, lottery_min_participants, lottery_cost, lottery_reward, lottery_fixed_reward. The API does not require the cstrike module. */
// #define EXTERNAL_MANAGEMENT
/*==============================================================================================*/
- We should get:
PHP Code:
#define EXTERNAL_MANAGEMENT
- After uncommenting that line and compiling, we should be able to use the API.
- Now, we have to download the .inc file from the main post (Were we downloaded the source code) and paste it in our addons/amxmodx/scripting/include folder (Or wherever is located our compiler).
Configuring our plugins to work with the lottery API- First of all: The lottery will NOT work properly if we don't have plugins communicating with the API.
- This is part of the .inc file:
PHP Code:
// Set the description of the cost or reward. native lottery_set_description(const What = LOTTERY_COST, const Description[] = "", any:...);
// Set the number of options for the players to choose. native lottery_set_options_num(const OptionsNum);
// Sets the minimun number of participants for the lottery. native lottery_set_min_participants(const MinParticipants)
// Forces a lottery to start. Returns true on success, false if the lottery couldn't begin. native bool:lottery_force_start();
// Returns the seconds (float with ms) remaining for the next lottery, 0.0 if a lottery is in progress. // Returns < 0 if there is not a next lottery. native Float:lottery_time_remaining();
// Called when a lottery is ready to start. // This is the correct place to call the natives lottery_set_* // Return PLUGIN_HANDLED to stop the lottery from beginning. forward lottery_start();
// Called when someone wants to participate. Here we must check if the player can pay. // Return PLUGIN_HANDLED if the player can't pay. forward lottery_check(id);
// After checking that the player can pay the lottery, this is called. // Here we collect the cost from the player. forward lottery_pay(id);
// This forward is called if someone payed to play but the lottery didn't start because of not enought participants. // Here we refund the cost to the player. forward lottery_refund(id);
// This forward is called when a player won the lottery. Here we give the reward to the player. forward lottery_give_reward(id);
- Here's a sample plugin to work with the API:
PHP Code:
#include <amxmodx> #include <cstrike> #include <fun> #include <lottery_api> // MUST have.
new cvar_cost;
public plugin_init() { register_plugin("Testing Lottery API", "0.1", "Mario AR."); cvar_cost = register_cvar("test_lottery_cost", "100"); }
// Forward called when a lottery is ready to start. public lottery_start() { // Let's set the description of the cost lottery_set_description(LOTTERY_COST, "%d Dollars", get_pcvar_num(cvar_cost)); lottery_set_description(LOTTERY_REWARD, "Godmode for 60 seconds."); // Here, we set the maximun ammount of options to choose. lottery_set_options_num(50); // The reward is godmode! Win must be hard. // In this lottery, I want at least 10 participants. lottery_set_min_participants(10); // The lottery will start. return PLUGIN_CONTINUE; }
// Forward called when someone wants to participate. Here we must check if he/she can pay. public lottery_check(id) { if (cs_get_user_money(id) < get_pcvar_num(cvar_cost)) return PLUGIN_HANDLED; // Return PLUGIN_HANDLED if he/she can't. return PLUGIN_CONTINUE; // If he/she can pay, return PLUGIN_CONTINUE. }
// Forward called after checkings are done. The player can pay, so let's take the money! public lottery_pay(id) { cs_set_user_money(id, cs_get_user_money(id) - get_pcvar_num(cvar_cost)); }
// This forward is called IF someone payed but the lottery was cancelled. public lottery_refund(id) { // I'm sorry, Take your money back. cs_set_user_money(id, cs_get_user_money(id) + get_pcvar_num(cvar_cost)); }
// This forward is called when someone won the lottery. public lottery_give_reward(id) { // Ok, you won. As I promised, godmode for 60 seconds. set_user_godmode(id, 1); set_task(60.0, "remove_godmode", id); }
public remove_godmode(id) { if (!is_user_connected(id)) return; set_user_godmode(id, 0); }
- With these plugin running along with the API, I've got:
[IMG]http://s11.************/b241f605v/test1.jpg[/IMG]
- This plugin will allow admins to start a lottery, and add a command for users to know when the next lottery will start.
PHP Code:
#include <amxmodx> #include <amxmisc> #include <lottery_api>
public plugin_init() { register_plugin("Lottery Test", "0.1", "Mario AR."); register_clcmd("say /lotterystart", "clcmd_lotterystart", ADMIN_KICK); register_clcmd("say /nextlottery", "clcmd_nextlottery"); }
public clcmd_lotterystart(id, level, cid) { if (!cmd_access(id, level, cid, 0)) return PLUGIN_HANDLED; if (lottery_force_start()) { new szName[32]; get_user_name(id, szName, 31); client_print(0, print_chat, "Admin %s - Start a lottery!", szName); } else { client_print(id, print_chat, "Lottery could not start."); } return PLUGIN_HANDLED; }
public clcmd_nextlottery(id) { new Float:nexttime = lottery_time_remaining(); if (nexttime > 0.0) { client_print(id, print_chat, "Next lottery in %.f seconds.", nexttime); } else if (nexttime == 0.0) { client_print(id, print_chat, "The lottery is in progress right now."); } else { client_print(id, print_chat, "There's no time for the next lottery."); } return PLUGIN_HANDLED; }
- Now, I want to do something different. I want 3 different lotteries! Let's go!
PHP Code:
#include <amxmodx> #include <cstrike> #include <fun> #include <zombieplague> #include <lottery_api> // MUST have.
new g_lottery;
public plugin_init() { register_plugin("Testing Lottery API", "0.1", "Mario AR."); }
// Forward called when a lottery is ready to start. public lottery_start() { g_lottery++; // Just a check to avoid problems. if (g_lottery > 3) g_lottery = 1; switch (g_lottery) { case 1: { lottery_set_description(LOTTERY_COST, "100 Dollars."); lottery_set_description(LOTTERY_REWARD, "Godmode for 60 seconds."); lottery_set_options_num(10); lottery_set_min_participants(5); } case 2: { lottery_set_description(LOTTERY_COST, "200 Ammo packs."); lottery_set_description(LOTTERY_REWARD, "You'll be turned into a Survivor/Nemesis."); lottery_set_options_num(50); lottery_set_min_participants(2); } case 3: { lottery_set_description(LOTTERY_COST, "500 HP."); lottery_set_description(LOTTERY_REWARD, "5000 Ammo Packs."); lottery_set_options_num(20); lottery_set_min_participants(5); } } // The lottery will start. return PLUGIN_CONTINUE; }
// Forward called when someone wants to participate. Here we must check if he/she can pay. public lottery_check(id) { switch (g_lottery) { case 1: { if (cs_get_user_money(id) < 100) return PLUGIN_HANDLED; } case 2: { if (zp_get_user_ammo_packs(id) < 200) return PLUGIN_HANDLED; } case 3: { if (get_user_health(id) <= 500) // He/she MUST have at least 501 HP, so will not die. return PLUGIN_HANDLED; } } return PLUGIN_CONTINUE; // If he/she can pay, return PLUGIN_CONTINUE. }
// Forward called after checkings are done. The player can pay, so let's take the money! public lottery_pay(id) { switch (g_lottery) { case 1: { cs_set_user_money(id, cs_get_user_money(id) - 100); } case 2: { zp_set_user_ammo_packs(id, zp_get_user_ammo_packs(id) - 200); } case 3: { set_user_health(id, get_user_health(id) - 500); } } }
// This forward is called IF someone payed but the lottery was cancelled. public lottery_refund(id) { switch (g_lottery) { case 1: { cs_set_user_money(id, cs_get_user_money(id) + 100); } case 2: { zp_set_user_ammo_packs(id, zp_get_user_ammo_packs(id) + 200); } case 3: { set_user_health(id, get_user_health(id) + 500); } } }
// This forward is called when someone won the lottery. public lottery_give_reward(id) { switch (g_lottery) { case 1: { set_user_godmode(id, 1); set_task(60.0, "remove_godmode", id); } case 2: { if (is_user_alive(id)) { if (zp_get_user_zombie(id)) zp_make_user_nemesis(id); else zp_make_user_survivor(id); } } case 3: { zp_set_user_ammo_packs(id, zp_get_user_ammo_packs(id) + 5000); } } }
public remove_godmode(id) { if (!is_user_connected(id)) return; set_user_godmode(id, 0); }
- The results:
[IMG]http://s29.************/gxkf1a7xz/test2.jpg[/IMG]
[IMG]http://s12.************/mdhtbqr5p/test3.jpg[/IMG]
- That's it. You can do a lot of things with the API. Just use your imagination.
|