33545b7a58d64eb7281428078669487633ae76ee
[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 redef fun react_event(game, e) do e.react_player_event(self, game)
33 end
34
35 redef class GithubEvent
36 # Reacts to a player related event.
37 #
38 # Called by `PlayerReactor::react_event`.
39 # No-op by default.
40 private fun react_player_event(reactor: PlayerReactor, game: Game) do end
41 end
42
43 redef class PullRequestEvent
44
45 # Rewards player for opened pull requests.
46 redef fun react_player_event(r, game) do
47 if action == "opened" then
48 var player = pull.user.player(game)
49 player.nitcoins += r.nc_pull_open
50 player.save
51 end
52 end
53 end
54
55 redef class IssueCommentEvent
56
57 # Rewards player for review comments.
58 #
59 # Actuallty we look if the comment contains the string `"+1"`.
60 #
61 # TODO only give nitcoins if reviewers < 2
62 redef fun react_player_event(r, game) do
63 # FIXME use a more precise way to locate reviews
64 if comment.body.has("\\+1\\b".to_re) then
65 var player = comment.user.player(game)
66 player.nitcoins += r.nc_pull_review
67 player.save
68 end
69 end
70 end