Jump to content

Code about fuzzy matching


Recommended Posts

I'm now trying to write a bit of fuzzy matching code, but it hasn't worked,Following the normal traversal of the target name, the code I used was:
string[] objectNames = new string[] { "Alliance Bonfire" };
foreach (string objectName in objectNames)
    {
          var gameObject = ObjectManager.GetWoWGameObjectByName(objectName)
            .Where(o => o.GetDistance <= maxDistance)
            .OrderBy(o => o.GetDistance)
            .FirstOrDefault();
        if (gameObject != null )
        {......
     }
    }
What I want to achieve is a fuzzy match, which doesn't require a full traversal of the "Alliance Bonfire", just a matching "Bonfire".

Link to comment
Share on other sites

Hello,

string[] objectNames = new string[] { "Bonfire" };
foreach (string objectName in objectNames)
{
    var gameObjects = ObjectManager.GetObjectWoWGameObject()
        .Where(o => o.Name.Contains(objectName) && o.GetDistance <= maxDistance)
        .OrderBy(o => o.GetDistance)
        .FirstOrDefault();

    if (gameObjects != null)
    {
        // ...
    }
}

 

Link to comment
Share on other sites

10 hours ago, Droidz said:

你好

string[] objectNames = new string[] { "Bonfire" };
foreach (string objectName in objectNames)
{
    var gameObjects = ObjectManager.GetObjectWoWGameObject()
        .Where(o => o.Name.Contains(objectName) && o.GetDistance <= maxDistance)
        .OrderBy(o => o.GetDistance)
        .FirstOrDefault();

    if (gameObjects != null)
    {
        // ...
    }
}

 

I tested it and it didn't work with a fuzzy match, and when I tested the full name with PNC "Auctioneer Billdu", I was able to match this PNC, but when I used the fuzzy name "Auctioneer", it didn't match properly, here's my code

string[] objectNames = new string[] { "Auctioneer Billdu" };

float maxDistance = 2250.0f;

while (true)
{
foreach (string objectName in objectNames)
{
    var gameObjects = ObjectManager.GetObjectWoWGameObject()


         
        .Where(o => objectNames.Contains(o.Name) && o.GetDistance <= maxDistance)
        .OrderBy(o => o.GetDistance)
        .FirstOrDefault();

    if (gameObjects != null)
    {
     
                string luaPrintCommand = string.Format("print('Targeted object: {0}')", gameObjects.Name);
                Lua.LuaDoString(luaPrintCommand);
                Thread.Sleep(50);

}
}
}


If you change it to the following code, it will not match the NPC
  
  
string[] objectNames = new string[] { "Auctioneer" };

float maxDistance = 2250.0f;

while (true)
{
foreach (string objectName in objectNames)
{
    var gameObjects = ObjectManager.GetObjectWoWGameObject()


         
        .Where(o => objectNames.Contains(o.Name) && o.GetDistance <= maxDistance)
        .OrderBy(o => o.GetDistance)
        .FirstOrDefault();

    if (gameObjects != null)
    {
     
                string luaPrintCommand = string.Format("print('Targeted object: {0}')", gameObjects.Name);
                Lua.LuaDoString(luaPrintCommand);
                Thread.Sleep(50);

}
}
}
Link to comment
Share on other sites

You're not using the elements of your foreach loop. The code Droidz gave you is absolutely correct.

Replace

    var gameObjects = ObjectManager.GetObjectWoWGameObject()        
        .Where(o => objectNames.Contains(o.Name) && o.GetDistance <= maxDistance)
        .OrderBy(o => o.GetDistance)
        .FirstOrDefault();   

with

    var gameObjects = ObjectManager.GetObjectWoWGameObject()        
        .Where(o => o.Name.Contains(objectName) && o.GetDistance <= maxDistance)
        .OrderBy(o => o.GetDistance)
        .FirstOrDefault();   

 

Link to comment
Share on other sites

On 7/5/2024 at 9:22 AM, Zer0 said:

You're not using the elements of your foreach loop. The code Droidz gave you is absolutely correct.

Replace

    var gameObjects = ObjectManager.GetObjectWoWGameObject()        
        .Where(o => objectNames.Contains(o.Name) && o.GetDistance <= maxDistance)
        .OrderBy(o => o.GetDistance)
        .FirstOrDefault();   

with

    var gameObjects = ObjectManager.GetObjectWoWGameObject()        
        .Where(o => o.Name.Contains(objectName) && o.GetDistance <= maxDistance)
        .OrderBy(o => o.GetDistance)
        .FirstOrDefault();   

 

Thanks for the reminder, I tested it again, it was me who got the code wrong, thanks

Link to comment
Share on other sites

On 7/4/2024 at 2:42 PM, Droidz said:

Hello,

string[] objectNames = new string[] { "Bonfire" };
foreach (string objectName in objectNames)
{
    var gameObjects = ObjectManager.GetObjectWoWGameObject()
        .Where(o => o.Name.Contains(objectName) && o.GetDistance <= maxDistance)
        .OrderBy(o => o.GetDistance)
        .FirstOrDefault();

    if (gameObjects != null)
    {
        // ...
    }
}

 

感谢,之前是我自己把代码搞错了,代码能正常使用的,非常感谢

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...