Advertisement
DarkNosS96

Untitled

Oct 17th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Newtonsoft.Json;
  5. using Oxide.Core.Libraries.Covalence;
  6.  
  7. namespace Oxide.Plugins
  8. {
  9. [Info("Promocodes", "LaserHydra", "3.0.0", ResourceId = 1471)]
  10. [Description("Create promotion codes which run commands when redeemed by players")]
  11. public sealed class Promocodes : CovalencePlugin
  12. {
  13. private static Promocodes _instance;
  14. private List<PromocodeGroup> _promocodeGroups;
  15.  
  16. #region Initialization
  17.  
  18. private void Init()
  19. {
  20. _instance = this;
  21. _promocodeGroups.ForEach(p => p.FillCodes());
  22. }
  23.  
  24. protected override void LoadDefaultMessages()
  25. {
  26. lang.RegisterMessages(new Dictionary<string, string>
  27. {
  28. ["Invalid Code"] = "The code you entered seems to be either invalid or already redeemed.",
  29. ["Code Redeemed"] = "You redeemed a code with and received {amount} reward packages."
  30. }, this);
  31. }
  32.  
  33. #endregion
  34.  
  35. #region Config Handling
  36.  
  37. protected override void LoadConfig()
  38. {
  39. base.LoadConfig();
  40. _promocodeGroups = Config.ReadObject<List<PromocodeGroup>>();
  41. SaveConfig();
  42. }
  43.  
  44. protected override void SaveConfig() => Config.WriteObject(_promocodeGroups);
  45.  
  46. protected override void LoadDefaultConfig() => _promocodeGroups = new List<PromocodeGroup> { PromocodeGroup.GetDefaultGroup() };
  47.  
  48. #endregion
  49.  
  50. #region Commands
  51.  
  52. [Command("redeem")]
  53. private void RedeemCommand(IPlayer player, string cmd, string[] args)
  54. {
  55. if (args.Length != 1)
  56. {
  57. player.Reply("Syntax: redeem <code>");
  58. return;
  59. }
  60.  
  61. string code = args[0];
  62.  
  63. int i = 0;
  64. foreach (var promocodeGroup in _promocodeGroups)
  65. {
  66. if (promocodeGroup.IsValidCode(code))
  67. {
  68. promocodeGroup.RedeemCode(player, code);
  69. i++;
  70. }
  71. }
  72.  
  73. player.Reply(i == 0
  74. ? lang.GetMessage("Invalid Code", this, player.Id)
  75. : lang.GetMessage("Code Redeemed", this, player.Id).Replace("{amount}", i.ToString()));
  76. }
  77.  
  78. #endregion
  79.  
  80. #region Classes
  81.  
  82. private sealed class PromocodeGroup
  83. {
  84. [JsonProperty("Automatically Fill To (Amount)")]
  85. private int _fillAmount = 5;
  86.  
  87. [JsonProperty("Codes")]
  88. private List<string> _codes = new List<string>();
  89.  
  90. [JsonProperty("Commands")]
  91. private CommandCall[] _commands = new CommandCall[0];
  92.  
  93. public bool IsValidCode(string code) => _codes.Contains(code);
  94.  
  95. public void RedeemCode(IPlayer player, string code)
  96. {
  97. if (!IsValidCode(code))
  98. return;
  99.  
  100. foreach (var command in _commands)
  101. _instance.server.Command(command.Command, command.GetParameters(player));
  102.  
  103. _codes.Remove(code);
  104.  
  105. FillCodes();
  106.  
  107. _instance?.SaveConfig();
  108. }
  109.  
  110. public void FillCodes()
  111. {
  112. if (_codes == null)
  113. return;
  114.  
  115. while (_codes.Count < _fillAmount)
  116. _codes.Add(GenerateCode());
  117.  
  118. _instance?.SaveConfig();
  119. }
  120.  
  121. private static string GenerateCode() => Guid.NewGuid().ToString();
  122.  
  123. public static PromocodeGroup GetDefaultGroup() => new PromocodeGroup
  124. {
  125. _commands = new[]
  126. {
  127. new CommandCall("say", new[] {"{username} just redeemed a code!"})
  128. }
  129. };
  130. }
  131.  
  132. public sealed class CommandCall
  133. {
  134. [JsonProperty("Parameters")]
  135. private string[] _parameters;
  136.  
  137. [JsonProperty("Command")]
  138. public string Command { get; private set; }
  139.  
  140. public string[] GetParameters(IPlayer player)
  141. {
  142. return _parameters.Select(parameter =>
  143. parameter.Replace("{id}", player.Id).Replace("{username}", player.Name)
  144. ).ToArray();
  145. }
  146.  
  147. public CommandCall()
  148. {
  149. }
  150.  
  151. public CommandCall(string command, string[] parameters)
  152. {
  153. Command = command;
  154. _parameters = parameters;
  155. }
  156. }
  157.  
  158. #endregion
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement