Merge: concurrent_collections: Add implementation of has method
[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 return event.game.api.deserialize(event.data["github_event"].as(JsonObject).to_json).as(PullRequestEvent)
59 end
60
61 # Load `github_event` data key as a IssueCommentEvent.
62 var issue_comment_event: IssueCommentEvent is lazy do
63 return event.game.api.deserialize(event.data["github_event"].as(JsonObject).to_json).as(IssueCommentEvent)
64 end
65
66 # Load `achievement` data key as an Achievement.
67 var achievement: Achievement is lazy do
68 return player.load_achievement(event.data["achievement"].to_s).as(not null)
69 end
70
71 # Display a media item for a reward event.
72 fun media_item: String do
73 return """<div class="media">
74 <a class="media-left" href="{{{player.url}}}">
75 <span class="badge progress-bar-success"
76 style=\"position: absolute\">+{{{reward}}}</span>
77 <img class=\"img-circle\" style="width:50px"
78 src="{{{player.user.avatar_url or else "#"}}}" alt="{{{player.name}}}">
79 </a>
80 <div class="media-body">
81 <h4 class="media-heading">{{{title}}}</h4>
82 <span class="text-muted">at {{{event.time}}}</span>
83 </div>
84 </div>"""
85 end
86 end
87
88 # Event: pull_open
89 class TplPullOpened
90 super TplEvent
91
92 redef var title is lazy do
93 var pull = pull_event.pull
94 return "{player.link} pushed {pull.link}"
95 end
96 end
97
98 # Event: pull_merged
99 class TplPullMerged
100 super TplEvent
101
102 redef var title is lazy do
103 var pull = pull_event.pull
104 return "{player.link} merged <strong>{pull.commits}</strong> commits with {pull.link}"
105 end
106 end
107
108 # Event: pull_review
109 class TplPullReview
110 super TplEvent
111
112 redef var title is lazy do
113 var issue = issue_comment_event.issue
114 return "{player.link} reviewed {issue.link}"
115 end
116 end
117
118 # Event: achievement_unlocked
119 class TplAchievementUnlocked
120 super TplEvent
121
122 redef var title is lazy do
123 return "{player.link} unlocked {achievement.link}"
124 end
125 end