1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014-2015 Alexandre Terrasa <alexandre@moz-code.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # `nitrpg` game events.
19 # In this module we introduce the concept of `GameEvent`.
20 # They can be attached to every GameEntities.
25 redef class GameEntity
27 # Register a new game event for this entity.
28 fun add_event
(event
: GameEvent) do
33 # List all events registered in this entity.
35 # This list is reloaded from game data each time its called.
37 # To add events see `add_event`.
38 fun load_events
: Array[GameEvent] do
39 var req
= new JsonObject
40 req
["game"] = game
.key
42 var res
= new Array[GameEvent]
43 for obj
in game
.db
.collection
("events").find_all
(req
) do
44 res
.add
new GameEvent.from_json
(game
, obj
)
46 (new EventTimeComparator).sort
(res
)
50 # Load the event from its `id`.
52 # Looks for the event save file in game data.
53 # Returns `null` if the event cannot be found.
54 fun load_event
(id
: String): nullable GameEvent do
55 var req
= new JsonObject
56 req
["game"] = game
.key
58 req
["internal_id"] = id
59 var res
= game
.db
.collection
("events").find
(req
)
60 if res
!= null then return new GameEvent.from_json
(game
, res
)
65 # An event that occurs in the `Game`.
69 redef var collection_name
= "events"
73 # Entity this event belongs to.
74 var owner
: nullable GameEntity = null
76 # String used to dissociate events in the display.
79 # GameEvents have raw data associated to them.
81 # These data are stored in a JsonObject.
82 var data
: JsonObject is writable
84 # GameEvent uniq id used for storage.
85 var internal_id
: String is noinit
87 redef var key
= internal_id
is lazy
89 # Date and time of the event.
90 var time
: ISODate is noinit
, writable
92 # An event initialized at now `time`.
94 internal_id
= "{get_time}{object_id}{100.rand}"
98 # Init `self` from a `json` object.
100 # Used to load events from json storage.
101 init from_json
(game
: Game, json
: JsonObject) do
102 init(game
, json
["kind"].as(String), json
["data"].as(JsonObject))
103 internal_id
= json
["internal_id"].as(String)
104 time
= new ISODate.from_string
(json
["time"].as(String))
107 redef fun to_json_object
do
108 var json
= new JsonObject
109 json
["internal_id"] = internal_id
.to_s
111 json
["time"] = time
.to_s
113 json
["game"] = game
.key
114 var owner
= self.owner
115 if owner
!= null then json
["owner"] = owner
.key
120 # Compare `GameEvent` to sort them from the most recent to the older.
121 class EventTimeComparator
124 redef type COMPARED: GameEvent
126 redef fun compare
(a
, b
) do return b
.time
<=> a
.time