Help Comparing 2 messages in LUA

c9angelo

New member
Joined
Aug 17, 2020
Messages
2
Reaction score
1
Location
Florida
Hello, alle.

I was wondering if there was a good way to compare two messages using the onServerMessage function from the SAMP.Lua library. The idea is that I want to see if two consecutive messages are the same, for example:

Server Log
Code:
* Player_A has killed Player_B
* Player_B has killed Player_C

I would want a way to compare the two messages to check for the common string Player_B.
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
146
Reaction score
80
Location
null
JavaScript:
function sampev.onServerMessage(color, text)
    if text:match('Player_A has killed Player_B') then
        doSomething()
    elseif text == 'Player_A has killed Player_B' then
        doSomething()
    end
end
 

Expl01T3R

Active member
Joined
Nov 20, 2022
Messages
133
Reaction score
21
Location
Czech Republic
I don't know if I understand correctly, but:
C++ Version:
C++:
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <unordered_set>
#include <algorithm>

std::vector<std::string> splitStringBySpaces(const std::string& str) {
    std::vector<std::string> result;
    std::istringstream iss(str);
    std::string word;
    while (iss >> word) {
        result.push_back(word);
    }
    return result;
}

std::string getCommonMsgWord(const std::string& msgMain, const std::string& msgToCompare, const std::vector<std::string>& wordsToIgnore) {
    std::vector<std::string> wordsMain = splitStringBySpaces(msgMain);
    std::vector<std::string> wordsCompare = splitStringBySpaces(msgToCompare);

    std::unordered_set<std::string> ignoredWords(wordsToIgnore.begin(), wordsToIgnore.end());
    std::unordered_set<std::string> compareWords(wordsCompare.begin(), wordsCompare.end());
    
    for (const std::string& word : wordsMain)
    {
        if (compareWords.find(word) != compareWords.end() && ignoredWords.find(word) == ignoredWords.end())
        {
            return word;
        }
    }
    
    return "";
}


int main()
{
    std::string msg1 = "* Player_A has killed Player_B";
    std::string msg2 = "* Player_B has killed Player_C";
    std::vector<std::string> wordsToIgnore = {"*", "has", "killed"};
    
    std::string commonWord = getCommonMsgWord(msg1, msg2, wordsToIgnore);
    
    if (!commonWord.empty()) {
        std::cout << "Common word found: " << commonWord << std::endl;
    } else {
        std::cout << "No common word found." << std::endl;
    }
    return 0;
}
Result:
Code:
Common word found: Player_B

LUA by ChatGPT:
Python:
-- Function to split a string by spaces
function splitStringBySpaces(str)
    local result = {}
    for word in str:gmatch("%S+") do
        table.insert(result, word)
    end
    return result
end

-- Function to get the common word between two messages ignoring specific words
function getCommonMsgWord(msgMain, msgToCompare, wordsToIgnore)
    -- Split the messages into words
    local wordsMain = splitStringBySpaces(msgMain)
    local wordsCompare = splitStringBySpaces(msgToCompare)
    
    -- Create a set of ignored words for quick lookup
    local ignoredWords = {}
    for _, word in ipairs(wordsToIgnore) do
        ignoredWords[word] = true
    end
    
    -- Create a set for words in the second message for quick lookup
    local compareWords = {}
    for _, word in ipairs(wordsCompare) do
        compareWords[word] = true
    end
    
    -- Find a common word not in the ignored words
    for _, word in ipairs(wordsMain) do
        if compareWords[word] and not ignoredWords[word] then
            return word -- Return the first common word found that is not ignored
        end
    end
    
    return "" -- Return an empty string if no common word is found
end

-- Example usage
local msg1 = "* Player_A has killed Player_B"
local msg2 = "* Player_B has killed Player_C"
local wordsToIgnore = {"*", "has", "killed"}

local commonWord = getCommonMsgWord(msg1, msg2, wordsToIgnore)

if commonWord ~= "" then
    print("Common word found: " .. commonWord)
else
    print("No common word found.")
end

Result is same.
 
Top