C#:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Oxide.Plugins {
[Info("VK Widget", "Mystery", "0.0.1")]
[Description("")]
class VKW : RustPlugin {
public class WidgetSettings {
[JsonProperty(PropertyName = "Включить обновление виджета?")]
public bool WgEnable {
get;
set;
}
= true;
[JsonProperty(PropertyName = "Таймер обновления (минуты)")]
public int UpdateTimer {
get;
set;
}
= 3;
[JsonProperty(PropertyName = "Заголовок виджета")]
public string WgTitle {
get;
set;
}
= "Мониторинг серверов";
[JsonProperty(PropertyName = "Ключ приложения для работы с виджетом")]
public string WgToken {
get;
set;
}
= "149716051ca57e8fc1c37a9525dcdafc42ca2546d93acf633a74fcd3a2ce64e27f27f473f8b716ed577";
}
private class Configuration {
[JsonProperty(PropertyName = "Виджет сообщества")]
public WidgetSettings GrWgSet {
get;
set;
}
public static Configuration Generate() {
return new Configuration {
GrWgSet = new WidgetSettings {
}
}
;
}
}
private static Configuration Settings = null;
protected override void LoadConfig() {
base.LoadConfig();
try {
Settings = Config.ReadObject<Configuration>();
}
catch {
PrintWarning($"Error reading config, creating one new config!");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig() => Settings = Configuration.Generate();
protected override void SaveConfig() => Config.WriteObject(Settings);
private void OnServerInitialized() {
PrintWarning(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
PrintWarning($" {Name} v{Version} loading");
PrintWarning(" Plugin loaded - OK");
PrintWarning(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
if (Settings.GrWgSet.WgEnable) {
if (Settings.GrWgSet.WgToken == "none") {
PrintWarning($"Ошибка! В файле конфигурации не указан ключ!");
return;
}
timer.Every(Settings.GrWgSet.UpdateTimer * 60, () => {
PrepareWidgetCode();
}
);
PrepareWidgetCode();
}
else PrintWarning($"Функция обновления статуса отключена.");
}
static string apiver = "v=5.92";
private void PrepareWidgetCode() {
string code = @"return{""title"":""" + Settings.GrWgSet.WgTitle + @""",""head"":[{""text"":""Сервер""},{""text"":""Онлайн""},{""text"":""Спящие""},{""text"":""Слоты""},{""text"":""Карта""}],""body"":[";
string map = ConVar.Server.level;
string online = BasePlayer.activePlayerList.Count.ToString();
string sleeping = BasePlayer.sleepingPlayerList.Count.ToString();
if (ConVar.Server.levelurl != string.Empty) map = "Custom Map";
code = code + @"[{""text"":""" + ConVar.Server.hostname + @"""},{""text"":""" + online + @"""},{""text"":""" + sleeping + @"""},{""text"":""" + ConVar.Server.maxplayers.ToString() + @"""},{""text"":""" + map + @"""}],";
code = code + @"],";
code = code + "};";
SendWidget(code);
}
private void SendWidget(string widget) => webrequest.Enqueue("https://api.vk.com/method/appWidgets.update?type=table" + "&code=" + URLEncode(widget) + "&" + apiver + "&access_token=" + Settings.GrWgSet.WgToken, null, (code, response) => GetCallback(code, response, "Виджет"), this);
private string URLEncode(string input) {
if (input.Contains("#")) input = input.Replace("#", "%23");
if (input.Contains("$")) input = input.Replace("$", "%24");
if (input.Contains("+")) input = input.Replace("+", "%2B");
if (input.Contains("/")) input = input.Replace("/", "%2F");
if (input.Contains(":")) input = input.Replace(":", "%3A");
if (input.Contains(";")) input = input.Replace(";", "%3B");
if (input.Contains("?")) input = input.Replace("?", "%3F");
if (input.Contains("@")) input = input.Replace("@", "%40");
return input;
}
void Log(string filename, string text) => LogToFile(filename, $"[{DateTime.Now}] {text}", this);
void GetCallback(int code, string response, string type, BasePlayer player = null) {
if (!response.Contains("response")) {
PrintWarning($"{type} не отправлен(о). Файлы лога: /oxide/logs/VKW/");
Log("Errors", $"{type} не отправлен(о). Ошибка: " + response);
return;
}
}
}
}