Skip to content

Commit

Permalink
Co-op related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DarwinBaker committed Mar 6, 2022
1 parent b8fe612 commit aec05a6
Show file tree
Hide file tree
Showing 35 changed files with 165 additions and 128 deletions.
2 changes: 1 addition & 1 deletion AATool/Data/Categories/AdventuringTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class AdventuringTime : SingleAdvancement
};

public override IEnumerable<string> GetSupportedVersions() => SupportedVersions;
public override IEnumerable<Objective> GetOverlayObjectives() => Tracker.Achievements.All.Values;
public override IEnumerable<Objective> GetOverlayObjectives() => Tracker.Achievements.AllAdvancements.Values;

public override int GetTargetCount() => this.Requirement?.Criteria.Count ?? 0;
public override int GetCompletedCount() => this.Requirement?.Criteria.MostCompleted ?? 0;
Expand Down
2 changes: 1 addition & 1 deletion AATool/Data/Categories/AllAchievements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class AllAchievements : Category
};

public override IEnumerable<string> GetSupportedVersions() => SupportedVersions;
public override IEnumerable<Objective> GetOverlayObjectives() => Tracker.Achievements.All.Values;
public override IEnumerable<Objective> GetOverlayObjectives() => Tracker.Achievements.AllAdvancements.Values;

public override int GetTargetCount() => Tracker.Achievements.Count;
public override int GetCompletedCount() => Tracker.Achievements.CompletedCount;
Expand Down
2 changes: 1 addition & 1 deletion AATool/Data/Categories/AllAdvancements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AllAdvancements : Category
};

public override IEnumerable<string> GetSupportedVersions() => SupportedVersions;
public override IEnumerable<Objective> GetOverlayObjectives() => Tracker.Advancements.All.Values;
public override IEnumerable<Objective> GetOverlayObjectives() => Tracker.Advancements.AllAdvancements.Values;

