Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

arena.gno

2.49 Kb · 86 lines
 1package arena
 2
 3import (
 4	"strconv"
 5	"time"
 6
 7	"gno.land/p/nt/bptree/v0"
 8)
 9
10// The whole state of the realm. Everything is unexported and nothing below is
11// ever returned by reference: the trees are /p/ types with mutator methods,
12// so handing one out would be handing out write access to the realm.
13var (
14	version int64 // bumps on every admin write, see Version
15
16	// Arenas exist from the first paid entry until their game is over, or
17	// their lobby is empty. An arena nobody paid into is never stored.
18	arenas bptree.BPTree // arena id -> *arena, with its lobby and its round
19
20	// The last games played, for readers: their result, by arena id, and the
21	// ids in the order they closed, oldest first.
22	results bptree.BPTree // arena id -> encoded result, see results.gno
23	closed  []string
24
25	refundDelay int64 // seconds after a game's time limit before anyone may refund it
26
27	escrowed int64 // ugnot owed to players: the stakes of every lobby and running game, and their unclaimed winnings
28
29	// Winnings not claimed yet, see claims.gno.
30	claims bptree.BPTree // player address -> encoded credits
31
32	// What players won, see earnings.gno.
33	earnings bptree.BPTree // player address -> encoded totals, allocated on a first settled game
34	winners  ranking       // the top players by winnings
35
36	webPath string // "/r/g1t2kg2vtr3fukg43eujkn6x53gfdyakhngt4sfd/gnofly/arena/v0", used to build links in Render
37)
38
39const defaultRefundDelay = 600 // seconds
40
41// init runs once, when the package is added to the chain. Unlike the game
42// realm this one has nothing to remember about its deployer: it has no role
43// of its own.
44func init(cur realm) {
45	bootstrap(cur.PkgPath())
46}
47
48// bootstrap (re)creates the initial state. The tests call it to start from a
49// clean slate.
50func bootstrap(pkgPath string) {
51	arenas = bptree.BPTree{}
52	results = bptree.BPTree{}
53	closed = nil
54	refundDelay = defaultRefundDelay
55	escrowed = 0
56	claims = bptree.BPTree{}
57	earnings = bptree.BPTree{}
58	winners = ranking{}
59	webPath = webPathOf(pkgPath)
60	version = 1
61}
62
63// Version returns a counter that bumps on every admin write: the refund
64// delay. Player and referee actions do not bump it.
65func Version() int64 {
66	return version
67}
68
69// now returns the block time in Unix seconds.
70func now() int64 {
71	return time.Now().Unix()
72}
73
74// webPathOf turns "gno.land/r/..." into "/r/...".
75func webPathOf(pkgPath string) string {
76	for i := 0; i < len(pkgPath); i++ {
77		if pkgPath[i] == '/' {
78			return pkgPath[i:]
79		}
80	}
81	return ""
82}
83
84func itoa(n int64) string {
85	return strconv.FormatInt(n, 10)
86}