Skip to content

Commit dcf9c2f

Browse files
authored
Fix percentages not accounting for multipliers (#65)
* Fix percentages not accounting for multipliers * Style * Recreate voteicon if multiplier is updated during vote * Apply code suggestion * Use local * Cleanup
1 parent 9ebe5fa commit dcf9c2f

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

lua/mapvote/client/vgui/mapvote_panel.lua

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ end
5454

5555
function PANEL:RemoveInvalidVotes()
5656
for identifier, vote in pairs( self.votes ) do
57-
local invalidPlayer = (type( identifier ) == "Player") and not IsValid( identifier )
57+
local invalidPlayer = ( type( identifier ) == "Player" ) and not IsValid( identifier )
5858
if not identifier or invalidPlayer then
5959
self:removeVote( vote )
6060
self.votes[identifier] = nil
@@ -110,16 +110,19 @@ end
110110

111111
function PANEL:calculateIconPercents()
112112
local totalVotes = 0
113-
for _, mapData in ipairs( self.maps ) do
114-
totalVotes = totalVotes + #mapData.voters
113+
local mapVotes = {}
114+
for _, vote in pairs( self.votes ) do
115+
totalVotes = totalVotes + vote.voteMult
116+
mapVotes[vote.mapIndex] = ( mapVotes[vote.mapIndex] or 0 ) + vote.voteMult
115117
end
116118

117119
if totalVotes == 0 then return end
118120

119-
for _, mapData in ipairs( self.maps ) do
120-
local percent = (#mapData.voters / totalVotes) * 100
121+
for mapIndex, mapData in ipairs( self.maps ) do
122+
local votes = mapVotes[mapIndex] or 0
123+
local percentage = ( votes / totalVotes ) * 100
121124
---@diagnostic disable-next-line: undefined-field
122-
mapData.panel:SetPercent( percent )
125+
mapData.panel:SetPercent( percentage )
123126
end
124127
end
125128

@@ -138,8 +141,14 @@ function PANEL:SetVote( identifier, mapIndex, voteMult )
138141
if oldVote.mapIndex == mapIndex then
139142
return
140143
end
141-
self:removeVote( oldVote, false )
142-
panel = oldVote.panel
144+
145+
if oldVote.voteMult ~= voteMult then
146+
self:removeVote( oldVote, true )
147+
panel = self:CreateVoterPanel( identifier, voteMult )
148+
else
149+
self:removeVote( oldVote, false )
150+
panel = oldVote.panel
151+
end
143152
else
144153
panel = self:CreateVoterPanel( identifier, voteMult )
145154
end
@@ -150,6 +159,7 @@ function PANEL:SetVote( identifier, mapIndex, voteMult )
150159
identifier = identifier,
151160
mapIndex = mapIndex,
152161
panel = panel,
162+
voteMult = voteMult
153163
}
154164

155165
local newX, newY, willOverflow = self:CalculateDesiredAvatarIconPosition( mapData )
@@ -176,7 +186,7 @@ function PANEL:CalculateDesiredAvatarIconPosition( mapData, index )
176186

177187
local mapIcon = mapData.panel
178188
local maxColumnCount = math.floor( mapIcon:GetWide() / avatarTotalSize )
179-
local maxRowCount = math.floor( (mapIcon:GetTall() - 20) / avatarTotalSize )
189+
local maxRowCount = math.floor( ( mapIcon:GetTall() - 20 ) / avatarTotalSize )
180190

181191
local column = index % maxColumnCount
182192
local row = math.floor( index / maxColumnCount )
@@ -187,8 +197,8 @@ function PANEL:CalculateDesiredAvatarIconPosition( mapData, index )
187197
local rootPosX, rootPosY = self:GetPositionRelativeToSelf( mapIcon )
188198

189199
if MapVote.style.bottomUpIconFilling then
190-
rootPosX = rootPosX + (mapIcon:GetWide() - avatarTotalSize - 2 * avatarIconPadding)
191-
rootPosY = rootPosY + (mapIcon:GetTall() - avatarTotalSize - 2 * avatarIconPadding)
200+
rootPosX = rootPosX + ( mapIcon:GetWide() - avatarTotalSize - 2 * avatarIconPadding )
201+
rootPosY = rootPosY + ( mapIcon:GetTall() - avatarTotalSize - 2 * avatarIconPadding )
192202
return rootPosX - x, rootPosY - y, row >= maxRowCount
193203
end
194204
return rootPosX + x, rootPosY + y, row >= maxRowCount
@@ -373,7 +383,7 @@ function PANEL:CalculateAvatarSize( maxW, _maxH )
373383
-- add an extra row for title area
374384
local rowCount = math.ceil( math.sqrt( plyCount ) ) + 1
375385

376-
local availableSpace = maxW - (avatarIconPadding * 2) * rowCount
386+
local availableSpace = maxW - ( avatarIconPadding * 2 ) * rowCount
377387
local newAvatarSize = math.ceil( availableSpace / rowCount ) - avatarIconPadding * 2
378388
self.avatarSize = newAvatarSize
379389
end

0 commit comments

Comments
 (0)