diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b800dba --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: c +sudo: true + +before_install: + - sudo apt update + - sudo apt install luarocks + - sudo luarocks install busted + +script: + - busted test.lua \ No newline at end of file diff --git a/dcs_cc.lua b/dcs_cc.lua index 2ad0c42..9c49ce4 100644 --- a/dcs_cc.lua +++ b/dcs_cc.lua @@ -1,30 +1,15 @@ -if GLOBAL_MOOSE_DEBUG then - BASE:TraceOnOff(true) - BASE:TraceAll(true) - BASE:TraceLevel(2) -end - - -env.info("LOADING DCS_CC", GLOBAL_DEBUG_MODE) - -dcs_cc = {} +local dcs_cc = {} dcs_cc.banks = {} -dcs_cc.banks.red = config.startingResources -dcs_cc.banks.blue = config.startingResources +dcs_cc.banks.red = 0 +dcs_cc.banks.blue = 0 -dcs_cc.coalitions = { - coalition.side.BLUE, - coalition.side.RED, -} +dcs_cc.coalitions = nil -dcs_cc.objects = config.objects +dcs_cc.objects = nil -dcs_cc.spawnZone = { - ["blue"] = ZONE:New(config.spawnZone.blue), - ["red"] = ZONE:New(config.spawnZone.red), -} +dcs_cc.spawnZone = nil dcs_cc.captureZones = {} dcs_cc.transportGroups = {} @@ -75,7 +60,7 @@ function dcs_cc.initCargoZones() } end -dcs_cc.cargoZones = dcs_cc.initCargoZones() +dcs_cc.cargoZones = nil dcs_cc.spawners = {} @@ -360,63 +345,27 @@ function dcs_cc.buyAsCargo(Item, Coalition, Group) end end --- setup menu -for _, _coalition in pairs(dcs_cc.coalitions) do - local _mainMenu = MENU_COALITION:New(_coalition, "DCS Command & Conquer") - local _side = dcs_cc.getCoalitionName(_coalition) - - MENU_COALITION_COMMAND:New(_coalition, "Balance", _mainMenu, dcs_cc.coalitionBalance, _coalition) +function dcs_cc.tickResources() + env.info("RUNNING TICKS", GLOBAL_DEBUG_MODE) + local _redTickAmount = config.baseResourceGeneration + local _blueTickAmount = config.baseResourceGeneration - local _buyMenu = MENU_COALITION:New(_coalition, "Buy", _mainMenu) + for _, _captureZone in pairs(dcs_cc.captureZones) do + if _captureZone:IsGuarded() then + local _tickAmount = config.zoneResourceGeneration - for item, details in pairs(dcs_cc.objects) do - if details.group[_side] ~= nil then - env.info("Adding item: " .. item .. " to side: " .. _side, GLOBAL_DEBUG_MODE) - local _title = item .. ": " .. details.price - MENU_COALITION_COMMAND:New(_coalition, _title, _buyMenu, dcs_cc.buyItem, item, _coalition) + if _captureZone:GetCoalition() == coalition.side.RED then + _redTickAmount = _redTickAmount + _tickAmount + else + _blueTickAmount = _blueTickAmount + _tickAmount + end end end - - for _, _groupName in pairs(config.transportGroups[_side]) do - dcs_cc.transportGroups[_groupName] = nil - SCHEDULER:New(nil, - function() - local _group = GROUP:FindByName(_groupName) - if _group and _group:IsAlive() then - local _unit = _group:GetPlayerUnits()[1] - _unit:HandleEvent(EVENTS.Dead) - function _unit:OnEventDead(EventData) - local _menuCommand = dcs_cc.transportGroups[_groupName] - if _menuCommand then - _menuCommand:Remove() - end - dcs_cc.transportGroups[_groupName] = nil - dcs_cc.unitZones[_groupName] = nil - -- The zone is not removed, but never checked again, can this cause a performance issue? Should it be left in and reused? - -- TODO TEST - end - if dcs_cc.unitZones[_groupName] == nil then - dcs_cc.unitZones[_groupName] = ZONE_UNIT:New(_groupName, _unit, 100, {}) - end - MENU_GROUP_COMMAND:New(_group, "Unpack crate", nil, dcs_cc.unpackCrate, _group, _coalition) - - local _buyAsCargoMenu = MENU_GROUP:New(_group, "Buy as cargo", _mainMenu) + dcs_cc.banks.red = dcs_cc.banks.red + _redTickAmount + dcs_cc.banks.blue = dcs_cc.banks.blue + _blueTickAmount - for item, details in pairs(dcs_cc.objects) do - if details.group[_side] ~= nil and details.transportable then - local _title = "" - if details.crates and details.crates > 0 then - _title = item .. ": " .. dcs_cc.getCargoPrice(details) .. " (crates required: " .. details.crates .. ")" - else - _title = item .. ": " .. dcs_cc.getCargoPrice(details) - end - MENU_GROUP_COMMAND:New(_group, _title, _buyAsCargoMenu, dcs_cc.buyAsCargo, item, _coalition, _group) - end - end - end - end, {}, 10, 10) - end + MESSAGE:New("Resources gained, Blue: " .. _blueTickAmount .. ", Red: " .. _redTickAmount, 10):ToAll() end function dcs_cc.maybeCargoZone(ZoneName) @@ -430,43 +379,108 @@ function dcs_cc.maybeCargoZone(ZoneName) return nil end --- Start Capture Zones +-- Sets everything up, only call this inside DCS +function dcs_cc.start() + + if GLOBAL_MOOSE_DEBUG then + BASE:TraceOnOff(true) + BASE:TraceAll(true) + BASE:TraceLevel(2) + end -for _zone, _side in pairs(config.captureZones) do - local _triggerZone = (dcs_cc.maybeCargoZone(_zone) or ZONE:New(_zone)) - local _coalition = dcs_cc.getMooseCoalition(_side) + env.info("LOADING DCS_CC", GLOBAL_DEBUG_MODE) - local _captureZone = ZONE_CAPTURE_COALITION:New(_triggerZone, _coalition) - -- Start the zone monitoring process in 30 seconds and check every 30 seconds. - _captureZone:Start(30, 30) - _captureZone:Mark() - table.insert(dcs_cc.captureZones, _captureZone) -end + dcs_cc.coalitions = { + coalition.side.BLUE, + coalition.side.RED, + } -function dcs_cc.tickResources() - env.info("RUNNING TICKS", GLOBAL_DEBUG_MODE) - local _redTickAmount = config.baseResourceGeneration - local _blueTickAmount = config.baseResourceGeneration + dcs_cc.banks.red = config.startingResources + dcs_cc.banks.blue = config.startingResources - for _, _captureZone in pairs(dcs_cc.captureZones) do - if _captureZone:IsGuarded() then - local _tickAmount = config.zoneResourceGeneration + dcs_cc.objects = config.objects - if _captureZone:GetCoalition() == coalition.side.RED then - _redTickAmount = _redTickAmount + _tickAmount - else - _blueTickAmount = _blueTickAmount + _tickAmount + dcs_cc.spawnZone = { + ["blue"] = ZONE:New(config.spawnZone.blue), + ["red"] = ZONE:New(config.spawnZone.red), + } + + dcs_cc.cargoZones = dcs_cc.initCargoZones() + + -- setup menu + for _, _coalition in pairs(dcs_cc.coalitions) do + local _mainMenu = MENU_COALITION:New(_coalition, "DCS Command & Conquer") + local _side = dcs_cc.getCoalitionName(_coalition) + + MENU_COALITION_COMMAND:New(_coalition, "Balance", _mainMenu, dcs_cc.coalitionBalance, _coalition) + + local _buyMenu = MENU_COALITION:New(_coalition, "Buy", _mainMenu) + + for item, details in pairs(dcs_cc.objects) do + if details.group[_side] ~= nil then + env.info("Adding item: " .. item .. " to side: " .. _side, GLOBAL_DEBUG_MODE) + local _title = item .. ": " .. details.price + MENU_COALITION_COMMAND:New(_coalition, _title, _buyMenu, dcs_cc.buyItem, item, _coalition) end end + + for _, _groupName in pairs(config.transportGroups[_side]) do + dcs_cc.transportGroups[_groupName] = nil + SCHEDULER:New(nil, + function() + local _group = GROUP:FindByName(_groupName) + if _group and _group:IsAlive() then + local _unit = _group:GetPlayerUnits()[1] + _unit:HandleEvent(EVENTS.Dead) + function _unit:OnEventDead(EventData) + local _menuCommand = dcs_cc.transportGroups[_groupName] + if _menuCommand then + _menuCommand:Remove() + end + dcs_cc.transportGroups[_groupName] = nil + dcs_cc.unitZones[_groupName] = nil + -- The zone is not removed, but never checked again, can this cause a performance issue? Should it be left in and reused? + -- TODO TEST + end + if dcs_cc.unitZones[_groupName] == nil then + dcs_cc.unitZones[_groupName] = ZONE_UNIT:New(_groupName, _unit, 100, {}) + end + + MENU_GROUP_COMMAND:New(_group, "Unpack crate", nil, dcs_cc.unpackCrate, _group, _coalition) + + local _buyAsCargoMenu = MENU_GROUP:New(_group, "Buy as cargo", _mainMenu) + + for item, details in pairs(dcs_cc.objects) do + if details.group[_side] ~= nil and details.transportable then + local _title = "" + if details.crates and details.crates > 0 then + _title = item .. ": " .. dcs_cc.getCargoPrice(details) .. " (crates required: " .. details.crates .. ")" + else + _title = item .. ": " .. dcs_cc.getCargoPrice(details) + end + MENU_GROUP_COMMAND:New(_group, _title, _buyAsCargoMenu, dcs_cc.buyAsCargo, item, _coalition, _group) + end + end + end + end, {}, 10, 10) + end end - dcs_cc.banks.red = dcs_cc.banks.red + _redTickAmount - dcs_cc.banks.blue = dcs_cc.banks.blue + _blueTickAmount + -- Start Capture Zones + for _zone, _side in pairs(config.captureZones) do + local _triggerZone = (dcs_cc.maybeCargoZone(_zone) or ZONE:New(_zone)) + local _coalition = dcs_cc.getMooseCoalition(_side) - MESSAGE:New("Resources gained, Blue: " .. _blueTickAmount .. ", Red: " .. _redTickAmount, 10):ToAll() -end + local _captureZone = ZONE_CAPTURE_COALITION:New(_triggerZone, _coalition) + -- Start the zone monitoring process in 30 seconds and check every 30 seconds. + _captureZone:Start(30, 30) + _captureZone:Mark() + table.insert(dcs_cc.captureZones, _captureZone) + end --- Resource ticking -SCHEDULER:New(nil, dcs_cc.tickResources, {}, config.resourceTickSeconds, config.resourceTickSeconds):Start() -env.info("LOADING DCS_CC FINISHED", GLOBAL_DEBUG_MODE) -MESSAGE:New("Ready to Command & Conquer", 10):ToAll() \ No newline at end of file + -- Resource ticking + SCHEDULER:New(nil, dcs_cc.tickResources, {}, config.resourceTickSeconds, config.resourceTickSeconds):Start() + env.info("LOADING DCS_CC FINISHED", GLOBAL_DEBUG_MODE) + MESSAGE:New("Ready to Command & Conquer", 10):ToAll() +end +return dcs_cc \ No newline at end of file diff --git a/dev.lua b/dev.lua index 6c533c0..5731d35 100644 --- a/dev.lua +++ b/dev.lua @@ -3,13 +3,12 @@ basePath = "C:\\Users\\Richard\\DCS_MISSIONS\\DCS_CC" function loadScript() assert(loadfile(basePath .. "\\dev_config.lua"))() - assert(loadfile(basePath .. "\\dcs_cc.lua"))() + return assert(loadfile(basePath .. "\\dcs_cc.lua"))() end -- Load it once -loadScript() +local dcs_cc = loadScript() --- Setup radio option to reload it ---devMenu = MENU_MISSION:New("DCS_CC DEVELOPMENT") ---MENU_MISSION_COMMAND:New("Reload scripts", devMenu, loadScript, nil) +-- run the start function +dcs_cc.start() diff --git a/test.lua b/test.lua new file mode 100644 index 0000000..b375dbd --- /dev/null +++ b/test.lua @@ -0,0 +1,70 @@ + +local dcs_cc = require("dcs_cc") + +describe("dcs_cc", function() + -- Some global config and mocks + local _zoneIdCounter = 0 + local ZONE = {} + function ZONE:New(Name) + _zoneIdCounter = _zoneIdCounter + 1 + o = {id = _zoneIdCounter, name = Name} + setmetatable(o, self) + self.__index = self + return o + end + function ZONE:GetName() + return self.name + end + + _G.ZONE = ZONE + _G.coalition = { + side = { + BLUE = 1, + RED = 2 + } + } + + dcs_cc.spawnZone = { + blue = ZONE:New("blueSpawn"), + red = ZONE:New("redSpawn") + } + + describe("dcs_cc.initCargoZones()", function() + it("Sets up cargo zones correctly", function() + + _G.config = {} + _G.config.cargoZones = { + red = { + "redSpawn", + "zone1" + }, + blue = { + "blueSpawn", + "zone1" + } + } + + local _expected = { + blue = { + {id = 1, name = "blueSpawn"}, + {id = 3, name = "zone1"} + }, + red = { + {id = 2, name = "redSpawn"}, + {id = 3, name = "zone1"} + } + } + + local _result = dcs_cc.initCargoZones() + assert.are.same(_expected, _result) + + end) + end) + + describe("dcs_cc.getCoalitionName()", function() + it("returns the right string", function() + assert.are.equal("red", dcs_cc.getCoalitionName(coalition.side.RED)) + assert.are.equal("blue", dcs_cc.getCoalitionName(coalition.side.BLUE)) + end) + end) +end) \ No newline at end of file