using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using Oxide.Core; using Oxide.Core.Libraries.Covalence; using Oxide.Game.Rust.Cui; using Rust; namespace Oxide.Plugins { [Info("CLottery", "Crazy", 2.1)] [Description("Randomly select player every X hours, offer him a gift based on the number of player connected")] class CLottery : RustPlugin { #region CONFIG #region GENERAL private cConfig c; private class cConfig { [JsonProperty("1. COMMAND CHAT")] public string command; [JsonProperty("2. SCHEDULES FOR SEND")] public string[] schedules; [JsonProperty("3. LOTTERY")] public List lottery; public class LotteryList { [JsonProperty("Minimum players")] public int min; [JsonProperty("Maximum players")] public int max; [JsonProperty("Gifts")] public List gift; } public static cConfig GetDefaultConfig() { return new cConfig { command = "lottery", schedules = new string[] {"01:00","03:00","05:00","07:00","09:00","11:00","13:00","15:00","17:00","19:00","21:00","23:00"}, lottery = new List { new LotteryList { min = 0, max = 2, gift = null }, new LotteryList { min = 3, max = 5, gift = new List { new GiftList { name = "scrap", quantity = 50, } } }, new LotteryList { min = 6, max = 10, gift = new List { new GiftList { name = "xmas.present.small", quantity = 1, } } }, new LotteryList { min = 11, max = 20, gift = new List { new GiftList { name = "xmas.present.small", quantity = 1, }, new GiftList { name = "scrap", quantity = 50, } } }, new LotteryList { min = 21, max = 30, gift = new List { new GiftList { name = "xmas.present.medium", quantity = 1, } } }, new LotteryList { min = 31, max = 50, gift = new List { new GiftList { name = "xmas.present.medium", quantity = 1, }, new GiftList { name = "scrap", quantity = 75 } } }, new LotteryList { min = 51, max = 75, gift = new List { new GiftList { name = "xmas.present.large", quantity = 1, } } }, new LotteryList { min = 76, max = 200, gift = new List { new GiftList { name = "xmas.present.large", quantity = 1, }, new GiftList { name = "scrap", quantity = 75, } } }, } }; } } #endregion #region OBJECT public class GiftList { [JsonProperty("Short name")] public string name; [JsonProperty("Quantity")] public int quantity; } #endregion #region LOAD / SAVE protected override void LoadConfig() { base.LoadConfig(); c = Config.ReadObject(); } protected override void LoadDefaultConfig() => c = cConfig.GetDefaultConfig(); protected override void SaveConfig() => Config.WriteObject(c); #endregion #endregion #region DICTIONARY public Dictionary Lang = new Dictionary { {"Title", "LOTTERY"}, {"Message", "As every 2 hours, one player is randomly chosen\n\n" + "This one will win a gift, depending on the number of players connected !\n\n" + "The list of rewards is available on Discord or on the interface /info."}, {"NotEnought", "Not enough players for the lottery\n\n"+ "Next lottery in 2 hours."}, {"Timer1", "3"}, {"Timer2", "2"}, {"Timer3", "1"}, {"Winner", "Congratulations to {winner}"}, {"Reclam", "Claim your gift /{command}"}, {"NotGifts", "You don't have a gift to reclaim."}, {"RestoreGifts", "Your gifts have been added to your inventory."}, }; public HashSet lGifts = new HashSet(); #endregion #region DATA void LoadData() => lGifts = Interface.Oxide.DataFileSystem.ReadObject>("CLottery/Lottery"); void SaveData() => Interface.Oxide.DataFileSystem.WriteObject("CLottery/Lottery", lGifts); public class LGifts { public ulong id; public string name; public int quantity; public LGifts() { } } #endregion #region SERVER private void OnServerInitialized() => cmd.AddChatCommand(c.command, this, nameof(LoterieCommand)); private void OnNewSave(string filename) { LoadData(); lGifts.Clear(); SaveData(); } private void Unload() { foreach (var player in BasePlayer.activePlayerList) DestroyUI(player); } private void OnTick() => Schedules(); #endregion #region MESSAGE private void MessageReply(IPlayer player, string m) => player.Reply($"SERVER : {m}"); private string Translate(string msg, Dictionary parameters = null) { if (string.IsNullOrEmpty(msg)) return string.Empty; if (parameters != null) { foreach (var lekey in parameters) { if (msg.Contains("{" + lekey.Key + "}")) msg = msg.Replace("{" + lekey.Key + "}", lekey.Value); } } return msg; } #endregion #region SCHEDULES private bool SchedulesSend = false; public void Schedules() { var dateNow = DateTime.Now.ToString("HH:mm:ss"); foreach (string time in c.schedules) { if (dateNow == $"{time}:00" && !SchedulesSend) { SendGift(); SchedulesSend = true; timer.Once(5, () => { SchedulesSend = false; }); } } } #endregion #region EFFECT private void SendEffect(BasePlayer player, string sound, float vol) { var effect = new Effect(sound, player, 0, new UnityEngine.Vector3(0, vol, 0), UnityEngine.Vector3.forward); EffectNetwork.Send(effect, player.net.connection); } #endregion #region FUNCTIONS public bool CheckHasGifts(BasePlayer player) { if (Interface.Oxide.DataFileSystem.ExistsDatafile("CLottery/Lottery")) { LoadData(); if (lGifts.Count() != 0) { bool exist = lGifts.Any(x => x.id == player.userID); if (!exist) return false; return true; } return false; } return false; } public void RestoreGifts(BasePlayer player) { LoadData(); if (!CheckHasGifts(player)) return; foreach (LGifts g in lGifts.Where(x => x.id == player.userID)) { Server.Command($"inventory.giveto {g.id} {g.name} {g.quantity}"); } lGifts.RemoveWhere(x => x.id == player.userID); SaveData(); } private BasePlayer RandPlayers() { if (BasePlayer.activePlayerList.Count == 0) return null; List WithoutAdmin = (BasePlayer.activePlayerList.Where(x => !x.IsAdmin).Select(player => player)).ToList(); if (WithoutAdmin.Count == 0) return null; BasePlayer[] array = WithoutAdmin.ToArray(); System.Random rand = new System.Random(); BasePlayer playerWin = array[rand.Next(array.Length)]; return playerWin; } public void SendGift() { BasePlayer playerWin = RandPlayers(); if (playerWin == null) { foreach (var player in BasePlayer.activePlayerList) { CreateUITitle(player); CreateUIMessage(player); SendEffect(player, "assets/prefabs/tools/pager/effects/vibrate.prefab", 1); timer.Once(10f, () => { CuiHelper.DestroyUi(player, "CLottery.message"); CreateUINotEnought(player); }); timer.Once(20f, () => { DestroyUI(player); }); } return; } List WithoutAdmin = (BasePlayer.activePlayerList.Where(x => !x.IsAdmin).Select(player => player)).ToList(); int count = WithoutAdmin.Count; foreach (var x in c.lottery) { if (count >= x.min && count <= x.max) { if (x.min == 0 && x.gift == null) { foreach (var player in BasePlayer.activePlayerList) { CreateUITitle(player); CreateUIMessage(player); SendEffect(player, "assets/prefabs/tools/pager/effects/vibrate.prefab", 1); timer.Once(10f, () => { CuiHelper.DestroyUi(player, "CLottery.message"); CreateUINotEnought(player); }); timer.Once(20f, () => { DestroyUI(player); }); } return; } if (x.gift != null) { foreach (var player in BasePlayer.activePlayerList) { CreateUITitle(player); CreateUIMessage(player); SendEffect(player, "assets/prefabs/tools/pager/effects/vibrate.prefab", 1); timer.Once(10f, () => { CuiHelper.DestroyUi(player, "CLottery.message"); }); timer.Once(10.1f, () => { CreateUITimers1(player); SendEffect(player, "assets/prefabs/locks/keypad/effects/lock.code.unlock.prefab", 2); }); timer.Once(11.1f, () => { CuiHelper.DestroyUi(player, "CLottery.timer1"); CreateUITimers2(player); SendEffect(player, "assets/prefabs/locks/keypad/effects/lock.code.unlock.prefab", 2); }); timer.Once(12.1f, () => { CuiHelper.DestroyUi(player, "CLottery.timer2"); CreateUITimers3(player); SendEffect(player, "assets/prefabs/locks/keypad/effects/lock.code.unlock.prefab", 2); }); timer.Once(13.3f, () => { CuiHelper.DestroyUi(player, "CLottery.timer3"); CreateUIWinner(player, playerWin.displayName); CreateUIReclam(player); SendEffect(player, "assets/prefabs/misc/halloween/lootbag/effects/gold_open.prefab", 3); }); timer.Once(30f, () => { DestroyUI(player); }); } foreach (var g in x.gift) { LoadData(); lGifts.Add(new LGifts() { id = playerWin.userID, name = g.name, quantity = g.quantity }); SaveData(); } var dateTime = System.DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"); LogToFile($"logs", $"[{dateTime}] Gagnant:{playerWin.userID}", this); } } } } #endregion #region UI private void CreateUITitle(BasePlayer player) { var container = new CuiElementContainer(); var title = container.Add(new CuiPanel { Image = { Color = "0 0 0 0" }, RectTransform = { AnchorMin = "0.79 0.63", AnchorMax = "0.99 0.89" } }, "Hud", "CLottery.title"); container.Add(new CuiElement { Parent = title, Components = { new CuiTextComponent { Text = Lang["Title"], FontSize = 16, FadeIn = 3f, Color = "1 1 1 0.6", Font = "robotocondensed-bold.ttf", Align = UnityEngine.TextAnchor.UpperLeft }, new CuiRectTransformComponent {AnchorMin = "0 0", AnchorMax = "1 1"} } }); CuiHelper.AddUi(player, container); } private void CreateUIMessage(BasePlayer player) { var container = new CuiElementContainer(); var message = container.Add(new CuiPanel { Image = { Color = "0 0 0 0" }, RectTransform = { AnchorMin = "0.79 0.70", AnchorMax = "0.99 0.89" } }, "Hud", "CLottery.message"); container.Add(new CuiElement { Parent = message, Components = { new CuiTextComponent { Text = Lang["Message"], FontSize = 12, FadeIn = 3f, Font = "robotocondensed-regular.ttf", Align = UnityEngine.TextAnchor.UpperLeft }, new CuiRectTransformComponent {AnchorMin = "0 0", AnchorMax = "1 0.8"} } }); CuiHelper.AddUi(player, container); } private void CreateUITimers1(BasePlayer player) { var container = new CuiElementContainer(); var timers1 = container.Add(new CuiPanel { Image = { Color = "0 0 0 0" }, RectTransform = { AnchorMin = "0.79 0.75", AnchorMax = "0.99 0.89" } }, "Hud", "CLottery.timer1"); container.Add(new CuiElement { Parent = timers1, Components = { new CuiTextComponent { Text = Lang["Timer1"].ToUpper(), FontSize = 50, FadeIn = 1f, Font = "robotocondensed-bold.ttf", Align = UnityEngine.TextAnchor.MiddleCenter }, new CuiRectTransformComponent {AnchorMin = "0 0", AnchorMax = "1 0.8"} } }); CuiHelper.AddUi(player, container); } private void CreateUITimers2(BasePlayer player) { var container = new CuiElementContainer(); var timers2 = container.Add(new CuiPanel { Image = { Color = "0 0 0 0" }, RectTransform = { AnchorMin = "0.79 0.75", AnchorMax = "0.99 0.89" } }, "Hud", "CLottery.timer2"); container.Add(new CuiElement { Parent = timers2, Components = { new CuiTextComponent { Text = Lang["Timer2"].ToUpper(), FontSize = 50, FadeIn = 1f, Font = "robotocondensed-bold.ttf", Align = UnityEngine.TextAnchor.MiddleCenter }, new CuiRectTransformComponent {AnchorMin = "0 0", AnchorMax = "1 0.8"} } }); CuiHelper.AddUi(player, container); } private void CreateUITimers3(BasePlayer player) { var container = new CuiElementContainer(); var timers3 = container.Add(new CuiPanel { Image = { Color = "0 0 0 0" }, RectTransform = { AnchorMin = "0.79 0.75", AnchorMax = "0.99 0.89" } }, "Hud", "CLottery.timer3"); container.Add(new CuiElement { Parent = timers3, Components = { new CuiTextComponent { Text = Lang["Timer3"].ToUpper(), FontSize = 50, FadeIn = 1f, Font = "robotocondensed-bold.ttf", Align = UnityEngine.TextAnchor.MiddleCenter }, new CuiRectTransformComponent {AnchorMin = "0 0", AnchorMax = "1 0.8"} } }); CuiHelper.AddUi(player, container); } private void CreateUIWinner(BasePlayer player, string playerWin) { var container = new CuiElementContainer(); var winner = container.Add(new CuiPanel { Image = { Color = "0 0 0 0" }, RectTransform = { AnchorMin = "0.79 0.7975", AnchorMax = "0.99 0.89" } }, "Hud", "CLottery.winner"); container.Add(new CuiElement { Parent = winner, Components = { new CuiTextComponent { Text = Translate(Lang["Winner"], new Dictionary{{ "winner", playerWin },}), FontSize = 16, FadeIn = 1f, Font = "robotocondensed-regular.ttf", Align = UnityEngine.TextAnchor.MiddleCenter }, new CuiRectTransformComponent {AnchorMin = "0 0", AnchorMax = "1 0.8"} } }); CuiHelper.AddUi(player, container); } private void CreateUIReclam(BasePlayer player) { var container = new CuiElementContainer(); var reclam = container.Add(new CuiPanel { Image = { Color = "0 0 0 0" }, RectTransform = { AnchorMin = "0.79 0.87", AnchorMax = "0.99 0.89" } }, "Hud", "CLottery.reclam"); container.Add(new CuiElement { Parent = reclam, Components = { new CuiTextComponent { Text = "| i |", FontSize = 14, FadeIn = 1f, Font = "robotocondensed-regular.ttf", Align = UnityEngine.TextAnchor.MiddleRight }, new CuiRectTransformComponent {AnchorMin = "0 0", AnchorMax = "0.557 1"} } }); container.Add(new CuiElement { Parent = reclam, Components = { new CuiTextComponent { Text = Translate(Lang["Reclam"], new Dictionary{{ "command", c.command },}), FontSize = 13, FadeIn = 1f, Font = "robotocondensed-regular.ttf", Align = UnityEngine.TextAnchor.MiddleRight }, new CuiRectTransformComponent {AnchorMin = "0.15 0", AnchorMax = "1 1"} } }); CuiHelper.AddUi(player, container); } private void CreateUINotEnought(BasePlayer player) { var container = new CuiElementContainer(); var notEnought = container.Add(new CuiPanel { Image = { Color = "0 0 0 0" }, RectTransform = { AnchorMin = "0.79 0.76", AnchorMax = "0.99 0.89" } }, "Hud", "CLottery.notenought"); container.Add(new CuiElement { Parent = notEnought, Components = { new CuiTextComponent { Text = Lang["NotEnought"], FontSize = 12, FadeIn = 3f, Font = "robotocondensed-regular.ttf", Align = UnityEngine.TextAnchor.MiddleLeft }, new CuiRectTransformComponent {AnchorMin = "0 0", AnchorMax = "1 1"} } }); CuiHelper.AddUi(player, container); } private void DestroyUI(BasePlayer player) { CuiHelper.DestroyUi(player, "CLottery.title"); CuiHelper.DestroyUi(player, "CLottery.message"); CuiHelper.DestroyUi(player, "CLottery.timer1"); CuiHelper.DestroyUi(player, "CLottery.timer2"); CuiHelper.DestroyUi(player, "CLottery.timer3"); CuiHelper.DestroyUi(player, "CLottery.winner"); CuiHelper.DestroyUi(player, "CLottery.reclam"); CuiHelper.DestroyUi(player, "CLottery.notenought"); } #endregion #region COMMAND private void LoterieCommand(BasePlayer player, string command, string[] args) { if (args.Count() != 0) return; if (!CheckHasGifts(player)) { MessageReply(player.IPlayer, Translate(Lang["NotGifts"])); SendEffect(player, "assets/prefabs/weapons/toolgun/effects/repairerror.prefab", 1); return; } MessageReply(player.IPlayer, Translate(Lang["RestoreGifts"])); SendEffect(player, "assets/prefabs/deployable/dropbox/effects/submit_items.prefab", 1); RestoreGifts(player); } #endregion /*#region TEST UI private void Loaded() { foreach (var player in BasePlayer.activePlayerList) { CreateUITitle(player); CreateUIMessage(player); timer.Once(5f, () => { CreateUITimers1(player); CuiHelper.DestroyUi(player, "CLottery.message"); }); timer.Once(7f, () => { CreateUITimers2(player); CuiHelper.DestroyUi(player, "CLottery.timer1"); }); timer.Once(9f, () => { CreateUITimers3(player); CuiHelper.DestroyUi(player, "CLottery.timer2"); }); timer.Once(11f, () => { CreateUIWinner(player, "TF Crazy"); CreateUIReclam(player); CuiHelper.DestroyUi(player, "CLottery.timer3"); }); timer.Once(13f, () => { CreateUINotEnought(player); CuiHelper.DestroyUi(player, "CLottery.winner"); CuiHelper.DestroyUi(player, "CLottery.reclam"); }); } } #endregion*/ } }