-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLeaseToOwn.lua
146 lines (113 loc) · 6.08 KB
/
LeaseToOwn.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
--
-- FS25 - LeaseToOwn mod
--
-- LeaseToOwn.lua
--
-- @Author: Bodzio528
--
LeaseToOwn = {
MOD_DIRECTORY = g_currentModDirectory or "",
MOD_NAME = g_currentModName or "FS25_LeaseToOwn",
LEASE_PERIOD = 36, -- lease agreement duration in months
LEASE_USAGE_LIMIT = 20, -- lease agreement working hours limit
LEASE_WAGE_PER_PERIOD = EconomyManager.PER_DAY_LEASING_FACTOR, -- 1% monthly fee
LEASE_WAGE_PER_HOUR = EconomyManager.DEFAULT_RUNNING_LEASING_FACTOR, -- 2.1% per operating hour fee
LEASE_INITIAL_FEE = EconomyManager.DEFAULT_LEASING_DEPOSIT_FACTOR -- 2% initial base fee
}
source(LeaseToOwn.MOD_DIRECTORY .. "Logger.lua")
source(LeaseToOwn.MOD_DIRECTORY .. "LeaseToOwnEvent.lua")
local logger = Logger.create(LeaseToOwn.MOD_NAME)
logger:setLevel(Logger.INFO)
function LeaseToOwn:onFrameOpen()
logger:debug("LeaseToOwn:onFrameOpen pageStatistics")
local clonedButton = g_inGameMenu.menuButton[1]:clone(self)
clonedButton:setDisabled(true)
clonedButton:setVisible(g_inGameMenu.pageStatistics.subCategoryPaging.state == 2)
clonedButton:setText(g_i18n:getText("LeaseToOwn_PURCHASE"))
clonedButton:setInputAction("MENU_EXTRA_1")
clonedButton.onClickCallback = LeaseToOwn.purchase
g_inGameMenu.menuButton[1].parent:addElement(clonedButton)
g_inGameMenu.leasePurchase_Button = clonedButton
LeaseToOwn:onVehicleListIndexChanged(g_inGameMenu.pageStatistics.vehiclesList.selectedIndex, g_inGameMenu.pageStatistics.vehiclesList.totalItemCount)
end
function LeaseToOwn:onFrameClose()
logger:debug("LeaseToOwn:onFrameClose pageStatistics")
local menuButton = g_inGameMenu.leasePurchase_Button
if menuButton ~= nil then
menuButton:unlinkElement()
menuButton:delete()
g_inGameMenu.leasePurchase_Button = nil
end
end
function LeaseToOwn:onPageStatisticsTabIndexChanged(index, count)
logger:debug("LeaseToOwn:onPageStatisticsTabIndexChanged pageStatistics")
local menuButton = g_inGameMenu.leasePurchase_Button
if menuButton ~= nil then
menuButton:setVisible(index == 2)
end
end
function LeaseToOwn:onVehicleListIndexChanged(index, count)
logger:debug("LeaseToOwn:onVehicleListIndexChanged vehiclesList")
if g_inGameMenu.leasePurchase_Button ~= nil and #g_inGameMenu.vehiclesList.dataSource.vehicles > 0 then
local vehicle = g_inGameMenu.vehiclesList.dataSource.vehicles[index].vehicle
if vehicle:getPropertyState() == VehiclePropertyState.LEASED then
g_inGameMenu.leasePurchase_Button:setDisabled(false)
else
g_inGameMenu.leasePurchase_Button:setDisabled(true)
end
end
end
function LeaseToOwn:purchase()
logger:debug("LeaseToOwn:purchase leasePurchaseElement callback begin")
local selectedVehicleIndex = g_inGameMenu.vehiclesList.dataSource.vehicles[g_inGameMenu.vehiclesList.selectedIndex]
local vehicle = selectedVehicleIndex.vehicle
local price = calculateVehicleValue(vehicle)
LeaseToOwn.price = price
LeaseToOwn.selectedVehicle = vehicle
local text = string.format(g_i18n:getText("LeaseToOwn_leasePurchaseQuestion"),
vehicle:getName(),
g_i18n:formatMoney(price))
YesNoDialog.show(LeaseToOwn.onConfirm, LeaseToOwn, text)
end
function LeaseToOwn:onConfirm(confirm)
logger:debug("LeaseToOwn:onConfirm "..tostring(confirm))
if confirm then
g_inGameMenu.leasePurchase_Button:setDisabled(false)
local farm = g_farmManager:getFarmByUserId(g_currentMission.playerUserId)
if farm ~= nil then
local vehicle = LeaseToOwn.selectedVehicle
local price = LeaseToOwn.price
g_client:getServerConnection():sendEvent(LeaseToOwnEvent.new(vehicle,
farm.farmId,
price))
g_inGameMenu.pageStatistics:updateVehicles()
InfoDialog.show(string.format(g_i18n:getText("LeaseToOwn_leasePurchaseCompleted"),
vehicle:getName(),
g_i18n:formatMoney(price)))
end
end
end
g_inGameMenu.vehiclesList:addIndexChangeObserver(LeaseToOwn, LeaseToOwn.onVehicleListIndexChanged)
g_inGameMenu.pageStatistics.subCategoryPaging:addIndexChangeObserver(LeaseToOwn, LeaseToOwn.onPageStatisticsTabIndexChanged)
local statisticsPage = g_inGameMenu.pageStatistics
statisticsPage.onFrameOpen = Utils.appendedFunction(statisticsPage.onFrameOpen, LeaseToOwn.onFrameOpen)
statisticsPage.onFrameClose = Utils.appendedFunction(statisticsPage.onFrameClose, LeaseToOwn.onFrameClose)
--------------------------------------------------------------------------------
function calculateVehicleValue(vehicle) -- put calculated value in confirmation dialog
local operatingTime = math.floor(vehicle:getOperatingTime() / (60 * 60 * 1000)) -- full hours
local vehicleAge = math.floor(vehicle.age) -- full months
local initialPrice = vehicle:getPrice() -- full shop price
local installments = LeaseToOwn.LEASE_WAGE_PER_PERIOD * initialPrice * math.min(LeaseToOwn.LEASE_PERIOD, vehicleAge)
local usage = LeaseToOwn.LEASE_WAGE_PER_HOUR * initialPrice * math.min(LeaseToOwn.LEASE_USAGE_LIMIT, operatingTime)
local commission = LeaseToOwn.LEASE_INITIAL_FEE * initialPrice * (1.0 - math.min(1.0, vehicleAge / LeaseToOwn.LEASE_PERIOD))
local price = math.max(0, initialPrice - installments - usage) - commission
logger:debug("LeaseToOwn: lease period "..LeaseToOwn.LEASE_PERIOD.." months")
logger:debug("LeaseToOwn: shop configuration initial price "..initialPrice)
logger:debug("LeaseToOwn: age "..vehicleAge.." month(s)")
logger:debug("LeaseToOwn: operating time "..operatingTime.. "h")
logger:debug("LeaseToOwn: lease installments paid "..installments)
logger:debug("LeaseToOwn: lease usage paid "..usage)
logger:debug("LeaseToOwn: lease commission to return "..commission)
logger:debug("LeaseToOwn: final purchase price for "..vehicle:getName().." is "..g_i18n:formatMoney(price))
return price
end