Skip to content

Multiple payments triggered within the same period (Financial drain) #27

@outstranger

Description

@outstranger

I encountered a critical issue where LeaseDeal:processMonthly() is triggered multiple times during the same in-game day (e.g., at 10:00, 13:00, 17:00), causing massive financial losses for the player. This seems to happen when the PERIOD_CHANGED event fires unexpectedly or conflicts with other environment mods.

The Solution:
I implemented a guard clause using lastPaidPeriod to ensure the monthly payment is processed exactly once per in-game period.

Here are the required changes in src/LeaseDeal.lua:

1. In LeaseDeal.new (Initialize the state):

function LeaseDeal.new(dealType, baseCost, deposit, durationMonths, finalFee, monthsPaid)
... existing code ...
self.vehicle = ""
self.farmId = -1
self.objectId = -1

-- ADD THIS LINE
self.lastPaidPeriod = -1 

return self

end

2. In LeaseDeal:processMonthly (Add the guard clause):

function LeaseDeal:processMonthly()
-- ADD THIS BLOCK AT THE START
local currentPeriod = -1
if g_currentMission.environment and g_currentMission.environment.currentPeriod then
currentPeriod = g_currentMission.environment.currentPeriod
end

-- Prevent double payment in the same month
if self.lastPaidPeriod ~= -1 and self.lastPaidPeriod == currentPeriod then
    return false
end

self.lastPaidPeriod = currentPeriod
-- END OF NEW BLOCK

local farm = g_farmManager:getFarmById(self.farmId)
... rest of the function

3. In LeaseDeal:saveToXmlFile (Persist the state):

function LeaseDeal:saveToXmlFile(xmlFile, key)
... existing lines ...
setXMLInt(xmlFile, key .. "#farmId", self.farmId)

-- ADD THIS LINE
setXMLInt(xmlFile, key .. "#lastPaidPeriod", self.lastPaidPeriod)

end

4. In LeaseDeal:loadFromXMLFile (Load the state):

function LeaseDeal:loadFromXMLFile(xmlFile, key)
... existing lines ...
self.farmId = getXMLInt(xmlFile, key .. "#farmId")

-- ADD THIS LINE
self.lastPaidPeriod = getXMLInt(xmlFile, key .. "#lastPaidPeriod") or -1

end

tested with no further issues from my side.
Good crops !

The Green Ant Farm

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions