Make PlayerList code nicer.

This commit is contained in:
UnknownShadow200 2016-06-01 11:34:07 +10:00
parent 15a6c1c562
commit b529e4693c
9 changed files with 41 additions and 50 deletions

View File

@ -93,7 +93,7 @@ namespace MCGalaxy.Commands.Moderation {
Player.GlobalMessage(message + " was &8ip-banned %S by (console).");
}
Server.bannedIP.Add(message);
Server.bannedIP.Save("banned-ip.txt", false);
Server.bannedIP.Save();
/*
foreach (Player pl in PlayerInfo.players) {

View File

@ -45,7 +45,7 @@ namespace MCGalaxy.Commands {
}
Server.ircControllers.Add(parts[1]);
Server.ircControllers.Save("IRC_Controllers.txt", true);
Server.ircControllers.Save();
Player.Message(p, parts[1] + " added to the IRC controller list.");
break;
case "remove":
@ -55,7 +55,7 @@ namespace MCGalaxy.Commands {
}
Server.ircControllers.Remove(parts[1]);
Server.ircControllers.Save("IRC_Controllers.txt", true);
Server.ircControllers.Save();
Player.Message(p, parts[1] + " removed from the IRC controller list.");
break;
case "list":

View File

@ -44,7 +44,7 @@ namespace MCGalaxy.Commands {
if (p != null) if (p.ip == message) { Player.Message(p, "You shouldn't be able to use this command..."); return; }
if (!Server.bannedIP.Contains(message)) { Player.Message(p, message + " is not a banned IP."); return; }
Server.bannedIP.Remove(message);
Server.bannedIP.Save("banned-ip.txt", false);
Server.bannedIP.Save();
string name = p == null ? "(console)" : p.name;
string fullName = p == null ? "(console)" : p.ColoredName;

View File

@ -46,7 +46,7 @@ namespace MCGalaxy.Commands
}
Server.whiteList.Add(player);
Chat.GlobalMessageOps(p.ColoredName + " %Sadded &f" + player + " %Sto the whitelist.");
Server.whiteList.Save("whitelist.txt", true);
Server.whiteList.Save();
Server.s.Log("WHITELIST: Added " + player);
break;
case "del":
@ -57,7 +57,7 @@ namespace MCGalaxy.Commands
}
Server.whiteList.Remove(player);
Chat.GlobalMessageOps(p.ColoredName + " %Sremoved &f" + player + " %Sfrom the whitelist.");
Server.whiteList.Save("whitelist.txt", true);
Server.whiteList.Save();
Server.s.Log("WHITELIST: Removed " + player);
break;
case "list":

View File

@ -38,23 +38,7 @@ namespace MCGalaxy.Commands
Player.Message(p, "This command can only be used if agree-to-rules-on-entry is enabled!");
return;
}
//If someone is ranked before agreeing to the rules they are locked and cannot use any commands unless demoted back to guest
/*if (p.group.Permission > LevelPermission.Guest)
{
Player.Message(p, "Your rank is higher than guest and you have already agreed to the rules!");
return;
}*/
var agreed = File.ReadAllText("ranks/agreed.txt");
/*
if (File.Exists("logs/" + DateTime.Now.ToString("yyyy") + "-" + DateTime.Now.ToString("MM") + "-" + DateTime.Now.ToString("dd") + ".txt"))
{
var checklogs = File.ReadAllText("logs/" + DateTime.Now.ToString("yyyy") + "-" + DateTime.Now.ToString("MM") + "-" + DateTime.Now.ToString("dd") + ".txt");
if (!checklogs.Contains(p.name.ToLower() + " used /rules"))
{
Player.Message(p, "&9You must read /rules before agreeing!");
return;
}
}*/
if (p.hasreadrules == false)
{
Player.Message(p, "&9You must read /rules before agreeing!");

View File

@ -15,6 +15,7 @@
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.IO;
using System.Collections.Generic;
@ -22,10 +23,9 @@ namespace MCGalaxy {
public sealed class PlayerList {
public Group group;
string file;
List<string> players = new List<string>();
readonly object locker = new object();
public PlayerList() { }
public void Add(string p) {
lock (locker)
@ -52,33 +52,47 @@ namespace MCGalaxy {
return players.Count;
} }
public void Save() { Save(group.fileName, true); }
public void Save() { Save(file, true); }
public void Save(string path, bool console) {
using (StreamWriter w = File.CreateText("ranks/" + path)) {
public void Save(string file, bool console) {
using (StreamWriter w = File.CreateText("ranks/" + file)) {
lock (locker) {
foreach (string p in players)
w.WriteLine(p);
}
}
if (console)
Server.s.Log("SAVED: " + path);
Server.s.Log("SAVED: " + file);
}
public static PlayerList Load(string path, Group groupName) {
public static PlayerList Load(string path) {
if (!Directory.Exists("ranks"))
Directory.CreateDirectory("ranks");
path = "ranks/" + path;
Directory.CreateDirectory("ranks");
PlayerList list = new PlayerList();
list.group = groupName;
list.file = path;
path = "ranks/" + path;
if (File.Exists(path)) {
foreach (string line in File.ReadAllLines(path)) { list.Add(line); }
foreach (string line in File.ReadAllLines(path)) {
string entry = line;
// Need to convert uppercase to lowercase, in case user added in entries.
bool anyUpper = false;
for (int i = 0; i < entry.Length; i++) {
char c = entry[i];
anyUpper |= (c >= 'A' && c <= 'Z');
}
if (anyUpper) entry = entry.ToLower();
list.Add(entry);
}
} else {
File.Create(path).Close();
Server.s.Log("CREATED NEW: " + path);
Server.s.Log("CREATED NEW: " + list.file);
}
return list;
}
[Obsolete("Group parameter is completely ignored.")]
public static PlayerList Load(string path, Group grp) { return Load(path); }
}
}

View File

@ -66,18 +66,17 @@ namespace MCGalaxy {
}
void LoadPlayerLists() {
bannedIP = PlayerList.Load("banned-ip.txt", null);
ircControllers = PlayerList.Load("IRC_Controllers.txt", null);
muted = PlayerList.Load("muted.txt", null);
frozen = PlayerList.Load("frozen.txt", null);
bannedIP = PlayerList.Load("banned-ip.txt");
ircControllers = PlayerList.Load("IRC_Controllers.txt");
muted = PlayerList.Load("muted.txt");
frozen = PlayerList.Load("frozen.txt");
//jailed = PlayerList.Load("jailed.txt");
if (!File.Exists("ranks/jailed.txt")) { File.Create("ranks/jailed.txt").Close(); Server.s.Log("CREATED NEW: ranks/jailed.txt"); }
foreach (Group grp in Group.GroupList)
grp.playerList = PlayerList.Load(grp.fileName, grp);
grp.playerList = PlayerList.Load(grp.fileName);
if (useWhitelist)
whiteList = PlayerList.Load("whitelist.txt", null);
if (!File.Exists("ranks/jailed.txt")) { File.Create("ranks/jailed.txt").Close(); Server.s.Log("CREATED NEW: ranks/jailed.txt"); }
Extensions.UncapitalizeAll("ranks/banned.txt");
Extensions.UncapitalizeAll("ranks/muted.txt");
whiteList = PlayerList.Load("whitelist.txt");
}
void LoadAutoloadCommands() {

View File

@ -119,6 +119,7 @@ namespace MCGalaxy
public static PlayerList muted;
public static PlayerList ignored;
public static PlayerList frozen;
//public static PlayerList jailed;
public static readonly List<string> Devs = new List<string>();
public static readonly List<string> Mods = new List<string>();

View File

@ -85,13 +85,6 @@ namespace MCGalaxy {
public static string Concatenate<T>(this List<T> list, string separator) {
return String.Join(separator, list);
}
public static void UncapitalizeAll(string file) {
string[] complete = File.ReadAllLines(file);
for (int i = 0; i < complete.Length; i++)
complete[i] = complete[i].ToLower();
File.WriteAllLines(file, complete);
}
public static string ToDBTime(this TimeSpan value) {
return value.Days + " " + value.Hours + " " + value.Minutes + " " + value.Seconds;