2025-06-28 11:24:06 +03:00
package event
2025-06-27 21:36:15 +03:00
import (
"code.haedhutner.dev/mvv/LastMUD/internal/ecs"
"code.haedhutner.dev/mvv/LastMUD/internal/game/data"
2025-06-28 11:24:06 +03:00
"code.haedhutner.dev/mvv/LastMUD/internal/game/logic/world"
2025-06-27 21:36:15 +03:00
)
2025-06-28 11:24:06 +03:00
func HandlePlayerConnect ( w * ecs . World , event ecs . Entity ) ( err error ) {
connectionId , ok := ecs . GetComponent [ data . ConnectionIdComponent ] ( w , event )
2025-06-27 21:36:15 +03:00
if ! ok {
return createEventHandlerError ( data . EventPlayerConnect , "Event does not contain connectionId" )
}
2025-06-28 11:24:06 +03:00
world . CreateJoiningPlayer ( w , connectionId . ConnectionId )
2025-06-29 18:21:22 +03:00
world . CreateGameOutput ( w , connectionId . ConnectionId , "Welcome to LastMUD!" )
world . CreateGameOutput ( w , connectionId . ConnectionId , "Before interacting with the game, you must either login or create a new account. Do so using the 'register' and 'login' command(s)." )
2025-06-27 21:36:15 +03:00
return
}
2025-06-28 11:24:06 +03:00
func HandlePlayerDisconnect ( w * ecs . World , event ecs . Entity ) ( err error ) {
connectionId , ok := ecs . GetComponent [ data . ConnectionIdComponent ] ( w , event )
2025-06-27 21:36:15 +03:00
if ! ok {
return createEventHandlerError ( data . EventPlayerDisconnect , "Event does not contain connectionId" )
}
playerEntity := ecs . QueryFirstEntityWithComponent (
2025-06-28 11:24:06 +03:00
w ,
2025-06-27 21:36:15 +03:00
func ( c data . ConnectionIdComponent ) bool { return c . ConnectionId == connectionId . ConnectionId } ,
)
if playerEntity == ecs . NilEntity ( ) {
return createEventHandlerError ( data . EventPlayerDisconnect , "Connection id cannot be associated with a player entity" )
}
2025-06-28 11:24:06 +03:00
ecs . DeleteEntity ( w , playerEntity )
2025-06-27 21:36:15 +03:00
return
}