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.

v1 source realm

Package live runs real-time matches between agents, checked by the chain.

Overview

Package live runs real-time matches between agents, checked by the chain.

The match itself is played off-chain, tick by tick, by two programs that read the same game state and send inputs through a relay. The chain never sees a plan. It does two things:

  1. Anchor: every 10 seconds (100 ticks) each player sends one hash that covers all of their inputs so far (see AnchorHash). An anchor can only land inside a short window after its time, so a player can neither commit a whole match at the start nor wait and rewrite. A hash shows nothing about the inputs, so the rival cannot copy them.
  2. Submit: afterwards each player sends their full input log. The chain checks it hashes to the anchors the player sent during the match, then replays both logs on the brawl rules and records the score.

A log written after the fact does not match its anchors, so neither side can rewrite their moves after seeing the other's. A player who does not anchor and submit in time forfeits.

Constants 2

const Waiting, Playing, Done, Forfeit, Void

1const (
2	Waiting = iota // created, waiting for a second player
3	Playing        // being played off-chain; anchors arrive
4	Done           // both logs replayed, score recorded
5	Forfeit        // a side did not deliver; see Winner
6	Void           // neither side delivered
7)
source

Phases of a match.

Functions 11

func Anchor

crossing Action
1func Anchor(cur realm, id int, hash string)
source

Anchor sends your next hash. Anchor k must land in the window from block start + 2k to start + 2k + AnchorSlack: not earlier, so you cannot commit a whole match in advance, and not later, so you cannot wait to see the rival's moves before committing yours. An anchor that misses its window can never be sent, and the match is then forfeit.

func AnchorHash

Action
1func AnchorHash(prev, chunk string) string
source

AnchorHash is the anchor for a chunk of inputs: hex sha256 of the previous anchor ("" for the first), a "|", and the next 100 inputs. Anchor k covers every input up to tick 100*k, so each anchor commits to the whole log.

func Anchors

Action
1func Anchors(id, side int) string
source

Anchors lists a side's anchors so far, joined by commas.

func Create

crossing Action
1func Create(cur realm, name string, class, seconds int) int
source

Create opens a match of the given length (60 to 180 seconds, in tens) and takes side A.

func Inputs

Action
1func Inputs(id, side int) string
source

Inputs is one side's submitted log ("" until it is in).

func Join

crossing Action
1func Join(cur realm, id int, name string, class int)
source

Join takes side B and starts the match. The arena and coins come from the block it starts in; both programs read Seed and start playing from tick 0.

func Render

1func Render(path string) string
source

Render draws the match list for gnoweb.

func Settle

crossing Action
1func Settle(cur realm, id int)
source

Settle closes a match whose deadline has passed. A side that delivered every anchor and its log beats one that did not; if both failed it is void. Anyone can call it.

func StateJSON

Action
1func StateJSON(id int) string
source

StateJSON describes a match for the programs and the browser.

func Submit

crossing Action
1func Submit(cur realm, id int, inputs string)
source

Submit sends your full input log: one character per tick (see the brawl package's Encode). It must hash to every anchor you sent. When both logs are in, the chain replays them and records the score.

Types 2

type Match

struct
 1type Match struct {
 2	ID       int
 3	Seed     uint32
 4	Ticks    int
 5	Phase    int
 6	Start    int64 // block the match started in
 7	Deadline int64 // after this block a missing side forfeits
 8	P        [2]*Player
 9	Gold     [2]int
10	Winner   int // -1 none, 0 or 1 once forfeit or done
11}
source

A Match is one real-time fight.

type Player

struct
1type Player struct {
2	Addr      address
3	Name      string
4	Class     int
5	Anchors   []string // hash after each 100 ticks
6	AnchorAt  []int64  // block each anchor landed in
7	Inputs    string   // the full log, once submitted
8	Submitted bool
9}
source

A Player is one side of a match.

Dependencies

Source Files 4