forked from RocketModPlugins/PlayerInfoLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerInfoLib.cs
176 lines (162 loc) · 8.14 KB
/
PlayerInfoLib.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using Rocket.Core.Logging;
using Rocket.Core.Plugins;
using Rocket.Unturned;
using Rocket.Unturned.Player;
using SDG.Unturned;
using Steamworks;
using System;
using System.Collections.Generic;
using Rocket.API.Collections;
using Rocket.API;
using GlobalBan;
namespace PlayerInfoLibrary
{
public class PlayerInfoLib : RocketPlugin<PlayerInfoLibConfig>
{
public static PlayerInfoLib Instance;
public static DatabaseManager Database;
private DateTime lastCheck = DateTime.Now;
internal static Dictionary<CSteamID, DateTime> LoginTime = new Dictionary<CSteamID, DateTime>();
private DateTime lastCheckExpiredPInfo = DateTime.Now;
protected override void Load()
{
Instance = this;
Database = new DatabaseManager();
U.Events.OnPlayerConnected += Events_OnPlayerConnected;
U.Events.OnPlayerDisconnected += Events_OnPlayerDisconnected;
//U.Events.OnBeforePlayerConnected += Events_OnBeforePlayerConnected;
if (Instance.Configuration.Instance.KeepaliveInterval <= 0)
{
Logger.LogWarning("Error: Keep alive config option must be above 0.");
Instance.Configuration.Instance.KeepaliveInterval = 10;
}
if (Instance.Configuration.Instance.ExpiredCheckInterval < 1)
{
Logger.LogWarning("Error: Expired check interval must bu above 1.");
Instance.Configuration.Instance.ExpiredCheckInterval = 30;
}
Instance.Configuration.Save();
if (Database.Initialized)
Logger.Log(string.Format("PlayerInfoLib plugin has been loaded, Server Instance ID is: {0}", Database.InstanceID), ConsoleColor.Yellow);
else
Logger.Log("There was in issue loading the plugin, please check your config.", ConsoleColor.Red);
}
protected override void Unload()
{
U.Events.OnPlayerConnected -= Events_OnPlayerConnected;
U.Events.OnPlayerDisconnected -= Events_OnPlayerDisconnected;
//U.Events.OnBeforePlayerConnected -= Events_OnBeforePlayerConnected;
Database.Unload();
Database = null;
}
private void Events_OnBeforePlayerConnected(UnturnedPlayer player)
{
try
{
if (player != null)
{
if (LoginTime.ContainsKey(player.CSteamID))
LoginTime.Remove(player.CSteamID);
LoginTime.Add(player.CSteamID, DateTime.Now);
PlayerData pData = Database.QueryById(player.CSteamID, false);
int totalTime = pData.TotalPlayime;
DateTime loginTime = PlayerInfoLib.LoginTime[player.CSteamID];
pData = new PlayerData(player.CSteamID, player.SteamName, player.CharacterName, player.CSteamID.GetIP(), player.CSteamID.GetHWID(), loginTime, Database.InstanceID, Provider.serverName, Database.InstanceID, loginTime, false, false, totalTime);
Database.SaveToDB(pData);
// Recheck the ip address in the component, the ip isn't always fully set by the time this event is called.
PlayerInfoLibPComponent pc = player.GetComponent<PlayerInfoLibPComponent>();
pc.Start(pData);
}
}
catch(Exception ex)
{
}
}
private void Events_OnPlayerConnected(UnturnedPlayer player)
{
if (player != null)
{
if (LoginTime.ContainsKey(player.CSteamID))
LoginTime.Remove(player.CSteamID);
LoginTime.Add(player.CSteamID, DateTime.Now);
PlayerData pData = Database.QueryById(player.CSteamID, false);
int totalTime = pData.TotalPlayime;
DateTime loginTime = PlayerInfoLib.LoginTime[player.CSteamID];
pData = new PlayerData(player.CSteamID, player.SteamName, player.CharacterName, player.CSteamID.GetIP(), player.CSteamID.GetHWID(), loginTime, Database.InstanceID, Provider.serverName, Database.InstanceID, loginTime, false, false, totalTime);
Database.SaveToDB(pData);
// Recheck the ip address in the component, the ip isn't always fully set by the time this event is called.
PlayerInfoLibPComponent pc = player.GetComponent<PlayerInfoLibPComponent>();
pc.Start(pData);
}
PlayerInfoLib.ExecuteDependencyCode("GlobalBan", (IRocketPlugin plugin) =>
{
if (GlobalBan.GlobalBan.Instance.State == PluginState.Loaded)
{
if (GlobalBan.GlobalBan.CheckIfBanned(player))
{
if (GlobalBan.GlobalBan.Instance.BannedReason.TryGetValue(player, out string reason))
{
Provider.kick(player.CSteamID, reason);
GlobalBan.GlobalBan.Instance.BannedReason.Remove(player);
}
}
}
});
}
private void Events_OnPlayerDisconnected(UnturnedPlayer player)
{
if (player != null)
{
if (LoginTime.ContainsKey(player.CSteamID))
{
PlayerData pData = Database.QueryById(player.CSteamID, false);
if (pData.IsValid() && pData.IsLocal())
{
int totalSessionTime = (int)(DateTime.Now - LoginTime[player.CSteamID]).TotalSeconds;
pData.TotalPlayime += totalSessionTime;
Database.SaveToDB(pData);
}
LoginTime.Remove(player.CSteamID);
}
}
}
public void FixedUpdate()
{
if (State == PluginState.Loaded)
{
if ((DateTime.Now - lastCheck).TotalMinutes >= Configuration.Instance.KeepaliveInterval)
{
lastCheck = DateTime.Now;
Database.CheckConnection();
}
if ((DateTime.Now - lastCheckExpiredPInfo).TotalMinutes >= Configuration.Instance.ExpiredCheckInterval)
{
lastCheckExpiredPInfo = DateTime.Now;
Database.CheckExpired();
if (Configuration.Instance.ExpiresAfter >= 1)
Database.PrecessExpiredPInfo();
}
}
}
public override TranslationList DefaultTranslations
{
get
{
return new TranslationList
{
{ "too_many_parameters", "Too many parameters." },
{ "investigate_help", CommandInvestigate.syntax + " - " + CommandInvestigate.help },
{ "delint_help", CommandDelInstance.syntax + " - " + CommandDelInstance.help },
{ "rnint_help", CommandRnInstance.syntax + " - " + CommandRnInstance.help },
{ "invalid_page", "Error: Invalid page number." },
{ "number_of_records_found", "{0} Records found for: {1}, Page: {2} of {3}" },
{ "delint_invalid", "Error: Invalid Instance ID." },
{ "delint_not_found", "Error: Failed to find Instance ID in the database." },
{ "delint_success", "Successfully Removed all data for this Instance ID, if you removed the data for this server, you will need to reload the plugin for it to be operational again." },
{ "rnint_success", "Successfully changed the instance name for this server in the Database, Server should be restarted now." },
{ "rnint_not_found", "Error: Failed to set the new instance name to the Database." },
};
}
}
}
}