contrib/nitrpg: display achievements in front end
[nit.git] / contrib / nitrpg / src / templates / templates_events.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014-2015 Alexandre Terrasa <alexandre@moz-code.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Templates to display `GameEvent` kinds.
18 module templates_events
19
20 import achievements
21 import templates_base
22
23 redef class GameEvent
24 # See `TplEvent`
25 fun tpl_event: TplEvent do
26 if kind == "pull_open" then
27 return new TplPullOpened(self)
28 else if kind == "pull_merged" then
29 return new TplPullMerged(self)
30 else if kind == "pull_review" then
31 return new TplPullReview(self)
32 else if kind == "achievement_unlocked" then
33 return new TplAchievementUnlocked(self)
34 end
35 abort
36 end
37 end
38
39 # A TplEvent factorizes HTML rendering methods for `GameEvent`.
40 class TplEvent
41
42 # Event to display.
43 var event: GameEvent
44
45 # Title to display.
46 var title: String is lazy do return "raw event"
47
48 # Load Player from event data.
49 var player: nullable Player is lazy do
50 return event.game.load_player(event.data["player"].to_s)
51 end
52
53 # Load reward from event data.
54 var reward: Int is lazy do return event.data["reward"].as(Int)
55
56 # Load `github_event` data key as a PullRequestEvent.
57 var pull_event: PullRequestEvent is lazy do
58 var obj = event.data["github_event"].as(JsonObject)
59 return new PullRequestEvent.from_json(event.game.api, obj)
60 end
61
62 # Load `github_event` data key as a IssueCommentEvent.
63 var issue_comment_event: IssueCommentEvent is lazy do
64 var obj = event.data["github_event"].as(JsonObject)
65 return new IssueCommentEvent.from_json(event.game.api, obj)
66 end
67
68 # Load `achievement` data key as an Achievement.
69 var achievement: Achievement is lazy do
70 return player.load_achievement(event.data["achievement"].to_s).as(not null)
71 end
72
73 # Display a media item for a reward event.
74 fun media_item: String do
75 return """<div class="media">
76 <a class="media-left" href="{{{player.url}}}">
77 <span class="badge progress-bar-success"
78 style=\"position: absolute\">+{{{reward}}}</span>
79 <img class=\"img-circle\" style="width:50px"
80 src="{{{player.user.avatar_url}}}" alt="{{{player.name}}}">
81 </a>
82 <div class="media-body">
83 <h4 class="media-heading">{{{title}}}</h4>
84 <span class="text-muted">at {{{event.time}}}</span>
85 </div>
86 </div>"""
87 end
88 end
89
90 # Event: pull_open
91 class TplPullOpened
92 super TplEvent
93
94 redef var title is lazy do
95 var pull = pull_event.pull
96 return "{player.link} pushed {pull.link}"
97 end
98 end
99
100 # Event: pull_merged
101 class TplPullMerged
102 super TplEvent
103
104 redef var title is lazy do
105 var pull = pull_event.pull
106 return "{player.link} merged <strong>{pull.commits}</strong> commits with {pull.link}"
107 end
108 end
109
110 # Event: pull_review
111 class TplPullReview
112 super TplEvent
113
114 redef var title is lazy do
115 var issue = issue_comment_event.issue
116 return "{player.link} reviewed {issue.link}"
117 end
118 end
119
120 # Event: achievement_unlocked
121 class TplAchievementUnlocked
122 super TplEvent
123
124 redef var title is lazy do
125 return "{player.link} unlocked {achievement.link}"
126 end
127 end