Advertisement
Guest User

Untitled

a guest
Oct 16th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Oxide.Core.Libraries;
  4. using Oxide.Core.Libraries.Covalence;
  5.  
  6. namespace Oxide.Plugins
  7. {
  8. [Info("SkipNightVote", "k1lly0u", "0.1.1", ResourceId = 2058)]
  9. class SkipNightVote : CovalencePlugin
  10. {
  11. #region Fields
  12. private List<string> ReceivedVotes;
  13.  
  14. private bool VoteOpen;
  15. private int TimeRemaining;
  16. private int RequiredVotes;
  17. private string TimeRemMSG;
  18. private Timer VotingTimer;
  19. private Timer TimeCheck;
  20. #endregion
  21.  
  22. #region Oxide Hooks
  23. void Loaded()
  24. {
  25. permission.RegisterPermission("skipnightvote.admin", this);
  26. lang.RegisterMessages(Messages, this);
  27. }
  28. void OnServerInitialized() {
  29. LoadVariables();
  30. ReceivedVotes = new List<string>();
  31. RequiredVotes = 0;
  32. VoteOpen = false;
  33. TimeRemaining = 0;
  34. TimeRemMSG = GetMSG("timeRem").Replace("{secCol}", configData.Messaging.MSGColor).Replace("{mainCol}", configData.Messaging.MainColor);
  35. CheckTime();
  36. }
  37. void Unload()
  38. {
  39. if (VotingTimer != null)
  40. VotingTimer.Destroy();
  41. if (TimeCheck != null)
  42. TimeCheck.Destroy();
  43. }
  44. #endregion
  45.  
  46. #region Functions
  47. private void OpenVote()
  48. {
  49. var rVotes = server.Players * configData.Options.RequiredVotePercentage;
  50. if (rVotes < 1) rVotes = 1;
  51. RequiredVotes = Convert.ToInt32(rVotes);
  52. VoteOpen = true;
  53. var msg = GetMSG("voteMSG").Replace("{secCol}", configData.Messaging.MSGColor).Replace("{mainCol}", configData.Messaging.MainColor).Replace("{reqVote}", (configData.Options.RequiredVotePercentage * 100).ToString());
  54. server.Broadcast(msg);
  55. VoteTimer();
  56. }
  57. private void VoteTimer()
  58. {
  59. TimeRemaining = configData.VoteTimers.VoteOpenTimer * 60;
  60. VotingTimer = timer.Repeat(1, TimeRemaining, () =>
  61. {
  62. TimeRemaining--;
  63. if (TimeRemaining == 0)
  64. {
  65. TallyVotes();
  66. return;
  67. }
  68. if (TimeRemaining == 180)
  69. {
  70. server.Broadcast(TimeRemMSG.Replace("{time}", "3").Replace("{type}", GetMSG("Minutes")));
  71. }
  72. if (TimeRemaining == 120)
  73. {
  74. server.Broadcast(TimeRemMSG.Replace("{time}", "2").Replace("{type}", GetMSG("Minutes")));
  75. }
  76. if (TimeRemaining == 60)
  77. {
  78. server.Broadcast(TimeRemMSG.Replace("{time}", "1").Replace("{type}", GetMSG("Minute")));
  79. }
  80. if (TimeRemaining == 30)
  81. {
  82. server.Broadcast(TimeRemMSG.Replace("{time}", "30").Replace("{type}", GetMSG("Seconds")));
  83. }
  84. if (TimeRemaining == 10)
  85. {
  86. server.Broadcast(TimeRemMSG.Replace("{time}", "10").Replace("{type}", GetMSG("Seconds")));
  87. }
  88. });
  89. }
  90. private void CheckTime()
  91. {
  92. if (!VoteOpen)
  93. {
  94. if ((server.Time.TimeOfDay >= TimeSpan.Parse(configData.Options.TimeToOpen) && server.Time.TimeOfDay < TimeSpan.Parse("23:59:59")) || (cmd"settime 0.4" && server.Time.TimeOfDay))
  95. {
  96. OpenVote();
  97. }
  98. else
  99. {
  100. TimeCheck = timer.Once(20, () => CheckTime());
  101. }
  102. }
  103. else
  104. {
  105. if (server.Time.TimeOfDay >= TimeSpan.Parse(configData.Options.TimeToSet) && server.Time.TimeOfDay < TimeSpan.Parse(configData.Options.TimeToOpen))
  106. {
  107. VoteEnd(false);
  108. }
  109. }
  110. }
  111. private void TallyVotes()
  112. {
  113. if (ReceivedVotes.Count >= RequiredVotes)
  114. VoteEnd(true);
  115. else VoteEnd(false);
  116. }
  117. private void VoteEnd(bool success)
  118. {
  119. VoteOpen = false;
  120. RequiredVotes = 0;
  121. VotingTimer.Destroy();
  122. ReceivedVotes.Clear();
  123. TimeRemaining = 0;
  124.  
  125. if (success)
  126. {
  127. server.Time = server.Time.Date + TimeSpan.Parse(configData.Options.TimeToSet);
  128. server.Broadcast($"{configData.Messaging.MainColor}{GetMSG("Voting was successful, skipping night.")}</color>");
  129. }
  130. else
  131. {
  132. server.Broadcast($"{configData.Messaging.MainColor}{GetMSG("Voting was unsuccessful.")}</color>");
  133. }
  134. TimeCheck = timer.Once(configData.VoteTimers.TimeBetweenVotes * 60, () => CheckTime());
  135. }
  136. #endregion
  137.  
  138. #region Helpers
  139. private bool AlreadyVoted(string player) => ReceivedVotes.Contains(player);
  140. #endregion
  141.  
  142. #region ChatCommands
  143. [Command("voteday")]
  144. private void cmdVoteDay(IPlayer player, string command, string[] args)
  145. {
  146. if (VoteOpen)
  147. {
  148. if (!AlreadyVoted(player.Id))
  149. {
  150. ReceivedVotes.Add(player.Id);
  151. player.Reply(GetMSG("You have voted to skip night", player.Id));
  152. server.Broadcast($"{configData.Messaging.MainColor}{ReceivedVotes.Count} / {RequiredVotes}</color> {configData.Messaging.MSGColor}{GetMSG("have voted to skip night", player.Id)}</color>");
  153. if (ReceivedVotes.Count >= RequiredVotes)
  154. VoteEnd(true);
  155. return;
  156. }
  157. }
  158. else player.Reply($"{configData.Messaging.MainColor}{GetMSG("There is not a vote currently open", player.Id)}</color>");
  159. }
  160. [Command("nightvote"), Permission("skipnightvote.admin")]
  161. private void cmdAdminVote(IPlayer player, string command, string[] args)
  162. {
  163. if (args == null || args.Length == 0)
  164. {
  165. player.Reply($"{configData.Messaging.MainColor}/nightvote open</color> {configData.Messaging.MSGColor}- {GetMSG("Force open a new vote", player.Id)}</color>");
  166. player.Reply($"{configData.Messaging.MainColor}/nightvote close</color> {configData.Messaging.MSGColor}- {GetMSG("Cancel the current vote", player.Id)}</color>");
  167. return;
  168. }
  169. switch (args[0].ToLower())
  170. {
  171. case "open":
  172. {
  173. OpenVote();
  174. }
  175. return;
  176. case "close":
  177. {
  178. VoteEnd(false);
  179. }
  180. return;
  181. default:
  182. break;
  183. }
  184. }
  185. #endregion
  186.  
  187. #region Config
  188. private ConfigData configData;
  189. class Messaging
  190. {
  191. public string MainColor { get; set; }
  192. public string MSGColor { get; set; }
  193. }
  194. class Timers
  195. {
  196. public int VoteOpenTimer { get; set; }
  197. public int TimeBetweenVotes { get; set; }
  198. }
  199. class Options
  200. {
  201. public float RequiredVotePercentage { get; set; }
  202. public string TimeToOpen { get; set; }
  203. public string TimeToSet { get; set; }
  204. }
  205. class ConfigData
  206. {
  207.  
  208. public Messaging Messaging { get; set; }
  209. public Timers VoteTimers { get; set; }
  210. public Options Options { get; set; }
  211.  
  212. }
  213. private void LoadVariables()
  214. {
  215. LoadConfigVariables();
  216. SaveConfig();
  217. }
  218. protected override void LoadDefaultConfig()
  219. {
  220. var config = new ConfigData
  221. {
  222. Messaging = new Messaging
  223. {
  224. MainColor = "<color=orange>",
  225. MSGColor = "<color=#939393>"
  226. },
  227. Options = new Options
  228. {
  229. RequiredVotePercentage = 0.4f,
  230. TimeToOpen = "18:00:00",
  231. TimeToSet = "07:00:00"
  232. },
  233. VoteTimers = new Timers
  234. {
  235. VoteOpenTimer = 4,
  236. TimeBetweenVotes = 5
  237. }
  238. };
  239. SaveConfig(config);
  240. }
  241. private void LoadConfigVariables() => configData = Config.ReadObject<ConfigData>();
  242. void SaveConfig(ConfigData config) => Config.WriteObject(config, true);
  243. #endregion
  244.  
  245. #region Messaging
  246. private string GetMSG(string key, string userid = null) => lang.GetMessage(key, this, userid);
  247. Dictionary<string, string> Messages = new Dictionary<string, string>
  248. {
  249. {"Force open a new vote", "Force open a new vote" },
  250. {"Cancel the current vote", "Cancel the current vote" },
  251. {"There is not a vote currently open", "There is not a vote currently open" },
  252. {"You have voted to skip night", "You have voted to skip night" },
  253. {"have voted to skip night", "players have voted to skip night" },
  254. {"Voting was successful, skipping night.", "Voting was successful, skipping night." },
  255. {"Voting was unsuccessful.", "Voting was unsuccessful." },
  256. {"Minutes", "Minutes" },
  257. {"Minute", "Minute" },
  258. {"Seconds", "Seconds" },
  259. {"voteMSG", "{secCol}Type</color> {mainCol}/voteday</color> {secCol}now if you want to skip night. If </color>{mainCol}{reqVote}%</color> {secCol}of players vote night will be skipped</color>" },
  260. {"timeRem", "{secCol}Voting ends in</color> {mainCol}{time} {type}</color>{secCol}, use </color>{mainCol}/voteday</color>{secCol} to cast your vote</color>" }
  261. };
  262. #endregion
  263. }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement