C#:
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using System.Text;
using ConVar;
using System.Collections.Generic;
using Oxide.Core.Libraries;
using Oxide.Core.Plugins;
using Oxide.Core.Database;
using Oxide.Core;
using System.Collections;
using UnityEngine;
using System.Linq;
using Object = System.Object;
using System;
using Oxide.Game.Rust.Cui;
using UnityEngine.Networking;
using Newtonsoft.Json;
namespace Oxide.Plugins {
[Info("TPPermissions", "pluginfuel.ru", "20.0.2")]
[Description("Extended privilege granting system")]
public class TPPermissions : RustPlugin {
[ConsoleCommand("user.groups")]
private void GetUserGroups_ConsoleCommand(ConsoleSystem.Arg arg) {
if (arg == null) return;
if (arg.Player() != null) if (!arg.Player().IsAdmin) return;
if (arg.Args == null || arg.Args.Length < 1) {
arg.ReplyWith(LanguageEn ? "Syntax error, use: user.groups Steam64ID" : "Ошибка синтаксиса, используйте : user.groups Steam64ID");
return;
}
UInt64 userID;
if (!UInt64.TryParse(arg.Args[0], out userID)) {
arg.ReplyWith(LanguageEn ? "You have specified an incorrect Steam64ID player!" : "Вы указали неккоректный Steam64ID игрока!");
return;
}
Dictionary<String, DateTime> GetParametres = InformationPrivilagesUser.GetParametresUser(userID, TypeData.Group);
String PermsList = GetParametres == null ? String.Empty : GetParametres.Aggregate(String.Empty, (current, keyValuePair) => current + $"- {keyValuePair.Key} | Expired : {keyValuePair.Value}\n");
String Information = LanguageEn ? $"Active Player Privileges {userID} ({GetParametres.Count} pieces(/s))\n{PermsList}" : $"Активные привилегии игрока {userID} ({GetParametres.Count} штук(/и))\n{PermsList}";
arg.ReplyWith(Information);
}
[PluginReference]
Plugin TPMenuSystem;
public class Fields {
public String name {
get;
set;
}
public String value {
get;
set;
}
public bool inline {
get;
set;
}
public Fields(String name, String value, bool inline) {
this.name = name;
this.value = value;
this.inline = inline;
}
}
private void DrawUI_PoopUp(BasePlayer player, TypeAlert typeAlert, String Key, TypeData typeData, String DataExpired) {
if (!IsFullLoaded) {
PrintWarning(LanguageEn ? "The plugin is still being initialized, wait!" : "Плагин еще инициализируется, ожидайте!");
return;
}
if (player == null) return;
String Interface = InterfaceBuilder.GetInterface("UI_POOPUP_TEMPLATE");
if (Interface == null) return;
String ReplaceKey = GetLanguageNamePrivilage(player, Key, typeData);
if (ReplaceKey == null) return;
Configuration.InterfaceConfiguration Interfaces = config.InterfaceSetting;
Interface = Interface.Replace("%PNGBackground%", _imageUI.GetImage("UI_BACKGROUND_PANEL"));
Interface = Interface.Replace("%ICON%", GetIcon(typeAlert));
Interface = Interface.Replace("%TITLE%", GetLang(GetTitle(typeAlert), player.UserIDString));
Interface = Interface.Replace("%DESCRIPTION%", GetDescription(player.UserIDString, typeAlert, ReplaceKey, DataExpired));
Interface = Interface.Replace("%COLOR_TITLE%", Interfaces.RGBAColorTitle);
Interface = Interface.Replace("%COLOR_DESCRIPTION%", Interfaces.RGBAColorDescription);
CuiHelper.DestroyUi(player, InterfaceBuilder.UI_POOPUP_PANEL);
CuiHelper.AddUi(player, Interface);
player.Invoke(() => CuiHelper.DestroyUi(player, InterfaceBuilder.UI_POOPUP_PANEL), config.AlertConfigurations.TimeAlert);
}
private String GetImage(String fileName, UInt64 skin = 0) {
String imageId = (String)plugins.Find("ImageLibrary").CallHook("ImageUi.GetImage", fileName, skin);
return !String.IsNullOrEmpty(imageId) ? imageId : String.Empty;
}
private void OnServerInitialized() {
Subscribe("OnUserGroupRemoved");
Subscribe("OnUserPermissionRevoked");
GrablingPermissions();
GrablingGroups();
PreLoadedPlugin();
foreach (var check in config.GroupList) plugins.Find("ImageLibrary").Call("AddImage", check.Value.Image, check.Value.Image);
foreach (var check in config.PermissionList) plugins.Find("ImageLibrary").Call("AddImage", check.Value.Image, check.Value.Image);
Interface.Call("OnIQPermissionInitialized");
}
public class GrantData {
public Dictionary<String, Dictionary<String, Int64>> Group = new Dictionary<String, Dictionary<String, Int64>>();
public Dictionary<String, Dictionary<String, Int64>> Permission = new Dictionary<String, Dictionary<String, Int64>>();
}
private void NewPermissionRegistered(String Permission) {
if (config.PermissionList.ContainsKey(Permission)) return;
String ExampleName = Permission.Substring(Permission.IndexOf('.') + 1).ToUpper();
config.PermissionList.Add(Permission, new Configuration.StructureLanguage(ExampleName, ExampleName));
SaveConfig();
}
private void WriteData() {
Oxide.Core.Interface.Oxide.DataFileSystem.WriteObject("IQSystem/TPPermissions/OldPlayers", OldPlayers);
if (sqlConnection != null) return;
Oxide.Core.Interface.Oxide.DataFileSystem.WriteObject("IQSystem/TPPermissions/Permissions", InformationPrivilagesUser);
}
private void ReadData() {
OldPlayers = Oxide.Core.Interface.Oxide.DataFileSystem.ReadObject<List<UInt64>>("IQSystem/TPPermissions/OldPlayers");
if (sqlConnection != null) return;
InformationPrivilagesUser = Oxide.Core.Interface.Oxide.DataFileSystem.ReadObject<StructureData>("IQSystem/TPPermissions/Permissions");
}
private void GrablingPermissions() {
String[] PermissionList = permission.GetPermissions();
Int32 CountPermissionGrabs = 0;
foreach (String perm in PermissionList.Where(p => !config.PermissionList.ContainsKey(p))) {
String ExampleName = perm.Substring(perm.IndexOf('.') + 1).ToUpper();
config.PermissionList.Add(perm, new Configuration.StructureLanguage(ExampleName, ExampleName));
CountPermissionGrabs++;
}
if (CountPermissionGrabs == 0) return;
}
private void NewGroupRegistered(String Group) {
if (config.GroupList.ContainsKey(Group)) return;
String ExampleName = Group.ToUpper();
config.GroupList.Add(Group, new Configuration.StructureLanguage(ExampleName, ExampleName));
SaveConfig();
}
public class Footer {
public String text {
get;
set;
}
public String icon_url {
get;
set;
}
public String proxy_icon_url {
get;
set;
}
public Footer(String text, String icon_url, String proxy_icon_url) {
this.text = text;
this.icon_url = icon_url;
this.proxy_icon_url = proxy_icon_url;
}
}
private String GetTitle(TypeAlert typeAlert) {
String Lang = typeAlert == TypeAlert.Add ? "TITLE_ALERT_ADD" : typeAlert == TypeAlert.Expired ? "TITLE_ALERT_EXPIRED" : typeAlert == TypeAlert.Alert ? "TITLE_ALERT_INFO" : String.Empty;
return Lang;
}
private String GetLanguageNamePrivilage(BasePlayer player, String Key, TypeData type) {
String ReplaceKey = String.Empty;
switch (type) {
case TypeData.Permission : {
if (config.PermissionList.ContainsKey(Key)) {
ReplaceKey = lang.GetLanguage(player.UserIDString) == "ru" ? config.PermissionList[Key].RussianName : config.PermissionList[Key].EnglishName;
return ReplaceKey;
}
break;
}
case TypeData.Group : if (config.GroupList.ContainsKey(Key)) {
ReplaceKey = lang.GetLanguage(player.UserIDString) == "ru" ? config.GroupList[Key].RussianName : config.GroupList[Key].EnglishName;
return ReplaceKey;
}
break;
}
return null;
}
private String GetImagePrivilage(BasePlayer player, String Key, TypeData type) {
String ReplaceKey = String.Empty;
switch (type) {
case TypeData.Permission : {
if (config.PermissionList.ContainsKey(Key)) {
return config.PermissionList[Key].Image;
}
break;
}
case TypeData.Group : if (config.GroupList.ContainsKey(Key)) {
return config.GroupList[Key].Image;
}
break;
}
return null;
}
[JsonProperty(LanguageEn ? "Information about privileges" : "Информация о привилегиях")]
private StructureData InformationPrivilagesUser = new StructureData();
private Boolean IsFullLoaded = false;
private Dictionary<String, DateTime> GetPermissions(UInt64 userID) => InformationPrivilagesUser.GetParametresUser(userID, TypeData.Permission);
private void RevokePermission(UInt64 userID, String Permission, DateTime DataExpired = default(DateTime)) => InformationPrivilagesUser.RemoveParametres(Permission, userID, TypeData.Permission, DataExpired);
private static Int64 ToEpochTime(DateTime dateTime) {
DateTime date = dateTime.ToLocalTime();
Int64 ticks = date.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0).Ticks;
Int64 ts = ticks / TimeSpan.TicksPerSecond;
return ts;
}
private void RevokeGroup(UInt64 userID, String Group, String DataExpired = null) {
if (DataExpired != null) {
TimeSpan duration;
if (!TryParseTimeSpan(DataExpired, out duration)) {
PrintError(LanguageEn ? "API : You specified an incorrect time, use 1d/1h/1m/1s!" : "API : Вы указали неккоректное время, используйте 1d/1h/1m/1s!");
return;
}
DateTime ThisTime = InformationPrivilagesUser.GetExpiredPermissionData(Group, userID, TypeData.Group) - duration;
InformationPrivilagesUser.RemoveParametres(Group, userID, TypeData.Group, ThisTime);
return;
}
InformationPrivilagesUser.RemoveParametres(Group, userID, TypeData.Group);
}
[ConsoleCommand("migration.timeprivilage")]
private void TimePrivilageMigration(ConsoleSystem.Arg arg) {
if (arg.Player() != null) return;
coroutineMigration = ServerMgr.Instance.StartCoroutine(MigrationTimePrivilage());
}
private void Unload() {
_.InformationPrivilagesUser.RemoveParametresUnload();
WriteData();
if (coroutineMigration != null) {
ServerMgr.Instance.StopCoroutine(coroutineMigration);
coroutineMigration = null;
}
if (RoutineQueue != null) {
foreach (KeyValuePair<UInt64, Coroutine> keyValuePair in RoutineQueue.Where(r => r.Value != null)) ServerMgr.Instance.StopCoroutine(keyValuePair.Value);
RoutineQueue.Clear();
}
InterfaceBuilder.DestroyAll();
if (_imageUI != null) {
_imageUI.UnloadImages();
_imageUI = null;
}
sqlLibrary.CloseDb(sqlConnection);
_.IsFullLoaded = false;
_ = null;
}
private class ExpiringAccessValue {
[JsonProperty]
public string Value {
get;
private set;
}
[JsonProperty]
public DateTime ExpireDate {
get;
set;
}
[JsonIgnore]
public bool IsExpired => DateTime.Compare(DateTime.UtcNow, ExpireDate) > 0;
}
private void AlertPlayer(UInt64 userID, TypeAlert typeAlert, String Key, TypeData typeData, String DataExpired) {
BasePlayer player = BasePlayer.FindByID(userID);
if (player == null) return;
if (UAlertSystem) {
String ReplaceKey = GetLanguageNamePrivilage(player, Key, typeData);
if (ReplaceKey == null) return;
UAlertSystem.CallHook("API_SEND_ALERT", GetLang(GetTitle(typeAlert), userID.ToString()), GetDescription(userID.ToString(), typeAlert, ReplaceKey, DataExpired), "0.84 0.26 0.26 1", "1 1 1 1", 5f);
return;
}
if (config.AlertConfigurations.UsePoopUp) DrawUI_PoopUp(player, typeAlert, Key, typeData, DataExpired);
else {
String ReplaceKey = GetLanguageNamePrivilage(player, Key, typeData);
if (ReplaceKey == null) return;
String Message = $"{GetLang(GetTitle(typeAlert), userID.ToString())}\n{GetDescription(userID.ToString(), typeAlert, ReplaceKey, DataExpired)}";
SendChat(Message, player);
}
}
public class FancyMessage {
public String content {
get;
set;
}
public Boolean tts {
get;
set;
}
public Embeds[] embeds {
get;
set;
}
public class Embeds {
public String title {
get;
set;
}
public Int32 color {
get;
set;
}
public List<Fields> fields {
get;
set;
}
public Footer footer {
get;
set;
}
public Authors author {
get;
set;
}
public Embeds(String title, Int32 color, List<Fields> fields, Authors author, Footer footer) {
this.title = title;
this.color = color;
this.fields = fields;
this.author = author;
this.footer = footer;
}
}
public FancyMessage(String content, bool tts, Embeds[] embeds) {
this.content = content;
this.tts = tts;
this.embeds = embeds;
}
public String toJSON() => JsonConvert.SerializeObject(this);
}
[ConsoleCommand("revoke.group")]
private void RevomeGroup_TwoConsoleCommand(ConsoleSystem.Arg arg) => RemoveGroup_ConsoleCommand(arg);
protected override void LoadConfig() {
base.LoadConfig();
try {
config = Config.ReadObject<Configuration>();
if (config == null) LoadDefaultConfig();
}
catch {
PrintWarning(LanguageEn ? $"Error #554548 reading the configuration 'oxide/config/{Name}', creating a new configuration!!" : $"Ошибка #554548 чтения конфигурации 'oxide/config/{Name}', создаём новую конфигурацию!!");
LoadDefaultConfig();
}
if (config.GeneralSettings.MySQLSetting.TableName == null || String.IsNullOrWhiteSpace(config.GeneralSettings.MySQLSetting.TableName)) config.GeneralSettings.MySQLSetting.TableName = "players";
NextTick(SaveConfig);
}
private static StringBuilder sb = new StringBuilder();
private void RevokeGroup(UInt64 userID, String Group, DateTime DataExpired = default(DateTime)) => InformationPrivilagesUser.RemoveParametres(Group, userID, TypeData.Group, DataExpired);
private void MigrationPrivilage(BasePlayer player) {
if (player == null || sqlConnection == null) return;
Configuration.GeneralSetting.MySQLInitialize.MigrationPrivilage MigrationInfo = config.GeneralSettings.MySQLSetting.MigrationSettings;
if (MigrationInfo.UseMigrationPermissionsList) foreach (KeyValuePair<String, Dictionary<UInt64, DateTime>> Permissions in InformationPrivilagesUser.UserPermissions.Where(x => MigrationInfo.MigrationPermissionList.Contains(x.Key))) foreach (KeyValuePair<UInt64, DateTime> PlayerInformation in Permissions.Value.Where(x => x.Key == player.userID)) if (!permission.UserHasPermission(PlayerInformation.Key.ToString(), Permissions.Key)) permission.GrantUserPermission(PlayerInformation.Key.ToString(), Permissions.Key, null);
if (MigrationInfo.UseMigrationGroupList) foreach (KeyValuePair<String, Dictionary<UInt64, DateTime>> Groups in InformationPrivilagesUser.UserGrpups.Where(x => MigrationInfo.MigrationGroupList.Contains(x.Key))) foreach (KeyValuePair<UInt64, DateTime> PlayerInformation in Groups.Value.Where(x => x.Key == player.userID)) if (!permission.UserHasGroup(PlayerInformation.Key.ToString(), Groups.Key)) permission.AddUserGroup(PlayerInformation.Key.ToString(), Groups.Key);
}
[ConsoleCommand("revoke.permission")]
private void RevokePerm_TwoConsoleCommand(ConsoleSystem.Arg arg) => RevokePerm_ConsoleCommand(arg);
[ConsoleCommand("revokeperm")]
private void RevokePerm_ConsoleCommand(ConsoleSystem.Arg arg) {
if (arg == null) return;
if (arg.Player() != null) if (!arg.Player().IsAdmin) return;
if (arg.Args == null || arg.Args.Length < 2) {
arg.ReplyWith(LanguageEn ? "Syntax error, use: revokeperm Steam64ID Permission\nExample : revokeperm 765611963065887930 iqchat.vip" : "Ошибка синтаксиса, используйте : revokeperm Steam64ID Permission\nПример : revokeperm 76561197235887930 iqchat.vip");
return;
}
UInt64 userID;
if (!UInt64.TryParse(arg.Args[0], out userID)) {
arg.ReplyWith(LanguageEn ? "You have specified an incorrect Steam64ID player!" : "Вы указали неккоректный Steam64ID игрока!");
return;
}
String Permission = arg.Args[1];
if (!config.PermissionList.ContainsKey(Permission)) {
arg.ReplyWith(LanguageEn ? "The permission you specified do not exist!" : "Права которые вы указали - не существуют!");
return;
}
if (!permission.UserHasPermission(userID.ToString(), Permission)) {
arg.ReplyWith(LanguageEn ? "The player does not have these rights!!" : "У игрока отсутствуют данные права!");
return;
}
if (arg.Args.Length == 3) {
TimeSpan duration;
if (!TryParseTimeSpan(arg.Args[2], out duration)) {
arg.ReplyWith(LanguageEn ? "You specified an incorrect time, use 1d/1h/1m/1s!" : "Вы указали неккоректное время, используйте 1d/1h/1m/1s!");
return;
}
DateTime ThisTime = InformationPrivilagesUser.GetExpiredPermissionData(Permission, userID, TypeData.Permission) - duration;
InformationPrivilagesUser.RemoveParametres(Permission, userID, TypeData.Permission, ThisTime);
return;
}
InformationPrivilagesUser.RemoveParametres(Permission, userID, TypeData.Permission);
}
public String FormatTime(TimeSpan time, String UserID) {
String Result = String.Empty;
String Days = GetLang("TITLE_FORMAT_DAYS", UserID);
String Hourse = GetLang("TITLE_FORMAT_HOURSE", UserID);
String Minutes = GetLang("TITLE_FORMAT_MINUTES", UserID);
String Seconds = GetLang("TITLE_FORMAT_SECONDS", UserID);
if (time.Days != 0) Result += $"{Format(time.Days, Days, Days, Days)} ";
if (time.Hours != 0) Result += $"{Format(time.Hours, Hourse, Hourse, Hourse)} ";
if (time.Minutes != 0) Result += $"{Format(time.Minutes, Minutes, Minutes, Minutes)} ";
if (time.Days == 0 && time.Hours == 0 && time.Minutes == 0 && time.Seconds != 0) Result = $"{Format(time.Seconds, Seconds, Seconds, Seconds)} ";
return Result;
}
private new void LoadDefaultMessages() {
lang.RegisterMessages(new Dictionary<String, String> {
["TITLE_ALERT_ADD"]
= "Privilege received!", ["TITLE_ALERT_INFO"] = "Privilege expires!", ["TITLE_ALERT_EXPIRED"] = "Privilege expired!", ["DESCRIPTION_ALERT_EXPIRED"] = "Privilege {0} expired", ["DESCRIPTION_ALERT_ADD"] = "You have successfully received {0}", ["DESCRIPTION_ALERT_INFO"] = "{0} ends : {1}", ["PINFO_ALERT"] = "YourPrivilages :\n{0}", ["PINFO_ALERT_NOT"] = "You don't have privileges", ["TITLE_FORMAT_DAYS"] = "D", ["TITLE_FORMAT_HOURSE"] = "H", ["TITLE_FORMAT_MINUTES"] = "M", ["TITLE_FORMAT_SECONDS"] = "S",
}
, this);
lang.RegisterMessages(new Dictionary<String, String> {
["TITLE_ALERT_ADD"]
= "Привилегия получена!", ["TITLE_ALERT_INFO"] = "Привилегия истекает!", ["TITLE_ALERT_EXPIRED"] = "Привилегия истекла!", ["DESCRIPTION_ALERT_EXPIRED"] = "Привилегия {0} истекла", ["DESCRIPTION_ALERT_ADD"] = "Вы успешно получили {0}", ["DESCRIPTION_ALERT_INFO"] = "{0} кончается : {1}", ["PINFO_ALERT"] = "Ваши привилегии :\n{0}", ["PINFO_ALERT_NOT"] = "У вас нет привилегий", ["TITLE_FORMAT_DAYS"] = "Д", ["TITLE_FORMAT_HOURSE"] = "Ч", ["TITLE_FORMAT_MINUTES"] = "М", ["TITLE_FORMAT_SECONDS"] = "С",
}
, this, "ru");
}
public class Authors {
public String name {
get;
set;
}
public String url {
get;
set;
}
public String icon_url {
get;
set;
}
public String proxy_icon_url {
get;
set;
}
public Authors(String name, String url, String icon_url, String proxy_icon_url) {
this.name = name;
this.url = url;
this.icon_url = icon_url;
this.proxy_icon_url = proxy_icon_url;
}
}
private const Boolean LanguageEn = false;
private void SetPermission(UInt64 userID, String Permission, DateTime DataExpired) => InformationPrivilagesUser.SetParametres(Permission, userID, DataExpired, TypeData.Permission);
private void SQL_OpenConnection() {
Configuration.GeneralSetting.MySQLInitialize.MySQLConnect SQLInfo = config.GeneralSettings.MySQLSetting.ConnectSetting;
if (String.IsNullOrWhiteSpace(SQLInfo.Host) || String.IsNullOrWhiteSpace(SQLInfo.Passowrd) || String.IsNullOrWhiteSpace(SQLInfo.Port) || String.IsNullOrWhiteSpace(SQLInfo.DatabaseName) || String.IsNullOrWhiteSpace(SQLInfo.UserName)) {
_.InformationPrivilagesUser.SetParametresUnload();
return;
}
sqlConnection = sqlLibrary.OpenDb(SQLInfo.Host, Convert.ToInt32(SQLInfo.Port), SQLInfo.DatabaseName, SQLInfo.UserName, SQLInfo.Passowrd, this);
if (sqlConnection == null) {
_.InformationPrivilagesUser.SetParametresUnload();
return;
}
Sql sql = Sql.Builder.Append(SQL_Query_CreatedDatabase());
sqlLibrary.Insert(sql, sqlConnection);
sql = Sql.Builder.Append(SQL_Query_SelectedDatabase());
sqlLibrary.Query(sql, sqlConnection, list => {
if (list.Count > 0) foreach (Dictionary<String, Object> entry in list) {
UInt64 SteamID = 0;
if (!UInt64.TryParse((String)entry["steamid"], out SteamID)) {
PrintError(LanguageEn ? "Error parsing SteamID player" : "Ошибка парсинга SteamID игрока");
return;
}
if (!String.IsNullOrWhiteSpace((String)entry["group"])) {
String Group = (String)entry["group"];
if (!_.InformationPrivilagesUser.UserGrpups.ContainsKey(Group)) _.InformationPrivilagesUser.UserGrpups.Add(Group, new Dictionary<UInt64, DateTime>());
if (!_.InformationPrivilagesUser.UserGrpups[Group].ContainsKey(SteamID)) _.InformationPrivilagesUser.UserGrpups[Group].Add(SteamID, DateTime.Parse((String)entry["data_expired"]));
else _.InformationPrivilagesUser.UserGrpups[Group][SteamID] = DateTime.Parse((String)entry["data_expired"]);
}
else if (!String.IsNullOrWhiteSpace((String)entry["permission"])) {
String Permission = (String)entry["permission"];
if (!_.InformationPrivilagesUser.UserPermissions.ContainsKey(Permission)) _.InformationPrivilagesUser.UserPermissions.Add(Permission, new Dictionary<UInt64, DateTime>());
if (!_.InformationPrivilagesUser.UserPermissions[Permission].ContainsKey(SteamID)) {
_.InformationPrivilagesUser.UserPermissions[Permission] .Add(SteamID, DateTime.Parse((String)entry["data_expired"]));
}
else {
_.InformationPrivilagesUser.UserPermissions[Permission][SteamID] = DateTime.Parse((String)entry["data_expired"]);
}
}
}
_.InformationPrivilagesUser.SetParametresUnload();
PrintWarning(LanguageEn ? "Updated information about users from the database" : "Обновлена информация о пользователях из БД");
}
);
}
private void OnPlayerConnected(BasePlayer player) {
MigrationPrivilage(player);
SetupAutoPrivilages(player);
if (!config.AlertConfigurations.UseAlertedGroup && !config.AlertConfigurations.UseAlertedPermission) return;
player.Invoke(() => {
if (!RoutineQueue.ContainsKey(player.userID)) RoutineQueue.Add(player.userID, ServerMgr.Instance.StartCoroutine(JoinedAlertPlayer(player)));
else RoutineQueue[player.userID] = ServerMgr.Instance.StartCoroutine(JoinedAlertPlayer(player));
}
, 10f);
}
class StructureTimePrivilage {
public Dictionary<String, String> groups = new Dictionary<String, String>();
public Dictionary<String, String> permissions = new Dictionary<String, String>();
}
[ConsoleCommand("grant.group")]
private void AddGroup_TwoConsoleCommand(ConsoleSystem.Arg arg) => AddGroup_ConsoleCommand(arg);
[ConsoleCommand("user.perms")]
private void GetUserPerms_ConsoleCommand(ConsoleSystem.Arg arg) {
if (arg == null) return;
if (arg.Player() != null) if (!arg.Player().IsAdmin) return;
if (arg.Args == null || arg.Args.Length < 1) {
arg.ReplyWith(LanguageEn ? "Syntax error, use: user.perms Steam64ID" : "Ошибка синтаксиса, используйте : user.perms Steam64ID");
return;
}
UInt64 userID;
if (!UInt64.TryParse(arg.Args[0], out userID)) {
arg.ReplyWith(LanguageEn ? "You have specified an incorrect Steam64ID player!" : "Вы указали неккоректный Steam64ID игрока!");
return;
}
Dictionary<String, DateTime> GetParametres = InformationPrivilagesUser.GetParametresUser(userID, TypeData.Permission);
String PermsList = GetParametres == null ? String.Empty : GetParametres.Aggregate(String.Empty, (current, keyValuePair) => current + $"- {keyValuePair.Key} | Expired : {keyValuePair.Value}\n");
String Information = LanguageEn ? $"Active Player Privileges {userID} ({GetParametres.Count} pieces(/s))\n{PermsList}" : $"Активные привилегии игрока {userID} ({GetParametres.Count} штук(/и))\n{PermsList}";
arg.ReplyWith(Information);
}
private String Format(Int32 units, String form1, String form2, String form3) {
var tmp = units % 10;
if (units >= 5 && units <= 20 || tmp >= 5 && tmp <= 9) return $"{units}{form1}";
if (tmp >= 2 && tmp <= 4) return $"{units}{form2}";
return $"{units}{form3}";
}
private static String GetIcon(TypeAlert typeAlert) {
Configuration.InterfaceConfiguration Interfaces = config.InterfaceSetting;
String Icon = typeAlert == TypeAlert.Add ? "UI_ICON_PRIVILAGE_ADD" : typeAlert == TypeAlert.Expired ? "UI_ICON_PRIVILAGE_EXPIRED" : typeAlert == TypeAlert.Alert ? "UI_ICON_PRIVILAGE_ALERT" : String.Empty;
return _imageUI.GetImage(Icon);
}
private void SetupAutoPrivilages(BasePlayer player) {
if (OldPlayers.Contains(player.userID)) return;
if (config.GeneralSettings.AutoGiveSettings.UseAutGivePermissions) {
foreach (KeyValuePair<String, String> Permission in config.GeneralSettings.AutoGiveSettings.PermissionList) {
TimeSpan duration;
if (!TryParseTimeSpan(Permission.Value, out duration)) {
_.LogToFile("LogPermission", $"=========> AutoGive : Not correct format time - {Permission.Key}", _);
return;
}
DateTime ThisTime = InformationPrivilagesUser.GetExpiredPermissionData(Permission.Key, player.userID, TypeData.Permission) + duration;
InformationPrivilagesUser.SetParametres(Permission.Key, player.userID, ThisTime, TypeData.Permission);
_.LogToFile("LogPermission", $"=========> AutoGive : Success give - {Permission.Key} : {player.userID}", _);
}
}
if (config.GeneralSettings.AutoGiveSettings.UseAutGiveGroups) {
foreach (KeyValuePair<String, String> Group in config.GeneralSettings.AutoGiveSettings.GroupList) {
TimeSpan duration;
if (!TryParseTimeSpan(Group.Value, out duration)) {
_.LogToFile("LogPermission", $"=========> AutoGive : Not correct format time - {Group.Key}", _);
return;
}
DateTime ThisTime = InformationPrivilagesUser.GetExpiredPermissionData(Group.Key, player.userID, TypeData.Group) + duration;
InformationPrivilagesUser.SetParametres(Group.Key, player.userID, ThisTime, TypeData.Group);
_.LogToFile("LogPermission", $"=========> AutoGive : Success give - {Group.Key} : {player.userID}", _);
}
}
OldPlayers.Add(player.userID);
}
private static InterfaceBuilder _interface;
private void InserDatabase(String UserID, TypeData typeData, String Key, DateTime dateExpired) {
if (sqlConnection == null) return;
String Permission = typeData == TypeData.Permission ? Key : String.Empty;
String Group = typeData == TypeData.Group ? Key : String.Empty;
String sqlQuery = typeData == TypeData.Permission ? $"UPDATE {config.GeneralSettings.MySQLSetting.TableName} SET `steamid` = @0, `permission` = @1,`group` = @2,`data_expired` = @3 WHERE `steamid` = @0 AND `permission` = @1" : $"UPDATE {config.GeneralSettings.MySQLSetting.TableName} SET `steamid` = @0, `permission` = @1,`group` = @2,`data_expired` = @3 WHERE `steamid` = @0 AND`group` = @2";
Sql updateCommand = Oxide.Core.Database.Sql.Builder.Append(sqlQuery, UserID, Permission, Group, dateExpired.ToString());
sqlLibrary.Update(updateCommand, sqlConnection, rowsAffected => {
if (rowsAffected <= 0) {
String Query = String.Format(SQL_Query_InsertUser(), UserID, Permission, Group, dateExpired.ToString());
Sql sql = Sql.Builder.Append(Query);
sqlLibrary.Insert(sql, sqlConnection, rowsAffecteds => {
}
);
}
}
);
}
private String SQL_Query_InsertUser() {
String InserUser = $"INSERT INTO `{config.GeneralSettings.MySQLSetting.TableName}`" + "(`steamid`, `permission`, `group`, `data_expired`) VALUES ('{0}','{1}','{2}','{3}')";
return InserUser;
}
private void RevokePermission(UInt64 userID, String Permission, String DataExpired = null) {
if (DataExpired != null) {
TimeSpan duration;
if (!TryParseTimeSpan(DataExpired, out duration)) {
PrintError(LanguageEn ? "API : You specified an incorrect time, use 1d/1h/1m/1s!" : "API : Вы указали неккоректное время, используйте 1d/1h/1m/1s!");
return;
}
DateTime ThisTime = InformationPrivilagesUser.GetExpiredPermissionData(Permission, userID, TypeData.Permission) - duration;
InformationPrivilagesUser.RemoveParametres(Permission, userID, TypeData.Permission, ThisTime);
return;
}
InformationPrivilagesUser.RemoveParametres(Permission, userID, TypeData.Permission);
}
public String GetLang(String LangKey, String userID = null, params Object[] args) {
sb.Clear();
if (args == null) return lang.GetMessage(LangKey, this, userID);
sb.AppendFormat(lang.GetMessage(LangKey, this, userID), args);
return sb.ToString();
}
private IEnumerator MigrationTimePrivilage() {
if (!Interface.Oxide.DataFileSystem.ExistsDatafile("Timeprivileges")) {
yield break;
}
Dictionary<String, StructureTimePrivilage> dataFile = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<String, StructureTimePrivilage>>("Timeprivileges");
foreach (KeyValuePair<String, StructureTimePrivilage> timePrivilage in dataFile) {
UInt64 userID;
if (!UInt64.TryParse(timePrivilage.Key, out userID)) continue;
foreach (KeyValuePair<String, String> Permissions in timePrivilage.Value.permissions) {
DateTime Time = Convert.ToDateTime(Permissions.Value);
InformationPrivilagesUser.SetParametres(Permissions.Key, userID, Time, TypeData.Permission);
yield return CoroutineEx.waitForSeconds(0.15f);
}
foreach (KeyValuePair<String, String> Groups in timePrivilage.Value.groups) {
DateTime Time = Convert.ToDateTime(Groups.Value);
InformationPrivilagesUser.SetParametres(Groups.Key, userID, Time, TypeData.Group);
yield return CoroutineEx.waitForSeconds(0.15f);
}
yield return CoroutineEx.waitForSeconds(0.25f);
}
Interface.Oxide.UnloadPlugin("TimePrivilage");
if (coroutineMigration != null) {
ServerMgr.Instance.StopCoroutine(coroutineMigration);
coroutineMigration = null;
}
}
[JsonProperty(LanguageEn ? "Players who are no longer considered beginners" : "Игроки, которые более не считают новичками")]
private List<UInt64> OldPlayers = new List<UInt64>();
private static ImageUI _imageUI;
private static Configuration config = new Configuration();
private void Request(String url, String payload, Action<Int32> callback = null) {
Dictionary<String, String> header = new Dictionary<String, String>();
header.Add("Content-Type", "application/json");
webrequest.Enqueue(url, payload, (code, response) => {
if (code != 200 && code != 204) {
if (response != null) {
try {
JObject json = JObject.Parse(response);
if (code == 429) {
Single seconds = Single.Parse(Math.Ceiling((Double)(Int32)json["retry_after"] / 1000).ToString());
}
else {
PrintWarning($" Discord rejected that payload! Responded with \"{json["message"].ToString()}\" Code: {code}");
}
}
catch {
PrintWarning($"Failed to get a valid response from discord! Error: \"{response}\" Code: {code}");
}
}
else {
PrintWarning($"Discord didn't respond (down?) Code: {code}");
}
}
try {
callback?.Invoke(code);
}
catch (Exception ex) {
}
}
, this, RequestMethod.POST, header);
}
private class InterfaceBuilder {
public static InterfaceBuilder Instance;
public const String UI_POOPUP_PANEL = "UI_POOPUP_PANEL";
public Dictionary<String, String> Interfaces;
public InterfaceBuilder() {
Instance = this;
Interfaces = new Dictionary<String, String>();
BuildingPlayer_PopUp();
}
public static void AddInterface(String name, String json) {
if (Instance.Interfaces.ContainsKey(name)) {
_.PrintError($"Error #3324635! Tried to add existing cui elements! -> {name}");
return;
}
Instance.Interfaces.Add(name, json);
}
public static String GetInterface(String name) {
String json = String.Empty;
if (Instance.Interfaces.TryGetValue(name, out json) == false) {
_.PrintWarning($"Warning! UI elements not found by name! -> {name}");
}
return json;
}
public static void DestroyAll() {
foreach (BasePlayer player in BasePlayer.activePlayerList) CuiHelper.DestroyUi(player, UI_POOPUP_PANEL);
}
private void BuildingPlayer_PopUp() {
CuiElementContainer container = new CuiElementContainer();
container.Add(new CuiElement {
Name = UI_POOPUP_PANEL, Parent = "Overlay", Components = {
new CuiRawImageComponent {
Color = "1 1 1 1", Png = "%PNGBackground%"
}
, new CuiRectTransformComponent {
AnchorMin = "1 0.5", AnchorMax = "1 0.5", OffsetMin = "-252.6 -135.1", OffsetMax = "7.4 -75.1"
}
}
}
);
container.Add(new CuiElement {
Name = "Icon", Parent = UI_POOPUP_PANEL, Components = {
new CuiRawImageComponent {
Color = "1 1 1 1", Png = "%ICON%"
}
, new CuiRectTransformComponent {
AnchorMin = "0.5 0.5", AnchorMax = "0.5 0.5", OffsetMin = "-116.9 -16", OffsetMax = "-84.9 16"
}
}
}
);
container.Add(new CuiElement {
Name = "Title", Parent = UI_POOPUP_PANEL, Components = {
new CuiTextComponent {
Text = "%TITLE%", Font = "robotocondensed-regular.ttf", FontSize = 16, Align = TextAnchor.MiddleCenter, Color = "%COLOR_TITLE%"
}
, new CuiRectTransformComponent {
AnchorMin = "0.5 0.5", AnchorMax = "0.5 0.5", OffsetMin = "-84.9 -2.06", OffsetMax = "125.33 19.46"
}
}
}
);
container.Add(new CuiElement {
Name = "Description", Parent = UI_POOPUP_PANEL, Components = {
new CuiTextComponent {
Text = "%DESCRIPTION%", Font = "robotocondensed-regular.ttf", FontSize = 10, Align = TextAnchor.UpperCenter, Color = "%COLOR_DESCRIPTION%"
}
, new CuiRectTransformComponent {
AnchorMin = "0.5 0.5", AnchorMax = "0.5 0.5", OffsetMin = "-84.9 -17.67", OffsetMax = "125.33 0"
}
}
}
);
AddInterface("UI_POOPUP_TEMPLATE", container.ToJson());
}
}
private IEnumerator MigrationGrant() {
if (!Interface.Oxide.DataFileSystem.ExistsDatafile("GrantData")) {
yield break;
}
GrantData dataFile = Interface.Oxide.DataFileSystem.ReadObject<GrantData>("GrantData");
Int64 curDt = ToEpochTime(DateTime.Now);
foreach (KeyValuePair<String, Dictionary<String, Int64>> Permissions in dataFile.Permission.Where(x => x.Value.Count != 0)) {
UInt64 userID;
if (!UInt64.TryParse(Permissions.Key, out userID)) continue;
foreach (KeyValuePair<String, Int64> PermissionsUser in Permissions.Value.Where(PermissionsUser => PermissionsUser.Value - curDt > 0)) {
Int32 TotalSeconds = (Int32)(PermissionsUser.Value - curDt);
DateTime time = DateTime.Now.AddSeconds(TotalSeconds);
InformationPrivilagesUser.SetParametres(PermissionsUser.Key, userID, time, TypeData.Permission);
yield return CoroutineEx.waitForSeconds(0.15f);
}
yield return CoroutineEx.waitForSeconds(0.25f);
}
foreach (KeyValuePair<String, Dictionary<String, Int64>> Groups in dataFile.Group.Where(x => x.Value.Count != 0)) {
UInt64 userID;
if (!UInt64.TryParse(Groups.Key, out userID)) continue;
foreach (KeyValuePair<String, Int64> GroupsUser in Groups.Value.Where(GroupsUser => GroupsUser.Value - curDt > 0)) {
Int32 TotalSeconds = (Int32)(GroupsUser.Value - curDt);
DateTime time = DateTime.Now.AddSeconds(TotalSeconds);
InformationPrivilagesUser.SetParametres(GroupsUser.Key, userID, time, TypeData.Group);
yield return CoroutineEx.waitForSeconds(0.15f);
}
yield return CoroutineEx.waitForSeconds(0.25f);
}
Interface.Oxide.UnloadPlugin("Grant");
if (coroutineMigration != null) {
ServerMgr.Instance.StopCoroutine(coroutineMigration);
coroutineMigration = null;
}
}
private void SetGroup(UInt64 userID, String Group, DateTime DataExpired) => InformationPrivilagesUser.SetParametres(Group, userID, DataExpired, TypeData.Group);
private static TPPermissions _;
private void DeleteDatabase(String UserID, TypeData typeData, String Key) {
if (sqlConnection == null) return;
String Permission = typeData == TypeData.Permission ? Key : String.Empty;
String Group = typeData == TypeData.Group ? Key : String.Empty;
String sqlQuery = typeData == TypeData.Permission ? $"DELETE FROM `{config.GeneralSettings.MySQLSetting.TableName}` WHERE `steamid` = @0 AND `permission` = @1" : $"DELETE FROM `{config.GeneralSettings.MySQLSetting.TableName}` WHERE `steamid` = @0 AND`group` = @1";
Sql deleteCommand = Oxide.Core.Database.Sql.Builder.Append(sqlQuery, UserID, typeData == TypeData.Permission ? Permission : Group);
sqlLibrary.Delete(deleteCommand, sqlConnection, rowsAffected => {
}
);
}
[PluginReference]
Plugin IQChat, UAlertSystem;
private void OnUserPermissionRevoked(String id, String permName) => InformationPrivilagesUser.RemoveParametres(permName, UInt64.Parse(id), TypeData.Permission);
private void OnUserGroupRemoved(String id, String groupName) => InformationPrivilagesUser.RemoveParametres(groupName, UInt64.Parse(id), TypeData.Group);
public void SendChat(String Message, BasePlayer player, ConVar.Chat.ChatChannel channel = ConVar.Chat.ChatChannel.Global) {
if (IQChat) if (config.AlertConfigurations.SettingIQChat.UIAlertUse) IQChat?.Call("API_ALERT_PLAYER_UI", player, Message);
else IQChat?.Call("API_ALERT_PLAYER", player, Message, config.AlertConfigurations.SettingIQChat.CustomPrefix, config.AlertConfigurations.SettingIQChat.CustomAvatar);
else player.SendConsoleCommand("chat.add", channel, 0, Message);
}
protected override void LoadDefaultConfig() => config = Configuration.GetNewConfiguration();
private void Init() {
_ = this;
ReadData();
}
[ConsoleCommand("group.users")]
private void GetInfoGroup_ConsoleCommand(ConsoleSystem.Arg arg) {
if (arg == null) return;
if (arg.Player() != null) if (!arg.Player().IsAdmin) return;
if (arg.Args == null || arg.Args.Length < 1) {
arg.ReplyWith(LanguageEn ? "Syntax error, use: group.users Group" : "Ошибка синтаксиса, используйте : group.users Group");
return;
}
String Group = arg.Args[0];
if (!config.GroupList.ContainsKey(Group)) {
arg.ReplyWith(LanguageEn ? "The group you specified do not exist!" : "Группу которую вы указали - не существуют!");
return;
}
Dictionary<UInt64, DateTime> GetPlayers = InformationPrivilagesUser.GePlayerList(Group, TypeData.Group);
String PlayerList = GetPlayers == null ? String.Empty : GetPlayers.Aggregate(String.Empty, (current, Players) => current + $"- ID : {Players.Key} | Expired : {GetLeftTime(Players.Value, Players.Key.ToString())}\n");
String Information = Lang
// ... [Code preview truncated due to length. Full source in attached file] ...