-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
207 lines (179 loc) · 5.47 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package janken
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/appengine/urlfetch"
)
type AllData struct {
ID1 string
ID2 string
Result string
ResultHTML string
Results []Result
Date_start string
Date_end string
ABC string
ARC string
AGC string
Other string
}
type Result struct {
Title string
ID string
Place1 int
Place2 int
BackGroundColor1 string
BackGroundColor2 string
}
type History struct {
IsRated bool `json:"IsRated"`
Place int `json:"Place"`
NewRating int `json:"NewRating"`
Performance int `json:"Performance"`
InnerPerformance int `json:"InnerPerformance"`
ContestScreenName string `json:"ContestScreenName"`
ContestName string `json:"ContestName"`
EndTime time.Time `json:"EndTime"`
}
func init() {
// router := gin.Default()
router := gin.New()
router.GET("/", func(c *gin.Context) {
// idを取得する
id1, id2, date_start, date_end := c.Query("id1"), c.Query("id2"), c.Query("date_start"), c.Query("date_end")
abc, arc, agc, other := c.Query("abc"), c.Query("arc"), c.Query("agc"), c.Query("other")
if date_start == "" {
date_start = "2010-01"
currentTime := time.Now()
date_end = fmt.Sprintf("%d-%02d", currentTime.Year(), int(currentTime.Month()))
abc = "1"
arc = "1"
agc = "1"
other = "1"
}
// 結果を取得する
data := GetData(id1, id2, date_start, date_end, abc, arc, agc, other, c)
// テンプレートHTMLにデータを入れる
t, _ := template.ParseFiles("main.html")
t.Execute(c.Writer, data)
// c.String(http.StatusOK, "%s \n\n %s", history1, history2)
})
http.Handle("/", router)
// router.Run(":8080")
}
func GetData(id1 string, id2 string, date_start string, date_end string, abc string, arc string, agc string, other string, c *gin.Context) AllData {
// 2人のhistoryを取得
var history1, history2 []History
SetUserHistory(id1, &history1, c)
SetUserHistory(id2, &history2, c)
// history2のmapを作る. [contestID]Place
history2Map := make(map[string]int)
for _, contest := range history2 {
history2Map[contest.ContestScreenName] = contest.Place
}
white := "FFFFFF"
red := "EBCCCC"
green := "D0E9C6"
layout := "2006-01"
ds, _ := time.Parse(layout, date_start)
dt, _ := time.Parse(layout, date_end)
dt2 := dt.AddDate(0, 1, 0)
// 結果を生成
var result []Result
var id1Count, id2Count = 0, 0
for _, contest := range history1 {
// ID2の順位
place2, ok := history2Map[contest.ContestScreenName]
if !ok || contest.EndTime.Before(ds) || contest.EndTime.After(dt2) {
continue
}
if abc == "" && strings.Contains(contest.ContestName, "Beginner") {
continue
}
if arc == "" && strings.Contains(contest.ContestName, "Regular") {
continue
}
if agc == "" && strings.Contains(contest.ContestName, "Grand") {
continue
}
if other == "" && (!strings.Contains(contest.ContestName, "Beginner") && !strings.Contains(contest.ContestName, "Regular") && !strings.Contains(contest.ContestName, "Grand")) {
continue
}
backGroundColor1 := white
backGroundColor2 := white
if contest.Place < place2 {
backGroundColor1 = green
backGroundColor2 = red
id1Count++
} else if contest.Place > place2 {
backGroundColor1 = red
backGroundColor2 = green
id2Count++
} else {
id1Count++
id2Count++
}
result = append(result, Result{
Title: contest.ContestName,
Place1: contest.Place,
Place2: place2,
BackGroundColor1: backGroundColor1,
BackGroundColor2: backGroundColor2,
})
}
resultStr := "【AtCoderじゃんけん】\n"
resultStr += id1 + " vs " + id2 + "\n"
resultStr += strconv.Itoa(id1Count) + "対" + strconv.Itoa(id2Count) + "で"
resultHTML := strconv.Itoa(id1Count) + "対" + strconv.Itoa(id2Count) + "で"
if id1Count > id2Count {
resultStr += id1 + "の勝利です!!"
resultHTML += id1 + "の勝利です!!"
} else if id1Count < id2Count {
resultStr += id2 + "の勝利です!!"
resultHTML += id2 + "の勝利です!!"
} else {
resultStr += "勝負は引き分けです!!"
resultHTML += "勝負は引き分けです!!"
}
resultHTML += "(" + date_start + " ~ " + date_end + ")"
resultStr += "(" + date_start + " ~ " + date_end + ")"
resultStr += "\n"
resultStr += "#AtCoderじゃんけん\n"
data := AllData{
ID1: id1,
ID2: id2,
Result: resultStr,
ResultHTML: resultHTML,
Results: result,
Date_start: date_start,
Date_end: date_end,
ABC: abc,
ARC: arc,
AGC: agc,
Other: other,
}
return data
}
func SetUserHistory(id string, history *[]History, c *gin.Context) {
var request *http.Request = c.Request
context := appengine.NewContext(request)
client := urlfetch.Client(context)
// GET
userURL := "https://beta.atcoder.jp/users/" + id + "/history/json"
resp, err := client.Get(userURL)
if err != nil {
log.Infof(context, "%v", err)
}
// バイナリ取得
byteArray, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(byteArray, &history)
}