Reduce award saving overhead, make award saving threadsafe.

This commit is contained in:
UnknownShadow200 2016-09-04 20:13:34 +10:00
parent 40cb0e0746
commit 1d8a4838ac
3 changed files with 21 additions and 12 deletions

View File

@ -46,6 +46,7 @@ namespace MCGalaxy.Commands {
if (Awards.GiveAward(plName, award)) {
Chat.MessageAll("{0} %Swas awarded: &b{1}",
PlayerInfo.GetColoredName(p, plName), award);
Awards.SavePlayers();
} else {
Player.Message(p, "The player already has that award."); return;
}
@ -53,11 +54,11 @@ namespace MCGalaxy.Commands {
if (Awards.TakeAward(plName, award)) {
Chat.MessageAll("{0} %Shad their &b{1} %Saward removed",
PlayerInfo.GetColoredName(p, plName), award);
Awards.SavePlayers();
} else {
Player.Message(p, "The player didn't have the award you tried to take"); return;
}
}
Awards.Save();
}
public override void Help(Player p) {

View File

@ -41,15 +41,16 @@ namespace MCGalaxy.Commands {
Player.Message(p, "This award already exists."); return;
} else {
Chat.MessageAll("Award added: &6{0} : {1}", args[0], args[1]);
Awards.SaveAwards();
}
} else {
if (!Awards.Remove(args[1])) {
Player.Message(p, "This award does not exist."); return;
} else {
Chat.MessageAll("Award removed: &6{0}", args[1]);
Awards.SaveAwards();
}
}
Awards.Save();
}
public override void Help(Player p) {

View File

@ -54,7 +54,6 @@ namespace MCGalaxy {
PropertiesFile.Read("text/awardsList.txt", AwardsListLineProcessor, ':');
PlayerAwards = new List<PlayerAward>();
PropertiesFile.Read("text/playerAwards.txt", PlayerAwardsLineProcessor, ':');
Save();
}
static void AwardsListLineProcessor(string key, string value) {
@ -76,8 +75,11 @@ namespace MCGalaxy {
PlayerAwards.Add(pl);
}
public static void Save() {
using (CP437Writer w = new CP437Writer("text/awardsList.txt")) {
static readonly object awardLock = new object();
public static void SaveAwards() {
lock (awardLock)
using (CP437Writer w = new CP437Writer("text/awardsList.txt"))
{
w.WriteLine("# This is a full list of awards. The server will load these and they can be awarded as you please");
w.WriteLine("# Format is:");
w.WriteLine("# AwardName : Description of award goes after the colon");
@ -85,8 +87,13 @@ namespace MCGalaxy {
foreach (Award award in AwardsList)
w.WriteLine(award.Name + " : " + award.Description);
}
}
using (StreamWriter w = new StreamWriter("text/playerAwards.txt")) {
static readonly object playerLock = new object();
public static void SavePlayers() {
lock (playerLock)
using (StreamWriter w = new StreamWriter("text/playerAwards.txt"))
{
foreach (PlayerAward pA in PlayerAwards)
w.WriteLine(pA.Name.ToLower() + " : " + pA.Awards.Join(","));
}