C#:
using Mono.Security.Authenticode;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Libraries.Covalence;
using Oxide.Game.Rust.Cui;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Oxide.Core.Plugins;
namespace Oxide.Plugins {
[Info("FBonus", "Frizen", "1.0.0")]
public class FBonus : RustPlugin {
[PluginReference]
private Plugin ImageLibrary;
#region Data
private PluginData _data;
private void SaveData() {
Interface.Oxide.DataFileSystem.WriteObject(Name, _data);
}
private void LoadData() {
try {
_data = Interface.Oxide.DataFileSystem.ReadObject<PluginData>(Name);
}
catch (Exception e) {
PrintError(e.ToString());
}
if (_data == null) _data = new PluginData();
}
private class PluginData {
[JsonProperty(PropertyName = "BonusTime", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public Dictionary<ulong, DateTime> LastBonusTime = new Dictionary<ulong, DateTime>();
}
#endregion
#region Config
private static Configuration _config;
public class Configuration {
[JsonProperty(PropertyName = "Bonus List", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<Bonus> BonusList = new List<Bonus>();
[JsonProperty(PropertyName = "Cooldown (minutes)")]
public float CooldownMinutes;
public static Configuration DefaultConfig() {
return new Configuration {
BonusList = new List<Bonus>() {
new Bonus {
Name = "Привелегия фармила 7д", Image = "", Command = "addgroup {player} farmila 7d", Chance = 60
}
, new Bonus {
Name = "Калаш", Image = "", Command = "giveto {player} rifle.ak 1", Chance = 30
}
, new Bonus {
Name = "", Image = "", Command = "", Chance = 34
}
}
, CooldownMinutes = 86400f,
}
;
}
}
protected override void LoadConfig() {
base.LoadConfig();
try {
_config = Config.ReadObject<Configuration>();
if (_config == null) LoadDefaultConfig();
SaveConfig();
}
catch (Exception e) {
Debug.LogException(e);
PrintWarning("Creating new config file.");
LoadDefaultConfig();
}
}
protected override void LoadDefaultConfig() => _config = Configuration.DefaultConfig();
protected override void SaveConfig() => Config.WriteObject(_config);
#endregion
#region Bonus class
public class Bonus {
[JsonProperty("Название бонуса")]
public string Name {
get;
set;
}
[JsonProperty("Команда бонуса")]
public string Command {
get;
set;
}
[JsonProperty("Изображение бонуса")]
public string Image {
get;
set;
}
[JsonProperty("Шанс выпадения бонуса")]
public double Chance {
get;
set;
}
}
#endregion
#region Methods
private Bonus GetRandomBonus() {
List<Bonus> availableBonuses = _config.BonusList.ToList();
if (availableBonuses.Count == 0) {
PrintWarning("No bonuses available!");
return null;
}
double totalProbability = availableBonuses.Sum(b => b.Chance);
double randomValue = UnityEngine.Random.Range(0f, (float)totalProbability);
double cumulativeProbability = 0f;
List<Bonus> possibleBonuses = new List<Bonus>();
foreach (Bonus bonus in availableBonuses) {
cumulativeProbability += bonus.Chance;
if (randomValue <= cumulativeProbability) {
possibleBonuses.Add(bonus);
}
}
double highestChance = possibleBonuses.Max(b => b.Chance);
List<Bonus> sameChanceBonuses = possibleBonuses.Where(b => b.Chance == highestChance).ToList();
if (sameChanceBonuses.Count > 1) {
int randomIndex = UnityEngine.Random.Range(0, sameChanceBonuses.Count);
return sameChanceBonuses[randomIndex];
}
else {
return possibleBonuses[0];
}
}
[ConsoleCommand("TakeBonus")]
private void BonusHandler(ConsoleSystem.Arg arg) {
var player = arg.Player();
double remainingTime = _data.LastBonusTime.ContainsKey(player.userID) ? (_data.LastBonusTime[player.userID].AddMinutes(_config.CooldownMinutes) - DateTime.Now).TotalSeconds : 0;
if (remainingTime > 0) {
PrintToChat(player, $"Вы можете получить следующий бонус через {TimeSpan.FromSeconds(remainingTime).ToShortString()}.");
return;
}
Bonus bonus = GetRandomBonus();
if (bonus != null) {
if (!string.IsNullOrEmpty(bonus.Command)) {
rust.RunServerCommand(bonus.Command.Replace("{player}", player.UserIDString));
}
_data.LastBonusTime[player.userID] = DateTime.Now;
SaveData();
PrintToChat(player, $"Поздравляем вас с успешным получением {bonus.Name}!");
RewardUi(player, bonus);
}
}
#endregion
#region UI
public void BonusUi(BasePlayer player) {
CuiElementContainer container = new CuiElementContainer();
CuiHelper.DestroyUi(player, "MainBonus");
container.Add(new CuiPanel {
CursorEnabled = true, RectTransform = {
AnchorMin = "0 0", AnchorMax = "1 1", OffsetMax = "0 0"
}
, Image = {
Color = $"{HexToRustFormat("#322D2499")}", Material = "assets/content/ui/uibackgroundblur-ingamemenu.mat"
}
}
, "Overlay", "MainBonus");
UI.AddButton(ref container, "MainBonus", "CloseOverlay", "", "MainBonus", "0 0 0 0", "", "", "0 0", "1 1", "0 0", "0 0");
UI.AddImage(ref container, "MainBonus", "BonusParent", $"{HexToRustFormat("#5D5D5899")}", "", "", "0.5 0.5", "0.5 0.5", "-200 -125", "203 125");
UI.AddImage(ref container, "BonusParent", "ClosePanel", $"{HexToRustFormat("#5D5D58FF")}", "", "", "1 0", "1 0", "-106 5", "-5 35");
UI.AddText(ref container, "ClosePanel", "CloseText", "1 1 1 1", "ЗАКРЫТЬ", TextAnchor.MiddleCenter, 14, "0 0", "1 1", "0 0", "0 0");
UI.AddButton(ref container, "ClosePanel", "CloseBTN", "", "MainBonus", "0 0 0 0", "", "", "0 0", "1 1", "0 0", "0 0");
UI.AddImage(ref container, "BonusParent", "TakePanel", $"{HexToRustFormat("#8cc83cff")}", "", "", "1 0", "1 0", "-212 5", "-111 35");
UI.AddText(ref container, "TakePanel", "TakeText", "1 1 1 1", "ПОЛУЧИТЬ", TextAnchor.MiddleCenter, 14, "0 0", "1 1", "0 0", "0 0");
UI.AddButton(ref container, "TakePanel", "TakeBTN", "TakeBonus", "", "0 0 0 0", "", "", "0 0", "1 1", "0 0", "0 0");
UI.AddText(ref container, "BonusParent", "Title", $"{HexToRustFormat("#63FF0099")}", "Ежедневный бонус", TextAnchor.MiddleCenter, 21, "0.5 0.5", "0.5 0.5", "-100 85", "100 125");
UI.AddText(ref container, "BonusParent", "Info", $"{HexToRustFormat("#8cc83cff")}", "Каждый день ты можешь получить случайный приз,\nпросто используй команду /bonus и испытывай свою удачу", TextAnchor.MiddleCenter, 14, "0.5 0.5", "0.5 0.5", "-180 -30", "180 10", font : "robotocondensed-regular.ttf");
double offsetminx = -172, offsetmaxx = -105;
int i = 1;
foreach (var item in _config.BonusList) {
if (i == 6) break;
UI.AddImage(ref container, "BonusParent", "Bonus", $"{HexToRustFormat("#74706A99")}", "", "", "0.5 0.5", "0.5 0.5", $"{offsetminx} 10", $"{offsetmaxx} 82");
UI.AddRawImage(ref container, "Bonus", "BonusImage", ImageLibrary?.Call<string>("GetImage", item.Name), "1 1 1 1", "", "", "0 0", "1 1", "0 0", "0 0");
offsetmaxx += 70;
offsetminx += 70;
i++;
}
CuiHelper.AddUi(player, container);
}
public void RewardUi(BasePlayer player, Bonus bonus) {
CuiElementContainer container = new CuiElementContainer();
CuiHelper.DestroyUi(player, "Reward");
container.Add(new CuiPanel {
CursorEnabled = true, RectTransform = {
AnchorMin = "0 0", AnchorMax = "1 1", OffsetMax = "0 0"
}
, Image = {
Color = $"{HexToRustFormat("#322D2499")}", Material = "assets/content/ui/uibackgroundblur-ingamemenu.mat"
}
}
, "MainBonus", "Reward");
UI.AddButton(ref container, "Reward", "CloseOverlay", "", "Reward", "0 0 0 0", "", "", "0 0", "1 1", "0 0", "0 0");
UI.AddImage(ref container, "Reward", "RewardParent", $"{HexToRustFormat("#74706A6D")}", "", "", "0.5 0.5", "0.5 0.5", "-125 -125", "125 125");
UI.AddImage(ref container, "RewardParent", "Bonus", $"{HexToRustFormat("#74706A6D")}", "", "", "0.5 0.5", "0.5 0.5", $"-70 -40", $"70 100");
UI.AddRawImage(ref container, "Bonus", "BonusImage", ImageLibrary?.Call<string>("GetImage", bonus.Name), "1 1 1 1", "", "", "0 0", "1 1", "0 0", "0 0");
UI.AddText(ref container, "RewardParent", "BonusName", $"{HexToRustFormat("#63FF00FF")}", $"{bonus.Name}", TextAnchor.MiddleCenter, 18, "0.5 0.5", "0.5 0.5", "-125 -80", "125 -50");
CuiHelper.AddUi(player, container);
}
#endregion
#region Helpers
string HexToRustFormat(string hex) {
if (string.IsNullOrEmpty(hex)) hex = "#FFFFFFFF";
var str = hex.Trim('#');
if (str.Length == 6) str += "FF";
if (str.Length != 8) throw new Exception(hex);
var r = byte.Parse(str.Substring(0, 2), NumberStyles.HexNumber);
var g = byte.Parse(str.Substring(2, 2), NumberStyles.HexNumber);
var b = byte.Parse(str.Substring(4, 2), NumberStyles.HexNumber);
var a = byte.Parse(str.Substring(6, 2), NumberStyles.HexNumber);
Color color = new Color32(r, g, b, a);
return $"{color.r:F2} {color.g:F2} {color.b:F2} {color.a:F2}";
}
[ChatCommand("bonus")]
void cmdBonus(BasePlayer player, string command, string[] args) {
BonusUi(player);
}
#endregion
#region Hooks
void OnServerInitialized() {
LoadData();
foreach (var check in _config.BonusList) ImageLibrary.Call("AddImage", check.Image, check.Name);
}
void Unload() {
SaveData();
foreach (var player in BasePlayer.activePlayerList) {
CuiHelper.DestroyUi(player, "MainBonus");
}
}
#endregion
#region UI class
public static class UI {
public static void AddImage(ref CuiElementContainer container, string parrent, string name, string color, string sprite, string mat, string aMin, string aMax, string oMin, string oMax, string outline = "", string dist = "") {
if (string.IsNullOrEmpty(sprite) && !string.IsNullOrEmpty(mat)) container.Add(new CuiElement() {
Parent = parrent, Name = name, Components = {
new CuiImageComponent {
Color = color, Material = "assets/icons/greyout.mat"
}
, new CuiRectTransformComponent {
AnchorMin = aMin, AnchorMax = aMax, OffsetMin = oMin, OffsetMax = oMax
}
}
}
);
if (string.IsNullOrEmpty(sprite) && string.IsNullOrEmpty(mat)) container.Add(new CuiElement() {
Parent = parrent, Name = name, Components = {
new CuiImageComponent {
Color = color
}
, new CuiRectTransformComponent {
AnchorMin = aMin, AnchorMax = aMax, OffsetMin = oMin, OffsetMax = oMax
}
}
}
);
}
public static void AddRawImage(ref CuiElementContainer container, string parrent, string name, string png, string color, string sprite, string mat, string aMin, string aMax, string oMin, string oMax) {
if (string.IsNullOrEmpty(sprite) && string.IsNullOrEmpty(mat)) container.Add(new CuiElement() {
Parent = parrent, Name = name, Components = {
new CuiRawImageComponent {
Color = color, Png = png
}
, new CuiOutlineComponent {
Color = "0 0 0 0", Distance = "0 0"
}
, new CuiRectTransformComponent {
AnchorMin = aMin, AnchorMax = aMax, OffsetMin = oMin, OffsetMax = oMax
}
}
}
);
}
public static void AddText(ref CuiElementContainer container, string parrent, string name, string color, string text, TextAnchor align, int size, string aMin, string aMax, string oMin, string oMax, string outColor = "0 0 0 0", string font = "robotocondensed-bold.ttf", string dist = "0.5 0.5", float FadeIN = 0f, float FadeOut = 0f) {
container.Add(new CuiElement() {
Parent = parrent, Name = name, FadeOut = FadeOut, Components = {
new CuiTextComponent {
Color = color, Text = text, Align = align, FontSize = size, Font = font, FadeIn = FadeIN
}
, new CuiOutlineComponent {
Color = outColor, Distance = dist
}
, new CuiRectTransformComponent {
AnchorMin = aMin, AnchorMax = aMax, OffsetMin = oMin, OffsetMax = oMax
}
}
}
);
}
public static void AddButton(ref CuiElementContainer container, string parrent, string name, string cmd, string close, string color, string sprite, string mat, string aMin, string aMax, string oMin, string oMax, string outline = "", string dist = "") {
if (!string.IsNullOrEmpty(sprite) && !string.IsNullOrEmpty(mat)) container.Add(new CuiElement() {
Parent = parrent, Name = name, Components = {
new CuiButtonComponent {
Command = cmd, Color = color, Close = close, Sprite = sprite, Material = "assets/icons/greyout.mat",
}
, new CuiRectTransformComponent {
AnchorMin = aMin, AnchorMax = aMax, OffsetMin = oMin, OffsetMax = oMax
}
}
}
);
if (!string.IsNullOrEmpty(sprite) && !string.IsNullOrEmpty(mat) && !string.IsNullOrEmpty(outline)) container.Add(new CuiElement() {
Parent = parrent, Name = name, Components = {
new CuiButtonComponent {
Command = cmd, Color = color, Close = close, Sprite = sprite, Material = "assets/icons/greyout.mat",
}
, new CuiOutlineComponent {
Color = outline, Distance = dist
}
, new CuiRectTransformComponent {
AnchorMin = aMin, AnchorMax = aMax, OffsetMin = oMin, OffsetMax = oMax
}
}
}
);
if (string.IsNullOrEmpty(sprite) && !string.IsNullOrEmpty(mat)) container.Add(new CuiElement() {
Parent = parrent, Name = name, Components = {
new CuiButtonComponent {
Command = cmd, Color = color, Close = close, Material = "assets/icons/greyout.mat",
}
, new CuiRectTransformComponent {
AnchorMin = aMin, AnchorMax = aMax, OffsetMin = oMin, OffsetMax = oMax
}
}
}
);
if (!string.IsNullOrEmpty(sprite) && string.IsNullOrEmpty(mat)) container.Add(new CuiElement() {
Parent = parrent, Name = name, Components = {
new CuiButtonComponent {
Command = cmd, Color = color, Close = close, Sprite = sprite
}
, new CuiRectTransformComponent {
AnchorMin = aMin, AnchorMax = aMax, OffsetMin = oMin, OffsetMax = oMax
}
}
}
);
if (string.IsNullOrEmpty(sprite) && string.IsNullOrEmpty(mat)) container.Add(new CuiElement() {
Parent = parrent, Name = name, Components = {
new CuiButtonComponent {
Command = cmd, Color = color, Close = close,
}
, new CuiRectTransformComponent {
AnchorMin = aMin, AnchorMax = aMax, OffsetMin = oMin, OffsetMax = oMax
}
}
}
);
}
}
#endregion
}
}