public override int GetCompletedCount() => Tracker.Advancements.CompletedCount;
public override int GetTargetCount() => Tracker.Advancements.Count;
Expand Down
6 changes: 3 additions & 3 deletions AATool/Data/Objectives/AchievementManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ public override void RefreshObjectives()
{
//recursively build achievement tree
this.Root = new Achievement(document.DocumentElement);
this.Root.GetAllChildrenRecursive(this.All);
this.Root.GetAllChildrenRecursive(this.AllAdvancements);

//add sub-criteria
foreach (Advancement advancement in this.All.Values)
foreach (Advancement advancement in this.AllAdvancements.Values)
{
if (!advancement.HasCriteria)
continue;

foreach (KeyValuePair<string, Criterion> criterion in advancement.Criteria.All)
this.Criteria[(advancement.Id, criterion.Key)] = criterion.Value;
this.AllCriteria[(advancement.Id, criterion.Key)] = criterion.Value;
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions AATool/Data/Objectives/Advancement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@ public void Designate(Uuid id)
if (id == Uuid.Empty)
return;

this.DesignatedPlayer = id;
if (Server.TryGet(out Server server))
server.DesignatePlayer(this.Id, this.DesignatedPlayer);
if (id != this.DesignatedPlayer)
{
this.DesignatedPlayer = id;
Tracker.InvalidateDesignations();

if (Server.TryGet(out Server server))
server.DesignatePlayer(this.Id, this.DesignatedPlayer);
}
}

public Uuid GetDesignatedPlayer()
Expand Down
50 changes: 36 additions & 14 deletions AATool/Data/Objectives/AdvancementManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,39 @@ namespace AATool.Data.Objectives
{
public class AdvancementManifest : IManifest
{
public readonly Dictionary<string, Advancement> All = new ();
public readonly Dictionary<string, Advancement> AllAdvancements = new ();
public readonly Dictionary<string, Advancement> RemainingAdvancements = new ();
public readonly Dictionary<string, HashSet<Advancement>> Groups = new ();
public readonly Dictionary<(string adv, string crit), Criterion> Criteria = new ();
public readonly Dictionary<(string adv, string crit), Criterion> AllCriteria = new ();
public readonly Dictionary<(string adv, string crit), Criterion> RemainingCriteria = new ();

public int CompletedCount { get; private set; }

public int Count => this.All.Count;
public int Count => this.AllAdvancements.Count;

public AdvancementManifest()
{
this.All = new Dictionary<string, Advancement>();
this.AllAdvancements = new Dictionary<string, Advancement>();
this.Groups = new Dictionary<string, HashSet<Advancement>>();
this.Criteria = new Dictionary<(string adv, string crit), Criterion>();
this.AllCriteria = new Dictionary<(string adv, string crit), Criterion>();
}

public bool TryGet(string advId, out Advancement advancement) =>
this.All.TryGetValue(advId, out advancement);
this.AllAdvancements.TryGetValue(advId, out advancement);

public bool TryGet(string advId, string critId, out Criterion criterion) =>
this.Criteria.TryGetValue((advId, critId), out criterion);
this.AllCriteria.TryGetValue((advId, critId), out criterion);

public bool TryGet(string groupId, out HashSet<Advancement> group) =>
this.Groups.TryGetValue(groupId, out group);

public void ClearObjectives()
{
this.Groups.Clear();
this.All.Clear();
this.Criteria.Clear();
this.AllAdvancements.Clear();
this.RemainingAdvancements.Clear();
this.AllCriteria.Clear();
this.RemainingCriteria.Clear();
this.CompletedCount = 0;
}

Expand Down Expand Up @@ -77,25 +81,43 @@ private void ParseFile(string file)
private void RequireAdvancement(XmlNode node, HashSet<Advancement> group)
{
var advancement = new Advancement(node);
this.All[advancement.Id] = advancement;
this.AllAdvancements[advancement.Id] = advancement;
group.Add(advancement);

if (advancement.HasCriteria)
{
foreach (KeyValuePair<string, Criterion> criterion in advancement.Criteria.All)
this.Criteria[(advancement.Id, criterion.Key)] = criterion.Value;
this.AllCriteria[(advancement.Id, criterion.Key)] = criterion.Value;
}
}

public void SetState(WorldState progress)
{
this.RemainingAdvancements.Clear();
this.CompletedCount = 0;
foreach (Advancement advancement in this.All.Values)

foreach (KeyValuePair<string, Advancement> advancement in this.AllAdvancements)
{
//update advancement and completion count
advancement.UpdateState(progress);
if (advancement.CompletedByAnyone())
advancement.Value.UpdateState(progress);
if (advancement.Value.CompletedByAnyone())
this.CompletedCount++;
else
this.RemainingAdvancements.Add(advancement.Key, advancement.Value);
}

this.RefreshRemainingCriteria();
}

public void RefreshRemainingCriteria()
{
this.RemainingCriteria.Clear();

//update global remaining criteria for overlay
foreach (var criterion in this.AllCriteria)
{
if (!criterion.Value.Owner.CompletedByAnyone() && !criterion.Value.CompletedByDesignated())
this.RemainingCriteria.Add(criterion.Key, criterion.Value);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions AATool/Data/Progress/WorldState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private void SyncAdvancements(WorldFolder world)
foreach (Uuid id in world.GetAllUuids())
this.Players[id] = new Contribution(id);

foreach (Advancement advancement in Tracker.Advancements.All.Values)
foreach (Advancement advancement in Tracker.Advancements.AllAdvancements.Values)
{
//add advancement if completed
if (world.Advancements.TryGetAdvancementCompletionFor(advancement.Id, out List<Uuid> ids))
Expand Down Expand Up @@ -217,7 +217,7 @@ private void SyncAchievements(WorldFolder world)
foreach (Uuid id in world.GetAllUuids())
this.Players[id] = new Contribution(id);

foreach (Achievement achievement in Tracker.Achievements.All.Values)
foreach (Achievement achievement in Tracker.Achievements.AllAdvancements.Values)
{
//add advancement if completed
if (world.Achievements.TryGetAchievementCompletionFor(achievement.Id, out List<Uuid> ids))
Expand Down
4 changes: 2 additions & 2 deletions AATool/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected override void Update(GameTime gameTime)

//check minecraft version
ActiveInstance.Update(this.Time);
SftpSave.Update(this.Time);
MinecraftServer.Update(this.Time);
Tracker.Update(this.Time);
OpenTracker.Update(this.Time);
SpriteSheet.Update(this.Time);
Expand Down Expand Up @@ -227,7 +227,7 @@ private void UpdateTitle()

//add igt to title
if (Tracker.InGameTime > TimeSpan.Zero)
this.AppendTitle($"{Tracker.GetPrettyIGT()} IGT");
this.AppendTitle($"{Tracker.GetPrettyIgt()} IGT");

//add fps cap to title
if (Config.Main.FpsCap < 60)
Expand Down
8 changes: 4 additions & 4 deletions AATool/Net/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class Client : Peer
public bool IsConnecting { get; private set; }
public bool WasKickedByServer { get; private set; }
public bool LostConnection { get; private set; }
public DateTime EstimatedRefresh { get; private set; }
public DateTime NextRefresh { get; private set; }

private readonly Dictionary<string, string> recieved;
private readonly Queue<Message> sendQueue;
Expand Down Expand Up @@ -46,7 +46,7 @@ public string GetStatusText()
if (!this.Connected())
return "Attempting connection...";

int remaining = (int)(this.EstimatedRefresh - DateTime.UtcNow).TotalSeconds;
int remaining = (int)(this.NextRefresh - DateTime.UtcNow).TotalSeconds;
if (remaining > 0)
{
string time = remaining >= 60
Expand Down Expand Up @@ -321,8 +321,8 @@ private void CopyData(Message message)
{
//deserialize datetime
message.TryGetItem(0, out string dateString);
if (DateTime.TryParse(dateString, out DateTime refreshEstimate))
this.EstimatedRefresh = refreshEstimate;
if (DateTime.TryParse(dateString, out DateTime nextEstimate))
this.NextRefresh = nextEstimate;
}
else if (message.TryGetItem(0, out string jsonString))
{
Expand Down
2 changes: 1 addition & 1 deletion AATool/Net/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static Message LogIn(string uuid, string password, string pronouns, strin
public static Message Kick(string reason) => NewCommand(Protocol.Headers.Kick, reason);
public static Message Progress(string jsonString) => NewData(Protocol.Headers.Progress, jsonString);
public static Message Lobby(string jsonString) => NewData(Protocol.Headers.Lobby, jsonString);
public static Message SftpEstimate(string nextRefresh) => NewData(Protocol.Headers.RefreshEstimate, nextRefresh);
public static Message SftpEstimate(DateTime nextRefresh) => NewData(Protocol.Headers.RefreshEstimate, nextRefresh.ToString());

private static Message NewCommand(string header, params string[] items) => new(Protocol.CommandPrefix, header, items);
private static Message NewData(string header, string data) => new(Protocol.DataPrefix, header, data);
Expand Down
2 changes: 1 addition & 1 deletion AATool/Net/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void SendProgress(Socket client = null)

public void SendNextRefresh(Socket client = null)
{
var message = Message.SftpEstimate(SftpSave.GetRefreshEstimate().ToString());
var message = Message.SftpEstimate(MinecraftServer.GetRefreshEstimate());

//send last to client(s)
if (client is null)
Expand Down
4 changes: 2 additions & 2 deletions AATool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("1.4.2.1")]
[assembly: AssemblyFileVersion("1.4.2.1")]
[assembly: AssemblyVersion("1.4.2.2")]
[assembly: AssemblyFileVersion("1.4.2.2")]
[assembly: NeutralResourcesLanguage("en")]
17 changes: 5 additions & 12 deletions AATool/Saves/SftpSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace AATool.Saves
{
public static class SftpSave
public static class MinecraftServer
{
private const string SftpPrefix = "sftp://";
private const double SaveInterval = (5 * 60) + 5;
Expand Down Expand Up @@ -56,13 +56,6 @@ public static string GetEstimateString(int seconds)

public static void Update(Time time)
{
if (Config.Tracking.UseSftp)
{
//update client refresh estimates if hosting
if (Server.TryGet(out Server server))
server.SendNextRefresh();
}

if (LastError is ArgumentException)
{
//invalid login credentials, don't try reconnecting
Expand Down Expand Up @@ -102,20 +95,20 @@ public static void Sync()
DateTime next = latest.Add(TimeSpan.FromSeconds(SaveInterval));
remaining = (next - DateTime.UtcNow).TotalSeconds;
if (remaining > 0)
RefreshTimer.SetAndStart(remaining);
RefreshTimer.SetAndStart(Math.Min(remaining, SaveInterval));

if (latest != LastWorldSave)
{
if (!TryDownloadProgress(sftp))
return;

LastWorldSave = latest;

//update client refresh estimates if hosting
if (Server.TryGet(out Server server))
server.SendNextRefresh();

//DODO: implement
//Tracker.Invalidate();
Tracker.Invalidate();
}
}
finally
Expand Down Expand Up @@ -316,7 +309,7 @@ private static bool TryGetWorldSaveTime(SftpClient sftp, out DateTime lastWorldS
{
if (exception is SftpPathNotFoundException)
{
//folder not found, so world name might be wrong. refresh it next time
//folder not found, world name might be wrong. refresh it next time
LastError = new SftpPathNotFoundException($"File not found: \"{remote}\".");
InvalidateWorld();
return false;
Expand Down
10 changes: 5 additions & 5 deletions AATool/Saves/WorldFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class WorldFolder
public DirectoryInfo CurrentFolder { get; private set; }
public bool ProgressChanged { get; private set; }
public bool Invalidated { get; private set; }
public bool ChangedPath { get; private set; }
public bool PathChanged { get; private set; }

public bool IsEmpty => this.CurrentFolder is null;

Expand All @@ -29,7 +29,7 @@ public WorldFolder()
this.Advancements = new AdvancementsFolder();
this.Achievements = new AchievementsFolder();
this.Statistics = new StatisticsFolder();
this.ChangedPath = true;
this.PathChanged = true;
this.ProgressChanged = true;
}

Expand All @@ -52,7 +52,7 @@ public WorldState GetState()

public void ClearFlags()
{
this.ChangedPath = false;
this.PathChanged = false;
this.ProgressChanged = false;
}

Expand Down Expand Up @@ -81,7 +81,7 @@ public void SetPath(DirectoryInfo worldFolder)
death.Clear();

this.CurrentFolder = worldFolder;
this.ChangedPath = true;
this.PathChanged = true;
string advancementsFolder = Path.Combine(this.CurrentFolder.FullName, "advancements");
string statisticsFolder = Path.Combine(this.CurrentFolder.FullName, "stats");

Expand All @@ -107,7 +107,7 @@ public bool TryRefresh()
this.ProgressChanged |= this.Invalidated;
this.Invalidated = false;

return this.ProgressChanged || this.ChangedPath;
return this.ProgressChanged || this.PathChanged;
}
}
}
Loading

0 comments on commit aec05a6

Please sign in to comment.