rps.gno
5.05 Kb · 215 lines
1package rps
2
3import (
4 "chain"
5 "chain/runtime"
6 "chain/runtime/unsafe"
7 "strconv"
8 "strings"
9
10 "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
11 "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/rps/v0"
12 "gno.land/p/nt/avl/v0"
13)
14
15// Outcomes.
16const (
17 outLose = 0 // player loses
18 outDraw = 1
19 outWin = 2 // player wins
20)
21
22// tally holds a single player's cumulative record.
23type tally struct {
24 wins int
25 losses int
26 draws int
27 plays int // total rounds this player has played (drives entropy)
28}
29
30// round is one recorded game (for the recent-rounds table).
31type round struct {
32 player address
33 height int64
34 player_m rps.Move
35 house_m rps.Move
36 outcome int
37}
38
39// State.
40var (
41 players = avl.NewTree() // address string -> *tally
42 rounds []round // append-only history; Render shows the tail
43
44 // Global tally (caller-agnostic).
45 totalWins int // player wins
46 totalLosses int // player losses
47 totalDraws int
48 totalRounds int
49)
50
51const maxRecent = 8
52
53// houseMove derives the house move deterministically from the chain height
54// and the caller's play count (so repeated calls in the same block differ).
55func houseMove(height int64, playCount int) rps.Move {
56 x := height + int64(playCount)*7
57 m := x % 3
58 if m < 0 {
59 m += 3
60 }
61 return rps.Move(m)
62}
63
64// decide returns the outcome from the player's perspective.
65func decide(playerMove, houseM rps.Move) int {
66 switch rps.Beats(playerMove, houseM) {
67 case 1:
68 return outWin
69 case 2:
70 return outLose
71 }
72 return outDraw
73}
74
75func getTally(key string) *tally {
76 if v := players.Get(key); v != nil {
77 return v.(*tally)
78 }
79 return nil
80}
81
82// Play plays one round against the chain. choice is "rock"|"paper"|"scissors"
83// (single-letter shortcuts accepted). It panics on an invalid choice.
84func Play(cur realm, choice string) {
85 mv, ok := rps.Parse(choice)
86 if !ok {
87 panic("invalid choice: use rock, paper, or scissors")
88 }
89
90 caller := unsafe.PreviousRealm().Address()
91 key := caller.String()
92
93 t := getTally(key)
94 if t == nil {
95 t = &tally{}
96 players.Set(key, t)
97 }
98
99 height := runtime.ChainHeight()
100 hm := houseMove(height, t.plays)
101 outcome := decide(mv, hm)
102
103 t.plays++
104 switch outcome {
105 case outWin:
106 t.wins++
107 totalWins++
108 case outLose:
109 t.losses++
110 totalLosses++
111 default:
112 t.draws++
113 totalDraws++
114 }
115 totalRounds++
116
117 rounds = append(rounds, round{
118 player: caller,
119 height: height,
120 player_m: mv,
121 house_m: hm,
122 outcome: outcome,
123 })
124
125 chain.Emit(
126 "RoundPlayed",
127 "player", key,
128 "choice", mv.String(),
129 "house", hm.String(),
130 "outcome", outcomeName(outcome),
131 )
132}
133
134func outcomeName(o int) string {
135 switch o {
136 case outWin:
137 return "win"
138 case outLose:
139 return "lose"
140 default:
141 return "draw"
142 }
143}
144
145func winRate(wins, plays int) string {
146 if plays == 0 {
147 return "0%"
148 }
149 return strconv.Itoa(wins*100/plays) + "%"
150}
151
152// Render returns a Markdown dashboard: global tally, per-player table, and the
153// last few rounds. It is caller-agnostic (no cur).
154func Render(path string) string {
155 var b strings.Builder
156
157 b.WriteString("# Rock-Paper-Scissors — play vs the chain\n\n")
158 b.WriteString("Call `Play(cur, \"rock\"|\"paper\"|\"scissors\")`. ")
159 b.WriteString("The house move is derived deterministically from the current block height ")
160 b.WriteString("and your personal play count.\n\n")
161
162 // Global tally.
163 b.WriteString("## Global tally\n\n")
164 b.WriteString("| Rounds | Player wins | Player losses | Draws | Player win rate |\n")
165 b.WriteString("|---|---|---|---|---|\n")
166 b.WriteString("| " + strconv.Itoa(totalRounds))
167 b.WriteString(" | " + strconv.Itoa(totalWins))
168 b.WriteString(" | " + strconv.Itoa(totalLosses))
169 b.WriteString(" | " + strconv.Itoa(totalDraws))
170 b.WriteString(" | " + winRate(totalWins, totalRounds) + " |\n\n")
171
172 // Per-player table (deterministic order via avl iteration).
173 b.WriteString("## Players\n\n")
174 if players.Size() == 0 {
175 b.WriteString("_No games played yet. Be the first!_\n\n")
176 } else {
177 b.WriteString("| Player | W | L | D | Rounds | Win rate |\n")
178 b.WriteString("|---|---|---|---|---|---|\n")
179 players.Iterate("", "", func(key string, v interface{}) bool {
180 t := v.(*tally)
181 b.WriteString("| " + ui.AddrOf(key))
182 b.WriteString(" | " + strconv.Itoa(t.wins))
183 b.WriteString(" | " + strconv.Itoa(t.losses))
184 b.WriteString(" | " + strconv.Itoa(t.draws))
185 b.WriteString(" | " + strconv.Itoa(t.plays))
186 b.WriteString(" | " + winRate(t.wins, t.plays) + " |\n")
187 return false
188 })
189 b.WriteString("\n")
190 }
191
192 // Recent rounds (tail).
193 b.WriteString("## Recent rounds\n\n")
194 if len(rounds) == 0 {
195 b.WriteString("_None yet._\n")
196 } else {
197 b.WriteString("| Height | Player | Choice | House | Result |\n")
198 b.WriteString("|---|---|---|---|---|\n")
199 start := len(rounds) - maxRecent
200 if start < 0 {
201 start = 0
202 }
203 // Show newest first.
204 for i := len(rounds) - 1; i >= start; i-- {
205 r := rounds[i]
206 b.WriteString("| " + strconv.FormatInt(r.height, 10))
207 b.WriteString(" | " + ui.Addr(r.player))
208 b.WriteString(" | " + r.player_m.String())
209 b.WriteString(" | " + r.house_m.String())
210 b.WriteString(" | " + outcomeName(r.outcome) + " |\n")
211 }
212 }
213
214 return b.String()
215}