Skip to content

Commit

Permalink
separated phrase erase and phrase import
Browse files Browse the repository at this point in the history
  • Loading branch information
bierdosenhalter committed Feb 3, 2025
1 parent 397da70 commit b071cb1
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 28 deletions.
38 changes: 13 additions & 25 deletions Client/Commands/PhraseImport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public override string Run(IClient handler, string command, Dictionary<string, o
memoryLine = uint.Parse(splits[0].Split(":")[0]);
memoryIndex = uint.Parse(splits[0].Split(":")[1]);

// Get a new phrase ID
phraseId = ryzomClient.GetPhraseManager().AllocatePhraseSlot();

phrase = new PhraseCom();
Expand Down Expand Up @@ -109,38 +110,25 @@ private static void SendPhraseToServer(RyzomClient ryzomClient, PhraseCom phrase
// Check for existing phrase ID in the specified memory line and index
var existingPhraseId = ryzomClient.GetPhraseManager().GetPhraseIdFromMemory(memoryLine, memoryIndex);

if (existingPhraseId != 0)
if (existingPhraseId == 0)
{
// Check how many times this phrase is used
var usageCount = ryzomClient.GetPhraseManager().CountAllThatUsePhrase(existingPhraseId);

// If the usage count is 1, delete the existing phrase
if (usageCount == 1)
{
ryzomClient.GetLogger().Info($"§cDeleting phrase {existingPhraseId} as it is used only used once.");
ryzomClient.GetPhraseManager().ErasePhrase(existingPhraseId);
ryzomClient.GetPhraseManager().SendDeleteToServer(existingPhraseId);
ryzomClient.GetNetworkManager().Update();
}

// server forget
ryzomClient.GetLogger().Info($"§eForgetting phrase on memory line {memoryLine} slot {memoryIndex}.");
ryzomClient.GetPhraseManager().SendForgetToServer(memoryLine, memoryIndex);
// learn and add to action bar
ryzomClient.GetLogger().Info($"§aImporting phrase {(phrase.Name.Length > 0 ? $"'{phrase.Name}'" : $"{phraseId}")} to memory line {memoryLine} slot {memoryIndex}.");
ryzomClient.GetPhraseManager().SendLearnToServer(phraseId);
ryzomClient.GetPhraseManager().SetPhraseInternal(phraseId, ryzomClient.GetPhraseManager().GetPhrase(phraseId), false, false);
ryzomClient.GetNetworkManager().Update();
}

// learn and add to action bar
ryzomClient.GetLogger().Info($"§aImporting phrase {(phrase.Name.Length > 0 ? $"'{phrase.Name}'" : $"{phraseId}")} to memory line {memoryLine} slot {memoryIndex}.");
ryzomClient.GetPhraseManager().SendLearnToServer(phraseId);
ryzomClient.GetPhraseManager().SetPhraseInternal(phraseId, ryzomClient.GetPhraseManager().GetPhrase(phraseId), false, false);
ryzomClient.GetNetworkManager().Update();

ryzomClient.GetPhraseManager().SendMemorizeToServer(memoryLine, memoryIndex, phraseId);
ryzomClient.GetPhraseManager().SendMemorizeToServer(memoryLine, memoryIndex, phraseId);
}
else
{
ryzomClient.GetLogger().Error($"Importing phrase {(phrase.Name.Length > 0 ? $"'{phrase.Name}'" : $"{phraseId}")} to memory line {memoryLine} slot {memoryIndex} failed. Already a phrase at this slot.");
}
}

public override IEnumerable<string> GetCmdAliases()
{
return ["ImportPhrase"];
}
}
}
}
2 changes: 1 addition & 1 deletion Client/Commands/PhraseSelectMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override string Run(IClient handler, string command, Dictionary<string, o

public override IEnumerable<string> GetCmdAliases()
{
return new string[] { };
return [];
}
}
}
74 changes: 74 additions & 0 deletions Client/Commands/PhrasesErase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using API;
using API.Commands;
using Client.Phrase;

namespace Client.Commands
{
public class PhrasesErase : CommandBase
{
public override string CmdName => "PhrasesErase";

public override string CmdUsage => "[page]";

public override string CmdDesc => "Erase phrases from the action bar. Specify an ActionBarPage to erase phrases from that page.";

public override string Run(IClient handler, string command, Dictionary<string, object> localVars)
{
if (handler is not RyzomClient ryzomClient)
throw new Exception("Command handler is not a Ryzom client.");

var args = GetArgs(command);
uint? actionBarPage = null;

if (args.Length == 1)
{
if (!uint.TryParse(args[0], out var page))
return "Invalid ActionBarPage specified. It must be a number.";

actionBarPage = page;
}

// Logic for erasing phrases
var phrasesToErase = new List<uint>();

// Parse all memories
for (uint memoryLine = 0; memoryLine < 10; memoryLine++)
{
for (uint memoryIndex = 0; memoryIndex < PhraseManager.PHRASE_MAX_MEMORY_SLOT; memoryIndex++)
{
var phraseId = ryzomClient.GetPhraseManager().GetPhraseIdFromMemory(memoryLine, memoryIndex);

var usageCount = ryzomClient.GetPhraseManager().CountAllThatUsePhrase(phraseId);

// Check if the action bar page is specified, and if it matches
if (!actionBarPage.HasValue || actionBarPage.Value == memoryLine)
{
// Only erase the phrases that are not used anywhere else
if (usageCount == 1)
phrasesToErase.Add(phraseId);

// Send forget to server for this memory line and index
ryzomClient.GetPhraseManager().SendForgetToServer(memoryLine, memoryIndex);
}
}
}

// Now erase the phrases that are marked for deletion
foreach (var phraseId in phrasesToErase)
{
ryzomClient.GetPhraseManager().ErasePhrase(phraseId);
}

ryzomClient.GetNetworkManager().Update();

return "Phrases erased successfully.";
}

public override IEnumerable<string> GetCmdAliases()
{
return ["DeletePhrases"];
}
}
}
4 changes: 2 additions & 2 deletions Client/Phrase/PhraseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ private enum SlotType
private SheetId _enchantWeaponMainBrick;

/// <summary>Map of All Phrase. Contains the Book + some system phrase (1: the Edition Phrase)</summary>
private readonly Dictionary<int, PhraseCom> _phraseMap = new();
private readonly Dictionary<int, PhraseCom> _phraseMap = [];

/// <summary>map each phrase to its sheet id</summary>
private readonly Dictionary<PhraseCom, int> _phraseToSheet = new();
private readonly Dictionary<PhraseCom, int> _phraseToSheet = [];

/// <summary>extra client data</summary>
private readonly List<PhraseClient> _phraseClient = [];
Expand Down

0 comments on commit b071cb1

Please sign in to comment.