Advertisement
Krythic

Lottery System

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