PHP Code:
#pragma semicolon 1
#include <sourcemod>
#define PLUGIN_VERSION "1.0.0"
new bool:g_bBoughtRaffle[MAXPLAYERS+1];
new bool:g_bEnabled;
new Handle:g_hCvarCost;
new Handle:g_hCvarCustom;
new Handle:g_hCvarPrize;
public Plugin:myinfo =
{
name = "Raffle",
author = "bl4nk",
description = "Allows players to buy raffle tickets and possibly win a prize",
version = PLUGIN_VERSION,
url = "http://forums.alliedmods.net"
};
public OnPluginStart()
{
CreateConVar("raffle_version", PLUGIN_VERSION, "Raffle Version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY|FCVAR_DONTRECORD);
g_hCvarCost = CreateConVar("raffle_cost", "500", "Cost to buy a raffle ticket", FCVAR_PLUGIN, true, 0.0);
g_hCvarCustom = CreateConVar("custom_prize", "0", "Sets the prize: 0 = all money gathered by raffle, 1 = value of cvar \"raffle_prize\", 2 = +100 health", FCVAR_PLUGIN, true, 0.0, true, 2.0);
g_hCvarPrize = CreateConVar("raffle_prize", "2000", "Amount of money the players wins when chosen in the raffle", FCVAR_PLUGIN, true, 0.0, true, 1600.0);
HookEvent("round_start", Event_RoundStart);
HookEvent("round_end", Event_RoundEnd);
RegConsoleCmd("raffle", Command_Raffle);
}
public OnClientDisconnect(client)
{
g_bBoughtRaffle[client] = false;
}
public Action:Command_Raffle(client, args)
{
if (!g_bEnabled)
{
ReplyToCommand(client, "[SM] You cannot buy a raffle ticket until the next round.");
return Plugin_Handled;
}
if (g_bBoughtRaffle[client])
{
ReplyToCommand(client, "[SM] You can only buy one raffle ticket per round.");
return Plugin_Handled;
}
new cost = GetConVarInt(g_hCvarCost);
if (GetClientMoney(client) < cost)
{
ReplyToCommand(client, "[SM] You do not have enough money (tickets are $%i each).", cost);
return Plugin_Handled;
}
g_bBoughtRaffle[client] = true;
RemoveClientMoney(client, cost);
ReplyToCommand(client, "[SM] You bought a raffle ticket!");
return Plugin_Handled;
}
public Event_RoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
g_bEnabled = true;
for (new i = 1; i <= MaxClients; i++)
{
g_bBoughtRaffle[i] = false;
}
}
public Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
{
g_bEnabled = false;
new Handle:hArray = CreateArray(MAXPLAYERS);
for (new i = 1; i <= MaxClients; i++)
{
if (g_bBoughtRaffle[i])
{
PushArrayCell(hArray, i);
}
}
SortADTArray(hArray, Sort_Random, Sort_Integer);
new client = GetArrayCell(hArray, 0);
PrintToChatAll("[SM] Player %N won the raffle!", client);
CloseHandle(hArray);
switch(GetConVarInt(g_hCvarCustom))
{
case 0:
{
new count;
for (new i = 1; i <= MaxClients; i++)
{
if (g_bBoughtRaffle[i])
{
count++;
}
}
new prize = GetConVarInt(g_hCvarCost) * count;
AddClientMoney(client, prize);
PrintToChat(client, "[SM] You won the raffle and get $%i!", prize);
}
case 1:
{
new prize = GetConVarInt(g_hCvarPrize);
AddClientMoney(client, prize);
PrintToChat(client, "[SM] You won the raffle and get $%i!", prize);
}
case 2:
{
new health = GetEntProp(client, Prop_Send, "m_iHealth");
SetEntProp(client, Prop_Send, "m_iHealth", health + 100);
PrintToChat(client, "[SM] You won the raffle and get +100 health!");
}
}
}
GetClientMoney(client)
{
return GetEntProp(client, Prop_Send, "m_iAccount");
}
SetClientMoney(client, amount)
{
SetEntProp(client, Prop_Send, "m_iAccount", amount);
}
RemoveClientMoney(client, amount)
{
new cash = GetClientMoney(client);
cash -= amount;
if (cash < 0)
{
cash = 0;
}
SetClientMoney(client, cash);
}
AddClientMoney(client, amount)
{
new cash = GetClientMoney(client);
cash += amount;
if (cash > 16000)
{
cash = 16000;
}
SetClientMoney(client, cash);
}
raffle_cost "500" - Cost to buy a raffle ticket
custom_prize "0" - Sets the prize: 0 = all money gathered by raffle, 1 = value of cvar "raffle_prize", 2 = +100 health
raffle_prize "2000" - Amount of money the players wins when chosen in the raffle
The command to buy raffle tickets is "!raffle" or "/raffle" in chat, or the console command "raffle".