Skip to content

Commit 9519a29

Browse files
committed
Merge: nitrpg: misc fixes
Many fixes for NitRPG in prepatation for the next PR wich introduces the use of MongoDB. Pull-Request: #1761 Reviewed-by: Jean Privat <jean@pryen.org> Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
2 parents 6c70ee6 + 0df1eba commit 9519a29

File tree

6 files changed

+67
-50
lines changed

6 files changed

+67
-50
lines changed

contrib/nitrpg/src/achievements.nit

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,15 @@ end
7474
class Achievement
7575
super GameEntity
7676

77-
redef var key is lazy do return "achievements" / id
7877

7978
redef var game
8079

80+
redef var key is lazy do
81+
var owner = self.owner
82+
if owner == null then return id
83+
return "{owner.key}-{id}"
84+
end
85+
8186
# Uniq ID for this achievement.
8287
var id: String
8388

@@ -93,6 +98,9 @@ class Achievement
9398
# Is this achievement unlocked by somebody?
9499
var is_unlocked: Bool is lazy do return not load_events.is_empty
95100

101+
# Game entity this achievement is about.
102+
var owner: nullable GameEntity = null
103+
96104
# Init `self` from a `json` object.
97105
#
98106
# Used to load achievements from storage.
@@ -106,6 +114,8 @@ class Achievement
106114
json["name"] = name
107115
json["desc"] = desc
108116
json["reward"] = reward
117+
json["game"] = game.key
118+
if owner != null then json["owner"] = owner.key
109119
return json
110120
end
111121
end
@@ -346,7 +356,7 @@ abstract class PlayerXCommits
346356
if not event.action == "closed" then return
347357
if not event.pull.merged then return
348358
var player = event.pull.user.player(game)
349-
if player.stats["commits"] == threshold then
359+
if player.stats["commits"] >= threshold then
350360
var a = new_achievement(game)
351361
player.unlock_achievement(a, event)
352362
end
@@ -447,7 +457,7 @@ end
447457
class Player1KComments
448458
super PlayerXComments
449459

450-
redef var id = "player_1000__comments"
460+
redef var id = "player_1000_comments"
451461
redef var name = "You sir, talk a lot!"
452462
redef var desc = "Comment 1000 times on issues."
453463
redef var reward = 1000

contrib/nitrpg/src/events.nit

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import game
2424

2525
redef class GameEntity
2626

27-
# Saves `event` in `self`.
28-
fun add_event(event: GameEvent) do event.save_in(self.key)
27+
fun add_event(event: GameEvent) do
28+
event.owner = self
29+
event.save
30+
end
2931

3032
# List all events registered in this entity.
3133
#
@@ -61,10 +63,12 @@ end
6163
class GameEvent
6264
super GameEntity
6365

64-
redef var key is lazy do return "events" / internal_id
6566

6667
redef var game
6768

69+
# Entity this event belongs to.
70+
var owner: nullable GameEntity = null
71+
6872
# String used to dissociate events in the display.
6973
var kind: String
7074

@@ -76,6 +80,8 @@ class GameEvent
7680
# GameEvent uniq id used for storage.
7781
var internal_id: String is noinit
7882

83+
redef var key = internal_id is lazy
84+
7985
# Date and time of the event.
8086
var time: ISODate is noinit, writable
8187

@@ -100,6 +106,8 @@ class GameEvent
100106
json["kind"] = kind
101107
json["time"] = time.to_s
102108
json["data"] = data
109+
json["game"] = game.key
110+
if owner != null then json["owner"] = owner.key
103111
return json
104112
end
105113
end

contrib/nitrpg/src/game.nit

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,20 @@ class Game
6565

6666
redef fun game do return self
6767

68-
# Returns the repo `full_name`.
69-
#
70-
# Example: `"nitlang/nit"`
71-
redef fun key do return repo.full_name
72-
7368
# We need a `GithubAPI` client to load Github data.
7469
var api: GithubAPI
7570

7671
# A game takes place in a `github::Repo`.
7772
var repo: Repo
7873

74+
# Game name
75+
var name: String = repo.full_name is lazy
76+
7977
# Directory where game data are stored.
8078
var game_dir: String is lazy do return "nitrpg_data" / repo.full_name
8179

80+
redef var key = name is lazy
81+
8282
# Used for data storage.
8383
#
8484
# File are stored in `game_dir`.
@@ -92,6 +92,12 @@ class Game
9292
# Used to load entities from saved data.
9393
fun from_json(json: JsonObject) do end
9494

95+
redef fun to_json do
96+
var json = super
97+
json["name"] = name
98+
return json
99+
end
100+
95101
# Create a player from a Github `User`.
96102
#
97103
# Or return the existing one from game data.
@@ -180,9 +186,6 @@ end
180186
class Player
181187
super GameEntity
182188

183-
# Key is based on player `name`.
184-
redef var key is lazy do return "players" / name
185-
186189
redef var game
187190

188191
# FIXME contructor should be private
@@ -195,6 +198,8 @@ class Player
195198
# The name is also used to load the user data lazilly from Github API.
196199
var name: String
197200

201+
redef var key = name is lazy
202+
198203
# Player amount of nitcoins.
199204
#
200205
# Nitcoins is the currency used in nitrpg.
@@ -218,6 +223,7 @@ class Player
218223

