Skip to content Search Apps Documentation Source Content Run Reset Eval Playground 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.

query.gno

4.73 Kb · 162 lines
  1package arena
  2
  3import (
  4	"encoding/base64"
  5	"strconv"
  6	"strings"
  7	"time"
  8
  9	"gno.land/p/nym-gfanton001/gnomepit/engine/v0"
 10)
 11
 12// MatchSummary is one match as a player's list shows it. Round is the round being
 13// played, or the last one resolved once the match is over. ToMove is the address that
 14// must move next, empty when none. Deadline is the end of the current move's 24 hours,
 15// or the expiry of an open challenge, in Unix seconds. Opponent is the address an open
 16// challenge is reserved for, "" when anyone can accept it and once accepted.
 17type MatchSummary struct {
 18	ID, Phase                    string
 19	Round                        int
 20	Challenger, Acceptor         string
 21	ChallengerClan, AcceptorClan string
 22	ToMove                       string
 23	Deadline                     int64
 24	Result, Reason               string
 25	Opponent                     string
 26}
 27
 28// MatchView is everything one match screen needs. Plans holds [challenger, acceptor]
 29// for each resolved round, and for the round being played while the match runs, ""
 30// while a plan is sealed or not yet made; HP holds both sides' HP after each resolved
 31// round; Box is the challenger's encrypted pending plan, in base64.
 32type MatchView struct {
 33	MatchSummary
 34	ChallengerDeck, AcceptorDeck string
 35	Plans                        [][]string
 36	Box                          string
 37	HP                           [][]int
 38	DeadlinePassed               bool
 39}
 40
 41// SessionLock binds the session key at address Session to the match whose id is Match.
 42// Expires is the session's expiry in Unix seconds, 0 for none.
 43type SessionLock struct {
 44	Session, Match string
 45	Expires        int64
 46}
 47
 48// Locks lists player's session locks. It can include locks whose session has expired.
 49func Locks(player address) []SessionLock {
 50	list := []SessionLock{}
 51	for _, l := range loadPlayer(player).locks {
 52		list = append(list, SessionLock{Session: l.session.String(), Match: strconv.Itoa(l.id), Expires: l.expires})
 53	}
 54	return list
 55}
 56
 57// Matches lists player's matches, newest first: limit of them after skipping offset.
 58func Matches(player address, offset, limit int) []MatchSummary {
 59	checkPage(offset, limit)
 60	rules := engine.ProofOfConcept()
 61	now := time.Now().Unix()
 62	list := []MatchSummary{}
 63	for _, id := range loadPlayer(player).newest(offset, limit) {
 64		m, ok := loadMatch(rules, id)
 65		if !ok {
 66			continue
 67		}
 68		list = append(list, m.summary(rules, id, now))
 69	}
 70	return list
 71}
 72
 73// Match returns the view of match id.
 74func Match(id string) MatchView {
 75	rules := engine.ProofOfConcept()
 76	n, m := find(rules, id)
 77	return m.view(rules, n, time.Now().Unix())
 78}
 79
 80func (m record) view(rules *engine.Ruleset, id int, now int64) MatchView {
 81	v := MatchView{
 82		MatchSummary:   m.summary(rules, id, now),
 83		ChallengerDeck: deckText(rules, m.decks[0]),
 84		AcceptorDeck:   deckText(rules, m.decks[1]),
 85		Plans:          [][]string{},
 86		Box:            base64.StdEncoding.EncodeToString([]byte(m.box)),
 87		HP:             [][]int{},
 88		DeadlinePassed: m.running() && now > m.deadline,
 89	}
 90	resolved := plansMade(m.plans[0])
 91	rounds := resolved
 92	if m.running() {
 93		rounds++
 94	}
 95	for r := 1; r <= rounds; r++ {
 96		a := planText(rules, planAt(m.plans[0], r))
 97		b := planText(rules, planAt(m.plans[1], r))
 98		v.Plans = append(v.Plans, []string{a, b})
 99	}
100	for r := 0; r < resolved; r++ {
101		v.HP = append(v.HP, []int{m.hp[r][0], m.hp[r][1]})
102	}
103	return v
104}
105
106func (m record) summary(rules *engine.Ruleset, id int, now int64) MatchSummary {
107	resolved := plansMade(m.plans[0])
108	s := MatchSummary{
109		ID:             strconv.Itoa(id),
110		Phase:          m.phase,
111		Round:          resolved + 1,
112		Challenger:     m.challenger.String(),
113		Acceptor:       m.acceptor.String(),
114		ChallengerClan: clanName(rules, m.clans[0]),
115		AcceptorClan:   clanName(rules, m.clans[1]),
116		Result:         m.result,
117		Reason:         m.reason,
118		Opponent:       m.opponent.String(),
119	}
120	switch m.phase {
121	case phaseOpen:
122		s.Deadline = m.deadline
123		if m.expired(now) {
124			s.Phase = phaseExpired
125		}
126	case phaseReveal:
127		s.ToMove = s.Challenger
128		s.Deadline = m.deadline
129	case phasePlay:
130		s.ToMove = s.Acceptor
131		s.Deadline = m.deadline
132	case phaseEnded:
133		if resolved > 0 {
134			s.Round = resolved
135		}
136	}
137	return s
138}
139
140func (m record) running() bool {
141	return m.phase == phaseReveal || m.phase == phasePlay
142}
143
144// deckText names the cards of deck in card id order.
145func deckText(rules *engine.Ruleset, deck uint64) string {
146	var names []string
147	for i := range rules.Cards {
148		if deck&(uint64(1)<<uint(i)) != 0 {
149			names = append(names, rules.Cards[i].Name)
150		}
151	}
152	return strings.Join(names, ",")
153}
154
155// planText names the cards of p in play order.
156func planText(rules *engine.Ruleset, p engine.Plan) string {
157	names := make([]string, len(p))
158	for i, id := range p {
159		names[i] = rules.Cards[id].Name
160	}
161	return strings.Join(names, ",")
162}