Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using Newtonsoft.Json;
- namespace Oxide.Plugins
- {
- [Info("LotterySystem", "Krythic", "1.0.0")]
- [Description("Allows players to use a lottery system")]
- public class LotterySystem : RustPlugin
- {
- private int _defaultJackpot = 1000;
- private int _ticketCost = 100;
- private int _maximumOdds = 4192;
- private int _scrapGameItemID = -932201673; // ID for Scrap
- private void Init()
- {
- cmd.AddChatCommand("lottery", this, "LotteryCommand");
- cmd.AddChatCommand("buyTicket", this, "BuyLotteryTicketCommand");
- }
- private int NextRoll(int min, int max)
- {
- return UnityEngine.Random.Range(min, max + 1);
- }
- private void LotteryCommand(BasePlayer player, string command, string[] args)
- {
- LoadConfig();
- Puts($"{player.displayName} displayed the Lottery info.");
- SendReply(player,
- "[Lottery System]\n" +
- "Creator: Krythic\n" +
- "v1.0.0\n" +
- "Current Jackpot: \n" +
- " " + config.jackpot + " scrap \n" +
- "Type /buyTicket to buy a lottery ticket for 100 scrap."
- );
- }
- private void BuyLotteryTicketCommand(BasePlayer player, string command, string[] args)
- {
- LoadConfig();
- if (HasEnoughScrap(player, _ticketCost))
- {
- Puts($"{player.displayName} purchased a lottery ticket...");
- bool lotteryResult = NextRoll(1, _maximumOdds) == 1;
- if (lotteryResult)
- {
- // The Player won the lottery
- PrintToChat("[Lottery] " + $"{player.displayName} purchased a lottery ticket...");
- PrintToChat("[Lottery] " + $"{player.displayName} won a jackpot of " + config.jackpot + " Scrap!");
- Puts($"{player.displayName} won a jackpot of " + config.jackpot + " Scrap!");
- GiveScrap(player, config.jackpot);
- config.jackpot = _defaultJackpot; // Reset the jackpot after giving the player their winnings
- SaveConfig();
- }
- else
- {
- // The Player did not win the lottery
- TakeScrap(player, _ticketCost);
- PrintToChat("[Lottery] " + $"{player.displayName} purchased a lottery ticket...");
- PrintToChat("[Lottery] " + $"{player.displayName} did not win.");
- Puts($"{player.displayName} did not win the lottery.");
- config.jackpot += _ticketCost;
- PrintToChat("[Lottery] The current jackpot is now: " + config.jackpot + " Scrap.");
- SaveConfig();
- }
- }
- else
- {
- SendReply(player, "[Lottery System] You do not have enough scrap to buy a lottery ticket.");
- }
- }
- private bool HasEnoughScrap(BasePlayer player, int amount)
- {
- int scrapAmount = player.inventory.GetAmount(_scrapGameItemID);
- return scrapAmount >= amount;
- }
- private void TakeScrap(BasePlayer player, int amount)
- {
- player.inventory.Take(null, _scrapGameItemID, amount);
- }
- private void GiveScrap(BasePlayer player, int amount)
- {
- player.inventory.GiveItem(ItemManager.CreateByItemID(_scrapGameItemID, amount));
- }
- #region Config
- private ConfigData config = new ConfigData();
- private class ConfigData
- {
- [JsonProperty(PropertyName = "Jackpot")]
- public int jackpot = 0;
- }
- protected override void LoadConfig()
- {
- base.LoadConfig();
- try
- {
- config = Config.ReadObject<ConfigData>();
- if (config == null) LoadDefaultConfig();
- }
- catch
- {
- PrintError("Configuration file is corrupt, check your config file at https://jsonlint.com/!");
- LoadDefaultConfig();
- return;
- }
- SaveConfig();
- }
- protected override void LoadDefaultConfig() => config = new ConfigData();
- protected override void SaveConfig() => Config.WriteObject(config);
- #endregion
- // Add the Unload method to increment the jackpot on server shutdown
- private void Unload()
- {
- LoadConfig();
- config.jackpot += 50;
- SaveConfig();
- Puts("[Lottery System] Server is shutting down. Jackpot has been increased by 50 scrap.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement