-
Notifications
You must be signed in to change notification settings - Fork 2
/
io_gib.go
144 lines (111 loc) · 2.64 KB
/
io_gib.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package sgf
// Basic GIB parser (i.e. for Tygem files)
import (
"fmt"
"strconv"
"strings"
)
// LoadGIB parses a GIB string, creating a tree of SGF nodes, and returns the
// root.
func LoadGIB(gib string) (*Node, error) {
root := NewTree(19)
node := root
lines := strings.Split(gib, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
// Names...
if strings.HasPrefix(line, "\\[GAMEBLACKNAME=") && strings.HasSuffix(line, "\\]") {
root.SetValue("PB", line[16: len(line) - 2])
}
if strings.HasPrefix(line, "\\[GAMEWHITENAME=") && strings.HasSuffix(line, "\\]") {
root.SetValue("PW", line[16: len(line) - 2])
}
// Game info...
if strings.HasPrefix(line, "\\[GAMETAG=") {
dt, re, km := parse_gib_gametag(line)
if dt != "" { root.SetValue("DT", dt) }
if re != "" { root.SetValue("RE", re) }
if km != "" { root.SetValue("KM", km) }
}
// Split the line into tokens for the handicap and move parsing...
fields := strings.Fields(line)
// Handicap...
if len(fields) >= 4 && fields[0] == "INI" {
if node != root {
return nil, fmt.Errorf("load_gib(): got INI field after moves were made")
}
handicap, _ := strconv.Atoi(fields[3])
if handicap > 1 {
root.SetValue("HA", strconv.Itoa(handicap))
root.SetValues("AB", HandicapPoints(19, handicap, true))
}
}
// Moves...
if len(fields) >= 6 && fields[0] == "STO" {
x, err1 := strconv.Atoi(fields[4])
y, err2 := strconv.Atoi(fields[5])
if err1 == nil && err2 == nil {
key := "B"; if fields[3] == "2" { key = "W" }
node = NewNode(node)
node.SetValue(key, Point(x, y))
}
}
}
return root, nil
}
func parse_gib_gametag(line string) (dt, re, km string) {
fields := strings.Split(line, ",")
var zipsu int
for _, s := range fields {
if len(s) < 2 {
continue
}
if s[0] == 'C' {
dt = s[1:]
if len(dt) > 10 {
dt = dt[:10]
}
dt = strings.Replace(dt, ":", "-", -1)
}
if s[0] == 'W' {
grlt, err := strconv.Atoi(s[1:])
if err == nil {
switch grlt {
case 0:
re = "B+"
case 1:
re = "W+"
case 3:
re = "B+R"
case 4:
re = "W+R"
case 7:
re = "B+T"
case 8:
re = "W+T"
}
}
}
if s[0] == 'G' {
gongje, err := strconv.Atoi(s[1:])
if err == nil {
km = fmt.Sprintf("%.1f", float64(gongje) / 10.0)
if strings.HasSuffix(km, ".0") {
km = km[:len(km) - 2]
}
}
}
if s[0] == 'Z' {
zipsu, _ = strconv.Atoi(s[1:])
}
}
if re == "B+" || re == "W+" {
if zipsu > 0 {
re += fmt.Sprintf("%.1f", float64(zipsu) / 10.0)
}
if strings.HasSuffix(re, ".0") {
re = re[:len(re) - 2]
}
}
return dt, re, km
}