|
| 1 | +-- ========================================================= |
| 2 | +-- CTCNetworkEvent.lua -- FS25_CustomTriggerCreator |
| 3 | +-- Routes economy addMoney calls from client to server. |
| 4 | +-- Only used when the local machine is a client (not the server). |
| 5 | +-- ========================================================= |
| 6 | + |
| 7 | +CTCNetworkEvent = {} |
| 8 | +CTCNetworkEvent_mt = Class(CTCNetworkEvent, Event) |
| 9 | +InitEventClass(CTCNetworkEvent, "CTCNetworkEvent") |
| 10 | + |
| 11 | +function CTCNetworkEvent.emptyNew() |
| 12 | + local self = Event.new(CTCNetworkEvent_mt) |
| 13 | + return self |
| 14 | +end |
| 15 | + |
| 16 | +---@param farmId number Farm to receive/pay the money |
| 17 | +---@param amount number Positive = farm receives, negative = farm pays |
| 18 | +function CTCNetworkEvent.new(farmId, amount) |
| 19 | + local self = CTCNetworkEvent.emptyNew() |
| 20 | + self.farmId = farmId |
| 21 | + self.amount = amount |
| 22 | + return self |
| 23 | +end |
| 24 | + |
| 25 | +function CTCNetworkEvent:writeStream(streamId, connection) |
| 26 | + streamWriteInt32(streamId, self.farmId or 0) |
| 27 | + streamWriteInt32(streamId, self.amount or 0) |
| 28 | +end |
| 29 | + |
| 30 | +function CTCNetworkEvent:readStream(streamId, connection) |
| 31 | + self.farmId = streamReadInt32(streamId) |
| 32 | + self.amount = streamReadInt32(streamId) |
| 33 | + self:run(connection) |
| 34 | +end |
| 35 | + |
| 36 | +-- Server-side: apply the money operation |
| 37 | +function CTCNetworkEvent:run(connection) |
| 38 | + if not g_currentMission:getIsServer() then return end |
| 39 | + if not self.farmId or self.farmId <= 0 then return end |
| 40 | + g_currentMission:addMoney(self.amount, self.farmId, MoneyType.OTHER, true) |
| 41 | +end |
0 commit comments