contrib/nitrpg: `save_in` uses string key instead of GameEntity
[nit.git] / contrib / nitrpg / src / 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 # `nitrpg` game events.
18 #
19 # In this module we introduce the concept of `GameEvent`.
20 # They can be attached to every GameEntities.
21 module events
22
23 import game
24
25 redef class GameEntity
26
27 # Saves `event` in `self`.
28 fun add_event(event: GameEvent) do event.save_in(self.key)
29
30 # List all events registered in this entity.
31 #
32 # This list is reloaded from game data each time its called.
33 #
34 # To add events see `add_event`.
35 fun load_events: Array[GameEvent] do
36 var key = key / "events"
37 var res = new Array[GameEvent]
38 if not game.store.has_collection(key) then return res
39 var coll = game.store.list_collection(key)
40 for id in coll do
41 var name = id.to_s
42 res.add load_event(name).as(not null)
43 end
44 (new EventTimeComparator).sort(res)
45 return res
46 end
47
48 # Load the event from its `id`.
49 #
50 # Looks for the event save file in game data.
51 # Returns `null` if the event cannot be found.
52 fun load_event(id: String): nullable GameEvent do
53 var key = key / "events" / id
54 if not game.store.has_key(key) then return null
55 var json = game.store.load_object(key)
56 return new GameEvent.from_json(game, json)
57 end
58 end
59
60 # An event that occurs in the `Game`.
61 class GameEvent
62 super GameEntity
63
64 redef var key is lazy do return "events" / internal_id
65
66 redef var game
67
68 # String used to dissociate events in the display.
69 var kind: String
70
71 # GameEvents have raw data associated to them.
72 #
73 # These data are stored in a JsonObject.
74 var data: JsonObject is writable
75
76 # GameEvent uniq id used for storage.
77 var internal_id: String is noinit
78
79 # Date and time of the event.
80 var time: ISODate is noinit, writable
81
82 # An event initialized at now `time`.
83 init do
84 internal_id = "{get_time}{object_id}{100.rand}"
85 time = new ISODate
86 end
87
88 # Init `self` from a `json` object.
89 #
90 # Used to load events from json storage.
91 init from_json(game: Game, json: JsonObject) do
92 self.game = game
93 internal_id = json["internal_id"].to_s
94 kind = json["kind"].to_s
95 time = new ISODate.from_string(json["time"].to_s)
96 data = json["data"].as(JsonObject)
97 end
98
99 redef fun to_json do
100 var json = new JsonObject
101 json["internal_id"] = internal_id.to_s
102 json["kind"] = kind
103 json["time"] = time.to_s
104 json["data"] = data
105 return json
106 end
107 end
108
109 # Compare `GameEvent` to sort them from the most recent to the older.
110 class EventTimeComparator
111 super Comparator
112
113 redef type COMPARED: GameEvent
114
115 redef fun compare(a, b) do return b.time <=> a.time
116 end