forked from RocketModPlugins/GlobalBan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseManager.cs
143 lines (124 loc) · 6.28 KB
/
DatabaseManager.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
using MySql.Data.MySqlClient;
using Rocket.Core.Logging;
using System;
using System.Text.RegularExpressions;
using Rocket.API.User;
namespace fr34kyn01535.GlobalBan
{
public class DatabaseManager
{
private readonly GlobalBan _globalBanPlugin;
public DatabaseManager(GlobalBan globalBanPlugin)
{
_globalBanPlugin = globalBanPlugin;
new I18N.West.CP1250();
CheckSchema();
}
private MySqlConnection createConnection()
{
MySqlConnection connection = null;
if (_globalBanPlugin.ConfigurationInstance.DatabasePort == 0)
_globalBanPlugin.ConfigurationInstance.DatabasePort = 3306;
connection = new MySqlConnection(
$"SERVER={_globalBanPlugin.ConfigurationInstance.DatabaseAddress};DATABASE={_globalBanPlugin.ConfigurationInstance.DatabaseName};UID={_globalBanPlugin.ConfigurationInstance.DatabaseUsername};PASSWORD={_globalBanPlugin.ConfigurationInstance.DatabasePassword};PORT={_globalBanPlugin.ConfigurationInstance.DatabasePort};");
return connection;
}
public bool IsBanned(IIdentity player)
{
MySqlConnection connection = createConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "select 1 from `" + _globalBanPlugin.ConfigurationInstance.DatabaseTableName + "` where `player_id` = '" + player.Id + "' and (banDuration is null or ((banDuration + UNIX_TIMESTAMP(banTime)) > UNIX_TIMESTAMP()));";
connection.Open();
object result = command.ExecuteScalar();
if (result != null) return true;
connection.Close();
return false;
}
public class Ban
{
public int Duration;
public DateTime Time;
public string Admin;
}
public Ban GetBan(string playerId)
{
MySqlConnection connection = createConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "select `banDuration`,`banTime`,`admin` from `" + _globalBanPlugin.ConfigurationInstance.DatabaseTableName + "` where `player_id` = '" + playerId + "' and (banDuration is null or ((banDuration + UNIX_TIMESTAMP(banTime)) > UNIX_TIMESTAMP()));";
connection.Open();
MySqlDataReader result = command.ExecuteReader(System.Data.CommandBehavior.SingleRow);
if (result != null && result.Read() && result.HasRows) return new Ban()
{
Duration = result["banDuration"] == DBNull.Value ? -1 : result.GetInt32("banDuration"),
Time = (DateTime)result["banTime"],
Admin = (result["admin"] == DBNull.Value || result["admin"].ToString() == "Console") ? "Console" : (string)result["admin"]
};
connection.Close();
return null;
}
public void CheckSchema()
{
MySqlConnection connection = createConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "show tables like '" + _globalBanPlugin.ConfigurationInstance.DatabaseTableName + "'";
connection.Open();
object test = command.ExecuteScalar();
if (test == null)
{
command.CommandText = "CREATE TABLE `" + _globalBanPlugin.ConfigurationInstance.DatabaseTableName + "` (`id` int(11) NOT NULL AUTO_INCREMENT,`player_id` varchar(32) NOT NULL,`admin` varchar(32) NOT NULL,`banMessage` varchar(512) DEFAULT NULL,`name` varchar(255) DEFAULT NULL,`banDuration` int NULL,`banTime` timestamp NULL ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`id`));";
command.ExecuteNonQuery();
}
connection.Close();
}
public void BanPlayer(IIdentity toBan, IIdentity bannedBy, string banMessage, int duration)
{
MySqlConnection connection = createConnection();
MySqlCommand command = connection.CreateCommand();
if (banMessage == null) banMessage = "";
command.Parameters.AddWithValue("@id", toBan.Id);
command.Parameters.AddWithValue("@admin", bannedBy.Name);
command.Parameters.AddWithValue("@name", toBan.Name);
command.Parameters.AddWithValue("@banMessage", banMessage);
if (duration == 0)
{
command.Parameters.AddWithValue("@banDuration", DBNull.Value);
}
else
{
command.Parameters.AddWithValue("@banDuration", duration);
}
command.CommandText = "insert into `" + _globalBanPlugin.ConfigurationInstance.DatabaseTableName + "` (`player_id`,`admin`,`banMessage`,`name`,`banTime`,`banDuration`) values(@id,@admin,@banMessage,@name,now(),@banDuration);";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
public class UnbanResult
{
public string Id;
public string Name;
}
public UnbanResult UnbanPlayer(IIdentity player)
{
MySqlConnection connection = createConnection();
MySqlCommand command = connection.CreateCommand();
command.Parameters.AddWithValue("@player", "%" + player.Id + "%");
command.CommandText = "select id,name from `" + _globalBanPlugin.ConfigurationInstance.DatabaseTableName + "` where `player_id` like @player or `name` like @player limit 1;";
connection.Open();
MySqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
string playerId = reader.GetString(0);
string playerName = reader.GetString(1);
connection.Close();
command = connection.CreateCommand();
command.Parameters.AddWithValue("@id", playerId);
command.CommandText = "delete from `" + _globalBanPlugin.ConfigurationInstance.DatabaseTableName + "` where `player_id` = @id;";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
return new UnbanResult { Id = playerId, Name = playerName };
}
return null;
}
}
}