-
Notifications
You must be signed in to change notification settings - Fork 7
/
player.go
104 lines (90 loc) · 2.5 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
package main
import (
"time"
"github.com/google/go-github/v32/github"
)
type Player struct {
Username string
Avatar string
Profile string
Merged, Accepted, Pending, Ignored, Invalids []*PullRequest
Issues []*Issue
Repos map[string]*Repo
LastMergeAt time.Time
}
type Players map[string]*Player
func NewPlayer(user *github.User) *Player {
return &Player{
Username: user.GetLogin(),
Avatar: user.GetAvatarURL(),
Profile: user.GetHTMLURL(),
Repos: make(map[string]*Repo),
}
}
func (p *Player) IsChallengeComplete() bool {
return p.ContributionCount() >= TARGET_OBJECTIVE
}
func (p *Player) Contributions() []*PullRequest {
prs := make([]*PullRequest, 0, p.ContributionCount())
prs = append(prs, p.Merged...)
prs = append(prs, p.Accepted...)
return prs
}
func (p *Player) ContributionCount() int {
return len(p.Merged) + len(p.Accepted)
}
func (_ *Player) Objective() int {
return TARGET_OBJECTIVE
}
func (p *Player) Badges() []Badge {
badges := []Badge{}
for _, b := range BADGES {
if b.EarnedBy(p) {
badges = append(badges, b)
}
}
return badges
}
func (p *Player) ChallengeCompletion() int {
percent := 100 * p.ContributionCount() / TARGET_OBJECTIVE
if percent > 100 {
percent = 100
}
return percent
}
func (p *Player) AddIssue(issue *Issue) {
p.Issues = append(p.Issues, issue)
p.Repos[issue.Repo.FullName()] = issue.Repo
}
// For all submissions after 3rd of October 2020, the rules have changed
var date2020, _ = time.Parse("2006-01-02", "2020-10-03")
func (p *Player) AddContrib(pr *PullRequest) {
p.Repos[pr.Repo.FullName()] = pr.Repo
if pr.HasOneOfLabels("invalid", "spam") {
p.Invalids = append(p.Invalids, pr)
} else if pr.HasOneOfLabels("hacktoberfest-accepted") || pr.CreatedAt.Before(date2020) {
if pr.Merged {
p.Merged = append(p.Merged, pr)
if p.LastMergeAt.Before(pr.MergedAt) {
p.LastMergeAt = pr.MergedAt
}
} else {
p.Accepted = append(p.Accepted, pr)
}
} else {
if pr.Repo.HasTopic("hacktoberfest") {
if pr.Merged {
p.Merged = append(p.Merged, pr)
if p.LastMergeAt.Before(pr.MergedAt) {
p.LastMergeAt = pr.MergedAt
}
} else if pr.State == "closed" {
p.Invalids = append(p.Invalids, pr)
} else {
p.Pending = append(p.Pending, pr)
}
} else {
p.Ignored = append(p.Ignored, pr)
}
}
}