Skip to content

Commit

Permalink
Added cooldown overview and extra servers.
Browse files Browse the repository at this point in the history
  • Loading branch information
josdemmers committed Mar 14, 2022
1 parent c0af0de commit 88ea508
Show file tree
Hide file tree
Showing 27 changed files with 694 additions and 74 deletions.
32 changes: 32 additions & 0 deletions NewWorldCompanion.Entities/CooldownTimer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace NewWorldCompanion.Entities
{
public class CooldownTimer
{
public string Name { get; set; } = string.Empty;
public TimeSpan Duration { get; set; } = TimeSpan.FromHours(24);
public DateTime StartTime { get; set; } = DateTime.Now;

[JsonIgnore]
public bool Ready
{
get { return DateTime.Now > StartTime + Duration; }
}

[JsonIgnore]
public TimeSpan RemainingTime
{
get
{
TimeSpan remainder = Duration - (DateTime.Now - StartTime);
return remainder > TimeSpan.Zero ? remainder : TimeSpan.Zero;
}
}
}
}
2 changes: 1 addition & 1 deletion NewWorldCompanion.Entities/PriceServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace NewWorldCompanion.Entities
public class PriceServer
{
public string Name { get; set; } = string.Empty;
public string Id { get; set; } = string.Empty;
public int Id { get; set; } = 1;
}
}
2 changes: 1 addition & 1 deletion NewWorldCompanion.Entities/SettingsNWC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class SettingsNWC
public bool DebugModeActive { get; set; } = false;

// Overlay
public string ServerId { get; set; } = "1";
public int PriceServerId { get; set; } = 1;

// Shape detection
public int EmguAreaLower { get; set; } = 10000;
Expand Down
13 changes: 13 additions & 0 deletions NewWorldCompanion.Events/PriceServerEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewWorldCompanion.Events
{
public class PriceServerListUpdatedEvent : PubSubEvent
{
}
}
2 changes: 1 addition & 1 deletion NewWorldCompanion.Helpers/ScreenCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Bitmap GetScreenCapture(IntPtr windowHandle)
PInvoke.User32.GetWindowRect(windowHandle, out region);

int width = (int)((region.right - region.left) * 0.75);
int height = 400;
int height = (int)((region.bottom - region.top) * 0.50);

