Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions WolfKillCounter/Commands/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Vintagestory.API.Common;
using Vintagestory.API.Server;
using Vintagestory.Common;
using WolfKillCounter.Config;

namespace WolfKillCounter.Commands
Expand All @@ -15,12 +11,11 @@ internal class Commands
private static ICoreServerAPI sapi;
public static void RegisterCommands(ICoreServerAPI api)
{

sapi = api;
// List Leaderboard Command
api.ChatCommands.Create("wkc")
.WithDescription("Provides the Wolf Kill Counter commands")
.RequiresPrivilege(Privilege.chat)
.HandleWith(args => HandleWKCCommand(args, api))
// List Wolf Kills Subcommand
.BeginSubCommand("listWolfKills")
.WithDescription("List the top 5 wolf killers")
Expand Down Expand Up @@ -50,34 +45,28 @@ public static void RegisterCommands(ICoreServerAPI api)
.EndSubCommand();
}

private static TextCommandResult HandleWKCCommand(TextCommandCallingArgs args, ICoreServerAPI api)
{
throw new NotImplementedException();
}

// Command function to print the Wolf Kills Leaderboard
private static TextCommandResult ListWolfKills(TextCommandCallingArgs args, ICoreServerAPI api)
{

string playerName = args.Caller.Player.PlayerName;
api.Logger.Notification($"{playerName}: Printing Wolf Kill List Top 5");

return TextCommandResult.Success(PrintList(playerName));
}

// Command function to reset the Wolf Kills Leaderboard
private static TextCommandResult ResetLeaderboardCommand(TextCommandCallingArgs args, ICoreServerAPI api)
private static TextCommandResult ResetLeaderboardCommand(TextCommandCallingArgs _, ICoreServerAPI api)
{
WolfKillCounterModSystem modSystem = api.ModLoader.GetModSystem<WolfKillCounterModSystem>();
modSystem.SetCurrentLeaderboard(new Dictionary<string, int>());
modSystem.GetConfig().SaveWolfKillData(api);
modSystem.GetConfig().SaveWolfKillData();

api.Logger.Notification("[WolfKillCounter] Resetting leaderboard.");

return TextCommandResult.Success("Wolf kill leaderboard has been reset. Total kill count remains unchanged.");
}

