contrib/nitrpg: also reward player for merged commits.
[nit.git] / contrib / nitrpg / src / reactors.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 # Various implementations of `GameReactor` can be found here.
18 module reactors
19
20 import game
21
22 # Reacts to event that can affect players (like giving nitcoins).
23 class PlayerReactor
24 super GameReactor
25
26 # Nitcoins rewarded when the player opens a new pull request.
27 var nc_pull_open = 10
28
29 # Nitcoins rewarded when the player reviews a pull request.
30 var nc_pull_review = 2
31
32 # Nitcoins rewarded when the player has a commit merged.
33 var nc_commit_merged = 1
34
35 redef fun react_event(game, e) do e.react_player_event(self, game)
36 end
37
38 redef class GithubEvent
39 # Reacts to a player related event.
40 #
41 # Called by `PlayerReactor::react_event`.
42 # No-op by default.
43 private fun react_player_event(reactor: PlayerReactor, game: Game) do end
44 end
45
46 redef class PullRequestEvent
47
48 # Rewards player for opened pull requests.
49 redef fun react_player_event(r, game) do
50 var player = pull.user.player(game)
51 if action == "opened" then
52 player.nitcoins += r.nc_pull_open
53 player.save
54 else if action == "closed" and pull.merged then
55 player.nitcoins += pull.commits * r.nc_commit_merged
56 player.save
57 end
58 end
59 end
60
61 redef class IssueCommentEvent
62
63 # Rewards player for review comments.
64 #
65 # Actuallty we look if the comment contains the string `"+1"`.
66 #
67 # TODO only give nitcoins if reviewers < 2
68 redef fun react_player_event(r, game) do
69 # FIXME use a more precise way to locate reviews
70 if comment.body.has("\\+1\\b".to_re) then
71 var player = comment.user.player(game)
72 player.nitcoins += r.nc_pull_review
73 player.save
74 end
75 end
76 end