tavernlogic.gno
7.68 Kb · 266 lines
1// Package tavernlogic holds the pure game rules of the Gnome Tavern:
2// the menu, the level ladder, the rotating daily quest board and the
3// per-gnome state machine (serving drinks, claiming quests, feasting).
4//
5// It is a pure package (gno.land/p/...): it owns no chain state of its
6// own. The Gnome Tavern realm keeps the state (a *Gnome per player) and
7// calls into these functions, which keeps every rule unit-testable
8// without a chain.
9package tavernlogic
10
11import "errors"
12
13// DayBlocks is the length of one tavern day, in blocks. Quest boards,
14// daily serve counters and feast allowances all reset when the day rolls
15// over. A short day keeps the game lively on a testnet: at Onyx's block
16// pace a tavern day lasts roughly an hour.
17const DayBlocks int64 = 2000
18
19// MaxServesPerDay caps how many items one gnome may serve per tavern day.
20const MaxServesPerDay = 40
21
22// FeastCost and FeastXP describe the Gnome Feast: a coin sink that lets a
23// hard-working gnome convert earnings into experience, once per day.
24const (
25 FeastCost = 50
26 FeastXP = 15
27)
28
29// DayAt maps a chain height to its tavern day number.
30func DayAt(height int64) int64 {
31 if height < 0 {
32 return 0
33 }
34 return height / DayBlocks
35}
36
37// --- Levels ---------------------------------------------------------------
38
39// XPPerLevel is the amount of XP between consecutive levels.
40const XPPerLevel = 40
41
42// LevelForXP returns the level of a gnome holding xp experience points.
43// Level 1 is the starting level; every XPPerLevel XP earns a new level.
44func LevelForXP(xp int) int {
45 if xp < 0 {
46 xp = 0
47 }
48 return 1 + xp/XPPerLevel
49}
50
51// XPForLevel returns the cumulative XP at which level l is reached.
52func XPForLevel(l int) int {
53 if l <= 1 {
54 return 0
55 }
56 return (l - 1) * XPPerLevel
57}
58
59// --- Menu -----------------------------------------------------------------
60
61// MenuItem is one serveable entry on the tavern menu. Serving it pays the
62// gnome Coins tavern coins and XP experience, and requires MinLevel.
63type MenuItem struct {
64 ID int
65 Name string
66 Detail string
67 MinLevel int
68 Coins int
69 XP int
70}
71
72var menu = []MenuItem{
73 {0, "Barley Soup", "A humble bowl that never empties.", 1, 3, 2},
74 {1, "Gnomebrew Ale", "Foamy, nutty, proudly gnome-sized.", 1, 5, 3},
75 {2, "Honeyed Ham", "Glazed with wildflower honey.", 2, 9, 5},
76 {3, "Emberwhisky", "Warms the beard from the inside.", 3, 14, 8},
77 {4, "Moonpetal Tea", "Brewed under a full moon, allegedly.", 4, 20, 12},
78 {5, "Dragonfire Stew", "Sign the waiver. Then enjoy.", 5, 30, 18},
79}
80
81// Menu returns the full tavern menu, in serving order.
82func Menu() []MenuItem {
83 return menu
84}
85
86// MenuItemByID looks up a menu item by its ID.
87func MenuItemByID(id int) (MenuItem, bool) {
88 if id < 0 || id >= len(menu) {
89 return MenuItem{}, false
90 }
91 return menu[id], true
92}
93
94// --- Quests ---------------------------------------------------------------
95
96// Quest kinds: what the progress counter measures.
97const (
98 QuestTotalServes = 0 // serve Count items of any kind today
99 QuestItemServes = 1 // serve Count of item ItemID today
100)
101
102// Quest is one entry on the daily quest board.
103type Quest struct {
104 ID int
105 Title string
106 Desc string
107 Kind int
108 ItemID int // only for QuestItemServes
109 Count int
110 CoinReward int
111 XPReward int
112}
113
114var questPool = []Quest{
115 {0, "First Round", "Serve 4 items today.", QuestTotalServes, 0, 4, 15, 8},
116 {1, "Ale Baron", "Serve 4 Gnomebrew Ales today.", QuestItemServes, 1, 4, 25, 12},
117 {2, "Morning Rush", "Serve 8 items today.", QuestTotalServes, 0, 8, 30, 15},
118 {3, "Ham It Up", "Serve 3 Honeyed Hams today.", QuestItemServes, 2, 3, 30, 14},
119 {4, "Full House", "Serve 14 items today.", QuestTotalServes, 0, 14, 55, 26},
120 {5, "Whisky Business", "Serve 3 Emberwhiskies today.", QuestItemServes, 3, 3, 45, 22},
121 {6, "Tea Ceremony", "Serve 3 Moonpetal Teas today.", QuestItemServes, 4, 3, 60, 30},
122 {7, "Dragon Slayer's Feast", "Serve 2 Dragonfire Stews today.", QuestItemServes, 5, 2, 70, 36},
123}
124
125// QuestPool returns every quest that can appear on the board.
126func QuestPool() []Quest {
127 return questPool
128}
129
130// QuestsForDay deterministically picks the 3 quests on the board for a
131// tavern day. The same day always yields the same board, on every node,
132// with no randomness source beyond the day number itself (a small LCG).
133func QuestsForDay(day int64) [3]Quest {
134 var out [3]Quest
135 state := day*2654435761 + 97
136 used := map[int]bool{}
137 for slot := 0; slot < 3; slot++ {
138 for {
139 state = state*1103515245 + 12345
140 idx := int(((state >> 16) & 0x7fffffff) % int64(len(questPool)))
141 if !used[idx] {
142 used[idx] = true
143 out[slot] = questPool[idx]
144 break
145 }
146 }
147 }
148 return out
149}
150
151// --- Gnome state ----------------------------------------------------------
152
153// Gnome is one player's tavern career. The realm owns one *Gnome per
154// registered address; all fields are plain data so the realm can persist
155// it directly.
156type Gnome struct {
157 Name string
158 XP int
159 Coins int
160
161 TotalServes int
162
163 // Daily counters; they belong to LastDay and reset on rollover.
164 LastDay int64
165 ServesToday int
166 ItemToday map[int]int
167 ClaimedToday map[int]bool // quest IDs already claimed today
168 FeastedToday bool
169}
170
171// NewGnome creates a fresh gnome registered under the given name.
172func NewGnome(name string, day int64) *Gnome {
173 return &Gnome{
174 Name: name,
175 LastDay: day,
176 ItemToday: map[int]int{},
177 ClaimedToday: map[int]bool{},
178 }
179}
180
181// Level returns the gnome's current level.
182func (g *Gnome) Level() int {
183 return LevelForXP(g.XP)
184}
185
186// rollover resets the daily counters when a new tavern day has begun.
187func (g *Gnome) rollover(day int64) {
188 if g.LastDay == day {
189 return
190 }
191 g.LastDay = day
192 g.ServesToday = 0
193 g.ItemToday = map[int]int{}
194 g.ClaimedToday = map[int]bool{}
195 g.FeastedToday = false
196}
197
198// Serve records the gnome serving one menu item on the given day and
199// returns what was earned. It enforces the menu's level requirement and
200// the daily serve cap.
201func (g *Gnome) Serve(day int64, itemID int) (coinsEarned, xpEarned int, err error) {
202 item, ok := MenuItemByID(itemID)
203 if !ok {
204 return 0, 0, errors.New("no such menu item")
205 }
206 g.rollover(day)
207 if g.Level() < item.MinLevel {
208 return 0, 0, errors.New("your level is too low for this item")
209 }
210 if g.ServesToday >= MaxServesPerDay {
211 return 0, 0, errors.New("you have served enough for one day; come back tomorrow")
212 }
213 g.ServesToday++
214 g.TotalServes++
215 g.ItemToday[itemID]++
216 g.Coins += item.Coins
217 g.XP += item.XP
218 return item.Coins, item.XP, nil
219}
220
221// ProgressFor reports the gnome's progress toward a quest today.
222func (g *Gnome) ProgressFor(day int64, q Quest) int {
223 if g.LastDay != day {
224 return 0
225 }
226 if q.Kind == QuestItemServes {
227 return g.ItemToday[q.ItemID]
228 }
229 return g.ServesToday
230}
231
232// ClaimQuest pays out the quest in board slot (0-2) for the given day, if
233// the gnome has met its requirement and has not claimed it yet today.
234func (g *Gnome) ClaimQuest(day int64, slot int) (Quest, error) {
235 if slot < 0 || slot > 2 {
236 return Quest{}, errors.New("quest slot must be 0, 1 or 2")
237 }
238 g.rollover(day)
239 q := QuestsForDay(day)[slot]
240 if g.ClaimedToday[q.ID] {
241 return Quest{}, errors.New("you already claimed this quest today")
242 }
243 if g.ProgressFor(day, q) < q.Count {
244 return Quest{}, errors.New("quest requirement not met yet")
245 }
246 g.ClaimedToday[q.ID] = true
247 g.Coins += q.CoinReward
248 g.XP += q.XPReward
249 return q, nil
250}
251
252// Feast lets the gnome spend FeastCost coins on a Gnome Feast, gaining
253// FeastXP experience. Once per tavern day.
254func (g *Gnome) Feast(day int64) error {
255 g.rollover(day)
256 if g.FeastedToday {
257 return errors.New("you already feasted today")
258 }
259 if g.Coins < FeastCost {
260 return errors.New("not enough coins for a feast")
261 }
262 g.Coins -= FeastCost
263 g.XP += FeastXP
264 g.FeastedToday = true
265 return nil
266}