forked from sgt-kabukiman/srapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
111 lines (90 loc) · 2.06 KB
/
player.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
// Copyright (c) 2015, Sgt. Kabukiman | MIT licensed
package srapi
import "net/url"
// Player is either a User or a Guest, i.e. only one of the two will ever be
// non-nil.
type Player struct {
User *User
Guest *Guest
}
// Name returns the international name for the player.
func (p *Player) Name() string {
if p.User != nil {
return p.User.Names.International
} else if p.Guest != nil {
return p.Guest.Name
} else {
return "(neither guest nor user)"
}
}
// toLink returns a link pointing to this player.
func (p *Player) toLink() PlayerLink {
var link PlayerLink
if p.User != nil {
link = PlayerLink{
Link: Link{
Relation: "user",
URI: BaseURL + "/users/" + p.User.ID,
},
ID: p.User.ID,
Name: "",
}
// try our best to use the provided link
l := firstLink(p.User, "self")
if l != nil {
link.URI = l.URI
}
} else {
link = PlayerLink{
Link: Link{
Relation: "guest",
URI: BaseURL + "/guests/" + url.QueryEscape(p.Guest.Name),
},
ID: "",
Name: p.Guest.Name,
}
// try our best to use the provided link
l := firstLink(p.Guest, "self")
if l != nil {
link.URI = l.URI
}
}
return link
}
// PlayerLink is a special link that points to either a user (then ID is given)
// or a guest (then Name is given).
type PlayerLink struct {
Link
// user ID
ID string
// guest name
Name string
}
// checks if the link exists
func (pl *PlayerLink) exists() bool {
return pl != nil
}
// request turns a link into a request
func (pl *PlayerLink) request(filter filter, sort *Sorting, embeds string) request {
relURL := pl.URI[len(BaseURL)+1:]
return request{"GET", relURL, filter, sort, nil, embeds}
}
// fetch retrieves the user or guest the link points to
func (pl *PlayerLink) fetch() (*Player, *Error) {
player := &Player{}
switch pl.Relation {
case "user":
user, err := fetchUserLink(pl)
if err != nil {
return player, err
}
player.User = user
case "guest":
guest, err := fetchGuestLink(pl)
if err != nil {
return player, err
}
player.Guest = guest
}
return player, nil
}