SS_Legend requested me to make a lottery command for him so I thought I'd share it with everyone also, in case anybody else may like the idea of it.
I made it so that players can join the lottery at a ticket price, and then a portion of this price (e.g. 60%) is added to the lottery jackpot. At the end of the round a random play who bought a ticket wins the whole prize and everything is reset for the next round. You can decide on the price and the rate at which the prize pool increases.
This is pretty basic right now so if anybody else wants to suggest things to make it better that would be cool. Hopefully it works well also, I tested it once and it was okay, if anything weird happens then let me know o.O
You can see the code here:
http://pastie.org/private/s9qoca3xykptgqllh6w1w#11
Clearer:
http://pastie.org/pastes/6251268/text?k ... tgqllh6w1w
Forum Text:
Spoiler:
- Code: Select all
using System;
using System.Collections.Generic;
using System.IO;
namespace MCDzienny
{
public class CmdLottery : Command
{
public override string name { get { return "lottery"; } }
public override string shortcut { get { return "lt"; } }
public override string type { get { return "other"; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
// This is the price of your lottery tickets
public static int Price = 20;
// Set this to true if you want everybody to be notified when players buy tickets
public static bool global = false;
// This is the % of the price of tickets that is added onto the prize pool. For example:
// If the price is 10, and the prizePercentageFactor is 50, 50% of 10 is 5. So the prize pool increases by 5 when a ticket is bought (costing 10).
// Typically you'll want to keep this below 100 to make sure there is no net increase in wealth on the server.
public static int prizePercentageFactor = 70;
public static List<Player> lotteryPlayers = new List<Player>();
public static int totalPrize = 0;
public static int previousPrize = 0;
public static string previousWinner = "Nobody";
public static string Path = "extra/lottery.properties";
public static bool lotteryEnabled = true;
public override void Help(Player p)
{
Player.SendMessage(p, "/lottery join - buy a lottery ticket for: " + Price.ToString() + " " + Server.moneys + ".");
Player.SendMessage(p, "/lottery info - see information for the current lottery status.");
if (p.group.Permission >= LevelPermission.Admin)
{
Player.SendMessage(p, "/lottery [enable/disable] - enable or disable the lottery.");
}
}
public override void Use(Player p, string message)
{
if (String.IsNullOrEmpty(message) || message.IndexOf(' ') != -1)
{
Help(p);
return;
}
switch (message.Trim().ToLower())
{
case "join":
if (!lotteryEnabled)
{
Player.SendMessage(p, "The lottery is currently disabled.");
return;
}
if (!lotteryPlayers.Contains(p))
{
if (p.money >= Price)
{
p.money -= Price;
lotteryPlayers.Add(p);
int prizeAdittion = (int)Math.Abs(((double)prizePercentageFactor / 100) * Price);
totalPrize += prizeAdittion;
if (global)
{
Player.GlobalMessage(p.color + p.PublicName + Server.DefaultColor + " has bought a lottery ticket!");
}
else
{
Player.SendMessage(p, "You have bought a lottery ticket! The winners are announced when the round ends.");
}
}
else
{
Player.SendMessage(p, "You do not have enough " + Server.moneys + " to buy a lottery ticket!");
Player.SendMessage(p, "You need " + (Price - p.money).ToString() + " more " + Server.moneys);
}
}
else
{
Player.SendMessage(p, "You have already bought a ticket! Type /lottery info for the current lottery status.");
}
break;
case "info":
string allPlayers = "";
if (lotteryPlayers.Count != 0)
{
lotteryPlayers.ForEach(pl =>
{
if (pl != null)
{
allPlayers += pl.color + pl.PublicName + Server.DefaultColor + ", ";
}
});
}
if (allPlayers == "")
{
allPlayers = "Nobody";
}
else
{
allPlayers = allPlayers.Remove(allPlayers.LastIndexOf(','));
}
if (!lotteryEnabled)
{
Player.SendMessage(p, "The lottery is currently disabled.");
}
else
{
Player.SendMessage(p, "Current players with tickets: " + allPlayers);
Player.SendMessage(p, "Current prize pool: " + totalPrize.ToString() + " " + Server.moneys);
Player.SendMessage(p, "Previous winner: " + previousWinner + Server.DefaultColor + " | Prize: " + previousPrize + " " + Server.moneys);
}
break;
case "enable":
if (p.group.Permission >= LevelPermission.Admin)
{
if (!lotteryEnabled)
{
File.WriteAllText(Path, "true");
lotteryEnabled = true;
Player.GlobalMessage("The lottery was enabled!");
}
else
{
Player.SendMessage(p, "The lottery is already enabled.");
}
}
else
{
Help(p);
}
break;
case "disable":
if (p.group.Permission >= LevelPermission.Admin)
{
if (lotteryEnabled)
{
File.WriteAllText(Path, "false");
lotteryEnabled = false;
Player.GlobalMessage("The lottery has been disabled!");
totalPrize = 0;
if (lotteryPlayers.Count != 0)
{
lotteryPlayers.ForEach(pl =>
{
if (pl != null)
{
Player.SendMessage(pl, "Unfortunately the lottery has been disabled. You have been refunded.");
pl.money += Price;
}
});
}
lotteryPlayers.Clear();
}
}
else
{
Help(p);
}
break;
default:
Help(p);
return;
}
}
public override void Init()
{
if (!File.Exists(Path))
{
File.WriteAllText(Path, "true");
}
bool checkEnabled = bool.Parse(File.ReadAllText(Path).Trim());
lotteryEnabled = checkEnabled;
InfectionSystem.InfectionSystem.AnnounceWinners += InfectionRoundEnded;
}
private void InfectionRoundEnded(object sender, InfectionSystem.AnnounceWinnersEventArgs e)
{
if (lotteryEnabled)
{
if (lotteryPlayers.Count != 0)
{
Random ran = new Random();
Player winner = lotteryPlayers[ran.Next(0, lotteryPlayers.Count)];
if (winner != null)
{
Player.GlobalMessage("The lottery has come to a close! And the winner of " + totalPrize.ToString() + " " + Server.moneys + " is " + winner.color + winner.PublicName + Server.DefaultColor + "! Congratulations!");
winner.money += totalPrize;
previousWinner = winner.color + winner.PublicName;
previousPrize = totalPrize;
totalPrize = 0;
lotteryPlayers.Clear();
}
else
{
Player.GlobalMessage("An offline player has won the lottery, but they are not here to collect their prize!");
previousWinner = "Offline Player";
previousPrize = 0;
totalPrize = 0;
}
}
else
{
Player.GlobalMessage("No players joined the lottery this round. Buy tickets next round!");
}
}
}
}
}
Oh, and I didn't want to fiddle with mySQL as I'm an amateur with it, and I don't want to mess anything up so for now offline players who win the prize aren't rewarded. But hey, its their fault for leaving!
Thanks.