private static TextCommandResult DisplayServerGoal(TextCommandCallingArgs args, ICoreServerAPI api)
private static TextCommandResult DisplayServerGoal(TextCommandCallingArgs _, ICoreServerAPI api)
{
WolfKillCounterModSystem modSystem = api.ModLoader.GetModSystem<WolfKillCounterModSystem>();

Expand All @@ -96,7 +85,7 @@ private static TextCommandResult DisplayPlayerGoal(TextCommandCallingArgs args,
if (wolfKillCount.ContainsKey(playerName))
{
modSystem.AddPlayer(playerName, new KillCountData { Kills = 0, Goal = 50, Deaths = 0 });
modSystem.GetConfig().SaveWolfKillData(api);
modSystem.GetConfig().SaveWolfKillData();
}

return TextCommandResult.Success($"{playerName}'s Kill Goal: {modSystem.GetWolfKillCount()[playerName].Goal}.\n" +
Expand Down
128 changes: 27 additions & 101 deletions WolfKillCounter/Config/WolfKillCounterConfig.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Vintagestory.API.Server;
using Vintagestory.API.Util;

Expand All @@ -25,11 +22,11 @@ public class KillCountData
[ProtoContract]
public class WolfKillData
{
// Players and their wolf kill counts
// Players and their wolf kill counts (OUTDATED)
[ProtoMember(1)]
public Dictionary<string, int> KillCounts { get; set; } = new Dictionary<string, int>();

// Old Dictionary format, REFORMAT USE ONLY
// New Format
[ProtoMember(2)]
public Dictionary<string, KillCountData> NewKillCounts { get; set; } = new Dictionary<string, KillCountData>();

Expand All @@ -44,118 +41,47 @@ public class WolfKillData
[ProtoMember(5)]
public int ServerKillGoal { get; set; } = 100;
}
public class WolfKillCounterConfig
public class WolfKillCounterConfig(ICoreServerAPI api)
{
ICoreServerAPI sapi;

private string SaveFilePath => sapi.GetOrCreateDataPath("wolfkills.json");

//private string SaveFilePath => api.GetOrCreateDataPath("wolfkills.json");
// Load the saved data from the json file
public WolfKillData LoadWolfKillData(ICoreServerAPI api)
public void LoadWolfKillData(ref WolfKillData data)
{
sapi = api;

// If the old json file exists, get the data and switch to new saving format
if (api.LoadModConfig("wolfkills.json") != null)
// Create new data if none exists
if (api.WorldManager.SaveGame.GetData("wolfkilldata") is null)
{
// Get Mod System
WolfKillCounterModSystem wolfKillCounter = api.ModLoader.GetModSystem<WolfKillCounterModSystem>();

// Get the file data
var data = api.LoadModConfig<WolfKillData>("wolfkills.json");

// Load Leaderboard and TotalKills
var currentLeaderboard = data.Leaderboard;
var totalWolfKillCount = data.TotalKills;
var serverKillGoal = wolfKillCounter.CalculateGoal(totalWolfKillCount);

Dictionary<string, KillCountData> wolfKillCount = new Dictionary<string, KillCountData>();
// Load KillCounts from the old format and convert it to the new format
if (data.KillCounts != null)
{
foreach (var kvp in data.KillCounts)
{
wolfKillCount.Add(kvp.Key, new KillCountData { Kills = kvp.Value, Goal = wolfKillCounter.CalculateGoal((int)kvp.Value), Deaths = 0 });
}
}

api.Logger.Notification("[WolfKillCounter]: KillCounts parsed and migrated if needed.");

// Try to Delete the JSON file, otherwise Error.
try
{
System.IO.File.Delete(SaveFilePath);
api.Logger.Notification("[WolfKillCounter]: Deleted old json file for SaveData Migration.");
}
catch (Exception)
{
api.Logger.Error($"[WolfKillCounter] - Error: File not deleted! Make sure the JSON file is manually deleted.\n");
}
// Fresh WolfKillData
data = new WolfKillData() { };

WolfKillData loadData = new WolfKillData()
{
NewKillCounts = wolfKillCount,
Leaderboard = currentLeaderboard,
TotalKills = totalWolfKillCount,
ServerKillGoal = serverKillGoal
};
// Store the fresh data
api.WorldManager.SaveGame.StoreData("wolfkilldata", data);
api.Logger.Notification("[WolfKillCounter]: Created new save data in SaveGame");

api.Logger.Notification("[WolfKillCounter]: Save Loaded.");
return loadData;

return;
}

// New SaveGame format if data exists already, get and load the data
else if (api.WorldManager.SaveGame.GetData("wolfkilldata") is Byte[] rawData)
{
// Read raw JSON from the file
rawData = api.WorldManager.SaveGame.GetData("wolfkilldata");
// Replace the line causing the error with the following line
WolfKillData parsedData = rawData == null ? new WolfKillData() : SerializerUtil.Deserialize<WolfKillData>(rawData);

// Load TotalKills and ServerKillGoal
var totalWolfKillCount = parsedData.TotalKills;
var serverKillGoal = parsedData.ServerKillGoal;
// Read raw JSON from the file
byte[] rawData = api.WorldManager.SaveGame.GetData("wolfkilldata");
// Replace the line causing the error with the following line
WolfKillData parsedData = rawData == null ? new WolfKillData() : SerializerUtil.Deserialize<WolfKillData>(rawData);

// Load Leaderboard
var currentLeaderboard = parsedData.Leaderboard;

// Load KillCounts with backward compatibility for the old format
var wolfKillCount = parsedData.NewKillCounts;

var loadData = new WolfKillData()
{
NewKillCounts = wolfKillCount,
Leaderboard = currentLeaderboard,
TotalKills = totalWolfKillCount,
ServerKillGoal = serverKillGoal
};

api.Logger.Notification("[WolfKillCounter]: Save Loaded.");
return loadData;
}
// No SaveGame data exists, create new save data
else
data = new WolfKillData()
{
// Fresh WolfKillData
var loadData = new WolfKillData()
{
NewKillCounts = new Dictionary<string, KillCountData>(),
Leaderboard = new Dictionary<string, int>(),
TotalKills = 0,
ServerKillGoal = 100
};
NewKillCounts = parsedData.NewKillCounts,
Leaderboard = parsedData.Leaderboard,
TotalKills = parsedData.TotalKills,
ServerKillGoal = parsedData.ServerKillGoal,
};

// Store the fresh data
api.WorldManager.SaveGame.StoreData("wolfkilldata", loadData);
api.Logger.Notification("[WolfKillCounter]: Created new save data in SaveGame");

api.Logger.Notification("[WolfKillCounter]: Save Loaded.");
return loadData;
}
api.Logger.Notification("[WolfKillCounter]: Save Loaded.");

}

// Save the current kill data to the json file
public void SaveWolfKillData(ICoreServerAPI api)
public void SaveWolfKillData()
{
WolfKillCounterModSystem modSystem = api.ModLoader.GetModSystem<WolfKillCounterModSystem>();

Expand Down
2 changes: 1 addition & 1 deletion WolfKillCounter/WolfKillCounter.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<OutputPath>bin\$(Configuration)\Mods\mod</OutputPath>
</PropertyGroup>
Expand Down
Loading