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

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

View File

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

View File

@ -54,7 +54,6 @@ namespace MCGalaxy {
PropertiesFile.Read("text/awardsList.txt", AwardsListLineProcessor, ':'); PropertiesFile.Read("text/awardsList.txt", AwardsListLineProcessor, ':');
PlayerAwards = new List<PlayerAward>(); PlayerAwards = new List<PlayerAward>();
PropertiesFile.Read("text/playerAwards.txt", PlayerAwardsLineProcessor, ':'); PropertiesFile.Read("text/playerAwards.txt", PlayerAwardsLineProcessor, ':');
Save();
} }
static void AwardsListLineProcessor(string key, string value) { static void AwardsListLineProcessor(string key, string value) {
@ -76,8 +75,11 @@ namespace MCGalaxy {
PlayerAwards.Add(pl); PlayerAwards.Add(pl);
} }
public static void Save() { static readonly object awardLock = new object();
using (CP437Writer w = new CP437Writer("text/awardsList.txt")) { 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("# 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("# Format is:");
w.WriteLine("# AwardName : Description of award goes after the colon"); w.WriteLine("# AwardName : Description of award goes after the colon");
@ -85,10 +87,15 @@ namespace MCGalaxy {
foreach (Award award in AwardsList) foreach (Award award in AwardsList)
w.WriteLine(award.Name + " : " + award.Description); 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) foreach (PlayerAward pA in PlayerAwards)
w.WriteLine(pA.Name.ToLower() + " : " + pA.Awards.Join(",")); w.WriteLine(pA.Name.ToLower() + " : " + pA.Awards.Join(","));
} }
} }
#endregion #endregion
@ -107,7 +114,7 @@ namespace MCGalaxy {
pl.Awards.Add(name); pl.Awards.Add(name);
return true; return true;
} }
PlayerAward newPl; PlayerAward newPl;
newPl.Name = playerName; newPl.Name = playerName;
newPl.Awards = new List<string>(); newPl.Awards = new List<string>();