-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem_drop.go
More file actions
99 lines (78 loc) · 1.68 KB
/
item_drop.go
File metadata and controls
99 lines (78 loc) · 1.68 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
91
92
93
94
95
96
97
98
99
package osrs_api
import (
"fmt"
"log"
"strconv"
"strings"
)
type DropCollection struct {
Drops []*ItemDrop
}
func (dc *DropCollection) Add(drop *ItemDrop) {
dc.Drops = append(dc.Drops, drop)
}
type ItemDrop struct {
Item Item
ItemId int32
Quantity Quantity
Rarity float32
RarityString string
RarityNotes []*RarityNote
}
type Quantity struct {
Min int
Max int
Noted bool
}
func (q *Quantity) Parse(str string) {
if strings.Contains(str, "noted") || strings.Contains(str, "Noted") {
str = strings.ReplaceAll(str, "(noted)", "")
str = strings.ReplaceAll(str, "(Noted)", "")
q.Noted = true
}
if "" == strings.ReplaceAll(str, " ", "") {
q.Min = 1
q.Max = 1
return
}
//Fix up common issues
str = strings.ReplaceAll(str, ",", "-")
str = strings.ReplaceAll(str, ";", "-")
//Remove descriptions on items
if strings.Contains(str, "<") {
split := strings.Split(str, "<")
str = split[0] //Only want the first part
}
res, err := strconv.Atoi(strings.ReplaceAll(str, " ", ""))
if err == nil {
q.Min = res
q.Max = res
return
}
slice := strings.Split(str, "-")
if len(slice) >= 2 {
var err error
min := strings.ReplaceAll(slice[0], " ", "")
max := strings.ReplaceAll(slice[len(slice)-1], " ", "")
q.Min, err = strconv.Atoi(min)
if err != nil {
log.Fatal(err)
}
q.Max, err = strconv.Atoi(max)
if err != nil {
log.Fatal(err)
}
return
}
log.Fatalf("Unable to parse quantity string: %s", str)
}
func (q *Quantity) String() string {
str := fmt.Sprintf("%v", q.Max)
if q.Min != q.Max {
str = fmt.Sprintf("%v-%v", q.Min, q.Max)
}
if q.Noted {
return fmt.Sprintf("%s (noted)", str)
}
return str
}