2573a58d1f8cba8ece96b7f7393836301296a811
[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 fun add_event(event: GameEvent) do
28 event.owner = self
29 event.save
30 end
31
32 # List all events registered in this entity.
33 #
34 # This list is reloaded from game data each time its called.
35 #
36 # To add events see `add_event`.
37 fun load_events: Array[GameEvent] do
38 var req = new JsonObject
39 req["game"] = game.key
40 req["owner"] = 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)
44 end
45 (new EventTimeComparator).sort(res)
46 return res
47 end
48
49 # Load the event from its `id`.
50 #
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
56 req["owner"] = 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)
60 return null
61 end
62 end
63
64 # An event that occurs in the `Game`.
65 class GameEvent
66 super GameEntity
67
68 redef var collection_name = "events"
69
70 redef var game
71
72 # Entity this event belongs to.
73 var owner: nullable GameEntity = null
74
75 # String used to dissociate events in the display.
76 var kind: String
77
78 # GameEvents have raw data associated to them.
79 #
80 # These data are stored in a JsonObject.
81 var data: JsonObject is writable
82
83 # GameEvent uniq id used for storage.
84 var internal_id: String is noinit
85
86 redef var key = internal_id is lazy
87
88 # Date and time of the event.
89 var time: ISODate is noinit, writable
90
91 # An event initialized at now `time`.
92 init do
93 internal_id = "{get_time}{object_id}{100.rand}"
94 time = new ISODate
95 end
96
97 # Init `self` from a `json` object.
98 #
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)
104 end
105
106 redef fun to_json do
107 var json = new JsonObject
108 json["internal_id"] = internal_id.to_s
109 json["kind"] = kind
110 json["time"] = time.to_s
111 json["data"] = data
112 json["game"] = game.key
113 if owner != null then json["owner"] = owner.key
114 return json
115 end
116 end
117
118 # Compare `GameEvent` to sort them from the most recent to the older.
119 class EventTimeComparator
120 super Comparator
121
122 redef type COMPARED: GameEvent
123
124 redef fun compare(a, b) do return b.time <=> a.time
125 end