Skip to content

Commit f2de7b0

Browse files
committed
Simplify attack and defend popup logic.
1 parent ac85088 commit f2de7b0

3 files changed

Lines changed: 54 additions & 36 deletions

File tree

LuaMenu/widgets/gui_planetwars_list_window.lua

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,12 @@ local function IsPhaseUrgent()
137137
return timeRemaining and timeRemaining < URGENT_ATTACK_TIME
138138
end
139139

140-
local function FindMyAttackPhasePlanet(lobby, planets)
141-
local planetID = lobby.planetwarsData.attackPhasePlanet
140+
local function FindMatchingPlanet(planetID, planetAttacker, planets)
142141
if not planetID then
143142
return false
144143
end
145-
local faction = lobby:GetMyFaction()
146144
for i = 1, #planets do
147-
if planets[i].PlanetID == planetID and planets[i].AttackerFaction == faction then
145+
if planets[i].PlanetID == planetID and planets[i].AttackerFaction == planetAttacker then
148146
return planets[i]
149147
end
150148
end
@@ -155,24 +153,23 @@ local function GetActivityToPrompt(lobby, attackerFactions, defenderFactions, cu
155153
if not (planets and planets[1]) then
156154
return false
157155
end
158-
159-
if lobby.planetwarsData.attackingPlanet and planets then
160-
local myPlanet = FindMyAttackPhasePlanet(lobby, planets)
156+
local activePlanet = lobby.planetwarsData.attackingPlanet or lobby.planetwarsData.defendingPlanet
157+
local activePlanetAttacker = lobby.planetwarsData.attackingPlanetAttacker or lobby.planetwarsData.defendingPlanetAttacker
158+
Spring.Echo("MODEMODEMODE", activePlanet, activePlanetAttacker, currentMode, lobby.PW_ATTACK)
159+
if currentMode == lobby.PW_ATTACK and activePlanet then
160+
local myPlanet = FindMatchingPlanet(activePlanet, activePlanetAttacker, planets)
161+
Spring.Echo("myPlanetmyPlanetmyPlanet", myPlanet)
161162
if myPlanet then
162163
return myPlanet, true, true, false
163164
end
164165
return false
165166
end
166-
local attackPhase, defendPhase = GetAttackingOrDefending(lobby, attackerFactions, defenderFactions)
167-
if lobby.planetwarsData.joinPlanet and planets then
168-
local planetID = lobby.planetwarsData.joinPlanet
169-
local attacker = lobby.planetwarsData.joinPlanetAttacker
170-
for i = 1, #planets do
171-
if planets[i].PlanetID == planetID and planets[i].AttackerFaction == attacker then
172-
return planets[i], attackPhase, true, true
173-
end
167+
local attackPhase = (currentMode == lobby.PW_ATTACK)
168+
if currentMode == lobby.PW_DEFEND and activePlanet then
169+
local myPlanet = FindMatchingPlanet(activePlanet, activePlanetAttacker, planets)
170+
if myPlanet then
171+
return myPlanet, attackPhase, true, true
174172
end
175-
return false
176173
end
177174

178175
if IsPhaseUrgent() then
@@ -1356,15 +1353,26 @@ local function InitializeControls(window)
13561353
local myFaction = lobby:GetMyFaction()
13571354
planetStatusData.myAttackers = 0
13581355
planetStatusData.myIncoming = 0
1359-
planetStatusData.neutralDefense = 0
1356+
planetStatusData.neutralIncoming = 0
1357+
planetStatusData.myDefend = 0
1358+
planetStatusData.myDefendMax = 0
1359+
planetStatusData.neutralDefend = 0
1360+
planetStatusData.neutralDefendMax = 0
13601361
for i = 1, #planets do
13611362
local planet = planets[i]
13621363
if planet.AttackerFaction == myFaction then
13631364
planetStatusData.myAttackers = planetStatusData.myAttackers + planet.Count
13641365
elseif planet.OwnerFaction == myFaction then
13651366
planetStatusData.myIncoming = planetStatusData.myIncoming + planet.Count
13661367
elseif not planet.OwnerFaction then
1367-
planetStatusData.neutralDefense = planetStatusData.neutralDefense + planet.Count
1368+
planetStatusData.neutralIncoming = planetStatusData.neutralIncoming + planet.Count
1369+
end
1370+
if planet.DefenderFaction == myFaction then
1371+
planetStatusData.myDefend = planetStatusData.myDefend + planet.Count
1372+
planetStatusData.myDefendMax = planetStatusData.myDefendMax + planet.Needed
1373+
elseif not planet.OwnerFaction then
1374+
planetStatusData.neutralDefend = planetStatusData.neutralDefend + planet.Count
1375+
planetStatusData.neutralDefendMax = planetStatusData.neutralDefendMax + planet.Needed
13681376
end
13691377
end
13701378
end
@@ -1378,13 +1386,13 @@ local function InitializeControls(window)
13781386
if charges == 0 then
13791387
statusText:SetText("You are out of attack charges. Regain charges by defending or waiting for the recharge timer.")
13801388
else
1381-
statusText:SetText("Select a planet to attack, it will launch when the timer runs out if enough allies join you.")
1389+
statusText:SetText("Select a planet to attack, it will launch when the timer runs out if enough allies join.")
13821390
end
13831391
if planets then
13841392
UpdatePlanetStatusData(attackPhase, planets)
13851393
planetStatusText.attackers:SetText("Fellow attackers: " .. planetStatusData.myAttackers)
13861394
planetStatusText.incoming:SetText("To defend: " .. planetStatusData.myIncoming)
1387-
planetStatusText.neutral:SetText("Neutrals to defend: " .. planetStatusData.neutralDefense)
1395+
planetStatusText.neutral:SetText("Neutrals to defend: " .. planetStatusData.neutralIncoming)
13881396
for i = 1, #planetStatusNames do
13891397
planetStatusText[planetStatusNames[i]]:SetVisibility(true)
13901398
end
@@ -1397,8 +1405,13 @@ local function InitializeControls(window)
13971405
statusText:SetText("Join planets that need defenders. Participating in defense generates an attack charge charge.")
13981406
end
13991407

1400-
for i = 1, #planetStatusNames do
1401-
planetStatusText[planetStatusNames[i]]:SetVisibility(false)
1408+
if planets then
1409+
UpdatePlanetStatusData(attackPhase, planets)
1410+
planetStatusText.attackers:SetText("Defenders: " .. planetStatusData.myDefend .. " / " .. planetStatusData.myDefendMax)
1411+
planetStatusText.incoming:SetText("Neutral Defenders: " .. planetStatusData.neutralDefend .. " / " .. planetStatusData.neutralDefendMax)
1412+
planetStatusText.attackers:SetVisibility(true)
1413+
planetStatusText.incoming:SetVisibility(true)
1414+
planetStatusText.neutral:SetVisibility(false)
14021415
end
14031416
else
14041417
statusText:SetText("Error fetching Planetwars state. Try logging out then in again.")

libs/liblobby/lobby/interface_zerok.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,9 +1040,9 @@ end
10401040

10411041
function Interface:PwCancel() -- Leave attack or defend queue
10421042
self.planetwarsData.attackingPlanet = nil
1043-
self.planetwarsData.attackingPlanetFaction = nil
1044-
self.planetwarsData.joinPlanet = nil
1045-
self.planetwarsData.joinPlanetAttacker = nil
1043+
self.planetwarsData.attackingPlanetAttacker = nil
1044+
self.planetwarsData.defendingPlanet = nil
1045+
self.planetwarsData.defendingPlanetAttacker = nil
10461046
self:_SendCommand("PwCancel {}")
10471047
return self
10481048
end

libs/liblobby/lobby/lobby.lua

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,18 +1295,27 @@ function Lobby:_OnPwStatus(planetWarsMode, minLevel, attackerMinutes, defenderMi
12951295
end
12961296

12971297
function Lobby:_OnPwMatchCommand(attackerFactions, defenderFactions, currentMode, planets, deadlineSeconds)
1298+
local modeSwitched = (self.planetwarsData.currentMode ~= currentMode) or ((deadlineSeconds or 0) > (self.planetwarsData.deadlineSeconds or 0) + 10)
12981299
self.planetwarsData.attackerFactions = attackerFactions
12991300
self.planetwarsData.defenderFactions = defenderFactions
13001301
self.planetwarsData.currentMode = currentMode
13011302
self.planetwarsData.planets = planets
1302-
1303-
local modeSwitched = (deadlineSeconds or 0) > (self.planetwarsData.deadlineSeconds or 0) + 10
13041303
self.planetwarsData.deadlineSeconds = deadlineSeconds
1305-
if modeSwitched then
1306-
self.planetwarsData.joinPlanet = nil
1307-
self.planetwarsData.joinPlanetAttacker = nil
1308-
self.planetwarsData.attackingPlanet = nil
1309-
self.planetwarsData.attackingPlanetFaction = nil
1304+
1305+
local attackPhase = self.planetwarsData.attackerFactions and #self.planetwarsData.attackerFactions > 0
1306+
self.planetwarsData.attackingPlanet = nil
1307+
self.planetwarsData.attackingPlanetAttacker = nil
1308+
self.planetwarsData.defendingPlanet = nil
1309+
self.planetwarsData.defendingPlanetAttacker = nil
1310+
for i = 1, #planets do
1311+
local planet = planets[i]
1312+
if planet.PlayerIsAttacker then
1313+
self.planetwarsData.attackingPlanet = planet.PlanetID
1314+
self.planetwarsData.attackingPlanetAttacker = planet.AttackerFaction
1315+
elseif planet.PlayerIsDefender then
1316+
self.planetwarsData.defendingPlanet = planet.PlanetID
1317+
self.planetwarsData.defendingPlanetAttacker = planet.AttackerFaction
1318+
end
13101319
end
13111320

13121321
self:_CallListeners("OnPwMatchCommand", attackerFactions, defenderFactions, currentMode, planets, deadlineSeconds, modeSwitched)
@@ -1323,14 +1332,10 @@ function Lobby:_OnPwRequestJoinPlanet(planetID, attacker)
13231332
end
13241333

13251334
function Lobby:_OnPwJoinPlanetSuccess(planetID, attackerFaction)
1326-
self.planetwarsData.joinPlanet = planetID
1327-
self.planetwarsData.joinPlanetAttacker = attackerFaction
13281335
self:_CallListeners("OnPwJoinPlanetSuccess", planetID, attackerFaction)
13291336
end
13301337

13311338
function Lobby:_OnPwAttackingPlanet(planetID, attacker)
1332-
self.planetwarsData.attackingPlanet = planetID
1333-
self.planetwarsData.attackingPlanetFaction = attacker
13341339
self:_CallListeners("OnPwAttackingPlanet", planetID, attacker)
13351340
end
13361341

0 commit comments

Comments
 (0)