This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AnimeListItem.go
104 lines (90 loc) · 3.59 KB
/
AnimeListItem.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
package arn
// AnimeListStatus values for anime list items
const (
AnimeListStatusWatching = "watching"
AnimeListStatusCompleted = "completed"
AnimeListStatusPlanned = "planned"
AnimeListStatusHold = "hold"
AnimeListStatusDropped = "dropped"
)
// AnimeListItem ...
type AnimeListItem struct {
AnimeID string `json:"animeId"`
Status string `json:"status" editable:"true"`
Episodes int `json:"episodes" editable:"true"`
Rating AnimeListItemRating `json:"rating"`
Notes string `json:"notes" editable:"true"`
RewatchCount int `json:"rewatchCount" editable:"true"`
Private bool `json:"private" editable:"true"`
Created string `json:"created"`
Edited string `json:"edited"`
}
// Anime fetches the associated anime data.
func (item *AnimeListItem) Anime() *Anime {
anime, _ := GetAnime(item.AnimeID)
return anime
}
// Link returns the URI for the given item.
func (item *AnimeListItem) Link(userNick string) string {
return "/+" + userNick + "/animelist/anime/" + item.AnimeID
}
// StatusHumanReadable returns the human readable representation of the status.
func (item *AnimeListItem) StatusHumanReadable() string {
switch item.Status {
case AnimeListStatusWatching:
return "Watching"
case AnimeListStatusCompleted:
return "Completed"
case AnimeListStatusPlanned:
return "Planned"
case AnimeListStatusHold:
return "On Hold"
case AnimeListStatusDropped:
return "Dropped"
default:
return "Unknown"
}
}
// OnEpisodesChange is called when the watched episode count changes.
func (item *AnimeListItem) OnEpisodesChange() {
maxEpisodesKnown := item.Anime().EpisodeCount != 0
// If we update episodes to the max, set status to completed automatically.
if item.Anime().Status == "finished" && maxEpisodesKnown && item.Episodes == item.Anime().EpisodeCount {
// Complete automatically.
item.Status = AnimeListStatusCompleted
}
// We set episodes lower than the max but the status is set as completed.
if item.Status == AnimeListStatusCompleted && maxEpisodesKnown && item.Episodes < item.Anime().EpisodeCount {
// Set status back to watching.
item.Status = AnimeListStatusWatching
}
// If we increase the episodes and status is planned, set it to watching.
if item.Status == AnimeListStatusPlanned && item.Episodes > 0 {
// Set status to watching.
item.Status = AnimeListStatusWatching
}
// If we set the episodes to 0 and status is not planned or dropped, set it to planned.
if item.Episodes == 0 && (item.Status != AnimeListStatusPlanned && item.Status != AnimeListStatusDropped) {
// Set status to planned.
item.Status = AnimeListStatusPlanned
}
}
// OnStatusChange is called when the status changes.
func (item *AnimeListItem) OnStatusChange() {
maxEpisodesKnown := item.Anime().EpisodeCount != 0
// We just switched to completed status but the episodes aren't max yet.
if item.Status == AnimeListStatusCompleted && maxEpisodesKnown && item.Episodes < item.Anime().EpisodeCount {
// Set episodes to max.
item.Episodes = item.Anime().EpisodeCount
}
// We just switched to plan to watch status but the episodes are greater than zero.
if item.Status == AnimeListStatusPlanned && item.Episodes > 0 {
// Set episodes back to zero.
item.Episodes = 0
}
// If we have an anime with max episodes watched and we change status to not completed, lower episode count by 1.
if maxEpisodesKnown && item.Status != AnimeListStatusCompleted && item.Episodes == item.Anime().EpisodeCount {
// Lower episodes by 1.
item.Episodes--
}
}