Skip to content

Commit

Permalink
Merge branch 'api4' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Ottermandias committed Oct 8, 2021
2 parents e8d8fec + 9103416 commit 33c5d6c
Show file tree
Hide file tree
Showing 81 changed files with 2,081 additions and 1,694 deletions.
28 changes: 14 additions & 14 deletions Classes/CatchData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ internal CatchData Intuition(int intuitionLength)
return this;
}

internal CatchData Uptime(uint startHour, uint endHour)
internal CatchData Uptime(uint startMinute, uint endMinute)
{
Hours = FromHours(startHour, endHour);
Minutes = FishUptime.FromStartEnd(startMinute, endMinute);
return this;
}

Expand Down Expand Up @@ -78,27 +78,27 @@ internal CatchData Tug(BiteType bite)
internal CatchData Gig(GigHead gigHead)
{
GigHead = gigHead;
if (gigHead != GigHead.None)
{
Snagging = Snagging.None;
HookSet = HookSet.None;
InitialBait = Game.Bait.Unknown;
Mooches = new Fish[0];
}
if (gigHead == GigHead.None)
return this;

Snagging = Snagging.None;
HookSet = HookSet.None;
InitialBait = Game.Bait.Unknown;
Mooches = System.Array.Empty<Fish>();

return this;
}

public uint[] PreviousWeather { get; private set; } = new uint[0];
public uint[] CurrentWeather { get; private set; } = new uint[0];
public uint[] PreviousWeather { get; private set; } = System.Array.Empty<uint>();
public uint[] CurrentWeather { get; private set; } = System.Array.Empty<uint>();

public Bait InitialBait { get; private set; } = Game.Bait.Unknown;
public Fish[] Mooches { get; private set; } = new Fish[0];
public Fish[] Mooches { get; private set; } = System.Array.Empty<Fish>();

public (Fish, int)[] Predator { get; private set; } = new (Fish, int)[0];
public (Fish, int)[] Predator { get; private set; } = System.Array.Empty<(Fish, int)>();
public int IntuitionLength { get; private set; }

public Uptime Hours { get; private set; } = AllHours;
public FishUptime Minutes { get; private set; } = FishUptime.AllTime;

public Patch Patch { get; }
public Snagging Snagging { get; private set; } = Snagging.Unknown;
Expand Down
10 changes: 4 additions & 6 deletions Classes/FishRecord.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Dalamud.Plugin;
using GatherBuddy.Enums;
using Dalamud.Logging;
using GatherBuddy.Game;

namespace GatherBuddy.Classes
Expand Down Expand Up @@ -82,7 +80,7 @@ public bool Update(Bait bait, ushort time, bool snagging, bool chum)

if (bait.Id != 0)
ret |= SuccessfulBaits.Add(bait.Id);

if (time > MinTime && time < MaxTime)
{
if (chum)
Expand Down Expand Up @@ -148,12 +146,12 @@ public string WriteLine(uint fishId)
}

private static readonly Regex V3MigrationRegex = new("(Unknown|Weak|Strong|Legendary) ", RegexOptions.Compiled);

private static string MigrateToV3(string line)
=> V3MigrationRegex.Replace(line, "");

