-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLeaseToOwnEvent.lua
103 lines (83 loc) · 3.4 KB
/
LeaseToOwnEvent.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
--
-- FS22 - LeaseToOwn mod
--
-- LeaseToOwnEvent.lua
--
-- @Author: Bodzio528
--
local logger = Logger.create(LeaseToOwn.MOD_NAME)
logger:setLevel(Logger.INFO)
LeaseToOwnEvent = {}
local LeaseToOwnEvent_mt = Class(LeaseToOwnEvent, Event)
InitEventClass(LeaseToOwnEvent, "LeaseToOwnEvent")
---Create instance of Event class
-- @return table self instance of class event
function LeaseToOwnEvent.emptyNew()
local self = Event.new(LeaseToOwnEvent_mt)
logger:debug("LeaseToOwnEvent:emptyNew")
return self
end
---Create new instance of event
-- @param table vehicle vehicle
function LeaseToOwnEvent.new(vehicle, farmId, price)
logger:debug("LeaseToOwnEvent:new")
local self = LeaseToOwnEvent.emptyNew()
self.vehicle = vehicle
self.farmId = farmId
self.price = price
return self
end
function LeaseToOwnEvent:writeStream(streamId, connection)
logger:debug("LeaseToOwnEvent:writeStream")
NetworkUtil.writeNodeObject(streamId, self.vehicle)
streamWriteUIntN(streamId, self.farmId, FarmManager.FARM_ID_SEND_NUM_BITS)
streamWriteInt32(streamId, self.price)
end
function LeaseToOwnEvent:readStream(streamId, connection)
logger:debug("LeaseToOwnEvent:readStream")
self.vehicle = NetworkUtil.readNodeObject(streamId)
self.farmId = streamReadUIntN(streamId, FarmManager.FARM_ID_SEND_NUM_BITS)
self.price = streamReadInt32(streamId)
self:run(connection)
end
function LeaseToOwnEvent:run(connection)
logger:debug("LeaseToOwnEvent:run")
if not connection:getIsServer() then
logger:debug("LeaseToOwnEvent: this is client")
if self.vehicle ~= nil and self.vehicle:getIsSynchronized() then
local player = g_currentMission:getPlayerByConnection(connection)
local farmAllowed = player ~= nil and g_currentMission:getHasPlayerPermission("farmManager", connection, player.farmId)
if player ~= nil and player.farmId > 0 and farmAllowed then
local isLeased = (self.vehicle:getPropertyState() == VehiclePropertyState.LEASED)
if isLeased and player.farmId == self.vehicle:getOwnerFarmId() then
if self.price > 0 then
local money = g_currentMission:getMoney(self.farmId)
if money < self.price then
logger:debug("LeaseToOwnEvent: Not enough money on the farm! Aborting.")
return
end
end
-- at this point all requirements are fulfilled
-- 1. deduct money from account
g_currentMission:addMoney(-self.price, self.farmId, MoneyType.SHOP_VEHICLE_BUY, true, true)
-- 2. change ownership of vehicle
setVehicleOwned(self.vehicle)
-- 3. broadcast event so others will know
g_server:broadcastEvent(self, true)
else
logger:debug("LeaseToOwnEvent: vehicle is NOT leased - abort!")
end
end
end
else -- this is server
logger:debug("LeaseToOwnEvent: this is server")
if self.vehicle ~= nil then
setVehicleOwned(self.vehicle)
end
end
end
function setVehicleOwned(vehicle)
vehicle.propertyState = VehiclePropertyState.OWNED
g_currentMission:removeLeasedItem(vehicle)
g_currentMission:addOwnedItem(vehicle)
end