Also log 'error:' message in normal log files, improve some countdown messages.

This commit is contained in:
UnknownShadow200 2017-06-29 09:10:30 +10:00
parent 1f83981ff2
commit 05f49b60c7
5 changed files with 182 additions and 177 deletions

View File

@ -37,7 +37,7 @@ namespace MCGalaxy.Commands.Fun {
public override CommandPerm[] ExtraPerms {
get { return new[] {
new CommandPerm(LevelPermission.Operator, "+ can send the countdown rules to everybody"),
new CommandPerm(LevelPermission.Operator, "+ can setup countdown (download/start/restart/enable/disable/cancel)"),
new CommandPerm(LevelPermission.Operator, "+ can setup countdown (generate/start/restart/enable/disable/cancel)"),
}; }
}
@ -83,90 +83,80 @@ namespace MCGalaxy.Commands.Fun {
void HandleJoin(Player p) {
switch (Server.Countdown.Status) {
case CountdownGameStatus.Disabled:
Player.Message(p, "Sorry - Countdown isn't enabled yet");
Player.Message(p, "Cannot join as countdown is not running.");
return;
case CountdownGameStatus.Enabled:
Server.Countdown.PlayerJoinedGame(p);
return;
case CountdownGameStatus.AboutToStart:
Player.Message(p, "Sorry - The game is about to start");
case CountdownGameStatus.RoundCountdown:
Player.Message(p, "Cannot join when a round is about to start. Wait until next round.");
return;
case CountdownGameStatus.InProgress:
Player.Message(p, "Sorry - The game is already in progress.");
case CountdownGameStatus.RoundInProgress:
Player.Message(p, "Cannot join when a round is in progress. Wait until next round.");
return;
case CountdownGameStatus.Finished:
case CountdownGameStatus.RoundFinished:
Player.Message(p, "Sorry - The game has finished. Get an op to reset it.");
return;
}
}
void HandleLeave(Player p) {
if (Server.Countdown.players.Contains(p)) {
if (Server.Countdown.Players.Contains(p)) {
switch (Server.Countdown.Status) {
case CountdownGameStatus.Disabled:
Player.Message(p, "Sorry - Countdown isn't enabled yet");
Player.Message(p, "Cannot leave as countdown is not running.");
return;
case CountdownGameStatus.Enabled:
Player.Message(p, "You've left the game.");
Player.Message(p, "You've left countdown.");
Server.Countdown.PlayerLeftGame(p);
break;
case CountdownGameStatus.AboutToStart:
Player.Message(p, "Sorry - The game is about to start");
case CountdownGameStatus.RoundCountdown:
Player.Message(p, "Cannot leave when a round is about to start.");
return; ;
case CountdownGameStatus.InProgress:
Player.Message(p, "Sorry - you are in a game that is in progress, please wait till its finished or till you've died.");
case CountdownGameStatus.RoundInProgress:
Player.Message(p, "Cannot leave when a round in progress - please wait until the round ends or you die.");
return;
case CountdownGameStatus.Finished:
Server.Countdown.players.Remove(p);
Server.Countdown.playersleftlist.Remove(p);
case CountdownGameStatus.RoundFinished:
Server.Countdown.Players.Remove(p);
Server.Countdown.PlayersRemaining.Remove(p);
p.playerofcountdown = false;
Player.Message(p, "You've left the game.");
break;
}
} else if (!(Server.Countdown.playersleftlist.Contains(p)) && Server.Countdown.players.Contains(p)) {
Server.Countdown.players.Remove(p);
} else if (!(Server.Countdown.PlayersRemaining.Contains(p)) && Server.Countdown.Players.Contains(p)) {
Server.Countdown.Players.Remove(p);
Player.Message(p, "You've left the game.");
} else {
Player.Message(p, "You haven't joined the game yet!!");
Player.Message(p, "Cannot leave as you did not join countdown to begin with.");
}
}
void HandlePlayers(Player p) {
switch (Server.Countdown.Status) {
case CountdownGameStatus.Disabled:
Player.Message(p, "The game has not been enabled yet.");
Player.Message(p, "Countdown is not running.");
break;
case CountdownGameStatus.Enabled:
Player.Message(p, "Players who have joined:");
foreach (Player plya in Server.Countdown.players)
Player.Message(p, plya.ColoredName);
break;
case CountdownGameStatus.RoundInProgress:
Player.Message(p, "Players in countdown:");
Player.Message(p, Server.Countdown.Players.Join(FormatPlayer));
break;
case CountdownGameStatus.AboutToStart:
Player.Message(p, "Players who are about to play:");
foreach (Player plya in Server.Countdown.players)
Player.Message(p, plya.ColoredName);
break;
case CountdownGameStatus.InProgress:
Player.Message(p, "Players left playing:");
foreach (Player plya in Server.Countdown.players) {
if (Server.Countdown.playersleftlist.Contains(plya))
Player.Message(p, plya.ColoredName + " %Swho is &aIN");
else
Player.Message(p, plya.ColoredName + " %Swho is &cOUT");
}
break;
case CountdownGameStatus.Finished:
Player.Message(p, "Players who were playing:");
foreach (Player plya in Server.Countdown.players)
Player.Message(p, plya.ColoredName);
default:
Player.Message(p, "Players in countdown: ");
Player.Message(p, Server.Countdown.Players.Join(pl => pl.ColoredName));
break;
}
}
static string FormatPlayer(Player pl) {
if (Server.Countdown.PlayersRemaining.Contains(pl)) {
return pl.ColoredName + " &a[IN]";
} else {
return pl.ColoredName + " &c[OUT]";
}
}
void HandleRules(Player p, string target) {
Player who = p;
if (target != "" && CheckExtraPerm(p, 1)) {
@ -246,12 +236,12 @@ namespace MCGalaxy.Commands.Fun {
void HandleDisable(Player p) {
if (!CheckExtraPerm(p, 2)) { MessageNeedExtra(p, 2); return; }
if (Server.Countdown.Status == CountdownGameStatus.AboutToStart || Server.Countdown.Status == CountdownGameStatus.InProgress) {
if (Server.Countdown.Status == CountdownGameStatus.RoundCountdown || Server.Countdown.Status == CountdownGameStatus.RoundInProgress) {
Player.Message(p, "A game is currently in progress - please wait until it is finished, or use '/cd cancel' to cancel the game"); return;
} else if (Server.Countdown.Status == CountdownGameStatus.Disabled) {
Player.Message(p, "Already disabled!!"); return;
} else {
foreach (Player pl in Server.Countdown.players)
foreach (Player pl in Server.Countdown.Players)
Player.Message(pl, "The countdown game was disabled.");
Server.Countdown.Reset(p, true);
Server.Countdown.Status = CountdownGameStatus.Disabled;
@ -262,7 +252,7 @@ namespace MCGalaxy.Commands.Fun {
void HandleCancel(Player p) {
if (!CheckExtraPerm(p, 2)) { MessageNeedExtra(p, 2); return; }
if (Server.Countdown.Status == CountdownGameStatus.AboutToStart || Server.Countdown.Status == CountdownGameStatus.InProgress) {
if (Server.Countdown.Status == CountdownGameStatus.RoundCountdown || Server.Countdown.Status == CountdownGameStatus.RoundInProgress) {
Server.Countdown.cancel = true;
Thread.Sleep(1500);
Player.Message(p, "Countdown has been canceled");
@ -270,7 +260,7 @@ namespace MCGalaxy.Commands.Fun {
} else if (Server.Countdown.Status == CountdownGameStatus.Disabled) {
Player.Message(p, "The game is disabled!!");
} else {
foreach (Player pl in Server.Countdown.players)
foreach (Player pl in Server.Countdown.Players)
Player.Message(pl, "The countdown game was canceled");
Server.Countdown.Reset(null, true);
}
@ -282,11 +272,11 @@ namespace MCGalaxy.Commands.Fun {
if (Server.Countdown.Status != CountdownGameStatus.Enabled) {
Player.Message(p, "Either a game is already in progress or it hasn't been enabled"); return;
}
if (Server.Countdown.players.Count < 2) {
if (Server.Countdown.Players.Count < 2) {
Player.Message(p, "Sorry, there aren't enough players to play."); return;
}
Server.Countdown.playersleftlist = Server.Countdown.players;
Server.Countdown.PlayersRemaining = Server.Countdown.Players;
CountdownGame game = Server.Countdown;
switch (par1) {
case "slow":
@ -313,9 +303,9 @@ namespace MCGalaxy.Commands.Fun {
switch (Server.Countdown.Status) {
case CountdownGameStatus.Disabled:
Player.Message(p, "Please enable countdown first."); break;
case CountdownGameStatus.AboutToStart:
case CountdownGameStatus.RoundCountdown:
Player.Message(p, "Sorry - The game is about to start"); break;
case CountdownGameStatus.InProgress:
case CountdownGameStatus.RoundInProgress:
Player.Message(p, "Sorry - The game is already in progress."); break;
default:
Player.Message(p, "Reseting");

View File

@ -22,8 +22,8 @@ using System.Threading;
namespace MCGalaxy.Games {
public sealed class CountdownGame : IGame {
public List<Player> players = new List<Player>();
public List<Player> playersleftlist = new List<Player>();
public List<Player> Players = new List<Player>();
public List<Player> PlayersRemaining = new List<Player>();
public List<SquarePos> squaresLeft = new List<SquarePos>();
public Level mapon;
@ -46,14 +46,14 @@ namespace MCGalaxy.Games {
switch (Status) {
case CountdownGameStatus.Disabled:
Player.Message(p, "Please enable Countdown first!!"); return;
case CountdownGameStatus.AboutToStart:
case CountdownGameStatus.RoundCountdown:
Player.Message(p, "Game is about to start"); return;
case CountdownGameStatus.InProgress:
case CountdownGameStatus.RoundInProgress:
Player.Message(p, "Game is already in progress"); return;
case CountdownGameStatus.Finished:
case CountdownGameStatus.RoundFinished:
Player.Message(p, "Game has finished"); return;
case CountdownGameStatus.Enabled:
Status = CountdownGameStatus.AboutToStart;
Status = CountdownGameStatus.RoundCountdown;
Thread.Sleep(2000); break;
}
@ -88,8 +88,8 @@ namespace MCGalaxy.Games {
mapon.ChatLevel("-----&b1%S-----"); Thread.Sleep(1000);
mapon.ChatLevel("GO!!!!!!!");
playersleftlist = players;
foreach (Player pl in players)
PlayersRemaining = Players;
foreach (Player pl in Players)
pl.InCountdown = true;
AfterStart();
Play();
@ -101,8 +101,8 @@ namespace MCGalaxy.Games {
} else {
SendFreezeMessages();
MessageAll("&bPlayers Frozen");
Status = CountdownGameStatus.InProgress;
foreach (Player pl in players)
Status = CountdownGameStatus.RoundInProgress;
foreach (Player pl in Players)
pl.CountdownSetFreezePos = true;
Thread.Sleep(500);
@ -113,7 +113,7 @@ namespace MCGalaxy.Games {
void SpawnPlayers(int x, int y, int z) {
Position pos = new Position(x, y, z);
foreach (Player pl in players) {
foreach (Player pl in Players) {
if (pl.level != mapon) {
pl.SendMessage("Sending you to the correct map.");
PlayerActions.ChangeMap(pl, mapon.name);
@ -163,8 +163,8 @@ namespace MCGalaxy.Games {
}
void RemoveRandomSquares() {
while (squaresLeft.Count > 0 && playersleftlist.Count != 0
&& (Status == CountdownGameStatus.InProgress || Status == CountdownGameStatus.Finished))
while (squaresLeft.Count > 0 && PlayersRemaining.Count != 0
&& (Status == CountdownGameStatus.RoundInProgress || Status == CountdownGameStatus.RoundFinished))
{
Random number = new Random();
int index = number.Next(squaresLeft.Count);
@ -172,8 +172,8 @@ namespace MCGalaxy.Games {
squaresLeft.RemoveAt(index);
RemoveSquare(nextsquare);
if (squaresLeft.Count % 10 == 0 && Status != CountdownGameStatus.Finished)
mapon.ChatLevel(squaresLeft.Count + " Squares Left and " + playersleftlist.Count + " Players left!!");
if (squaresLeft.Count % 10 == 0 && Status != CountdownGameStatus.RoundFinished)
mapon.ChatLevel(squaresLeft.Count + " Squares Left and " + PlayersRemaining.Count + " Players left!!");
if (cancel)
End(null);
}
@ -246,58 +246,58 @@ namespace MCGalaxy.Games {
Cuboid(maxX - 4, 4, 4, maxX - 4, 4, maxZ - 4, Block.air, mapon);
if (!freezemode) {
Status = CountdownGameStatus.InProgress;
Status = CountdownGameStatus.RoundInProgress;
}
}
public void Death(Player p) {
mapon.ChatLevel(p.ColoredName + " %Sis out of countdown!!");
p.InCountdown = false;
playersleftlist.Remove(p);
PlayersRemaining.Remove(p);
MessagePlayersLeft();
}
public void MessagePlayersLeft() {
switch (playersleftlist.Count) {
switch (PlayersRemaining.Count) {
case 1:
mapon.ChatLevel(playersleftlist[0].ColoredName + " %Sis the winner!!");
End(playersleftlist[0]);
mapon.ChatLevel(PlayersRemaining[0].ColoredName + " %Sis the winner!!");
End(PlayersRemaining[0]);
break;
case 2:
mapon.ChatLevel("Only 2 Players left:");
mapon.ChatLevel(playersleftlist[0].ColoredName + " %Sand " + playersleftlist[1].ColoredName);
mapon.ChatLevel(PlayersRemaining[0].ColoredName + " %Sand " + PlayersRemaining[1].ColoredName);
break;
case 5:
mapon.ChatLevel("Only 5 Players left:");
foreach (Player pl in playersleftlist) {
foreach (Player pl in PlayersRemaining) {
mapon.ChatLevel(pl.ColoredName);
Thread.Sleep(500);
}
break;
default:
mapon.ChatLevel("Now there are " + playersleftlist.Count + " players left!!");
mapon.ChatLevel("Now there are " + PlayersRemaining.Count + " players left!!");
break;
}
}
void End(Player winner) {
squaresLeft.Clear();
Status = CountdownGameStatus.Finished;
playersleftlist.Clear();
Status = CountdownGameStatus.RoundFinished;
PlayersRemaining.Clear();
if (winner != null) {
winner.SendMessage("Congratulations!! You won!!!");
Command.all.Find("spawn").Use(winner, "");
winner.InCountdown = false;
} else {
foreach (Player pl in players) {
foreach (Player pl in Players) {
Player.Message(pl, "The countdown game was canceled!");
Command.all.Find("spawn").Use(pl, "");
}
Chat.MessageGlobal("The countdown game was canceled!!");
Status = CountdownGameStatus.Enabled;
playersleftlist.Clear();
players.Clear();
PlayersRemaining.Clear();
Players.Clear();
squaresLeft.Clear();
Reset(null, true);
cancel = false;
@ -305,7 +305,7 @@ namespace MCGalaxy.Games {
}
public void Reset(Player p, bool all) {
if (!(Status == CountdownGameStatus.Enabled || Status == CountdownGameStatus.Finished || Status == CountdownGameStatus.Disabled)) {
if (!(Status == CountdownGameStatus.Enabled || Status == CountdownGameStatus.RoundFinished || Status == CountdownGameStatus.Disabled)) {
switch (Status) {
case CountdownGameStatus.Disabled:
Player.Message(p, "Please enable the game first"); return;
@ -323,7 +323,7 @@ namespace MCGalaxy.Games {
if (!all) {
Player.Message(p, "The Countdown map has been reset");
if (Status == CountdownGameStatus.Finished)
if (Status == CountdownGameStatus.RoundFinished)
Player.Message(p, "You do not need to re-enable it");
Status = CountdownGameStatus.Enabled;
@ -336,16 +336,16 @@ namespace MCGalaxy.Games {
} else {
Player.Message(pl, "You've been removed from countdown because you aren't on the map");
pl.playerofcountdown = false;
players.Remove(pl);
Players.Remove(pl);
}
}
} else {
Player.Message(p, "Countdown has been reset");
if (Status == CountdownGameStatus.Finished)
if (Status == CountdownGameStatus.RoundFinished)
Player.Message(p, "You do not need to re-enable it");
Status = CountdownGameStatus.Enabled;
playersleftlist.Clear();
players.Clear();
PlayersRemaining.Clear();
Players.Clear();
squaresLeft.Clear();
speed = 750;
@ -394,8 +394,8 @@ namespace MCGalaxy.Games {
public override void PlayerJoinedGame(Player p) {
if (!Server.Countdown.players.Contains(p)) {
Server.Countdown.players.Add(p);
if (!Server.Countdown.Players.Contains(p)) {
Server.Countdown.Players.Add(p);
Player.Message(p, "You've joined the Countdown game!!");
Chat.MessageGlobal("{0} has joined Countdown!!", p.name);
if (p.level != Server.Countdown.mapon)
@ -409,13 +409,26 @@ namespace MCGalaxy.Games {
public override void PlayerLeftGame(Player p) {
p.InCountdown = false;
p.playerofcountdown = false;
players.Remove(p);
playersleftlist.Remove(p);
Players.Remove(p);
PlayersRemaining.Remove(p);
MessagePlayersLeft();
}
}
public enum CountdownGameStatus {
Disabled, Enabled, AboutToStart, InProgress, Finished,
/// <summary> Countdown is not running. </summary>
Disabled,
/// <summary> Countdown is running, but no round has been started at all yet. </summary>
Enabled,
/// <summary> Timer is counting down to start of round. </summary>
RoundCountdown,
/// <summary> Round is in progress. </summary>
RoundInProgress,
/// <summary> Round has ended. </summary>
RoundFinished,
}
}

View File

@ -38,7 +38,7 @@ namespace MCGalaxy.Games {
void HandlePlayerMove(Player p, Position next, byte yaw, byte pitch) {
if (!p.InCountdown || Game.Status != CountdownGameStatus.InProgress || !Game.freezemode)
if (!p.InCountdown || Game.Status != CountdownGameStatus.RoundInProgress || !Game.freezemode)
return;
if (p.CountdownSetFreezePos) {
@ -60,13 +60,13 @@ namespace MCGalaxy.Games {
}
void HandlePlayerDisconnect(Player p, string reason) {
if (!Game.players.Contains(p)) return;
if (!Game.Players.Contains(p)) return;
if (Game.playersleftlist.Contains(p)) {
Game.mapon.ChatLevel(p.ColoredName + " %Slogged out and so is out of countdown");
if (Game.PlayersRemaining.Contains(p)) {
Game.mapon.ChatLevel(p.ColoredName + " %Slogged out, and so is out of countdown");
Game.PlayerLeftGame(p);
}
Game.players.Remove(p);
Game.Players.Remove(p);
}
}
}

View File

@ -487,7 +487,7 @@ namespace MCGalaxy {
//}
Game.team.SpawnPlayer(this);
//this.health = 100;
} else if ( Server.Countdown.playersleftlist.Contains(this) ) {
} else if ( Server.Countdown.PlayersRemaining.Contains(this) ) {
Server.Countdown.Death(this);
Command.all.Find("spawn").Use(this, "");
} else if ( PlayingTntWars ) {

View File

@ -22,88 +22,90 @@ using System.Text;
using MCGalaxy.Tasks;
namespace MCGalaxy {
public static class FileLogger {
public static string LogPath { get { return msgPath; } }
public static string ErrorLogPath { get { return errPath; } }
public static class FileLogger {
public static string LogPath { get { return msgPath; } }
public static string ErrorLogPath { get { return errPath; } }
static bool disposed;
static DateTime last;
static bool disposed;
static DateTime last;
static object logLock = new object();
static string errPath, msgPath;
static Queue<string> errCache = new Queue<string>(), msgCache = new Queue<string>();
static SchedulerTask logTask;
static object logLock = new object();
static string errPath, msgPath;
static Queue<string> errCache = new Queue<string>(), msgCache = new Queue<string>();
static SchedulerTask logTask;
public static void Init() {
if (!Directory.Exists("logs")) Directory.CreateDirectory("logs");
if (!Directory.Exists("logs/errors")) Directory.CreateDirectory("logs/errors");
UpdatePaths();
Logger.LogHandler += LogMessage;
logTask = Server.MainScheduler.QueueRepeat(Flush, null,
TimeSpan.FromMilliseconds(500));
}
// Update paths only if a new date
static void UpdatePaths() {
DateTime now = DateTime.Now;
if (now.Year == last.Year && now.Month == last.Month && now.Day == last.Day) return;
last = now;
msgPath = "logs/" + now.ToString("yyyy-MM-dd").Replace("/", "-") + ".txt";
errPath = "logs/errors/" + now.ToString("yyyy-MM-dd").Replace("/", "-") + "error.log";
}
static void LogMessage(LogType type, string message) {
if (String.IsNullOrEmpty(message)) return;
if (type == LogType.Error) {
StringBuilder sb = new StringBuilder();
sb.AppendLine("----" + DateTime.Now + " ----");
sb.AppendLine(message);
sb.Append('-', 25);
string output = sb.ToString();
lock (logLock) errCache.Enqueue(output);
} else {
string now = DateTime.Now.ToString("(HH:mm:ss) ");
lock (logLock) msgCache.Enqueue(now + message);
}
}
public static void Init() {
if (!Directory.Exists("logs")) Directory.CreateDirectory("logs");
if (!Directory.Exists("logs/errors")) Directory.CreateDirectory("logs/errors");
UpdatePaths();
Logger.LogHandler += LogMessage;
logTask = Server.MainScheduler.QueueRepeat(Flush, null,
TimeSpan.FromMilliseconds(500));
}
// Update paths only if a new date
static void UpdatePaths() {
DateTime now = DateTime.Now;
if (now.Year == last.Year && now.Month == last.Month && now.Day == last.Day) return;
last = now;
msgPath = "logs/" + now.ToString("yyyy-MM-dd").Replace("/", "-") + ".txt";
errPath = "logs/errors/" + now.ToString("yyyy-MM-dd").Replace("/", "-") + "error.log";
}
static void LogMessage(LogType type, string message) {
if (String.IsNullOrEmpty(message)) return;
if (type == LogType.Error) {
StringBuilder sb = new StringBuilder();
sb.AppendLine("----" + DateTime.Now + " ----");
sb.AppendLine(message);
sb.Append('-', 25);
string output = sb.ToString();
lock (logLock) errCache.Enqueue(output);
message = "!!!Error! See " + ErrorLogPath + " for more information.";
}
string now = DateTime.Now.ToString("(HH:mm:ss) ");
lock (logLock) msgCache.Enqueue(now + message);
}
public static void Flush(SchedulerTask task) {
lock (logLock) {
if (errCache.Count > 0 || msgCache.Count > 0) UpdatePaths();
if (errCache.Count > 0) FlushCache(errPath, errCache);
if (msgCache.Count > 0) FlushCache(msgPath, msgCache);
}
}
public static void Flush(SchedulerTask task) {
lock (logLock) {
if (errCache.Count > 0 || msgCache.Count > 0) UpdatePaths();
if (errCache.Count > 0) FlushCache(errPath, errCache);
if (msgCache.Count > 0) FlushCache(msgPath, msgCache);
}
}
static void FlushCache(string path, Queue<string> cache) {
//TODO: not happy about constantly opening and closing a stream like this but I suppose its ok (Pidgeon)
using (StreamWriter w = new StreamWriter(path, true)) {
while (cache.Count > 0) {
string item = cache.Dequeue();
item = Colors.StripColors(item);
w.WriteLine(item);
}
}
}
static void FlushCache(string path, Queue<string> cache) {
//TODO: not happy about constantly opening and closing a stream like this but I suppose its ok (Pidgeon)
using (StreamWriter w = new StreamWriter(path, true)) {
while (cache.Count > 0) {
string item = cache.Dequeue();
item = Colors.StripColors(item);
w.WriteLine(item);
}
}
}
public static void Dispose() {
if (disposed) return;
disposed = true;
Server.MainScheduler.Cancel(logTask);
lock (logLock) {
if (errCache.Count > 0)
FlushCache(errPath, errCache);
msgCache.Clear();
}
}
}
public static void Dispose() {
if (disposed) return;
disposed = true;
Server.MainScheduler.Cancel(logTask);
lock (logLock) {
if (errCache.Count > 0)
FlushCache(errPath, errCache);
msgCache.Clear();
}
}
}
}