public static (uint, FishRecord)? FromLine(string line)
{

(uint, FishRecord)? Error()
{
PluginLog.Error($"Could not create fishing record from \"{line}\".");
Expand Down
184 changes: 184 additions & 0 deletions Classes/FishUptime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
using System;
using System.Diagnostics;
using System.Globalization;
using GatherBuddy.Utility;

namespace GatherBuddy.Classes
{
public readonly struct FishUptime
{
private const uint BadHour = RealTime.HoursPerDay + 1;
private const uint AllHoursValue = RealTime.MinutesPerDay << 16;
private readonly uint _fromFor; // two ushorts, first start time in minute of day, second length in minutes.
public static FishUptime AllTime { get; } = new(AllHoursValue);
public static FishUptime NoHours { get; } = new(0);

private FishUptime(uint value)
{
Debug.Assert((value & 0xFFFF) < RealTime.MinutesPerDay);
Debug.Assert(value >> 16 <= RealTime.MinutesPerDay);
_fromFor = value;
}

public uint StartMinute
=> _fromFor & 0xFFFF;

public uint Duration
=> _fromFor >> 16;

public uint EndMinute
=> StartAndEnd().Item2;

public float FirstHour
=> (float) StartMinute / RealTime.MinutesPerHour;

public float EndHour
=> (float) EndMinute / RealTime.MinutesPerHour;

public (uint, uint) StartAndEnd()
{
var start = StartMinute;
var end = start + Duration;
if (end > RealTime.MinutesPerDay)
end -= RealTime.MinutesPerDay;
return (start, end);
}

public FishUptime Overlap(FishUptime rhs)
{
var start1 = StartMinute;
var end1 = start1 + Duration;
var start2 = rhs.StartMinute;
var end2 = start2 + rhs.Duration;
start1 = Math.Max(start1, start2);
end1 = Math.Min(end1, end2);
if (end1 <= start1)
return NoHours;

return new FishUptime(start1, end1 - start1);
}

public bool Overlaps(FishUptime rhs)
=> !Overlap(rhs).Equals(NoHours);

public bool Equals(FishUptime rhs)
=> _fromFor == rhs._fromFor;

public bool AlwaysUp()
=> _fromFor == AllHoursValue;

public FishUptime(uint startMinute, uint length)
{
if (length >= RealTime.MinutesPerDay)
{
_fromFor = AllHoursValue;
}
else
{
if (startMinute > RealTime.MinutesPerDay)
startMinute = (ushort) (startMinute % RealTime.MinutesPerDay);
_fromFor = startMinute | ((uint) length << 16);
}
}

public static FishUptime FromStartEnd(uint startMinute, uint endMinute)
=> new(startMinute, endMinute > startMinute ? endMinute - startMinute : RealTime.MinutesPerDay - startMinute + endMinute);

public static FishUptime FromStartEnd(float startHour, float endHour)
{
var startMinute = (uint) (startHour * RealTime.MinutesPerHour + 0.5f);
var endMinute = (uint) (endHour * RealTime.MinutesPerHour + 0.5f);
return FromStartEnd(startMinute, endMinute);
}

private static uint NextTime(uint currentHour, uint hours)
{
if (hours == 0)
return BadHour;

var rotatedHours = currentHour == 0 ? hours : (hours >> (int) currentHour) | ((hours << (32 - (int) currentHour)) >> 8);
return Util.TrailingZeroCount(rotatedHours);
}

private uint NextTime(uint currentMinute, uint start, uint end)
{
if (end > start)
{
if (start <= currentMinute)
return end > currentMinute ? 0 : start + RealTime.MinutesPerDay - currentMinute;

return start - currentMinute;
}

if (currentMinute >= start || currentMinute < end)
return 0;

return start - currentMinute;
}

public uint NextUptime(uint currentMinute)
{
var (start, end) = StartAndEnd();
return NextTime(currentMinute, start, end);
}

public uint NextDowntime(uint currentMinute)
{
var (start, end) = StartAndEnd();
return NextTime(currentMinute, end, start);
}

public RealUptime NextRealUptime()
{
if (AlwaysUp())
return RealUptime.Always;
if (Equals(NoHours))
return RealUptime.Never;

var now = DateTime.UtcNow;
var syncedNow = EorzeaTime.SyncToEorzeaDay(now);
var start = StartMinute;
var duration = Duration;
var end = StartMinute + Duration;
var startTime = syncedNow.AddSeconds((start - (end > RealTime.MinutesPerDay ? RealTime.MinutesPerDay : 0))
* EorzeaTime.SecondsPerEorzeaMinute);

var endTime = startTime.AddSeconds(duration * EorzeaTime.SecondsPerEorzeaMinute);
if (now > endTime)
{
startTime = startTime.AddSeconds(RealTime.MinutesPerDay * EorzeaTime.SecondsPerEorzeaMinute);
endTime = endTime.AddSeconds(RealTime.MinutesPerDay * EorzeaTime.SecondsPerEorzeaMinute);
}

return new RealUptime(startTime, endTime);
}

// Print hours in human readable format.
public string PrintHours(bool simple = false)
{
var (start, end) = StartAndEnd();
var hStart = start / RealTime.MinutesPerHour;
var hEnd = end / RealTime.MinutesPerHour;
var mStart = start - hStart * RealTime.MinutesPerHour;
var mEnd = end - hEnd * RealTime.MinutesPerHour;
var sStart = $"{hStart:D2}:{mStart:D2}";
var sEnd = $"{hEnd:D2}:{mEnd:D2}";

return simple ? $"{sStart}-{sEnd}" : $"{sStart} - {sEnd} ET";
}

// For Weather.
public FishUptime(DateTime time)
{
var hour = EorzeaTime.HourOfDay(time);
var startTime = (hour / 8) switch
{
0 => 0,
1 => 8 * RealTime.MinutesPerHour,
2 => 16 * RealTime.MinutesPerHour,
_ => RealTime.MinutesPerDay,
};
_fromFor = (uint) (startTime | ((8 * RealTime.MinutesPerHour) << 16));
}
}
}
12 changes: 6 additions & 6 deletions Classes/Records.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class Records : ISerializable

public void Merge(Records rhs)
{
foreach (var p in rhs.Nodes.Where(p => p.Value != null))
Nodes[p.Key].AddLocation(p.Value);
foreach (var (id, location) in rhs.Nodes)
Nodes[id].AddLocation(location);
}

public byte[] SerializeCompressed()
Expand All @@ -26,10 +26,10 @@ public byte[] SerializeCompressed()
using var writer = new BinaryWriter(c);

writer.Write(Nodes.Count);
foreach (var n in Nodes)
foreach (var (id, node) in Nodes)
{
writer.Write(n.Key);
n.Value.Write(writer);
writer.Write(id);
node.Write(writer);
}

c.Close();
Expand Down Expand Up @@ -63,6 +63,6 @@ public Records()
{ }

public Records(SerializationInfo info, StreamingContext context)
=> DeserializeCompressed(Convert.FromBase64String((string) info.GetValue("nodes", typeof(string))));
=> DeserializeCompressed(Convert.FromBase64String((string) info.GetValue("nodes", typeof(string))!));
}
}
12 changes: 6 additions & 6 deletions Classes/TimedGroup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using Dalamud;
using Dalamud.Game.Internal.Gui;
using Dalamud.Plugin;
using Dalamud.Game.Gui;
using Dalamud.Logging;
using GatherBuddy.Nodes;
using GatherBuddy.Utility;

