-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmnbot.go
113 lines (105 loc) · 2.54 KB
/
kmnbot.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
package kmnbot
import (
"bufio"
"fmt"
"io"
"net/http"
"strconv"
"strings"
)
type Kmn struct {
Name string
// 稀有度
Star int64
// 等級
Level int64
// 若 Level 滿時為 true,滿級等級可從 HandbookItem.Level 取得
FullLevel bool
// 階級
Rank int64
// 若 Rank 為 100 則為 true
FullRank bool
// 必為 紅, 藍, 黃, 綠, 黑, 白 其中之一
Type string
// -1:未知, 0:普通, 1:不錯, 2:太完美了!
// 體質未知的機器狼需要使用「靈能者機器狼」系列的技能「靈腦檢驗」使其顯示
Evaluate int64
}
func (k Kmn) Data() HandbookItem {
if data, ok := Handbook[k.Name]; ok {
return data
}
return HandbookItem{}
}
func FetchBox(id string) (kmns Box, err error) {
res, err := http.Get("https://www.kmnbot.com/pets/" + id + ".txt")
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("statusCode %d", res.StatusCode)
}
return ParseBox(res.Body)
}
func ParseBox(reader io.Reader) (kmns Box, err error) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "★") {
continue
}
spart := filterNotEmpty(strings.Split(line, " "))
starStr := strings.TrimPrefix(spart[0], "★")
star, err := strconv.ParseInt(starStr, 10, 64)
if err != nil {
return nil, err
}
typeStr := strings.TrimSuffix(strings.TrimPrefix(spart[1], "["), "]")
suffixPart := strings.Split(spart[3], "/")
var level int64 = 0
var rank int64 = 0
var fullLevel = false
var fullRank = false
var evaluate = -1
for _, s := range suffixPart {
switch {
case strings.HasPrefix(s, "等級"):
if strings.Contains(s, "(滿)") {
fullLevel = true
}
levelStr := strings.TrimSuffix(strings.TrimPrefix(s, "等級"), "(滿)")
level, err = strconv.ParseInt(levelStr, 10, 64)
if err != nil {
return nil, err
}
case strings.HasPrefix(s, "階級"):
if strings.Contains(s, "(滿)") {
fullRank = true
}
rankStr := strings.TrimSuffix(strings.TrimPrefix(s, "階級"), "(滿)")
rank, err = strconv.ParseInt(rankStr, 10, 64)
if err != nil {
return nil, err
}
case s == "普通":
evaluate = 0
case s == "很不錯":
evaluate = 1
case s == "太完美了!":
evaluate = 2
}
}
kmns = append(kmns, Kmn{
Name: spart[2],
Star: star,
Level: level,
FullLevel: fullLevel,
Rank: rank,
FullRank: fullRank,
Type: typeStr,
Evaluate: int64(evaluate),
})
}
return
}