ElunaLuaEngine / Eluna

Eluna Lua Engine © for WoW Emulators
https://elunaluaengine.github.io
GNU General Public License v3.0
377 stars 362 forks source link

Eluna states communication #496

Open iThorgrim opened 1 month ago

iThorgrim commented 1 month ago

This system introduces a Mediator pattern to Eluna, allowing communication between different Lua states based on "mapId". It enables the registration and notification of events across states, such as player logins or custom actions.

How It Works:

Little Example

local mapId = GetStateMapId()

if mapId == -1 then
    -- World state (-1)
    local function OnGiveExperience(playerName)
        print(playerName .. " has connected from another state!")
    end
    RegisterStateEvent(mapId, "OnGiveExperience", OnGiveExperience)
else
    -- Map state (mapId)
    local function OnGiveExperience(event, player)
        NotifyStateEvent(-1, "OnGiveExperience", tostring(player:GetName()))
    end
    RegisterPlayerEvent(12, OnGiveExperience)
end

This system can be expanded in the future to support additional data types and more complex interactions between Lua states.

Shauren commented 1 month ago

Modifying a global unordered_map is not threadsafe

Foereaper commented 1 month ago

I think a better approach would be to implement something like a topic based asynchronous message queue, where each state would have a consumer to just consume messages from the queue at the start of the state update cycle. It would probably be relatively easy to implement something like AMQP and just use an external message queue server.

iThorgrim commented 1 month ago

I think a better approach would be to implement something like a topic based asynchronous message queue, where each state would have a consumer to just consume messages from the queue at the start of the state update cycle. It would probably be relatively easy to implement something like AMQP and just use an external message queue server.

Integrating AMQP would be an excellent idea, especially as it would also allow you to “chat” with Eluna via external applications, but I think that's a bit too advanced for me ^^.

However, I've taken note of your messages and tried to make the code a bit ""cleaner"", I've used ChatGPT for some things (especially on the mutex issue), I took the opportunity to comment my code, so that it's a bit more readable.

There's the beginnings of a message system that I think is better than the old one, at least better than my first draft.

Shauren commented 1 month ago

Thread issues are still there, for example this map modification is still completely unprotected https://github.com/ElunaLuaEngine/Eluna/pull/496/files#diff-292f7269c07f69f285305bde0d329bf6235c448853ba6fd0df6e67590f8176acR34

From what I see this is also broken if you use RegisterStateEvent with a instance map id, only the last instance registered will be receiving events with multistate