Expand All @@ -13,7 +13,7 @@ public class TimedGroup
public string Name { get; }
public string Desc { get; }

private readonly (Node node, string desc)[] _nodes;
private readonly (Node? node, string desc)[] _nodes;

private static string? CorrectItemName(ClientLanguage lang, Node? node, string? itemName)
{
Expand All @@ -28,14 +28,14 @@ public class TimedGroup
return null;
}

public TimedGroup(ClientLanguage lang, string name, string desc, params (Node? node, string? desc)[] nodes)
public TimedGroup(string name, string desc, params (Node? node, string? desc)[] nodes)
{
Name = name;
Desc = desc;
_nodes = new (Node Node, string desc)[24];
_nodes = new (Node? Node, string desc)[24];

nodes = nodes.Where(n => n.node != null)
.Select(n => (n.node, CorrectItemName(lang, n.node!, n.desc!)))
.Select(n => (n.node, CorrectItemName(GatherBuddy.Language, n.node!, n.desc!)))
.ToArray();

for (var i = 0; i < _nodes.Length; ++i)
Expand Down
18 changes: 2 additions & 16 deletions Classes/Uptime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public bool AlwaysUp()

public bool IsUp(uint hour)
{
hour %= RealTime.HoursPerDay;
if (hour >= RealTime.HoursPerDay)
hour %= RealTime.HoursPerDay;
return ((_hours >> (int) hour) & 1) == 1;
}

Expand Down Expand Up @@ -200,21 +201,6 @@ public static Uptime FromHours(uint startHour, uint endHour)
return new Uptime(hours);
}


// For Weather.
public Uptime(DateTime time)
{
var hour = EorzeaTime.HourOfDay(time);
_hours = (hour / 8) switch
{
0 => 0x000000FF,
1 => 0x0000FF00,
2 => 0x00FF0000,
_ => 0x00000000,
};
}


// Convert the rare pop time given by the table to a bitfield.
public Uptime(GatheringRarePopTimeTable table)
{
Expand Down
Loading

0 comments on commit 33c5d6c

Please sign in to comment.