Skip to content

Commit f0fa434

Browse files
committed
Planetwars UI logic fix and improve activity prompt.
1 parent 09f0a7c commit f0fa434

3 files changed

Lines changed: 55 additions & 50 deletions

File tree

LuaMenu/widgets/gui_planetwars_list_window.lua

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ local function TryToJoinPlanet(planetData)
9595
WG.Analytics.SendOnetimeEvent("lobby:multiplayer:planetwars:join_site")
9696
end
9797

98-
local function GetattackPhaseOrDefending(lobby, attackerFactions, defenderFactions, currentMode)
98+
local function GetAttackingOrDefending(lobby, attackerFactions, defenderFactions, currentMode)
9999
local myFaction = lobby:GetMyFaction()
100100
if defenderFactions and #defenderFactions > 0 then
101101
return false, true
@@ -123,18 +123,19 @@ local function GetattackPhaseOrDefending(lobby, attackerFactions, defenderFactio
123123
--return attackPhase, defendPhase
124124
end
125125

126-
local function IsAttackUrgent()
126+
local function IsPhaseUrgent()
127127
local timeRemaining = phaseTimer and phaseTimer.GetTimeRemaining()
128128
return timeRemaining and timeRemaining < URGENT_ATTACK_TIME
129129
end
130130

131-
local function FindMyattackPhasePlanet(planets)
131+
local function FindMyAttackPhasePlanet(planets)
132132
local planetID = lobby.planetwarsData.attackPhasePlanet
133133
if not planetID then
134134
return false
135135
end
136+
local faction = lobby:GetMyFaction()
136137
for i = 1, #planets do
137-
if planets[i].PlanetID == planetID then
138+
if planets[i].PlanetID == planetID and planets[i].AttackerFaction == faction then
138139
return planets[i]
139140
end
140141
end
@@ -146,48 +147,45 @@ local function GetActivityToPrompt(lobby, attackerFactions, defenderFactions, cu
146147
return false
147148
end
148149

149-
if lobby.planetwarsData.attackPhasePlanet and planets then
150-
local myPlanet = FindMyattackPhasePlanet(planets)
150+
Spring.Echo("GetActivityToPromptGetActivityToPrompt", lobby.planetwarsData.attackingPlanet, lobby.planetwarsData.joinPlanet)
151+
if lobby.planetwarsData.attackingPlanet and planets then
152+
local myPlanet = FindMyAttackPhasePlanet(planets)
151153
if myPlanet then
152154
return myPlanet, true, true, false
153155
end
154156
return false
155157
end
156158

157-
local attackPhase, defendPhase = GetattackPhaseOrDefending(lobby, attackerFactions, defenderFactions)
158-
159+
local attackPhase, defendPhase = GetAttackingOrDefending(lobby, attackerFactions, defenderFactions)
159160
if lobby.planetwarsData.joinPlanet and planets then
160161
local planetID = lobby.planetwarsData.joinPlanet
162+
local attacker = lobby.planetwarsData.joinPlanetAttacker
161163
for i = 1, #planets do
162-
if planets[i].PlanetID == planetID then
164+
if planets[i].PlanetID == planetID and planets[i].AttackerFaction == attacker then
163165
return planets[i], attackPhase, true, true
164166
end
165167
end
166168
return false
167169
end
168170

169-
attackPhase, defendPhase = (currentMode == lobby.PW_ATTACK) and attackPhase, (currentMode == lobby.PW_DEFEND) and defendPhase
170-
171-
if attackPhase then
172-
if IsAttackUrgent() then
173-
local attackPlanet, attackMissing
174-
for i = 1, #planets do
175-
local missingPlayers = planets[i].Needed - planets[i].Count
176-
if (not attackMissing) or (missingPlayers < attackMissing) then
177-
attackPlanet = planets[i]
178-
attackMissing = missingPlayers
171+
if IsPhaseUrgent() then
172+
local targetPlanet, minMissing
173+
for i = 1, #planets do
174+
if planets[i].CanSelectForBattle then
175+
local missing = planets[i].Needed - planets[i].Count
176+
if missing > 0 and ((not minMissing) or (missing < minMissing)) then
177+
targetPlanet = planets[i]
178+
minMissing = missing
179179
end
180180
end
181-
return attackPlanet, true
182-
else
183-
for i = 1, #planets do
184-
if planets[i].Count + 1 == planets[i].Needed then
185-
return planets[i], true
186-
end
181+
end
182+
return targetPlanet, attackPhase
183+
else
184+
for i = 1, #planets do
185+
if planets[i].CanSelectForBattle and planets[i].Count + 1 == planets[i].Needed then
186+
return planets[i], attackPhase
187187
end
188188
end
189-
elseif defendPhase then
190-
return planets[1], false
191189
end
192190
return false
193191
end
@@ -516,7 +514,7 @@ local function InitializeActivityPromptHandler()
516514
PossiblyPlayWarning(isAttacker)
517515
if alreadyJoined then
518516
if isAttacker then
519-
planetStatusTextBox:SetText("attackPhase: " .. planetData.PlanetName)
517+
planetStatusTextBox:SetText("Attack: " .. planetData.PlanetName)
520518
if waitingForAllies then
521519
battleStatusText = "Attackers " .. newPlanetData.Count .. "/" .. newPlanetData.Needed .. ", "
522520
else
@@ -677,7 +675,7 @@ local function MakePlanetControl(planetData, DeselectOtherFunc, attackPhase, def
677675
name = "missionName",
678676
x = 270,
679677
y = 55,
680-
width = 350,
678+
width = 480,
681679
height = 20,
682680
valign = 'center',
683681
objectOverrideFont = Configuration:GetFont(3),
@@ -695,7 +693,7 @@ local function MakePlanetControl(planetData, DeselectOtherFunc, attackPhase, def
695693

696694
local function UpdateCaptions()
697695
if myAttacking and defendPhase then
698-
playerCaption:SetText("You are attacking this planet - Defenders: " .. currentPlayers .. "/" .. maxPlayers)
696+
playerCaption:SetText("You are attacking - Defenders: " .. currentPlayers .. "/" .. maxPlayers)
699697
elseif joinedBattle and not (myAttacking and defendPhase) then
700698
playerCaption:SetText("Joined - Waiting for players: " .. currentPlayers .. "/" .. maxPlayers)
701699
else
@@ -705,7 +703,7 @@ local function MakePlanetControl(planetData, DeselectOtherFunc, attackPhase, def
705703

706704
local function UpdateJoinButton()
707705
local showButton = false
708-
if not joinedBattle then
706+
if not joinedBattle and canSelectPlanet then
709707
playerCaption:SetPos(270)
710708
local needMap = not VFS.HasArchive(mapName)
711709
if needMap then
@@ -715,7 +713,7 @@ local function MakePlanetControl(planetData, DeselectOtherFunc, attackPhase, def
715713
else
716714
btnJoin:SetCaption(i18n("download_map"))
717715
end
718-
elseif canSelectPlanet and HasAttackCharges() then
716+
elseif HasAttackCharges() then
719717
showButton = true
720718
if attackPhase then
721719
btnJoin:SetCaption(i18n("attack_planet"))
@@ -730,7 +728,7 @@ local function MakePlanetControl(planetData, DeselectOtherFunc, attackPhase, def
730728
UpdateCaptions()
731729
playerCaption:SetPos(showButton and 270 or 104)
732730
btnJoin:SetVisibility(showButton)
733-
btnLeave:SetVisibility(joinedBattle)
731+
btnLeave:SetVisibility(joinedBattle and not (myAttacking and defendPhase))
734732
end
735733

736734
btnJoin = Button:New {
@@ -1181,9 +1179,9 @@ local function InitializeControls(window)
11811179
local listHolder = Control:New {
11821180
x = 5,
11831181
right = 5,
1184-
y = 180,
1185-
bottom = 52,
1186-
padding = {0, 0, 0, 0},
1182+
y = 170,
1183+
bottom = 46,
1184+
padding = {5, 5, 5, 5},
11871185
parent = window,
11881186
}
11891187

@@ -1238,7 +1236,7 @@ local function InitializeControls(window)
12381236
local chargesText = TextBox:New {
12391237
x = 20,
12401238
right = 16,
1241-
y = 120,
1239+
y = 116,
12421240
height = 50,
12431241
objectOverrideFont = Configuration:GetFont(2),
12441242
text = "",
@@ -1302,7 +1300,7 @@ local function InitializeControls(window)
13021300
return true
13031301
end
13041302

1305-
local function UpdateChargesText(charges, rechargeTime)
1303+
local function UpdateChargesText()
13061304
if not CheckPlanetwarsRequirements() or missingResources then
13071305
return
13081306
end
@@ -1355,7 +1353,7 @@ local function InitializeControls(window)
13551353
else
13561354
local myAttack, myFactionAttack = GetAttackingPlanet()
13571355
if myAttack then
1358-
statusText:SetText("You are attacking " .. myAttack .. " wait for defenders to muster.")
1356+
statusText:SetText("You are attacking " .. myAttack .. ", the battle will start at the end of the phase.")
13591357
elseif myFactionAttack then
13601358
statusText:SetText("Your faction is attacking but may also be under attack. Join planets that need defenders.")
13611359
else
@@ -1400,8 +1398,8 @@ local function InitializeControls(window)
14001398
end
14011399
end
14021400

1403-
local attackPhase, defendPhase = GetattackPhaseOrDefending(lobby, attackerFactions, defenderFactions, currentMode)
1404-
UpdateChargesText(charges, rechargeTime)
1401+
local attackPhase, defendPhase = GetAttackingOrDefending(lobby, attackerFactions, defenderFactions, currentMode)
1402+
UpdateChargesText()
14051403
UpdateStatusText(attackPhase, defendPhase, currentMode)
14061404

14071405
planetList.SetPlanetList(planets, attackPhase, defendPhase, modeSwitched)
@@ -1410,24 +1408,24 @@ local function InitializeControls(window)
14101408
lobby:AddListener("OnPwMatchCommand", OnPwMatchCommand)
14111409

14121410
local function OnPwattackPhasePlanet()
1413-
local attackPhase, defendPhase = GetattackPhaseOrDefending(lobby, oldAttackerFaction, oldDefenderFactions, oldMode)
1414-
UpdateChargesText(charges, rechargeTime)
1411+
local attackPhase, defendPhase = GetAttackingOrDefending(lobby, oldAttackerFaction, oldDefenderFactions, oldMode)
1412+
UpdateChargesText()
14151413
UpdateStatusText(attackPhase, defendPhase, oldMode)
14161414
end
14171415
lobby:AddListener("OnPwattackPhasePlanet", OnPwattackPhasePlanet)
14181416

14191417
local function OnUpdateUserStatus(listener, userName, status)
14201418
if lobby:GetMyUserName() == userName then
1421-
local attackPhase, defendPhase = GetattackPhaseOrDefending(lobby, oldAttackerFaction, oldDefenderFactions, oldMode)
1422-
UpdateChargesText(charges, rechargeTime)
1419+
local attackPhase, defendPhase = GetAttackingOrDefending(lobby, oldAttackerFaction, oldDefenderFactions, oldMode)
1420+
UpdateChargesText()
14231421
UpdateStatusText(attackPhase, defendPhase, oldMode)
14241422
end
14251423
end
14261424
lobby:AddListener("OnUpdateUserStatus", OnUpdateUserStatus)
14271425

14281426
local function OnPwAttackCharges(listener, newCharges, newRechargeTime)
14291427
charges, rechargeTime = newCharges, newRechargeTime
1430-
UpdateChargesText(charges, rechargeTime)
1428+
UpdateChargesText()
14311429
UpdateStatusText(attackPhase, defendPhase, oldMode)
14321430
planetList.UpdateJoinCheck()
14331431
end
@@ -1457,11 +1455,11 @@ local function InitializeControls(window)
14571455
externalFunctions.CheckPlanetwarsRequirements = CheckPlanetwarsRequirements
14581456

14591457
function externalFunctions.UpdateTimer()
1458+
UpdateChargesText()
14601459
local timeRemaining = phaseTimer.GetTimeRemaining()
14611460
if timeRemaining then
14621461
lblTitle:SetCaption(title .. Spring.Utilities.FormatTime(timeRemaining, true))
14631462
end
1464-
UpdateChargesText()
14651463
end
14661464

14671465
function externalFunctions.SetPlanetJoined(planetID)
@@ -1657,7 +1655,7 @@ function widget:Update()
16571655
activityPromptHandler.UpdateTimer()
16581656
end
16591657
if DoUnMatchedActivityUpdate then
1660-
local newUrgent = IsAttackUrgent()
1658+
local newUrgent = IsPhaseUrgent()
16611659
if newUrgent ~= attackUrgent then
16621660
attackUrgent = newUrgent
16631661
DoUnMatchedActivityUpdate()

libs/liblobby/lobby/interface_zerok.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,10 @@ function Interface:PwJoinPlanet(planetID, attacker)
10391039
end
10401040

10411041
function Interface:PwCancel() -- Leave attack or defend queue
1042+
self.planetwarsData.attackingPlanet = nil
1043+
self.planetwarsData.attackingPlanetFaction = nil
1044+
self.planetwarsData.joinPlanet = nil
1045+
self.planetwarsData.joinPlanetAttacker = nil
10421046
self:_SendCommand("PwCancel {}")
10431047
return self
10441048
end

libs/liblobby/lobby/lobby.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,9 @@ function Lobby:_OnPwMatchCommand(attackerFactions, defenderFactions, currentMode
13041304
self.planetwarsData.deadlineSeconds = deadlineSeconds
13051305
if modeSwitched then
13061306
self.planetwarsData.joinPlanet = nil
1307+
self.planetwarsData.joinPlanetAttacker = nil
13071308
self.planetwarsData.attackingPlanet = nil
1309+
self.planetwarsData.attackingPlanetFaction = nil
13081310
end
13091311

13101312
self:_CallListeners("OnPwMatchCommand", attackerFactions, defenderFactions, currentMode, planets, deadlineSeconds, modeSwitched)
@@ -1320,9 +1322,10 @@ function Lobby:_OnPwRequestJoinPlanet(planetID, attacker)
13201322
self:_CallListeners("OnPwRequestJoinPlanet", planetID, attacker)
13211323
end
13221324

1323-
function Lobby:_OnPwJoinPlanetSuccess(planetID)
1325+
function Lobby:_OnPwJoinPlanetSuccess(planetID, attackerFaction)
13241326
self.planetwarsData.joinPlanet = planetID
1325-
self:_CallListeners("OnPwJoinPlanetSuccess", planetID)
1327+
self.planetwarsData.joinPlanetAttacker = attackerFaction
1328+
self:_CallListeners("OnPwJoinPlanetSuccess", planetID, attackerFaction)
13261329
end
13271330

13281331
function Lobby:_OnPwAttackingPlanet(planetID, attacker)

0 commit comments

Comments
 (0)