aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_undig.go
blob: 2215c51344fc4ff2aa917c0d70cd15d3f0c3cfd2 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package game

import (
	"fmt"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"

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

func (g *Game) executeUndig(sess *net.Session, args []string, rawInput string) {
	if !g.checkAdmin(sess) {
		sess.WriteLine("Unknown command.")
		return
	}
	p := sess.Player
	if p == nil {
		return
	}
	if len(args) == 0 {
		sess.WriteLine("Undig which direction?")
		return
	}

	dir := g.World.ResolveExit(args[0])
	if dir == "" {
		sess.WriteLine("Invalid direction. Use n/s/e/w/ne/nw/se/sw/u/d.")
		return
	}

	curRoom, err := g.World.LoadRoom(p.RoomID)
	if err != nil {
		sess.WriteLine("Error loading current room.")
		return
	}

	exitDef, ok := curRoom.Exits[dir]
	if !ok {
		sess.WriteLine(fmt.Sprintf("There is no exit to the %s from here.", dir))
		return
	}

	targetID := exitDef.Room
	targetRoom, err := g.World.LoadRoom(targetID)
	if err != nil {
		sess.WriteLine(fmt.Sprintf("Target room %d not found.", targetID))
		return
	}

	sess.PendingUndigDir = string(dir)
	sess.PendingUndigRoom = targetID
	sess.State = net.StateUndigConfirm

	sess.WriteLine(fmt.Sprintf("You are about to DELETE Room #%d (%s) and all exits leading to it.", targetID, targetRoom.Name))
	g.listUndigOrphans(sess, targetID)
	sess.WriteLine("")
	sess.WriteLine("Undig? [y/N]")
}

func (g *Game) listUndigOrphans(sess *net.Session, targetID int) {
	roomIndex := g.World.RoomIndex()

	inbound := make(map[int]bool)
	roomsDir := filepath.Join(g.DataDir, "rooms")
	re := regexp.MustCompile(fmt.Sprintf(`\b%d\b`, targetID))
	_ = filepath.WalkDir(roomsDir, func(path string, d os.DirEntry, err error) error {
		if err != nil || d.IsDir() || filepath.Ext(path) != ".yaml" {
			return nil
		}
		data, rerr := os.ReadFile(path)
		if rerr != nil {
			return nil
		}
		if re.MatchString(string(data)) {
			idStr := strings.TrimSuffix(filepath.Base(path), ".yaml")
			if rid, serr := strconv.Atoi(idStr); serr == nil && rid != targetID {
				inbound[rid] = true
			}
		}
		return nil
	})

	if len(inbound) == 0 {
		return
	}

	var orphans []int
	for rid := range inbound {
		room, err := g.World.LoadRoom(rid)
		if err != nil {
			continue
		}
		hasOtherExit := false
		for _, exitDef := range room.Exits {
			if roomIndex[exitDef.Room] && exitDef.Room != targetID {
				hasOtherExit = true
				break
			}
		}
		if !hasOtherExit {
			orphans = append(orphans, rid)
		}
	}

	if len(orphans) > 0 {
		sess.WriteLine(g.colorize(sess, "warning",
			fmt.Sprintf("WARNING: The following rooms will become unreachable after deletion:")))
		for _, rid := range orphans {
			room, err := g.World.LoadRoom(rid)
			name := fmt.Sprintf("#%d", rid)
			if err == nil {
				name = fmt.Sprintf("#%d (%s)", rid, room.Name)
			}
			sess.WriteLine(fmt.Sprintf("  %s", name))
		}
	}
}

func (g *Game) handleUndigConfirm(sess *net.Session, input string) {
	p := sess.Player
	if p == nil {
		sess.State = net.StateGame
		g.writePrompt(sess)
		return
	}

	choice := strings.ToLower(strings.TrimSpace(input))
	dir := sess.PendingUndigDir
	targetID := sess.PendingUndigRoom
	sess.PendingUndigDir = ""
	sess.PendingUndigRoom = 0
	sess.State = net.StateGame

	if dir == "" || targetID == 0 {
		g.writePrompt(sess)
		return
	}

	if choice != "y" && choice != "yes" {
		sess.WriteLine("Undig cancelled.")
		g.writePrompt(sess)
		return
	}

	g.performUndig(sess, p, targetID)
	g.writePrompt(sess)
}

func (g *Game) performUndig(sess *net.Session, p *player.Player, targetID int) {
	targetRoom, err := g.World.LoadRoom(targetID)
	roomName := fmt.Sprintf("#%d", targetID)
	if err == nil {
		roomName = fmt.Sprintf("#%d (%s)", targetID, targetRoom.Name)
	} else {
		sess.WriteLine(fmt.Sprintf("Target room %d no longer exists.", targetID))
		return
	}

	roomsDir := filepath.Join(g.DataDir, "rooms")
	re := regexp.MustCompile(fmt.Sprintf(`\b%d\b`, targetID))
	var cleaned int
	_ = filepath.WalkDir(roomsDir, func(path string, d os.DirEntry, err error) error {
		if err != nil || d.IsDir() || filepath.Ext(path) != ".yaml" {
			return nil
		}
		data, rerr := os.ReadFile(path)
		if rerr != nil {
			return nil
		}
		if !re.MatchString(string(data)) {
			return nil
		}

		idStr := strings.TrimSuffix(filepath.Base(path), ".yaml")
		rid, serr := strconv.Atoi(idStr)
		if serr != nil || rid == targetID {
			return nil
		}

		var room world.Room
		if yerr := yaml.Unmarshal(data, &room); yerr != nil {
			return nil
		}
		if room.Exits == nil {
			return nil
		}

		changed := false
		for ed, exitDef := range room.Exits {
			if exitDef.Room == targetID {
				delete(room.Exits, ed)
				changed = true
			}
		}

		if changed {
			newData, merr := yaml.Marshal(&room)
			if merr != nil {
				return nil
			}
			if werr := os.WriteFile(path, newData, 0644); werr != nil {
				return werr
			}
			cleaned++
		}
		return nil
	})

	targetPath, ok := g.World.GetRoomPath(targetID)
	if !ok {
		sess.WriteLine(fmt.Sprintf("Room file for %d not found.", targetID))
		return
	}
	if rerr := os.Remove(targetPath); rerr != nil {
		sess.WriteLine(fmt.Sprintf("Error deleting room file: %v", rerr))
		return
	}

	g.World.RebuildRoomIndex(g.DataDir)

	if p.RoomID == targetID {
		p.RoomID = 0
	}

	for _, other := range g.Hub.AllSessions() {
		if other.Player != nil && other.Player.RoomID == targetID {
			other.Player.RoomID = p.RoomID
			if g.Hub != nil {
				g.Hub.EnterRoom(other, p.RoomID)
			}
			other.WriteLine(fmt.Sprintf("The room around you dissolves. You find yourself elsewhere."))
			g.doLook(other)
		}
	}

	g.MobStore.RemoveMobsInRoom(targetID)
	g.World.ClearRoomState(targetID)

	sess.WriteLine(fmt.Sprintf("Deleted room %s and %d exit(s) pointing to it.", roomName, cleaned))
}