aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_room_mob.go
blob: ced170fe4800d01fed814d91325dbd299b90ab31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package game

import (
	"fmt"
	"os"

	"gopkg.in/yaml.v3"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/world"
)

func (g *Game) roomMob(sess *net.Session, action string, args []string) {
	switch action {
	case "addmob":
		g.roomAddMob(sess, args)
	case "remmob":
		g.roomRemMob(sess, args)
	}
}

func (g *Game) roomAddMob(sess *net.Session, args []string) {
	if len(args) == 0 {
		sess.WriteLine("Usage: room addmob <mob_id>")
		return
	}
	mobID := args[0]
	if _, err := g.MobStore.LoadDef(mobID); err != nil {
		sess.WriteLine(fmt.Sprintf("Mob '%s' not found in data/mobs/.", mobID))
		return
	}
	p := sess.Player
	room, _, err := g.loadRoomWrite(p)
	if err != nil {
		sess.WriteLine(fmt.Sprintf("Error loading room: %v", err))
		return
	}
	for _, m := range room.Mobs {
		if m.ID == mobID {
			sess.WriteLine(fmt.Sprintf("Mob '%s' is already in this room.", mobID))
			return
		}
	}
	room.Mobs = append(room.Mobs, world.RoomMob{ID: mobID})

	path, _ := g.World.GetRoomPath(p.RoomID)
	data, _ := yaml.Marshal(room)
	os.WriteFile(path, data, 0644)
	sess.WriteLine(fmt.Sprintf("Added mob '%s' to room.", mobID))
}

func (g *Game) roomRemMob(sess *net.Session, args []string) {
	if len(args) == 0 {
		sess.WriteLine("Usage: room remmob <mob_id>")
		return
	}
	mobID := args[0]
	p := sess.Player
	room, _, err := g.loadRoomWrite(p)
	if err != nil {
		sess.WriteLine(fmt.Sprintf("Error loading room: %v", err))
		return
	}
	found := false
	filtered := room.Mobs[:0]
	for _, m := range room.Mobs {
		if m.ID == mobID {
			found = true
		} else {
			filtered = append(filtered, m)
		}
	}
	if !found {
		sess.WriteLine(fmt.Sprintf("Mob '%s' not found in this room.", mobID))
		return
	}
	room.Mobs = filtered

	path, _ := g.World.GetRoomPath(p.RoomID)
	data, _ := yaml.Marshal(room)
	os.WriteFile(path, data, 0644)
	sess.WriteLine(fmt.Sprintf("Removed mob '%s' from room.", mobID))
}