2573a58d1f8cba8ece96b7f7393836301296a811
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 fun add_event
(event
: GameEvent) do
32 # List all events registered in this entity.
34 # This list is reloaded from game data each time its called.
36 # To add events see `add_event`.
37 fun load_events
: Array[GameEvent] do
38 var req
= new JsonObject
39 req
["game"] = game
.key
41 var res
= new Array[GameEvent]
42 for obj
in game
.db
.collection
("events").find_all
(req
) do
43 res
.add
new GameEvent.from_json
(game
, obj
)
45 (new EventTimeComparator).sort
(res
)
49 # Load the event from its `id`.
51 # Looks for the event save file in game data.
52 # Returns `null` if the event cannot be found.
53 fun load_event
(id
: String): nullable GameEvent do
54 var req
= new JsonObject
55 req
["game"] = game
.key
57 req
["internal_id"] = id
58 var res
= game
.db
.collection
("events").find
(req
)
59 if res
!= null then return new GameEvent.from_json
(game
, res
)
64 # An event that occurs in the `Game`.
68 redef var collection_name
= "events"
72 # Entity this event belongs to.
73 var owner
: nullable GameEntity = null
75 # String used to dissociate events in the display.
78 # GameEvents have raw data associated to them.
80 # These data are stored in a JsonObject.
81 var data
: JsonObject is writable
83 # GameEvent uniq id used for storage.
84 var internal_id
: String is noinit
86 redef var key
= internal_id
is lazy
88 # Date and time of the event.
89 var time
: ISODate is noinit
, writable
91 # An event initialized at now `time`.
93 internal_id
= "{get_time}{object_id}{100.rand}"
97 # Init `self` from a `json` object.
99 # Used to load events from json storage.
100 init from_json
(game
: Game, json
: JsonObject) do
101 init(game
, json
["kind"].to_s
, json
["data"].as(JsonObject))
102 internal_id
= json
["internal_id"].to_s
103 time
= new ISODate.from_string
(json
["time"].to_s
)
107 var json
= new JsonObject
108 json
["internal_id"] = internal_id
.to_s
110 json
["time"] = time
.to_s
112 json
["game"] = game
.key
113 if owner
!= null then json
["owner"] = owner
.key
118 # Compare `GameEvent` to sort them from the most recent to the older.
119 class EventTimeComparator
122 redef type COMPARED: GameEvent
124 redef fun compare
(a
, b
) do return b
.time
<=> a
.time