-
Notifications
You must be signed in to change notification settings - Fork 5
/
sc2json.go
90 lines (79 loc) · 2.07 KB
/
sc2json.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
package main
import (
"bufio"
"encoding/json"
"io"
"log"
"net"
)
var _ = log.Ldate
var (
pythonPort string
)
type ReplayPlayer struct {
Uid int64 `json:"uid"`
GameRace string `json:"play_race"`
LobbyRace string `json:"pick_race"`
Handicap int64 `json:"handicap"`
Pid int `json:"pid"`
Name string `json:"name"`
Url string `json:"url"`
Victory string `json:"result"`
}
type Replay struct {
IsLadder bool `json:"is_ladder"`
RealType string `json:"real_type"`
Frames int64 `json:"frames"`
Speed string `json:"speed"`
Category string `json:"category"`
Filehash string `json:"filehash"`
Build int64 `json:"build"`
GameLength int64 `json:"game_length"`
RealLength int64 `json:"real_length"`
GameFps float64 `json:"game_fps"`
UnixTimestamp int64 `json:"unix_timestamp"`
Type string `json:"type"`
Observers []ReplayPlayer `json:"observers"`
Players []ReplayPlayer `json:"players"`
MapName string `json:"map_name"`
Versions []int `json:"versions"`
Region string `json:"region"`
Gateway string `json:"gateway"`
Release string `json:"release"`
}
func NewReplay(path string) (replay *Replay, err error) {
conn, err := net.Dial("tcp", pythonPort)
if err != nil {
matchmaker.logger.Println("Dial error", err)
return nil, err
}
defer conn.Close()
writer := bufio.NewWriter(conn)
_, err = writer.WriteString(path + "\n")
if err != nil {
return nil, err
}
err = writer.Flush()
if err != nil {
return nil, err
}
reader := bufio.NewReader(conn)
data, err := reader.ReadBytes('\n')
if err != nil {
if err != io.EOF {
log.Println("Socket Error", err)
}
return
}
if len(data) == 0 {
matchmaker.logger.Println("Data retreieved for", path, "empty")
return nil, nil
}
var rep Replay
err = json.Unmarshal(data, &rep)
if err != nil {
return nil, err
}
replay = &rep
return
}