219224
redef fun to_json do
220225
var json = super
226+
json["game"] = game.key
221227
json["name"] = name
222228
json["nitcoins"] = nitcoins
223229
return json

contrib/nitrpg/src/reactors.nit

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ redef class PullRequestEvent
5858

5959
# Rewards player for opened pull requests.
6060
redef fun react_player_event(r, game) do
61-
if action == "opened" then
61+
if action == "opened" or action == "reopened" then
6262
react_pull_open(r, game)
6363
else if action == "closed" then
6464
react_pull_close(r, game)
@@ -95,14 +95,18 @@ redef class IssueCommentEvent
9595
# Rewards player for review comments.
9696
#
9797
# TODO only give nitcoins if reviewers < 2
98+
# TODO give more points to first reviewer
9899
redef fun react_player_event(r, game) do
99100
if comment.is_ack then
100101
react_player_review(r, game)
101102
end
102103
end
103104

105+
# TODO same player should not be authorized to review multiple times? How to handle rerols?
104106
private fun react_player_review(r: PlayerReactor, game: Game) do
107+
if issue.state == "closed" then return
105108
var player = comment.user.player(game)
109+
if issue.user == player.user then return
106110
player.nitcoins += r.nc_pull_review
107111
player.save
108112
var event = player_reward_event("pull_review", player, r.nc_pull_review)

contrib/nitrpg/src/statistics.nit

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,49 +79,32 @@ class GameStatsManager
7979
# The GameEntity monitored by these statistics.
8080
var owner: GameEntity
8181

82-
redef var key = "stats"
82+
# Current date to extract stats
83+
private var date = new Tm.gmtime
8384

8485
# Returns the `GameStats` instance for the overall statistics.
85-
var overall: GameStats is lazy do
86-
return load_stats_for("all")
87-
end
86+
var overall: GameStats = load_stats_for("all") is lazy
8887

8988
# Returns the `GameStats` instance for the current year statistics.
90-
var yearly: GameStats is lazy do
91-
var date = new Tm.gmtime
92-
var key = date.strftime("%Y")
93-
return load_stats_for(key)
94-
end
89+
var yearly: GameStats = load_stats_for(date.strftime("%Y")) is lazy
9590

9691
# Returns the `GameStats` instance for the current month statistics.
97-
var monthly: GameStats is lazy do
98-
var date = new Tm.gmtime
99-
var key = date.strftime("%Y-%m")
100-
return load_stats_for(key)
101-
end
92+
var monthly: GameStats = load_stats_for(date.strftime("%Y-%m")) is lazy
10293

10394
# Returns the `GameStats` instance for the current day statistics.
104-
var daily: GameStats is lazy do
105-
var date = new Tm.gmtime
106-
var key = date.strftime("%Y-%m-%d")
107-
return load_stats_for(key)
108-
end
95+
var daily: GameStats = load_stats_for(date.strftime("%Y-%m-%d")) is lazy
10996

11097
# Returns the `GameStats` instance for the current week statistics.
111-
var weekly: GameStats is lazy do
112-
var date = new Tm.gmtime
113-
var key = date.strftime("%Y-W%U")
114-
return load_stats_for(key)
115-
end
98+
var weekly: GameStats = load_stats_for(date.strftime("%Y-W%U")) is lazy
11699

117100
# Load statistics for a `period` key.
118101
fun load_stats_for(period: String): GameStats do
119102
var key = owner.key / self.key / period
120103
if not game.store.has_key(key) then
121-
return new GameStats(game, period)
104+
return new GameStats(game, period, owner)
122105
end
123106
var json = game.store.load_object(key)
124-
return new GameStats.from_json(game, period, json)
107+
return new GameStats.from_json(game, period, owner, json)
125108
end
126109

127110
redef fun [](key) do return overall[key]
@@ -168,19 +151,28 @@ class GameStats
168151

169152
redef var game
170153

171-
# The pedriod these stats are about.
154+
# The period these stats are about.
172155
var period: String
173156

174-
redef fun key do return period
157+
# The game entity these stats are about.
158+
var owner: GameEntity
159+
160+
redef var key = "{owner.key}-{period}" is lazy
175161

176162
# Load `self` from saved data.
177-
init from_json(game: Game, period: String, json: JsonObject) do
178-
for k, v in json do self[k] = v.as(Int)
163+
init from_json(game: Game, period: String, owner: GameEntity, json: JsonObject) do
164+
var values = json.get_or_null("values")
165+
if not values isa JsonObject then return
166+
for k, v in values do self[k] = v.as(Int)
179167
end
180168

181169
redef fun to_json do
182-
var obj = new JsonObject
183-
for k, v in self do obj[k] = v
170+
var obj = super
171+
obj["period"] = period
172+
obj["owner"] = owner.key
173+
var values = new JsonObject
174+
values.recover_with(self)
175+
obj["values"] = values
184176
return obj
185177
end
186178

contrib/nitrpg/src/templates/templates_base.nit

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ redef class Game
3434

3535
redef fun url do return "{root_url}/games" / key
3636

37-
# Displayed name.
38-
fun name: String do return repo.full_name
39-
4037
# Return a HTML link to this Game.
4138
fun link: String do return "<a href=\"{url}\">{name}</a>"
4239
end

0 commit comments

Comments
 (0)