2025-06-28 11:24:06 +03:00
|
|
|
package world
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.haedhutner.dev/mvv/LastMUD/internal/ecs"
|
|
|
|
"code.haedhutner.dev/mvv/LastMUD/internal/game/data"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateJoiningPlayer(world *ecs.World, connectionId uuid.UUID) (entity ecs.Entity) {
|
|
|
|
entity = ecs.NewEntity()
|
|
|
|
|
|
|
|
ecs.SetComponent(world, entity, data.ConnectionIdComponent{ConnectionId: connectionId})
|
|
|
|
ecs.SetComponent(world, entity, data.PlayerStateComponent{State: data.PlayerStateJoining})
|
|
|
|
ecs.SetComponent(world, entity, data.NameComponent{Name: connectionId.String()})
|
|
|
|
ecs.SetComponent(world, entity, data.IsPlayerComponent{})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2025-06-29 18:21:22 +03:00
|
|
|
|
|
|
|
func SendMessageToPlayer(world *ecs.World, player ecs.Entity, message string) {
|
|
|
|
connId, ok := ecs.GetComponent[data.ConnectionIdComponent](world, player)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateGameOutput(world, connId.ConnectionId, message)
|
|
|
|
}
|
2025-06-30 08:40:15 +03:00
|
|
|
|
|
|
|
func SendDisconnectMessageToPlayer(world *ecs.World, player ecs.Entity, message string) {
|
|
|
|
connId, ok := ecs.GetComponent[data.ConnectionIdComponent](world, player)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateClosingGameOutput(world, connId.ConnectionId, []byte(message))
|
|
|
|
}
|
2025-07-02 20:42:25 +03:00
|
|
|
|
|
|
|
func FindPlayerByConnectionId(w *ecs.World, connectionId uuid.UUID) (entity ecs.Entity) {
|
|
|
|
player := ecs.NilEntity()
|
|
|
|
|
|
|
|
for p := range ecs.IterateEntitiesWithComponent[data.IsPlayerComponent](w) {
|
|
|
|
playerConnId, ok := ecs.GetComponent[data.ConnectionIdComponent](w, p)
|
|
|
|
|
|
|
|
if ok && playerConnId.ConnectionId == connectionId {
|
|
|
|
player = p
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return player
|
|
|
|
}
|