Merge: Nitdoc: more cleaning and uniformize method names
[nit.git] / contrib / nitrpg / src / statistics.nit
index 39c05af..7b493c6 100644 (file)
@@ -26,6 +26,9 @@ import counter
 
 redef class GameEntity
 
+       # Statistics for this entity.
+       fun stats: GameStats is abstract
+
        # Load statistics for this `MEntity` if any.
        fun load_statistics: nullable GameStats do
                var key = self.key / "statistics"
@@ -33,23 +36,17 @@ redef class GameEntity
                var json = game.store.load_object(key)
                return new GameStats.from_json(game, json)
        end
-
-       # Save statistics under this `MEntity`.
-       fun save_statistics(stats: GameStats) do
-               game.store.store_object(key / stats.key, stats.to_json)
-       end
 end
 
 redef class Game
 
-       # Statistics for this game instance.
-       var stats: GameStats is lazy do
+       redef var stats is lazy do
                return load_statistics or else new GameStats(game)
        end
 
        redef fun save do
                super
-               save_statistics(stats)
+               stats.save_in(self)
        end
 
        redef fun pretty do
@@ -63,14 +60,13 @@ end
 
 redef class Player
 
-       # Statistics for this player.
-       var stats: GameStats is lazy do
+       redef var stats is lazy do
                return load_statistics or else new GameStats(game)
        end
 
        redef fun save do
                super
-               save_statistics(stats)
+               stats.save_in(self)
        end
 
        redef fun pretty do
@@ -125,9 +121,7 @@ end
 class StatisticsReactor
        super GameReactor
 
-       redef fun react_event(game, e) do
-               e.react_stats_event(game)
-       end
+       redef fun react_event(game, e) do e.react_stats_event(game)
 end
 
 redef class GithubEvent
@@ -193,3 +187,27 @@ redef class PullRequestEvent
                end
        end
 end
+
+redef class IssueCommentEvent
+
+       # Count posted comments
+       redef fun react_stats_event(game) do
+               if action == "created" then
+                       var player = comment.user.player(game)
+                       game.stats.inc("comments")
+                       player.stats.inc("comments")
+                       # FIXME use a more precise way to locate reviews
+                       if comment.has_ok_review then
+                               game.stats.inc("reviews")
+                               player.stats.inc("reviews")
+                       end
+                       game.save
+                       player.save
+               end
+       end
+end
+
+redef class IssueComment
+       # Does this comment contain a "+1"?
+       fun has_ok_review: Bool do return body.has("\\+1\\b".to_re)
+end