gnometavern.gno
5.84 Kb · 188 lines
1// Realm gnometavern is the Gnome Tavern: a small on-chain tavern game
2// on Gno.land. Players register a gnome, work shifts serving drinks and
3// food from the menu, complete a rotating daily quest board, level up,
4// feast, and leave notes on the tavern wall.
5//
6// All game rules live in the pure package tavernlogic; this realm owns
7// the persistent state and the caller authentication, and renders the
8// tavern board for gnoweb.
9package gnometavern
10
11import (
12 "chain/runtime"
13 "strconv"
14 "strings"
15
16 "gno.land/p/g17dvvky49vjqj6a4cjhwcvzctteqjm9stee000h/tavernlogic"
17)
18
19// RealmPath is this realm's on-chain address, used in help text.
20const RealmPath = "gno.land/r/g17dvvky49vjqj6a4cjhwcvzctteqjm9stee000h/gnometavern"
21
22// MaxWallNotes caps how many wall notes are kept (oldest are dropped).
23const MaxWallNotes = 30
24
25// WallNote is one message pinned to the tavern wall.
26type WallNote struct {
27 Author string
28 Text string
29 Day int64
30}
31
32var (
33 gnomes = map[address]*tavernlogic.Gnome{}
34 joinOrder []address
35 wall []WallNote
36 totalServes int
37)
38
39// --- helpers --------------------------------------------------------------
40
41// callerOf returns the address of the user who directly called the
42// realm. Calls routed through another realm are rejected: a gnome's
43// career can only be advanced by its own owner.
44func callerOf(cur realm) address {
45 prev := cur.Previous()
46 if !prev.IsUserCall() {
47 panic("Gnome Tavern: only direct user calls are allowed")
48 }
49 return prev.Address()
50}
51
52// currentDay returns the current tavern day, derived from chain height.
53func currentDay() int64 {
54 return tavernlogic.DayAt(runtime.ChainHeight())
55}
56
57func mustGnome(addr address) *tavernlogic.Gnome {
58 g, ok := gnomes[addr]
59 if !ok {
60 panic("Gnome Tavern: no gnome registered for your address; call Register first")
61 }
62 return g
63}
64
65func validName(name string) bool {
66 if len(name) < 3 || len(name) > 16 {
67 return false
68 }
69 for _, r := range name {
70 ok := (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') ||
71 (r >= '0' && r <= '9') || r == ' ' || r == '_' || r == '-'
72 if !ok {
73 return false
74 }
75 }
76 return true
77}
78
79// --- actions --------------------------------------------------------------
80
81// Register creates a gnome for the caller's address. One gnome per
82// address, and names are unique across the tavern.
83func Register(cur realm, name string) string {
84 addr := callerOf(cur)
85 if _, ok := gnomes[addr]; ok {
86 panic("Gnome Tavern: you already have a gnome")
87 }
88 if !validName(name) {
89 panic("Gnome Tavern: names are 3-16 chars of letters, digits, space, _ or -")
90 }
91 for _, other := range joinOrder {
92 if gnomes[other].Name == name {
93 panic("Gnome Tavern: that name is already taken")
94 }
95 }
96 gnomes[addr] = tavernlogic.NewGnome(name, currentDay())
97 joinOrder = append(joinOrder, addr)
98 return "Welcome to the Gnome Tavern, " + name + "! You are gnome #" +
99 strconv.Itoa(len(joinOrder)) + ". Start with Serve(0) or Serve(1), and check today's quests on the board."
100}
101
102// Serve serves one menu item to a tavern guest. The item is chosen by
103// its menu ID (see the menu page). Pays coins and XP per the menu.
104func Serve(cur realm, itemID int) string {
105 addr := callerOf(cur)
106 g := mustGnome(addr)
107 coins, xp, err := g.Serve(currentDay(), itemID)
108 if err != nil {
109 panic("Gnome Tavern: " + err.Error())
110 }
111 item, _ := tavernlogic.MenuItemByID(itemID)
112 totalServes++
113 return g.Name + " served " + item.Name + ": +" + strconv.Itoa(coins) +
114 " coins, +" + strconv.Itoa(xp) + " XP. (Level " + strconv.Itoa(g.Level()) +
115 ", " + strconv.Itoa(g.Coins) + " coins, " + strconv.Itoa(g.ServesToday) +
116 " serves today.)"
117}
118
119// ClaimQuest claims the reward for today's quest in board slot 0, 1 or
120// 2, once its requirement has been met by serving.
121func ClaimQuest(cur realm, slot int) string {
122 addr := callerOf(cur)
123 g := mustGnome(addr)
124 q, err := g.ClaimQuest(currentDay(), slot)
125 if err != nil {
126 panic("Gnome Tavern: " + err.Error())
127 }
128 return "Quest complete: " + q.Title + "! " + g.Name + " earned " +
129 strconv.Itoa(q.CoinReward) + " coins and " + strconv.Itoa(q.XPReward) +
130 " XP. (Level " + strconv.Itoa(g.Level()) + ", " + strconv.Itoa(g.Coins) + " coins.)"
131}
132
133// Feast spends 50 coins on a Gnome Feast, gaining 15 XP. Once per day.
134func Feast(cur realm) string {
135 addr := callerOf(cur)
136 g := mustGnome(addr)
137 if err := g.Feast(currentDay()); err != nil {
138 panic("Gnome Tavern: " + err.Error())
139 }
140 return g.Name + " feasts like a true gnome: -" + strconv.Itoa(tavernlogic.FeastCost) +
141 " coins, +" + strconv.Itoa(tavernlogic.FeastXP) + " XP. (Level " +
142 strconv.Itoa(g.Level()) + ", " + strconv.Itoa(g.Coins) + " coins.)"
143}
144
145// PostNote pins a one-line note (max 140 chars) to the tavern wall.
146func PostNote(cur realm, text string) string {
147 addr := callerOf(cur)
148 g := mustGnome(addr)
149 if len(text) == 0 || len(text) > 140 {
150 panic("Gnome Tavern: notes are 1-140 characters")
151 }
152 if strings.ContainsAny(text, "\n\r") {
153 panic("Gnome Tavern: notes must fit on one line")
154 }
155 wall = append(wall, WallNote{Author: g.Name, Text: text, Day: currentDay()})
156 if len(wall) > MaxWallNotes {
157 wall = wall[len(wall)-MaxWallNotes:]
158 }
159 return "Note pinned to the tavern wall."
160}
161
162// --- views ----------------------------------------------------------------
163
164// GnomeCount returns how many gnomes are registered.
165func GnomeCount() int {
166 return len(gnomes)
167}
168
169// TotalServes returns how many items have been served, all time.
170func TotalServes() int {
171 return totalServes
172}
173
174// CurrentDay returns the current tavern day number.
175func CurrentDay() int64 {
176 return currentDay()
177}
178
179// Profile returns a one-line summary of the gnome at addr.
180func Profile(addr address) string {
181 g, ok := gnomes[addr]
182 if !ok {
183 return "No gnome registered for " + addr.String() + "."
184 }
185 return g.Name + " — level " + strconv.Itoa(g.Level()) + ", " +
186 strconv.Itoa(g.XP) + " XP, " + strconv.Itoa(g.Coins) + " coins, " +
187 strconv.Itoa(g.TotalServes) + " lifetime serves."
188}