codec.gno
4.02 Kb · 153 lines
1package standings
2
3import "encoding/binary"
4
5// The standings and the result log are stored as byte strings laid out here, big-endian.
6
7const (
8 addressSize = 40 // bytes in an address's text
9 standingFix = 2 + 2 + 2 + 2 + 1 // a standing's bytes before its per-clan counts
10 resultSize = 1 + 2*addressSize + 1 + 1 + 1 + 4
11
12 // resultsPerChunk is the most entries in a log chunk, one string, so a new result grows a
13 // string instead of adding tree nodes.
14 resultsPerChunk = 64
15)
16
17type writer struct{ b []byte }
18
19func (w *writer) u8(x uint64) { w.b = append(w.b, byte(x)) }
20func (w *writer) text(s string) { w.b = append(w.b, s...) }
21
22func (w *writer) u16(x uint64) {
23 var t [2]byte
24 binary.BigEndian.PutUint16(t[:], uint16(x))
25 w.b = append(w.b, t[:]...)
26}
27
28func (w *writer) u32(x uint64) {
29 var t [4]byte
30 binary.BigEndian.PutUint32(t[:], uint32(x))
31 w.b = append(w.b, t[:]...)
32}
33
34// addr writes a's 40 characters.
35func (w *writer) addr(a address) {
36 if len(a) != addressSize {
37 panic("invalid address")
38 }
39 w.text(string(a))
40}
41
42func (w *writer) String() string { return string(w.b) }
43
44type reader struct {
45 s string
46 at int
47}
48
49func (r *reader) left() int { return len(r.s) - r.at }
50
51func (r *reader) text(n int) string {
52 if r.left() < n {
53 panic("truncated record")
54 }
55 s := r.s[r.at : r.at+n]
56 r.at += n
57 return s
58}
59
60func (r *reader) u8() uint64 { return uint64(r.text(1)[0]) }
61func (r *reader) u16() uint64 { return uint64(binary.BigEndian.Uint16([]byte(r.text(2)))) }
62func (r *reader) u32() uint64 { return uint64(binary.BigEndian.Uint32([]byte(r.text(4)))) }
63func (r *reader) addr() address { return address(r.text(addressSize)) }
64
65// standing is a player's rated record. lastClan is the clan number of their last rated
66// match; perClan[i] counts their rated matches with clan number i + 1.
67type standing struct {
68 rating, wins, losses, draws int
69 lastClan int
70 perClan []int
71}
72
73// decodeStanding reads a standing's bytes: the rating, wins, losses and draws at 2 bytes
74// each, the last clan number at 1, then a 2-byte count per clan number played.
75func decodeStanding(s string) *standing {
76 if s == "" {
77 return nil
78 }
79 if len(s) < standingFix || (len(s)-standingFix)%2 != 0 {
80 panic("malformed standing")
81 }
82 r := &reader{s: s}
83 st := &standing{rating: int(r.u16()), wins: int(r.u16()), losses: int(r.u16()), draws: int(r.u16()), lastClan: int(r.u8())}
84 for r.left() > 0 {
85 st.perClan = append(st.perClan, int(r.u16()))
86 }
87 return st
88}
89
90func encodeStanding(st *standing) string {
91 w := &writer{}
92 w.u16(uint64(st.rating))
93 w.u16(uint64(st.wins))
94 w.u16(uint64(st.losses))
95 w.u16(uint64(st.draws))
96 w.u8(uint64(st.lastClan))
97 for _, n := range st.perClan {
98 w.u16(uint64(n))
99 }
100 return w.String()
101}
102
103// ladderKey orders the ladder: highest rating first, then address order.
104func ladderKey(rating int, player address) string {
105 w := &writer{}
106 w.u16(uint64(maxRating - rating))
107 w.text(string(player))
108 return w.String()
109}
110
111// resultEntry is one logged result. writer is the reporting realm's writer number, clans
112// the two clan numbers, result 'a', 'b' or 'd', and time the block's Unix seconds.
113type resultEntry struct {
114 writer int
115 challenger, acceptor address
116 clans [2]int
117 result byte
118 time int64
119}
120
121// resultChunkKey is the log key of the chunk that holds result seq, 1 first.
122func resultChunkKey(seq int) string {
123 w := &writer{}
124 w.u32(uint64((seq - 1) / resultsPerChunk))
125 return w.String()
126}
127
128func encodeResult(e resultEntry) string {
129 w := &writer{}
130 w.u8(uint64(e.writer))
131 w.addr(e.challenger)
132 w.addr(e.acceptor)
133 w.u8(uint64(e.clans[0]))
134 w.u8(uint64(e.clans[1]))
135 w.u8(uint64(e.result))
136 w.u32(uint64(e.time))
137 return w.String()
138}
139
140func decodeResult(s string) resultEntry {
141 if len(s) != resultSize {
142 panic("malformed result")
143 }
144 r := &reader{s: s}
145 return resultEntry{
146 writer: int(r.u8()),
147 challenger: r.addr(),
148 acceptor: r.addr(),
149 clans: [2]int{int(r.u8()), int(r.u8())},
150 result: byte(r.u8()),
151 time: int64(r.u32()),
152 }
153}