forked from bodzio528/FS22_LeaseToOwn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeaseToOwn.lua
145 lines (115 loc) · 5.85 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
--
-- FS22 - LeaseToOwn mod
--
-- LeaseToOwn.lua
--
-- @Author: Bodzio528
--
LeaseToOwn = {
MOD_DIRECTORY = g_currentModDirectory or "",
MOD_NAME = g_currentModName or "FS22_LeaseToOwn",
}
-- TODO: search & destroy
LeaseToOwn.modName = g_currentModName or "FS22_LeaseToOwn"
LeaseToOwn.LEASE_PERIOD = 36 -- lease agreement duration in months
LeaseToOwn.LEASE_USAGE_LIMIT = 20 -- lease agreement working hours limit
-- 1% monthly fee
LeaseToOwn.LEASE_WAGE_PER_PERIOD = EconomyManager.PER_DAY_LEASING_FACTOR
-- 2.1% per operating hour fee
LeaseToOwn.LEASE_WAGE_PER_HOUR = EconomyManager.DEFAULT_RUNNING_LEASING_FACTOR
-- 2% initial base fee
LeaseToOwn.LEASE_INITIAL_FEE = EconomyManager.DEFAULT_LEASING_DEPOSIT_FACTOR
source(LeaseToOwn.MOD_DIRECTORY .. "Logger.lua")
source(LeaseToOwn.MOD_DIRECTORY .. "LeaseToOwnEvent.lua")
local logger = Logger.create(LeaseToOwn.MOD_NAME)
logger:setLevel(Logger.INFO)
local pageShopItemDetails = g_gui.screenControllers[ShopMenu].pageShopItemDetails
function LeaseToOwn:onShopItemDetailsOpen()
logger:debug("LeaseToOwn:onShopItemDetailsOpen pageShopItemDetails")
local shopMenu = g_currentMission.shopMenu
local currentPage = shopMenu.pageShopItemDetails
local itemsList = currentPage.itemsList
if itemsList ~= nil and itemsList.totalItemCount == 0 then
logger.info("LeaseToOwn: page itemList (is nil = "..tostring(itemsList ~= nil)..") is empty. Abort!")
return
end
local selectedItemIdx = itemsList.selectedIndex
if selectedItemIdx < 1 then
logger:info("LeaseToOwn: selected item idx "..selectedItemIdx.." is invalid. Abort!")
return
end
local vehicle = itemsList.dataSource.displayItems[selectedItemIdx].concreteItem
if vehicle ~= nil then
local isPageListOfLeasedVehicles = (vehicle.propertyState == Vehicle.PROPERTY_STATE_LEASED)
if isPageListOfLeasedVehicles and shopMenu.leasePurchase_Button == nil then
local leasePurchaseElement = shopMenu.menuButton[1]:clone(self)
leasePurchaseElement:setText(g_i18n:getText("LeaseToOwn_PURCHASE"))
leasePurchaseElement:setInputAction("MENU_EXTRA_1")
leasePurchaseElement.onClickCallback = function ()
LeaseToOwn.purchase()
end
shopMenu.menuButton[1].parent:addElement(leasePurchaseElement)
shopMenu.leasePurchase_Button = leasePurchaseElement
end
end
end
function LeaseToOwn:purchase()
logger:debug("LeaseToOwn:purchase leasePurchaseElement callback begin")
local shopMenu = g_currentMission.shopMenu
local currentPage = shopMenu.pageShopItemDetails
local itemsList = currentPage.itemsList
local selectedItemIdx = itemsList.selectedIndex
local vehicle = itemsList.dataSource.displayItems[selectedItemIdx].concreteItem
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))
g_gui:showYesNoDialog({text = text,
title = g_i18n:getText("LeaseToOwn_leasePurchaseQuestionHeader"),
callback = LeaseToOwn.onConfirm,
target = LeaseToOwn})
end
function LeaseToOwn:onConfirm(confirm)
logger:debug("LeaseToOwn:onConfirm "..tostring(confirm))
if confirm then
local farm = g_farmManager:getFarmByUserId(g_currentMission.playerUserId)
if farm ~= nil then
g_client:getServerConnection():sendEvent(LeaseToOwnEvent.new(LeaseToOwn.selectedVehicle,
farm.farmId,
LeaseToOwn.price))
end
end
g_currentMission.shopMenu:updateGarageItems()
end
pageShopItemDetails.onFrameOpen = Utils.appendedFunction(pageShopItemDetails.onFrameOpen, LeaseToOwn.onShopItemDetailsOpen)
function LeaseToOwn:onShopItemDetailsClose()
logger:debug("LeaseToOwn:onShopItemDetailsClose pageShopItemDetails")
local shopMenu = g_currentMission.shopMenu
if shopMenu.leasePurchase_Button ~= nil then
shopMenu.leasePurchase_Button:unlinkElement()
shopMenu.leasePurchase_Button:delete()
shopMenu.leasePurchase_Button = nil
end
end
pageShopItemDetails.onFrameClose = Utils.appendedFunction(pageShopItemDetails.onFrameClose, LeaseToOwn.onShopItemDetailsClose)
--------------------------------------------------------------------------------
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