using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Game.Rust.Cui;
using Oxide.Core.Libraries.Covalence; //сообщения
using Oxide.Core.Plugins;
using UnityEngine;
using System.Linq;
using Newtonsoft.Json;
using Steamworks.Data;
using Epic.OnlineServices.UI;
using static ByteQuadtree;
namespace Oxide.Plugins
{
[Info("Day or night", "Pagan", "12.1.24")]
[Description("Vote to skip the night")]
class DeyNight : RustPlugin
{
#region GUIWins
//ID Проголосовавших За день или ночь
private readonly HashSet<BasePlayer> VoteDN = new HashSet<BasePlayer>();
int vDey = 0;
int vNight = 0;
string Gui_But = "{\"name\":\"bytDey\",\"parent\":\"CuiElement\",\"components\":[{\"type\":\"UnityEngine.UI.Button\",\"command\":\"VoteDey\",\"color\":\"0 0 0 1\"},{\"type\":\"RectTransform\",\"anchormin\":\"0.01776308 0.04819337\",\"anchormax\":\"0.4777628 0.4459194\",\"offsetmin\":\"0 0\",\"offsetmax\":\"0 0\"}]},{\"name\":\"imgDey\",\"parent\":\"bytDey\",\"components\":[{\"type\":\"UnityEngine.UI.RawImage\",\"url\":\"
https://i.ibb.co/GRtST4b/yarilo.png\"},{\"type\":\"RectTransform\",\"anchormin\":\"0.3104664 -0.0354557\",\"anchormax\":\"0.6449146 0.8788328\",\"offsetmin\":\"0 0\",\"offsetmax\":\"0 0\"}]},{\"name\":\"BytNight\",\"parent\":\"CuiElement\",\"components\":[{\"type\":\"UnityEngine.UI.Button\",\"command\":\"VoteNight\",\"color\":\"0 0 0 1\"},{\"type\":\"RectTransform\",\"anchormin\":\"0.5218011 0.04819372\",\"anchormax\":\"0.9785317 0.4345573\",\"offsetmin\":\"0 0\",\"offsetmax\":\"0 0\"}]},{\"name\":\"imgNight\",\"parent\":\"BytNight\",\"components\":[{\"type\":\"UnityEngine.UI.RawImage\",\"url\":\"
https://i.ibb.co/c8f35YB/lune.png\"},{\"type\":\"RectTransform\",\"anchormin\":\"0.3264315 -0.03339091\",\"anchormax\":\"0.6632737 0.9077855\",\"offsetmin\":\"0 0\",\"offsetmax\":\"0 0\"}]},";
string Gui_Shet = "{\"name\":\"imDdey\",\"parent\":\"CuiElement\",\"components\":[{\"type\":\"UnityEngine.UI.RawImage\",\"url\":\"
https://i.ibb.co/GRtST4b/yarilo.png\"},{\"type\":\"RectTransform\",\"anchormin\":\"0.02884644 0.1477274\",\"anchormax\":\"0.1875003 0.5227274\",\"offsetmin\":\"0 0\",\"offsetmax\":\"0 0\"}]},{\"name\":\"txtDey\",\"parent\":\"CuiElement\",\"components\":[{\"type\":\"UnityEngine.UI.Text\",\"text\":\"{Dey}\",\"fontSize\":20,\"align\":\"MiddleCenter\",\"color\":\"0.9618622 0.9025098 0.5232553 1\"},{\"type\":\"RectTransform\",\"anchormin\":\"0.2115384 0.1249999\",\"anchormax\":\"0.4567307 0.5568181\",\"offsetmin\":\"0 0\",\"offsetmax\":\"0 0\"}]},{\"name\":\"imgNight\",\"parent\":\"CuiElement\",\"components\":[{\"type\":\"UnityEngine.UI.RawImage\",\"url\":\"
https://i.ibb.co/c8f35YB/lune.png\"},{\"type\":\"RectTransform\",\"anchormin\":\"0.5192302 0.1477273\",\"anchormax\":\"0.6778841 0.5227274\",\"offsetmin\":\"0 0\",\"offsetmax\":\"0 0\"}]},{\"name\":\"txtNight\",\"parent\":\"CuiElement\",\"components\":[{\"type\":\"UnityEngine.UI.Text\",\"text\":\"{Night}\",\"fontSize\":20,\"align\":\"MiddleCenter\",\"color\":\"0.4044405 0.7205415 0.9538622 1\"},{\"type\":\"RectTransform\",\"anchormin\":\"0.7163466 0.1477273\",\"anchormax\":\"0.9615389 0.5795455\",\"offsetmin\":\"0 0\",\"offsetmax\":\"0 0\"}]},";
string Gui_end = "{\"name\":\"textend\",\"parent\":\"CuiElement\",\"components\":[{\"type\":\"UnityEngine.UI.Text\",\"text\":\"{endText}\",\"fontSize\":20,\"align\":\"MiddleCenter\",\"color\":\"0.682353 0.8980392 0.9529412 1\"},{\"type\":\"RectTransform\",\"anchormin\":\"0.01442316 0.1136366\",\"anchormax\":\"0.9615384 0.5681821\",\"offsetmin\":\"0 0\",\"offsetmax\":\"0 0\"}]},";
string GUI_Teloin = "[{\"name\":\"CuiElement\",\"parent\":\"Overlay\",\"components\":[{\"type\":\"UnityEngine.UI.Image\",\"material\":\"\",\"color\":\"0 0 0 0.6000942\"},{\"type\":\"RectTransform\",\"anchormin\":\"0.214375 0.004444424\",\"anchormax\":\"0.344375 0.1022222\",\"offsetmin\":\"0 0\",\"offsetmax\":\"0 0\"}]},";
string GUI_Teloout = "{\"name\":\"txtwin\",\"parent\":\"CuiElement\",\"components\":[{\"type\":\"UnityEngine.UI.Text\",\"text\":\"День или ночь?\",\"align\":\"MiddleCenter\"},{\"type\":\"RectTransform\",\"anchormin\":\"0.01923072 0.5608587\",\"anchormax\":\"0.9855768 0.9545456\",\"offsetmin\":\"0 0\",\"offsetmax\":\"0 0\"}]}]";
string WindowUi ="";
// метка что окно не запущенно что бы избежать повторного открытия окна
bool StopUi = false;
//счёт дней
int NumDey = 0;
//=====================================
int SendTime = 0;
int NumLidtChat = 0;
//Команды кнопок голосования
[ConsoleCommand("VoteNight")]
void killWindowN(ConsoleSystem.Arg arg)
{
var player = arg.Connection.player as BasePlayer;
if (player == null)
return;
//var player = arg.Connection.player as BasePlayer;
//CuiHelper.DestroyUi(player, "CuiElement");
CuiHelper.DestroyUi(player, "CuiElement");
// Добавляем в лист и меняем окно у пользователя..
VoteDN.Add(player);
vNight++;
opengui("shet");
if (StopUi == false)
Puts("StopUi2 == false");
else
{
Puts("StopUi2 == true");
}
}
[ConsoleCommand("VoteDey")]
void killWindowD(ConsoleSystem.Arg arg)
{
var player = arg.Connection.player as BasePlayer;
if (player == null)
return;
CuiHelper.DestroyUi(player, "CuiElement");
VoteDN.Add(player);
vDey++;
opengui("shet");
if (StopUi == false)
Puts("StopUi3 == false");
else
{
Puts("StopUi3 == true");
}
}
[ChatCommand("pila")]// /pila (начало ивента)
void HideWin(BasePlayer player, string command, string[] arg)
{
CuiHelper.DestroyUi(player, "CuiElement");
}
#endregion
private Timer tvrm;
private Timer tmr;
void OnServerInitialized()
{
#region MainTimer
string timed = "";
//Узнаём время начала голосования и разбиваем на часы и минуты
string[] timeed = _configData.setDeyNight.StarTtime.Split(":");
//Если к минутам прибавить два и результат больше или равно 60
//Puts("Minute - " + timeed[1]);
if (Convert.ToInt32(timeed[1]) + 2 >= 60)
{
int hour = Convert.ToInt32(timeed[0]);
int minute = Convert.ToInt32(timeed[1]);
//к часам прибавляем 1
hour = hour + 1;
// к минутам прибавляем 3
minute = minute + 2;
// и вычитаем 60
minute = minute - 60;
if (Math.Abs(minute).ToString().Length == 1)
{
timed = hour.ToString() + ":0" + minute.ToString();
}
else
{
timed = hour.ToString() + ":" + minute.ToString();
}
//Puts(hour.ToString() + " 1!!!! " + minute.ToString());
}
else
{
int minute = Convert.ToInt32(timeed[1]);
minute = minute + 2;
timeed[1] = minute.ToString();
minute = timeed[1].Length;
if (minute == 1)
{
timeed[1] = "0" + timeed[1];
}
timed = timeed[0] + ":" + timeed[1];
}
/////Рабочий таймер
tvrm = timer.Every(1f, () =>
{
#region NightDey
if (_configData.setDeyNight.nvote)
{
if (int.Parse(TOD_Sky.Instance.Cycle.DateTime.ToString("HH:mm").Replace(":", "")) == int.Parse(_configData.setDeyNight.StarTtime.Replace(":", ""))) //&& int.Parse(TOD_Sky.Instance.Cycle.DateTime.ToString("HH:mm").Replace(":", "")) < int.Parse(timed.Replace(":", "")))
{
if (StopUi == false)
Puts("StopUi1 == false");
else
{
Puts("StopUi1 == true");
}
if (StopUi == false)
{
StopUi = true;
//присваиваем значение первого окна голосования
string rnvote = "vots";
//Прибавляем новый день недели
NumDey++;
//если счётчик больше количества дней в неделе из конфига
if (NumDey > _configData.PrmDeyNight.DeyWick) //_configData.PrmDeyNight.DeyWick)
{
//делаем первый день
NumDey = 1;
}
//получаем список дней недели для пропуска из конфига
var NgDey = _configData.PrmDeyNight.DeyNight.Split(",");
//Начинаем цикл перебирая дни
Puts("День недели == " + NumDey.ToString());
foreach (string n in NgDey)
{
int aln = int.Parse(n);
//если день недели равен сегодняшнему
if (aln == NumDey)
{
//Запускаем окно с пропуском ночи
rnvote = "";
endvote();
NgDey = null;
break;
}
}
if (rnvote != "")
{
opengui("vots");
};
}
//Puts("StopUi == true");
//StopUi = true;
}
}
#endregion NightDey
#region DendText
//Если сообщения включены
if (_configData.sandMail.loockMail)
{
if (_configData.sandMail.timeMail <= SendTime)
{
SendTime = 0;
if (NumLidtChat > _configData.sandMail.SandTick.Count - 1)
{
NumLidtChat = 0;
}
var playersCount = BasePlayer.activePlayerList.Count;
foreach (BasePlayer player in BasePlayer.activePlayerList)
{
if (player != null)
{
SendReply(player, _configData.sandMail.SandTick[NumLidtChat]);
}
}
NumLidtChat++;
}
}
SendTime++;
#endregion DendText
});
#endregion
}
#region GUI
private void opengui(string formCount)
{
if (formCount == "vots")
{
Puts("PROVERKA 001 ");
WindowUi = GUI_Teloin + Gui_But + GUI_Teloout;
foreach (var player in BasePlayer.activePlayerList)
{
DrawCUI(player);
}
//var playersCount = BasePlayer.activePlayerList.Count;
//for (var i = 0; i < playersCount; i++)
//{
// var player = BasePlayer.activePlayerList
;
// DrawCUI(player);
//}
float OutRun = _configData.setDeyNight.DamageTypeCold;
timer.Once(OutRun, () =>
{
CloseCUIAll("CuiElement");
endvote();
});
}
else if (formCount == "shet")
{
Puts("PROVERKA 000 ");
string Gui_ShetNew = Gui_Shet
.Replace("{Dey}", vDey.ToString())
.Replace("{Night}", vNight.ToString());
WindowUi = GUI_Teloin + Gui_ShetNew + GUI_Teloout;
CloseCUIAll("CuiElement");
CloseCUIAll("CuiElement");
foreach (var Pler in VoteDN)
{
DrawCUI(Pler);
}
}
}
private void DrawCUI(BasePlayer player)
{
CuiHelper.AddUi(player, WindowUi);
}
//Удаление окон
private void CloseCUI(BasePlayer player)
{
CuiHelper.DestroyUi(player, "NightDey");
}
private void CloseCUIAll(string NameWin)
{
//var playersCount = BasePlayer.activePlayerList.Count;
foreach (BasePlayer player in BasePlayer.activePlayerList)
{
Puts("PROVERKA 003 ");
Puts(NameWin);
CuiHelper.DestroyUi(player, NameWin);
}
// for (var i = 0; i < playersCount; i++)
//{
// var player = BasePlayer.activePlayerList;
// CuiHelper.DestroyUi(player, NameWin);
//};
}
private void endvote()
{
StopUi = true;
string textvote = null;
if(vDey > vNight)
{
textvote = "Ночь пропущена";
TOD_Sky.Instance.Cycle.Hour = (float)TimeSpan.Parse(_configData.setDeyNight.EndTime).TotalHours;
}
else if(vDey < vNight)
{
textvote = "Наступает ночь";
}
else
{
textvote = "Наступает ночь";
}
CloseCUIAll("CuiElement");
string Gui_ShetNew = Gui_end.Replace("{endText}", textvote);
WindowUi = GUI_Teloin + Gui_ShetNew + GUI_Teloout;
var playersCount = BasePlayer.activePlayerList.Count;
foreach (var player in BasePlayer.activePlayerList)
{
DrawCUI(player);
}
//for (var i = 0; i < playersCount; i++)
//{
// var player = BasePlayer.activePlayerList;
// DrawCUI(player);
//}
//запускаем функцию смены дня и ночи
timer.Once(30f , () =>
{
CloseCUIAll("CuiElement");
StopUi = false;
});
VoteDN.Clear();
vDey = 0;
vNight = 0;
Gui_ShetNew = "";
}
#endregion
#region Configuration
private ConfigData _configData;
private class ConfigData
{
[JsonProperty(PropertyName = "Настройка пропуска")]
public setDeyNights setDeyNight { get; set; }
[JsonProperty(PropertyName = "Параметры пропуска")]
public PrmDeyNights PrmDeyNight { get; set; }
[JsonProperty(PropertyName = "Сообщения")]
public SandMails sandMail { get; set; }
public class setDeyNights
{
[JsonProperty(PropertyName = "Включить голосование")]
public bool nvote { get; set; }
[JsonProperty(PropertyName = "Время заката")]
public string StarTtime { get; set; }
[JsonProperty(PropertyName = "Время восхода")]
public string EndTime { get; set; }
[JsonProperty(PropertyName = "Время голосования")]
public int DamageTypeCold { get; set; }
}
public class PrmDeyNights
{
//DeyWick
[JsonProperty(PropertyName = "Обязательная ночь")]
public bool EtsNight { get; set; }
[JsonProperty(PropertyName = "Дней в неделе")]
public int DeyWick { get; set; }
[JsonProperty(PropertyName = "Дни пропуска ночи")]
public string DeyNight { get; set; }
}
public class SandMails
{
[JsonProperty(PropertyName = "Показывать сообщения")]
public bool loockMail { get; set; }
[JsonProperty(PropertyName = "Время между сообщениями")]
public int timeMail { get; set; }
[JsonProperty(PropertyName = "Список сообщений")]
public List<string> SandTick = new List<string>();
}
}
private ConfigData GetBaseConfig()
{
return new ConfigData
{
setDeyNight = new ConfigData.setDeyNights
{
nvote = true,
StarTtime = "19:00",
EndTime = "07:00",
DamageTypeCold = 60
},
PrmDeyNight = new ConfigData.PrmDeyNights
{
EtsNight = false,
DeyWick = 7,
DeyNight = "2,4,6"
},
sandMail = new ConfigData.SandMails
{
loockMail = false,
timeMail = 120,
SandTick = new List<string>
{
"У нас много замечательных ивентов",
"Не забывайте заглядывать в магазин сервера",
"<color=red>Event zone</color>\r\nСкоро запустится :)))))"
}
}
};
}
protected override void LoadDefaultConfig() => _configData = GetBaseConfig();
protected override void LoadConfig()
{
base.LoadConfig();
_configData = Config.ReadObject<ConfigData>();
SaveConfig();
}
protected override void SaveConfig()
{
Config.WriteObject(_configData);
//SaveConfig();
}
// protected override void SaveConfig() => Config.WriteObject(Configuration, true);
#endregion Configuration
}
}