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