PInvoke.User32.CURSORINFO cursorInfo = new PInvoke.User32.CURSORINFO();
cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);
Expand Down
22 changes: 22 additions & 0 deletions NewWorldCompanion.Interfaces/ICooldownManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NewWorldCompanion.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewWorldCompanion.Interfaces
{
public interface ICooldownManager
{
List<CooldownTimer> CooldownTimers
{
get;
}

void AddCooldown(CooldownTimer cooldownTimer);
void RemoveCooldown(CooldownTimer cooldownTimer);

void SaveCooldowns();
}
}
1 change: 1 addition & 0 deletions NewWorldCompanion.Interfaces/INewWorldDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public interface INewWorldDataStore
{
List<CraftingRecipe> GetCraftingRecipes();
bool IsBindOnPickup(string itemName);
string GetItemId(string itemName);
}
}
98 changes: 98 additions & 0 deletions NewWorldCompanion.Services/CooldownManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using NewWorldCompanion.Entities;
using NewWorldCompanion.Interfaces;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace NewWorldCompanion.Services
{
public class CooldownManager : ICooldownManager
{
private readonly IEventAggregator _eventAggregator;

private List<CooldownTimer> _cooldownTimers = new List<CooldownTimer>();

// Start of Constructor region

#region Constructor

public CooldownManager(IEventAggregator eventAggregator)
{
// Init IEventAggregator
_eventAggregator = eventAggregator;

// Init cooldowns
LoadCooldowns();
}

#endregion

// Start of Properties region

#region Properties

public List<CooldownTimer> CooldownTimers { get => _cooldownTimers; }

#endregion

// Start of Events region

#region Events

#endregion

// Start of Methods region

#region Methods

public void AddCooldown(CooldownTimer cooldownTimer)
{
_cooldownTimers.Add(cooldownTimer);
}

public void RemoveCooldown(CooldownTimer cooldownTimer)
{
_cooldownTimers.Remove(cooldownTimer);
}

private void LoadCooldowns()
{
_cooldownTimers.Clear();

string fileName = "Config/Cooldowns.json";
if (File.Exists(fileName))
{
using FileStream stream = File.OpenRead(fileName);
_cooldownTimers = JsonSerializer.Deserialize<List<CooldownTimer>>(stream) ?? new List<CooldownTimer>();
}

// Sort list
_cooldownTimers.Sort((x, y) =>
{
int result = x.RemainingTime - y.RemainingTime > TimeSpan.Zero ? 1 : -1;
return result != 0 ? result : string.Compare(x.Name, y.Name, StringComparison.Ordinal);
});

SaveCooldowns();
}

public void SaveCooldowns()
{
string fileName = "Config/Cooldowns.json";
string path = Path.GetDirectoryName(fileName) ?? string.Empty;
Directory.CreateDirectory(path);

using FileStream stream = File.Create(fileName);
var options = new JsonSerializerOptions { WriteIndented = true };
JsonSerializer.Serialize(stream, CooldownTimers, options);
}

#endregion

}
}
7 changes: 5 additions & 2 deletions NewWorldCompanion.Services/CraftingRecipeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void LoadRecipes()
{
_craftingRecipes.Clear();

string fileName = "CraftingRecipeProgress.json";
string fileName = "Config/CraftingRecipeProgress.json";
if (File.Exists(fileName))
{
using FileStream stream = File.OpenRead(fileName);
Expand Down Expand Up @@ -93,7 +93,10 @@ private void LoadRecipes()

public void SaveRecipes()
{
string fileName = "CraftingRecipeProgress.json";
string fileName = "Config/CraftingRecipeProgress.json";
string path = Path.GetDirectoryName(fileName) ?? string.Empty;
Directory.CreateDirectory(path);

using FileStream stream = File.Create(fileName);
var options = new JsonSerializerOptions { WriteIndented = true };
JsonSerializer.Serialize(stream, CraftingRecipes, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28420,5 +28420,7 @@
<string key="2hBow_AngryEarthsWarningT5_Description">What was previously a standard hunter's bow has been warped by the Land. It serves as a reminder to its wielder to fear the Earth.</string>
<string key="2hGreatAxe_RoyalExecutionersAxeT5_MasterName">Royal Executioner's Axe</string>
<string key="2hGreatAxe_RoyalExecutionersAxeT5_Description">With the fall of this blade I pray that this sinner be redeemed.</string>
<string key="Skin_Head_PrimeEyes_MasterName">The Indigo Flame</string>
<string key="Skin_Head_PrimeEyes_Description">The Flame of your dedication burns bright within you.</string>
</resources>

12 changes: 12 additions & 0 deletions NewWorldCompanion.Services/NewWorldDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public List<CraftingRecipe> GetCraftingRecipes()

public bool IsBindOnPickup(string itemName)
{
//TODO Needs improvement. Look into more properties like IsTradable, quests, etc.
var localisationId = _itemDefinitionsLocalisation.FirstOrDefault(x => x.Value.Equals(itemName, StringComparison.OrdinalIgnoreCase)).Key;
var item = _masterItemDefinitionsCraftingJson.FirstOrDefault(i => i.Name.Equals($"@{localisationId}", StringComparison.OrdinalIgnoreCase));
if (item != null)
Expand All @@ -154,6 +155,17 @@ public bool IsBindOnPickup(string itemName)
return true;
}

public string GetItemId(string itemName)
{
var localisationId = _itemDefinitionsLocalisation.FirstOrDefault(x => x.Value.Equals(itemName, StringComparison.OrdinalIgnoreCase)).Key;
var item = _masterItemDefinitionsCraftingJson.FirstOrDefault(i => i.Name.Equals($"@{localisationId}", StringComparison.OrdinalIgnoreCase));
if (item != null)
{
return item.ItemID;
}
return string.Empty;
}

#endregion


Expand Down
1 change: 1 addition & 0 deletions NewWorldCompanion.Services/OverlayHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ private void Timer_Tick(object? sender, EventArgs e)
private void HandleOcrTextReadyEvent()
{
_itemName = _ocrHandler.OcrText;
//TODO Cleanup name. For example Toilvium -> Tolvium
if (!_itemName.Equals(_itemNamePrevious))
{
_priceManager.UpdatePriceData(_itemName);
Expand Down
Loading

0 comments on commit 88ea508

Please sign in to comment.