Advertisement
Krythic

Lottery System

Jun 16th, 2024
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.85 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using Newtonsoft.Json;
  4.  
  5. namespace Oxide.Plugins
  6. {
  7.     [Info("LotterySystem", "Krythic", "1.0.0")]
  8.     [Description("Allows players to use a lottery system")]
  9.  
  10.     public class LotterySystem : RustPlugin
  11.     {
  12.         private int _defaultJackpot = 1000;
  13.         private int _ticketCost = 100;
  14.         private int _maximumOdds = 4192;
  15.         private int _scrapGameItemID = -932201673; // ID for Scrap
  16.  
  17.         private void Init()
  18.         {
  19.             cmd.AddChatCommand("lottery", this, "LotteryCommand");
  20.             cmd.AddChatCommand("buyTicket", this, "BuyLotteryTicketCommand");
  21.         }
  22.  
  23.         private int NextRoll(int min, int max)
  24.         {
  25.             return UnityEngine.Random.Range(min, max + 1);
  26.         }
  27.  
  28.         private void LotteryCommand(BasePlayer player, string command, string[] args)
  29.         {
  30.             LoadConfig();
  31.             Puts($"{player.displayName} displayed the Lottery info.");
  32.             SendReply(player,
  33.                 "[Lottery System]\n" +
  34.                 "Creator: Krythic\n" +
  35.                 "v1.0.0\n" +
  36.                 "Current Jackpot: \n" +
  37.                 "  " + config.jackpot + " scrap \n" +
  38.                 "Type /buyTicket to buy a lottery ticket for 100 scrap."
  39.             );
  40.         }
  41.  
  42.         private void BuyLotteryTicketCommand(BasePlayer player, string command, string[] args)
  43.         {
  44.             LoadConfig();
  45.             if (HasEnoughScrap(player, _ticketCost))
  46.             {
  47.                 Puts($"{player.displayName} purchased a lottery ticket...");
  48.                 bool lotteryResult = NextRoll(1, _maximumOdds) == 1;
  49.                 if (lotteryResult)
  50.                 {
  51.                     // The Player won the lottery
  52.                     PrintToChat("[Lottery] " + $"{player.displayName} purchased a lottery ticket...");
  53.                     PrintToChat("[Lottery] " + $"{player.displayName} won a jackpot of " + config.jackpot + " Scrap!");
  54.                     Puts($"{player.displayName} won a jackpot of " + config.jackpot + " Scrap!");
  55.                     GiveScrap(player, config.jackpot);
  56.                     config.jackpot = _defaultJackpot; // Reset the jackpot after giving the player their winnings
  57.                     SaveConfig();
  58.                 }
  59.                 else
  60.                 {
  61.                     // The Player did not win the lottery
  62.                     TakeScrap(player, _ticketCost);
  63.                     PrintToChat("[Lottery] " + $"{player.displayName} purchased a lottery ticket...");
  64.                     PrintToChat("[Lottery] " + $"{player.displayName} did not win.");
  65.                     Puts($"{player.displayName} did not win the lottery.");
  66.                     config.jackpot += _ticketCost;
  67.                     PrintToChat("[Lottery] The current jackpot is now: " + config.jackpot + " Scrap.");
  68.                     SaveConfig();
  69.                 }
  70.             }
  71.             else
  72.             {
  73.                 SendReply(player, "[Lottery System] You do not have enough scrap to buy a lottery ticket.");
  74.             }
  75.         }
  76.  
  77.         private bool HasEnoughScrap(BasePlayer player, int amount)
  78.         {
  79.             int scrapAmount = player.inventory.GetAmount(_scrapGameItemID);
  80.             return scrapAmount >= amount;
  81.         }
  82.  
  83.         private void TakeScrap(BasePlayer player, int amount)
  84.         {
  85.             player.inventory.Take(null, _scrapGameItemID, amount);
  86.         }
  87.  
  88.         private void GiveScrap(BasePlayer player, int amount)
  89.         {
  90.             player.inventory.GiveItem(ItemManager.CreateByItemID(_scrapGameItemID, amount));
  91.         }
  92.  
  93.         #region Config
  94.  
  95.         private ConfigData config = new ConfigData();
  96.  
  97.         private class ConfigData
  98.         {
  99.             [JsonProperty(PropertyName = "Jackpot")]
  100.             public int jackpot = 0;
  101.         }
  102.  
  103.         protected override void LoadConfig()
  104.         {
  105.             base.LoadConfig();
  106.  
  107.             try
  108.             {
  109.                 config = Config.ReadObject<ConfigData>();
  110.  
  111.                 if (config == null) LoadDefaultConfig();
  112.             }
  113.             catch
  114.             {
  115.                 PrintError("Configuration file is corrupt, check your config file at https://jsonlint.com/!");
  116.                 LoadDefaultConfig();
  117.                 return;
  118.             }
  119.  
  120.             SaveConfig();
  121.         }
  122.  
  123.         protected override void LoadDefaultConfig() => config = new ConfigData();
  124.  
  125.         protected override void SaveConfig() => Config.WriteObject(config);
  126.  
  127.         #endregion
  128.  
  129.         // Add the Unload method to increment the jackpot on server shutdown
  130.         private void Unload()
  131.         {
  132.             LoadConfig();
  133.             config.jackpot += 50;
  134.             SaveConfig();
  135.             Puts("[Lottery System] Server is shutting down. Jackpot has been increased by 50 scrap.");
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement