This repository was archived by the owner on Oct 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.go
More file actions
90 lines (83 loc) · 1.36 KB
/
reader.go
File metadata and controls
90 lines (83 loc) · 1.36 KB
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
package main
import (
"net/http"
"regexp"
"strconv"
"strings"
"golang.org/x/net/html"
)
// Read Does stuff
func Read(url string, board [][]rune) {
//Reads
board[0][0] = 'X'
resp, err := http.Get(url)
if err != nil {
// handle error
}
defer resp.Body.Close()
z := html.NewTokenizer(resp.Body)
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
// End of the document, we're done
return
case tt == html.StartTagToken:
t := z.Token()
isAnchor := t.Data == "piece"
if isAnchor {
var name rune
for _, a := range t.Attr {
if a.Key == "class" {
atts := strings.Split(a.Val, " ")
name = piece(atts[1], atts[0])
}
if a.Key == "style" {
re := regexp.MustCompile("[0-9 + .]+")
locs := re.FindAllString(string(a.Val), -1)
x, _ := strconv.ParseFloat(locs[0], 32)
xint := int(x / 12.5)
y, _ := strconv.ParseFloat(locs[1], 32)
yint := int(y / 12.5)
board[xint][yint] = name
}
}
}
}
}
}
func piece(name string, color string) rune {
var code rune
switch name {
case "king":
{
code = 'K'
}
case "queen":
{
code = 'Q'
}
case "pawn":
{
code = 'P'
}
case "knight":
{
code = 'N'
}
case "bishop":
{
code = 'B'
}
case "rook":
{
code = 'R'
}
default:
code = '-'
}
if color == "black" {
code += 32
}
return code
}