20f7b5880e9f30b2e746cc5c98448a9feb6c1432
[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 events
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 end
33 abort
34 end
35 end
36
37 # A TplEvent factorizes HTML rendering methods for `GameEvent`.
38 class TplEvent
39
40 # Event to display.
41 var event: GameEvent
42
43 # Title to display.
44 var title: String is lazy do return "raw event"
45
46 # Load Player from event data.
47 var player: nullable Player is lazy do
48 return event.game.load_player(event.data["player"].to_s)
49 end
50
51 # Load reward from event data.
52 var reward: Int is lazy do return event.data["reward"].as(Int)
53
54 # Load `github_event` data key as a PullRequestEvent.
55 var pull_event: PullRequestEvent is lazy do
56 var obj = event.data["github_event"].as(JsonObject)
57 return new PullRequestEvent.from_json(event.game.api, obj)
58 end
59
60 # Load `github_event` data key as a IssueCommentEvent.
61 var issue_comment_event: IssueCommentEvent is lazy do
62 var obj = event.data["github_event"].as(JsonObject)
63 return new IssueCommentEvent.from_json(event.game.api, obj)
64 end
65
66 # Display a media item for a reward event.
67 fun media_item: String do
68 return """<div class="media">
69 <a class="media-left" href="{{{player.url}}}">
70 <span class="badge progress-bar-success"
71 style=\"position: absolute\">+{{{reward}}}</span>
72 <img class=\"img-circle\" style="width:50px"
73 src="{{{player.user.avatar_url}}}" alt="{{{player.name}}}">
74 </a>
75 <div class="media-body">
76 <h4 class="media-heading">{{{title}}}</h4>
77 <span class="text-muted">at {{{event.time}}}</span>
78 </div>
79 </div>"""
80 end
81 end
82
83 # Event: pull_open
84 class TplPullOpened
85 super TplEvent
86
87 redef var title is lazy do
88 var pull = pull_event.pull
89 return "{player.link} pushed {pull.link}"
90 end
91 end
92
93 # Event: pull_merged
94 class TplPullMerged
95 super TplEvent
96
97 redef var title is lazy do
98 var pull = pull_event.pull
99 return "{player.link} merged <strong>{pull.commits}</strong> commits with {pull.link}"
100 end
101 end
102
103 # Event: pull_review
104 class TplPullReview
105 super TplEvent
106
107 redef var title is lazy do
108 var issue = issue_comment_event.issue
109 return "{player.link} reviewed {issue.link}"
110 end
111 end