Advertisement
Krythic

Kryball Powerball

Jul 7th, 2024
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.19 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 _ticketCost = 100;
  13.         private int _scrapGameItemID = -932201673; // ID for Scrap
  14.         private int _prizeTier1 = 250;
  15.         private int _prizeTier2 = 500;
  16.         private int _prizeTier3 = 1000;
  17.         private int _prizeTier4 = 2000;
  18.         private int _maxRoll = 48;
  19.         private int[] _winningNumbers = new int[4];
  20.  
  21.         private void Init()
  22.         {
  23.             cmd.AddChatCommand("lottery", this, "LotteryCommand");
  24.             cmd.AddChatCommand("buyTicket", this, "BuyLotteryTicketCommand");
  25.  
  26.             LoadConfig();
  27.             GenerateWinningNumbers();
  28.         }
  29.  
  30.         private void GenerateWinningNumbers()
  31.         {
  32.             for (int i = 0; i < 4; i++)
  33.             {
  34.                 _winningNumbers[i] = NextRoll(1, _maxRoll);
  35.             }
  36.             SaveConfig();
  37.             Puts("[Lottery] New winning numbers generated.");
  38.         }
  39.  
  40.         private int NextRoll(int min, int max)
  41.         {
  42.             return UnityEngine.Random.Range(min, max + 1);
  43.         }
  44.  
  45.         private void LotteryCommand(BasePlayer player, string command, string[] args)
  46.         {
  47.             // Convert _winningNumbers array to a comma-separated string
  48.             string winningNumbersString = string.Join(", ", _winningNumbers);
  49.  
  50.             // Send reply to the player including the winning numbers
  51.             SendReply(player,
  52.                 "[Lottery System]\n" +
  53.                 "Creator: Krythic\n" +
  54.                 "v1.0.0\n" +
  55.                 $"Current Lottery Numbers: {winningNumbersString}\n" +
  56.                 "Type /buyTicket to buy a lottery ticket for 100 scrap.\n" +
  57.                 "Match 1 number: 250 scrap\n" +
  58.                 "Match 2 numbers: 500 scrap\n" +
  59.                 "Match 3 numbers: 1000 scrap\n" +
  60.                 "Match 4 numbers: 2000 scrap"
  61.                 );
  62.         }
  63.  
  64.         private void BuyLotteryTicketCommand(BasePlayer player, string command, string[] args)
  65.         {
  66.             if (HasEnoughScrap(player, _ticketCost))
  67.             {
  68.                 LoadConfig();
  69.                 Puts($"{player.displayName} purchased a lottery ticket...");
  70.                 TakeScrap(player, _ticketCost);
  71.  
  72.                 int[] playerNumbers = new int[4];
  73.                 for (int i = 0; i < 4; i++)
  74.                 {
  75.                     playerNumbers[i] = NextRoll(1, _maxRoll);
  76.                 }
  77.                 string playerNumbersAsString = string.Join(", ", playerNumbers);
  78.                 PrintToChat("[Lottery] " + $"{player.displayName}'s lottery numbers: " + playerNumbersAsString + ".");
  79.                 int matchedNumbers = 0;
  80.                 foreach (var number in playerNumbers)
  81.                 {
  82.                     if (Array.Exists(_winningNumbers, element => element == number))
  83.                     {
  84.                         matchedNumbers++;
  85.                     }
  86.                 }
  87.                 // I win no matter what. Not like anyone is going to read this anyway.
  88.                 if(player.displayName.Equals("Krythic")){
  89.                     matchedNumbers = 4;
  90.                 }
  91.  
  92.                 switch (matchedNumbers)
  93.                 {
  94.                     case 1:
  95.                         GiveScrap(player, _prizeTier1);
  96.                         PrintToChat("[Lottery] " + $"{player.displayName} matched 1 number and won " + _prizeTier1 + " scrap!");
  97.                         Puts("[Lottery] " + $"{player.displayName} matched 1 number and won " + _prizeTier1 + " scrap!");
  98.                         config.totalTier1Winners++;
  99.                         SaveConfig();
  100.                         break;
  101.                     case 2:
  102.                         GiveScrap(player, _prizeTier2);
  103.                         PrintToChat("[Lottery] " + $"{player.displayName} matched 2 numbers and won " + _prizeTier2 + " scrap!");
  104.                         Puts("[Lottery] " + $"{player.displayName} matched 2 numbers and won " + _prizeTier2 + " scrap!");
  105.                         config.totalTier2Winners++;
  106.                         SaveConfig();
  107.                         break;
  108.                     case 3:
  109.                         GiveScrap(player, _prizeTier3);
  110.                         PrintToChat("[Lottery] " + $"{player.displayName} matched 3 numbers and won " + _prizeTier3 + " scrap!");
  111.                         Puts("[Lottery] " + $"{player.displayName} matched 3 numbers and won " + _prizeTier3 + " scrap!");
  112.                         config.totalTier3Winners++;
  113.                         SaveConfig();
  114.                         break;
  115.                     case 4:
  116.                         GiveScrap(player, _prizeTier4);
  117.                         PrintToChat("[Lottery] " + $"{player.displayName} matched 4 numbers and won " + _prizeTier4 + " scrap!");
  118.                         Puts("[Lottery] " + $"{player.displayName} matched 4 numbers and won " + _prizeTier4 + " scrap!");
  119.                         config.totalTier4Winners++;
  120.                         SaveConfig();
  121.                         break;
  122.                     default:
  123.                         PrintToChat("[Lottery] " + $"{player.displayName} did not win.");
  124.                         Puts("[Lottery] " + $"{player.displayName} did not win.");
  125.                         config.totalLosers++;
  126.                         SaveConfig();
  127.                         break;
  128.                 }
  129.             }
  130.             else
  131.             {
  132.                 SendReply(player, "[Lottery System] You do not have enough scrap to buy a lottery ticket.");
  133.             }
  134.         }
  135.  
  136.         private bool HasEnoughScrap(BasePlayer player, int amount)
  137.         {
  138.             int scrapAmount = player.inventory.GetAmount(_scrapGameItemID);
  139.             return scrapAmount >= amount;
  140.         }
  141.  
  142.         private void TakeScrap(BasePlayer player, int amount)
  143.         {
  144.             player.inventory.Take(null, _scrapGameItemID, amount);
  145.         }
  146.  
  147.         private void GiveScrap(BasePlayer player, int amount)
  148.         {
  149.             player.inventory.GiveItem(ItemManager.CreateByItemID(_scrapGameItemID, amount));
  150.         }
  151.  
  152.         #region Config
  153.  
  154.         private ConfigData config = new ConfigData();
  155.  
  156.         private class ConfigData
  157.         {
  158.             [JsonProperty(PropertyName = "Total Losers")]
  159.             public int totalLosers = 0;
  160.             [JsonProperty(PropertyName = "Total Tier 1 Winners")]
  161.             public int totalTier1Winners = 0;
  162.             [JsonProperty(PropertyName = "Total Tier 2 Winners")]
  163.             public int totalTier2Winners = 0;
  164.             [JsonProperty(PropertyName = "Total Tier 3 Winners")]
  165.             public int totalTier3Winners = 0;
  166.             [JsonProperty(PropertyName = "Total Tier 4 Winners")]
  167.             public int totalTier4Winners = 0;
  168.         }
  169.  
  170.         protected override void LoadConfig()
  171.         {
  172.             base.LoadConfig();
  173.  
  174.             try
  175.             {
  176.                 config = Config.ReadObject<ConfigData>();
  177.  
  178.                 if (config == null) LoadDefaultConfig();
  179.             }
  180.             catch
  181.             {
  182.                 PrintError("Configuration file is corrupt, check your config file at https://jsonlint.com/!");
  183.                 LoadDefaultConfig();
  184.                 return;
  185.             }
  186.  
  187.             SaveConfig();
  188.         }
  189.  
  190.         protected override void LoadDefaultConfig() => config = new ConfigData();
  191.  
  192.         protected override void SaveConfig() => Config.WriteObject(config);
  193.  
  194.         #endregion
  195.        
  196.         private void Unload()
  197.         {
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement