aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael VanOverbeek <[email protected]>2017-06-03 11:48:04 -0400
committerGitHub <[email protected]>2017-06-03 11:48:04 -0400
commit4faeb54225919bab1692859b4fe9d9b65f3bedea (patch)
tree2ff98b9d969edb5ea67e439c833e9adee739b722
parentcc55af0c8b4d14053bfb46ec14c3e1887d375f7d (diff)
parent7fe5d790dc9d73056e86af8116ba4db9674fd612 (diff)
downloadshiftos_thereturn-4faeb54225919bab1692859b4fe9d9b65f3bedea.tar.gz
shiftos_thereturn-4faeb54225919bab1692859b4fe9d9b65f3bedea.tar.bz2
shiftos_thereturn-4faeb54225919bab1692859b4fe9d9b65f3bedea.zip
Merge pull request #125 from RogueAI42/master
fixed shiftorium
-rw-r--r--ShiftOS.Objects/Save.cs75
-rw-r--r--ShiftOS.WinForms/Applications/ShiftoriumFrontend.Designer.cs3
-rw-r--r--ShiftOS.WinForms/Applications/ShiftoriumFrontend.cs186
-rw-r--r--ShiftOS_TheReturn/SaveSystem.cs11
4 files changed, 136 insertions, 139 deletions
diff --git a/ShiftOS.Objects/Save.cs b/ShiftOS.Objects/Save.cs
index cbd77e2..e0282e8 100644
--- a/ShiftOS.Objects/Save.cs
+++ b/ShiftOS.Objects/Save.cs
@@ -26,8 +26,7 @@ using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using System.Threading;
namespace ShiftOS.Objects
{
@@ -41,33 +40,77 @@ namespace ShiftOS.Objects
[Obsolete("This save variable is no longer used in Beta 2.4 and above of ShiftOS. Please use ShiftOS.Engine.SaveSystem.CurrentUser.Username to access the current user's username.")]
public string Username { get; set; }
- private ulong _cp = 0;
+ private List<Action> _setCpCallbacks = new List<Action>(); // everything in this list is called by Codepoints.set() and syncCp().
+ private ulong _cp = 0; // locally cached codepoints counter
+ private Object _cpLock = new Object(); // locked when modifying or reading the codepoints counter
+ private Object _webLock = new Object(); // locked when communicating with the server
+ private Timer _updTimer; // timer to start a new sync thread every 5 minutes
+
+ // Sync local Codepoints count with the server.
+ public void syncCp()
+ {
+ new Thread(() =>
+ {
+ lock (_cpLock)
+ {
+ lock (_webLock)
+ {
+ var uc = new ShiftOS.Unite.UniteClient("", UniteAuthToken);
+ _cp = uc.GetCodepoints();
+ }
+ }
+ foreach (Action a in _setCpCallbacks)
+ a();
+ }).Start();
+ }
+
+ // we have to write these wrapper functions so we can keep _setCpCallbacks private,
+ // so that it doesn't get serialised
+ public void addSetCpCallback(Action callback)
+ {
+ _setCpCallbacks.Add(callback);
+ }
+
+ public void removeSetCpCallback(Action callback)
+ {
+ _setCpCallbacks.Remove(callback);
+ }
public ulong Codepoints
{
get
{
- try
- {
- var uc = new ShiftOS.Unite.UniteClient("", UniteAuthToken);
- return uc.GetCodepoints();
- }
- catch
+ if (_updTimer == null)
+ _updTimer = new Timer((o) => syncCp(), null, 0, 300000);
+ lock (_cpLock)
{
return _cp;
}
}
set
{
- try
+ lock (_cpLock)
+ {
+ _cp = value;
+ new Thread(() =>
{
- var uc = new ShiftOS.Unite.UniteClient("", UniteAuthToken);
- uc.SetCodepoints(value);
- }
- catch
+ lock (_webLock)
+ {
+ try
+ {
+ var uc = new ShiftOS.Unite.UniteClient("", UniteAuthToken);
+ uc.SetCodepoints(value);
+ }
+ catch
+ { }
+ }
+ })
{
- _cp = value;
- }
+ IsBackground = false
+ }.Start();
+ }
+ foreach (Action a in _setCpCallbacks)
+ a();
}
}
diff --git a/ShiftOS.WinForms/Applications/ShiftoriumFrontend.Designer.cs b/ShiftOS.WinForms/Applications/ShiftoriumFrontend.Designer.cs
index dc107c4..e4e493e 100644
--- a/ShiftOS.WinForms/Applications/ShiftoriumFrontend.Designer.cs
+++ b/ShiftOS.WinForms/Applications/ShiftoriumFrontend.Designer.cs
@@ -189,7 +189,6 @@ namespace ShiftOS.WinForms.Applications
this.lblcategorytext.TabIndex = 2;
this.lblcategorytext.Text = "No Upgrades";
this.lblcategorytext.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
- this.lblcategorytext.Click += new System.EventHandler(this.lblcategorytext_Click);
//
// btncat_forward
//
@@ -226,7 +225,6 @@ namespace ShiftOS.WinForms.Applications
this.lbcodepoints.Size = new System.Drawing.Size(135, 13);
this.lbcodepoints.TabIndex = 3;
this.lbcodepoints.Text = "You have: %cp Codepoints";
- this.lbcodepoints.Click += new System.EventHandler(this.lbcodepoints_Click);
//
// label1
//
@@ -280,7 +278,6 @@ namespace ShiftOS.WinForms.Applications
this.ForeColor = System.Drawing.Color.LightGreen;
this.Name = "ShiftoriumFrontend";
this.Size = new System.Drawing.Size(782, 427);
- this.Load += new System.EventHandler(this.Shiftorium_Load);
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.pnlupgradeactions.ResumeLayout(false);
diff --git a/ShiftOS.WinForms/Applications/ShiftoriumFrontend.cs b/ShiftOS.WinForms/Applications/ShiftoriumFrontend.cs
index d6b014d..0ae0803 100644
--- a/ShiftOS.WinForms/Applications/ShiftoriumFrontend.cs
+++ b/ShiftOS.WinForms/Applications/ShiftoriumFrontend.cs
@@ -47,19 +47,22 @@ namespace ShiftOS.WinForms.Applications
public partial class ShiftoriumFrontend : UserControl, IShiftOSWindow
{
public int CategoryId = 0;
- public static System.Timers.Timer timer100;
+ private string[] cats = backend.GetCategories();
+ private ShiftoriumUpgrade[] avail;
+ public void updatecounter()
+ {
+ Desktop.InvokeOnWorkerThread(() => { lbcodepoints.Text = $"You have {SaveSystem.CurrentSave.Codepoints} Codepoints."; });
+ }
+
public ShiftoriumFrontend()
{
- cp_update = new System.Windows.Forms.Timer();
- cp_update.Tick += (o, a) =>
- {
- lbcodepoints.Text = $"You have {SaveSystem.CurrentSave.Codepoints} Codepoints.";
- };
- cp_update.Interval = 100;
InitializeComponent();
- PopulateShiftorium();
+ SaveSystem.CurrentSave.addSetCpCallback(updatecounter);
+ updatecounter();
+ Populate();
+ SetList();
lbupgrades.SelectedIndexChanged += (o, a) =>
{
try
@@ -82,84 +85,57 @@ namespace ShiftOS.WinForms.Applications
public void SelectUpgrade(string name)
{
btnbuy.Show();
- var upg = upgrades[name];
+ var upg = upgrades[CategoryId][name];
lbupgradetitle.Text = Localization.Parse(upg.Name);
lbupgradedesc.Text = Localization.Parse(upg.Description);
}
- Dictionary<string, ShiftoriumUpgrade> upgrades = new Dictionary<string, ShiftoriumUpgrade>();
-
- public void PopulateShiftorium()
+ Dictionary<string, ShiftoriumUpgrade>[] upgrades;
+
+ private void Populate()
{
- var t = new Thread(() =>
+ upgrades = new Dictionary<string, ShiftoriumUpgrade>[cats.Length];
+ int numComplete = 0;
+ avail = backend.GetAvailable();
+ foreach (var it in cats.Select((catName, catId) => new { catName, catId }))
{
- try
+ var upl = new Dictionary<string, ShiftoriumUpgrade>();
+ upgrades[it.catId] = upl;
+ var t = new Thread((tupobj) =>
{
- Desktop.InvokeOnWorkerThread(() =>
- {
- lbnoupgrades.Hide();
- lbupgrades.Items.Clear();
- upgrades.Clear();
- Timer();
- });
-
- foreach (var upg in backend.GetAvailable().Where(x => x.Category == backend.GetCategories()[CategoryId]))
- {
- string name = Localization.Parse(upg.Name) + " - " + upg.Cost.ToString() + "CP";
- upgrades.Add(name, upg);
- Desktop.InvokeOnWorkerThread(() =>
- {
- lbupgrades.Items.Add(name);
- });
- }
-
- if (lbupgrades.Items.Count == 0)
- {
- Desktop.InvokeOnWorkerThread(() =>
- {
- lbnoupgrades.Show();
- lbnoupgrades.Location = new Point(
- (lbupgrades.Width - lbnoupgrades.Width) / 2,
- lbupgrades.Top + (lbupgrades.Height - lbnoupgrades.Height) / 2
- );
- });
- }
- else
- {
- Desktop.InvokeOnWorkerThread(() =>
- {
- lbnoupgrades.Hide();
- });
- }
-
- Desktop.InvokeOnWorkerThread(() =>
- {
- try
- {
- lblcategorytext.Text = Shiftorium.GetCategories()[CategoryId];
- btncat_back.Visible = (CategoryId > 0);
- btncat_forward.Visible = (CategoryId < backend.GetCategories().Length - 1);
- }
- catch
- {
+ foreach (var upg in avail.Where(x => x.Category == it.catName))
+ upl.Add(Localization.Parse(upg.Name) + " - " + upg.Cost.ToString() + "CP", upg);
+ numComplete++;
+ });
+ t.Start();
+ }
+ while (numComplete < cats.Length) { } // wait for all threads to finish their job
+ }
- }
- });
- }
- catch
- {
- Desktop.InvokeOnWorkerThread(() =>
- {
- lbnoupgrades.Show();
- lbnoupgrades.Location = new Point(
- (lbupgrades.Width - lbnoupgrades.Width) / 2,
- lbupgrades.Top + (lbupgrades.Height - lbnoupgrades.Height) / 2
- );
- });
- }
- });
- t.IsBackground = true;
- t.Start();
+ private void SetList()
+ {
+ lbnoupgrades.Hide();
+ lbupgrades.Items.Clear();
+ try
+ {
+ lbupgrades.Items.AddRange(upgrades[CategoryId].Keys.ToArray());
+ }
+ catch
+ {
+ Engine.Infobox.Show("Shiftorium Machine Broke", "Category ID " + CategoryId.ToString() + " is invalid, modulo is broken, and the world is doomed. Please tell Declan about this.");
+ return;
+ }
+ if (lbupgrades.Items.Count == 0)
+ {
+ lbnoupgrades.Show();
+ lbnoupgrades.Location = new Point(
+ (lbupgrades.Width - lbnoupgrades.Width) / 2,
+ lbupgrades.Top + (lbupgrades.Height - lbnoupgrades.Height) / 2
+ );
+ }
+ else
+ lbnoupgrades.Hide();
+ lblcategorytext.Text = cats[CategoryId];
}
public static bool UpgradeInstalled(string upg)
@@ -218,8 +194,9 @@ namespace ShiftOS.WinForms.Applications
Dictionary<string, ulong> UpgradesToBuy = new Dictionary<string, ulong>();
foreach (var itm in lbupgrades.SelectedItems)
{
- cpCost += upgrades[itm.ToString()].Cost;
- UpgradesToBuy.Add(upgrades[itm.ToString()].ID, upgrades[itm.ToString()].Cost);
+ var upg = upgrades[CategoryId][itm.ToString()];
+ cpCost += upg.Cost;
+ UpgradesToBuy.Add(upg.ID, upg.Cost);
}
if (SaveSystem.CurrentSave.Codepoints < cpCost)
{
@@ -230,7 +207,6 @@ namespace ShiftOS.WinForms.Applications
{
foreach(var upg in UpgradesToBuy)
{
- SaveSystem.CurrentSave.Codepoints -= upg.Value;
if (SaveSystem.CurrentSave.Upgrades.ContainsKey(upg.Key))
{
SaveSystem.CurrentSave.Upgrades[upg.Key] = true;
@@ -242,20 +218,15 @@ namespace ShiftOS.WinForms.Applications
SaveSystem.SaveGame();
backend.InvokeUpgradeInstalled();
}
+ SaveSystem.CurrentSave.Codepoints -= cpCost;
}
backend.Silent = false;
- PopulateShiftorium();
btnbuy.Hide();
}
- private void Shiftorium_Load(object sender, EventArgs e) {
-
- }
-
public void OnLoad()
{
- cp_update.Start();
lbnoupgrades.Hide();
}
@@ -264,12 +235,9 @@ namespace ShiftOS.WinForms.Applications
}
- System.Windows.Forms.Timer cp_update = new System.Windows.Forms.Timer();
-
public bool OnUnload()
{
- cp_update.Stop();
- cp_update = null;
+ SaveSystem.CurrentSave.removeSetCpCallback(updatecounter);
return true;
}
@@ -277,44 +245,26 @@ namespace ShiftOS.WinForms.Applications
{
lbupgrades.SelectionMode = (UpgradeInstalled("shiftorium_gui_bulk_buy") == true) ? SelectionMode.MultiExtended : SelectionMode.One;
lbcodepoints.Visible = Shiftorium.UpgradeInstalled("shiftorium_gui_codepoints_display");
+ Populate();
+ SetList();
}
- private void lbcodepoints_Click(object sender, EventArgs e)
+ private void moveCat(short direction) // direction is -1 to move backwards or 1 to move forwards
{
-
- }
-
- void Timer()
- {
- timer100 = new System.Timers.Timer();
- timer100.Interval = 2000;
- //CLARIFICATION: What is this supposed to do? - Michael
- //timer100.Elapsed += ???;
- timer100.AutoReset = true;
- timer100.Enabled = true;
+ CategoryId += direction;
+ CategoryId %= cats.Length;
+ if (CategoryId < 0) CategoryId += cats.Length; // fix modulo on negatives
+ SetList();
}
private void btncat_back_Click(object sender, EventArgs e)
{
- if(CategoryId > 0)
- {
- CategoryId--;
- PopulateShiftorium();
- }
+ moveCat(-1);
}
private void btncat_forward_Click(object sender, EventArgs e)
{
- if(CategoryId < backend.GetCategories().Length - 1)
- {
- CategoryId++;
- PopulateShiftorium();
- }
- }
-
- private void lblcategorytext_Click(object sender, EventArgs e)
- {
-
+ moveCat(1);
}
}
}
diff --git a/ShiftOS_TheReturn/SaveSystem.cs b/ShiftOS_TheReturn/SaveSystem.cs
index 395db85..155f002 100644
--- a/ShiftOS_TheReturn/SaveSystem.cs
+++ b/ShiftOS_TheReturn/SaveSystem.cs
@@ -570,8 +570,15 @@ namespace ShiftOS.Engine
Console.Write("{SE_SAVING}... ");
if (SaveSystem.CurrentSave != null)
{
- Utils.WriteAllText(Paths.GetPath("user.dat"), CurrentSave.UniteAuthToken);
- ServerManager.SendMessage("mud_save", JsonConvert.SerializeObject(CurrentSave, Formatting.Indented));
+ Utils.WriteAllText(Paths.GetPath("user.dat"), CurrentSave.UniteAuthToken);
+ var serialisedSaveFile = JsonConvert.SerializeObject(CurrentSave, Formatting.Indented);
+ new Thread(() =>
+ {
+ // please don't do networking on the main thread if you're just going to
+ // discard the response, it's extremely slow
+ ServerManager.SendMessage("mud_save", serialisedSaveFile);
+ })
+ { IsBackground = false }.Start();
}
if (!Shiftorium.Silent)
Console.WriteLine(" ...{DONE}.");