Skip to content

Commit 0f20d80

Browse files
committed
Fix log trimming logic, rename some variables to be consitent and more intuitive
1 parent c6c7edc commit 0f20d80

File tree

1 file changed

+134
-74
lines changed

1 file changed

+134
-74
lines changed

data/modules/FlightLog/FlightLog.lua

+134-74
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,138 @@ local Character = require 'Character'
1919

2020
local FlightLogEntry = require 'modules.FlightLog.FlightLogEntries'
2121

22-
2322
-- default values (private)
2423
---@type integer
25-
local MaxTotalDefaultElements = 3000
24+
local MaxTotalNonCustomElements = 3000
2625

2726
-- private data - the log itself
2827
---@type FlightLogEntry.Base[]
2928
local FlightLogData = {}
30-
local FlightLogDefaultElementCount = 0
3129

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
34114

35115
--
36116
-- Group: Methods
37117
--
118+
-- Method: AddEntry
38119
--
120+
-- Adds an entry to the flightlog
39121
--
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+
40154
-- Method: MakeCustomEntry
41155
--
42156
-- Create a custom entry. A set of information is automatically
@@ -92,8 +206,11 @@ function FlightLog:MakeCustomEntry(text)
92206
location = {state, sysname}
93207
end
94208

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 ) )
96210
end
211+
212+
213+
97214
-- Method: GetLogEntries
98215
--
99216
-- Example:
@@ -141,91 +258,27 @@ function FlightLog:GetLogEntries(types, maximum, earliest_first)
141258
end
142259
end
143260

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-
204261
-- LOGGING
205262

206263
-- onLeaveSystem
207264
local AddSystemDepartureToLog = function (ship)
208265
if not ship:IsPlayer() then return end
209266

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 ) )
213268
end
214269

215270
-- onEnterSystem
216271
local AddSystemArrivalToLog = function (ship)
217272
if not ship:IsPlayer() then return end
218273

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 ) )
221275
end
222276

223277
-- onShipDocked
224278
local AddStationToLog = function (ship, station)
225279
if not ship:IsPlayer() then return end
226280

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 ) )
229282
end
230283

231284
-- LOADING AND SAVING
@@ -273,12 +326,19 @@ local onGameStart = function ()
273326
FlightLogData = loaded_data.Data
274327
end
275328

329+
NonCustomElementCount = 0
330+
for _, e in pairs(FlightLogData) do
331+
if not e:IsCustom() then
332+
AdjustNonCustomElementCount( 1 )
333+
end
334+
end
335+
276336
loaded_data = nil
277337
end
278338

279339
local onGameEnd = function ()
280340
FlightLogData = {}
281-
FlightLogEntry.TotalDefaultElements = 0
341+
NonCustomElementCount = 0
282342
end
283343

284344
local serialize = function ()

0 commit comments

Comments
 (0)