aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_estate_directory.go
blob: b28e634c6135699ff29e5d495fffcd2755c18164 (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
package game

import (
	"fmt"
	"os"
	"path/filepath"
	"sort"
	"strings"

	"thehouseoficarus/internal/behavior"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"

	"gopkg.in/yaml.v3"
)

func (g *Game) startEstateDirectory(sess *net.Session, verb string) {
	p := sess.Player

	homeowners := g.scanHomeowners("owns_house_local_neighborhood")
	ownedByMe := false
	for _, h := range homeowners {
		if h == p.Name {
			ownedByMe = true
			break
		}
	}

	if verb == "talk" {
		g.showEstateDirectoryTalk(sess, p, homeowners, ownedByMe)
		return
	}

	if verb == "use" {
		if ownedByMe {
			houseRoom := g.getOwnedHouseRoom(p, "owns_house_local_neighborhood")
			if houseRoom > 0 {
				g.cancelAction(p)
				g.teleportPlayer(sess, p, houseRoom)
				return
			}
		}
		sess.WriteLine("You don't own a house here. Talk to the estate broker to purchase one.")
		return
	}
}

func (g *Game) lookEstateDirectory(sess *net.Session) {
	homeowners := g.scanHomeowners("owns_house_local_neighborhood")
	sess.WriteLine("")
	sess.WriteLine(g.colorize(sess, "dialog", "Estate Directory"))
	sess.WriteLine(fmt.Sprintf("  A holographic terminal displaying property records for the Local Neighborhood."))
	sess.WriteLine("")
	if len(homeowners) == 0 {
		sess.WriteLine("  No registered homeowners in this area.")
	} else {
		sess.WriteLine("  Registered homeowners:")
		for i, name := range homeowners {
			sess.WriteLine(fmt.Sprintf("    %d. %s", i+1, name))
		}
	}
	sess.WriteLine("")
	sess.WriteLine("  Use the directory to enter your home, or talk to the estate broker to purchase.")
}

func (g *Game) showEstateDirectoryTalk(sess *net.Session, p *player.Player, homeowners []string, ownedByMe bool) {
	sess.WriteLine("")
	sess.WriteLine(g.colorize(sess, "dialog", "The directory terminal hums quietly, displaying a list of registered homeowners..."))

	if ownedByMe {
		sess.WriteLine("")
		sess.WriteLine("  You are a registered homeowner in this neighborhood.")
		sess.WriteLine("  Type 'enter' to go to your house, or 'leave' to step away.")
		sess.State = net.StateTalk
		p.Action = &behavior.Action{
			Type:       "talk",
			TargetID:   "estate_directory",
			TargetName: "Estate Directory",
			Data:       &behavior.TalkData{Node: "start", EstateDirectory: true},
		}
		sess.Write("\nChoice: ")
		return
	}

	sess.WriteLine("")
	if len(homeowners) > 0 {
		sess.WriteLine("  Currently owned by: " + strings.Join(homeowners, ", "))
	}
	sess.WriteLine("  You don't own a house here yet.")
	sess.WriteLine("  Talk to the estate broker to purchase a plot for 10 credits.")
}

func (g *Game) handleEstateDirectoryTalk(sess *net.Session, input string) {
	input = strings.TrimSpace(strings.ToLower(input))

	switch input {
	case "enter":
		p := sess.Player
		if p == nil {
			return
		}
		houseRoom := g.getOwnedHouseRoom(p, "owns_house_local_neighborhood")
		if houseRoom > 0 {
			sess.State = net.StateGame
			p.Action = nil
			g.teleportPlayer(sess, p, houseRoom)
			return
		}
		sess.State = net.StateGame
		p.Action = nil
		g.writePrompt(sess)
	case "leave", "bye", "exit", "quit":
		p := sess.Player
		if p != nil {
			p.Action = nil
		}
		sess.State = net.StateGame
		g.writePrompt(sess)
	default:
		sess.WriteLine("Type 'enter' to go to your house, or 'leave' to step away.")
		sess.Write("\nChoice: ")
	}
}

func (g *Game) scanHomeowners(flagKey string) []string {
	var homeowners []string

	charsDir := filepath.Join(g.DataDir, "players", "characters")
	entries, err := os.ReadDir(charsDir)
	if err != nil {
		return homeowners
	}

	for _, entry := range entries {
		if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".yaml") {
			continue
		}

		name := strings.TrimSuffix(entry.Name(), ".yaml")
		data, err := os.ReadFile(filepath.Join(charsDir, entry.Name()))
		if err != nil {
			continue
		}

		var p struct {
			Flags map[string]any `yaml:"flags"`
		}
		if err := yaml.Unmarshal(data, &p); err != nil {
			continue
		}

		if p.Flags != nil {
			if val, ok := p.Flags[flagKey]; ok && val != nil {
				owned := false
				switch v := val.(type) {
				case bool:
					owned = v
				case string:
					owned = v != "" && v != "false"
				}
				if owned {
					homeowners = append(homeowners, name)
				}
			}
		}
	}

	sort.Strings(homeowners)
	return homeowners
}

func (g *Game) getOwnedHouseRoom(p *player.Player, flagKey string) int {
	p.EnsureFlags()
	val, ok := p.Flags[flagKey]
	if !ok || val == nil {
		return 0
	}

	v, ok := val.(string)
	if !ok || v == "" {
		return 0
	}

	houseMap := map[string]int{
		"local_neighborhood": 300,
	}
	return houseMap[v]
}

func (g *Game) teleportPlayer(sess *net.Session, p *player.Player, roomID int) {
	if g.Hub != nil {
		for _, other := range g.Hub.PlayersInRoom(p.RoomID) {
			if other != sess {
				other.WriteLine(fmt.Sprintf("%s disappears in a shimmer of light.", p.Name))
			}
		}
	}

	p.RoomID = roomID
	p.Stats.RecordRoomVisit(roomID)
	if g.Hub != nil {
		g.Hub.LeaveRoom(sess)
		g.Hub.EnterRoom(sess, p.RoomID)
	}

	g.World.SeedGroundItems(p.RoomID)
	g.seedRoomMobs(p.RoomID)
	g.seedRoomObjects(p.RoomID)
	g.AccountStore.SaveCharacter(p)
	g.doLook(sess)
	g.runEnterSteps(sess, p.RoomID)
	g.writePrompt(sess)
}