@@ -19,24 +19,138 @@ local Character = require 'Character'
19
19
20
20
local FlightLogEntry = require ' modules.FlightLog.FlightLogEntries'
21
21
22
-
23
22
-- default values (private)
24
23
--- @type integer
25
- local MaxTotalDefaultElements = 3000
24
+ local MaxTotalNonCustomElements = 3000
26
25
27
26
-- private data - the log itself
28
27
--- @type FlightLogEntry.Base[]
29
28
local FlightLogData = {}
30
- local FlightLogDefaultElementCount = 0
31
29
32
- local FlightLog
33
- FlightLog = {}
30
+ local FlightLog = {}
31
+
32
+ --- How many default (so without custom elements) entries are there?
33
+ --- @type integer
34
+ local NonCustomElementCount = 0
35
+
36
+ --- @praram count integer The amount to alter the count of default elements by
37
+ local function AdjustNonCustomElementCount ( count )
38
+ NonCustomElementCount = NonCustomElementCount + count
39
+ end
40
+
41
+ FlightLogEntry .Base .non_custom_count_change = AdjustNonCustomElementCount
42
+
43
+ --- If there are two system events back to back, starting at first_index
44
+ --- entering and leaving the same system, it will put them together
45
+ --- as a single system event
46
+ ---
47
+ --- @param first_index integer The index of the first element in the array (so the latest event ) to collapse
48
+ --- @return boolean true if the two were collapesed.
49
+ local function ConsiderCollapseSystemEventPair ( first_index )
50
+
51
+ if # FlightLogData < 2 then return false end
52
+
53
+ local second = FlightLogData [first_index ]
54
+ local first = FlightLogData [first_index + 1 ]
55
+
56
+ if ( second :IsCustom () ) then return false end
57
+ if ( second :GetType () ~= " System" ) then return false end
58
+ --- @cast second SystemLogEntry
59
+
60
+ -- is the latest one actually an arrival event, or already collapsed.
61
+ if ( second .arrtime ~= nil ) then return fale end
62
+
63
+ if ( first :IsCustom () ) then return false end
64
+ if ( first :GetType () ~= " System" ) then return false end
65
+ --- @cast first SystemLogEntry
66
+
67
+ -- is the first one actually a departure event or already collapsed
68
+ if ( first .deptime ~= nil ) then return false end
69
+
70
+ if ( first .systemp ~= second .systemp ) then return false end
71
+
72
+ second .arrtime = first .arrtime
73
+ table.remove ( FlightLogData , first_index + 1 )
74
+
75
+ AdjustNonCustomElementCount ( - 1 )
76
+
77
+ return true
78
+ end
79
+
80
+
81
+ -- This will run through the array of events and if there are two system events
82
+ -- back to back, entering and leaving the same system, it will put them together
83
+ -- as a single system event
84
+ local function CollapseSystemEvents ()
85
+ for i = # FlightLogData - 1 , 1 , - 1 do
86
+ ConsiderCollapseSystemEventPair ( i )
87
+ end
88
+ end
89
+
90
+ -- This will run through the array of events and remove any non custom ones
91
+ -- if we have exceeded our maximum size, until that maximum size is reattained.
92
+ local function TrimLogSize ()
93
+ if NonCustomElementCount > MaxTotalNonCustomElements then
94
+ CollapseSystemEvents ()
95
+
96
+ if NonCustomElementCount > MaxTotalNonCustomElements then
97
+ for i = # FlightLogData , 1 , - 1 do
98
+ local v = FlightLogData [i ]
99
+ if not v :IsCustom () then
100
+ table.remove ( FlightLogData , i )
101
+ AdjustNonCustomElementCount ( - 1 )
102
+ if i > 1 then
103
+ ConsiderCollapseSystemEventPair ( i - 1 )
104
+ end
105
+
106
+ if NonCustomElementCount <= MaxTotalNonCustomElements then
107
+ return
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
34
114
35
115
--
36
116
-- Group: Methods
37
117
--
118
+ -- Method: AddEntry
38
119
--
120
+ -- Adds an entry to the flightlog
39
121
--
122
+ -- Parameters:
123
+ -- entry - The entry to add to the list
124
+ --- @param entry FlightLogEntry.Base
125
+ function FlightLog .AddEntry (entry )
126
+ table.insert (FlightLogData , 1 , entry )
127
+
128
+ if not entry :IsCustom () then
129
+ AdjustNonCustomElementCount ( 1 )
130
+ end
131
+
132
+ if not ConsiderCollapseSystemEventPair (1 ) then
133
+ TrimLogSize ()
134
+ end
135
+ end
136
+
137
+ -- Method: RemoveEntry
138
+ --
139
+ -- Removes an entry from the flightlog
140
+ --
141
+ -- Parameters:
142
+ -- entry - The entry to remove from the list
143
+ --- @param entry FlightLogEntry.Base
144
+ function FlightLog :RemoveEntry (entry )
145
+ if not entry :IsCustom () then
146
+ AdjustNonCustomElementCount ( - 1 )
147
+ end
148
+ local index = utils .remove_elem ( FlightLogData , entry )
149
+ if index > 1 then
150
+ ConsiderCollapseSystemEventPair ( index - 1 )
151
+ end
152
+ end
153
+
40
154
-- Method: MakeCustomEntry
41
155
--
42
156
-- Create a custom entry. A set of information is automatically
@@ -92,8 +206,11 @@ function FlightLog:MakeCustomEntry(text)
92
206
location = {state , sysname }
93
207
end
94
208
95
- table.insert ( FlightLogData , 1 , FlightLogEntry .Custom .New ( path , Game .time , Game .player :GetMoney (), location , text ) )
209
+ FlightLog . AddEntry ( FlightLogEntry .Custom .New ( path , Game .time , Game .player :GetMoney (), location , text ) )
96
210
end
211
+
212
+
213
+
97
214
-- Method: GetLogEntries
98
215
--
99
216
-- Example:
@@ -141,91 +258,27 @@ function FlightLog:GetLogEntries(types, maximum, earliest_first)
141
258
end
142
259
end
143
260
144
- --- If there are two system events back to back, starting at first_index
145
- --- entering and leaving the same system, it will put them together
146
- --- as a single system event
147
- ---
148
- --- @param first_index integer The index of the first element in the array (so the latest event ) to collapse
149
- local function ConsiderCollapseSystemEventPair ( first_index )
150
-
151
- local second = FlightLogData [first_index ]
152
- local first = FlightLogData [first_index + 1 ]
153
-
154
- if ( second :IsCustom () ) then return end
155
- if ( second :GetType () ~= " System" ) then return end
156
- --- @cast second SystemLogEntry
157
-
158
- -- is the latest one actually an arrival event, or already collapsed.
159
- if ( second .arrtime ~= nil ) then return end
160
-
161
- if ( first :IsCustom () ) then return end
162
- if ( first :GetType () ~= " System" ) then return end
163
- --- @cast first SystemLogEntry
164
-
165
- -- is the first one actually a departure event or already collapsed
166
- if ( first .deptime ~= nil ) then return end
167
-
168
- if ( first .systemp ~= second .systemp ) then return end
169
-
170
- second .arrtime = first .arrtime
171
- table.remove ( FlightLogData , first_index + 1 )
172
-
173
- end
174
-
175
-
176
- -- This will run through the array of events and if there are two system events
177
- -- back to back, entering and leaving the same system, it will put them together
178
- -- as a single system event
179
- local function CollapseSystemEvents ()
180
- for i = # FlightLogData - 1 , 1 , - 1 do
181
- ConsiderCollapseSystemEventPair ( i )
182
- end
183
- end
184
-
185
- -- This will run through the array of events and remove any non custom ones
186
- -- if we have exceeded our maximum size, until that maximum size is reattained.
187
- local function TrimLogSize ()
188
- if FlightLogEntry .TotalDefaultElements > MaxTotalDefaultElements then
189
- CollapseSystemEvents ()
190
- while FlightLogEntry .TotalDefaultElements > MaxTotalDefaultElements do
191
- for i = # FlightLogData , 1 , - 1 do
192
- local v = FlightLogData [i ]
193
- if not v :IsCustom () then
194
- table.remove ( FlightLogData , i )
195
- FlightLogEntry .TotalDefaultElements = FlightLogEntry .TotalDefaultElements - 1
196
- end
197
- end
198
- end
199
- CollapseSystemEvents ()
200
- end
201
- end
202
-
203
-
204
261
-- LOGGING
205
262
206
263
-- onLeaveSystem
207
264
local AddSystemDepartureToLog = function (ship )
208
265
if not ship :IsPlayer () then return end
209
266
210
- table.insert ( FlightLogData , 1 , FlightLogEntry .System .New ( Game .system .path , nil , Game .time , nil ) );
211
- ConsiderCollapseSystemEventPair ( 1 )
212
- TrimLogSize ()
267
+ FlightLog .AddEntry ( FlightLogEntry .System .New ( Game .system .path , nil , Game .time , nil ) )
213
268
end
214
269
215
270
-- onEnterSystem
216
271
local AddSystemArrivalToLog = function (ship )
217
272
if not ship :IsPlayer () then return end
218
273
219
- table.insert ( FlightLogData , 1 , FlightLogEntry .System .New ( Game .system .path , Game .time , nil , nil ) );
220
- TrimLogSize ()
274
+ FlightLog .AddEntry ( FlightLogEntry .System .New ( Game .system .path , Game .time , nil , nil ) )
221
275
end
222
276
223
277
-- onShipDocked
224
278
local AddStationToLog = function (ship , station )
225
279
if not ship :IsPlayer () then return end
226
280
227
- table.insert ( FlightLogData , 1 , FlightLogEntry .Station .New ( station .path , Game .time , Game .player :GetMoney (), nil ) );
228
- TrimLogSize ()
281
+ FlightLog .AddEntry ( FlightLogEntry .Station .New ( station .path , Game .time , Game .player :GetMoney (), nil ) )
229
282
end
230
283
231
284
-- LOADING AND SAVING
@@ -273,12 +326,19 @@ local onGameStart = function ()
273
326
FlightLogData = loaded_data .Data
274
327
end
275
328
329
+ NonCustomElementCount = 0
330
+ for _ , e in pairs (FlightLogData ) do
331
+ if not e :IsCustom () then
332
+ AdjustNonCustomElementCount ( 1 )
333
+ end
334
+ end
335
+
276
336
loaded_data = nil
277
337
end
278
338
279
339
local onGameEnd = function ()
280
340
FlightLogData = {}
281
- FlightLogEntry . TotalDefaultElements = 0
341
+ NonCustomElementCount = 0
282
342
end
283
343
284
344
local serialize = function ()
0 commit comments