diff --git a/lua/autorun/mapvote.lua b/lua/autorun/mapvote.lua index 7e1baf4..aef6c78 100644 --- a/lua/autorun/mapvote.lua +++ b/lua/autorun/mapvote.lua @@ -43,6 +43,8 @@ MapVote.Allow = false MapVote.UPDATE_VOTE = 1 MapVote.UPDATE_WIN = 3 +MapVote.RandomMapID = 4294967295 + if SERVER then AddCSLuaFile() AddCSLuaFile("mapvote/cl_mapvote.lua") diff --git a/lua/mapvote/cl_mapvote.lua b/lua/mapvote/cl_mapvote.lua index 357749f..769f341 100644 --- a/lua/mapvote/cl_mapvote.lua +++ b/lua/mapvote/cl_mapvote.lua @@ -247,47 +247,70 @@ function PANEL:Think() self.countDown:CenterHorizontal() end +function PANEL:GetButtonHeight() + return 24 +end + +function PANEL:GetButtonWidth() + return 285 + (math.Clamp(300, 0, ScrW() - 640) / 2) +end + +function PANEL:CreateButton(ID, text) + local button = vgui.Create("DButton", self.mapList) + button.ID = ID + button:SetText(text) + + button.DoClick = function() + net.Start("RAM_MapVoteUpdate") + net.WriteUInt(MapVote.UPDATE_VOTE, 3) + net.WriteUInt(button.ID, 32) + net.SendToServer() + end + + do + local Paint = button.Paint + button.Paint = function(s, w, h) + local col = Color(255, 255, 255, 10) + + if(button.bgColor) then + col = button.bgColor + end + + draw.RoundedBox(4, 0, 0, w, h, col) + Paint(s, w, h) + end + end + + button:SetTextColor(color_white) + button:SetContentAlignment(4) + button:SetTextInset(8, 0) + button:SetFont("RAM_VoteFont") + + button:SetDrawBackground(false) + button:SetTall(self:GetButtonHeight()) + button:SetWide(self:GetButtonWidth()) + button.NumVotes = 0 + + return button +end + +function PANEL:CreateSpacer(width) + local spacer = vgui.Create("Panel", self.mapList) + spacer:SetTall(self:GetButtonHeight()) + spacer:SetWide(width) + return spacer +end + function PANEL:SetMaps(maps) self.mapList:Clear() - + + local buttonWidth = self:GetButtonWidth() + self.mapList:AddItem(self:CreateSpacer(buttonWidth / 2)) + self.mapList:AddItem(self:CreateButton(MapVote.RandomMapID, "Random Map")) + self.mapList:AddItem(self:CreateSpacer(buttonWidth / 2)) + for k, v in RandomPairs(maps) do - local button = vgui.Create("DButton", self.mapList) - button.ID = k - button:SetText(v) - - button.DoClick = function() - net.Start("RAM_MapVoteUpdate") - net.WriteUInt(MapVote.UPDATE_VOTE, 3) - net.WriteUInt(button.ID, 32) - net.SendToServer() - end - - do - local Paint = button.Paint - button.Paint = function(s, w, h) - local col = Color(255, 255, 255, 10) - - if(button.bgColor) then - col = button.bgColor - end - - draw.RoundedBox(4, 0, 0, w, h, col) - Paint(s, w, h) - end - end - - button:SetTextColor(color_white) - button:SetContentAlignment(4) - button:SetTextInset(8, 0) - button:SetFont("RAM_VoteFont") - - local extra = math.Clamp(300, 0, ScrW() - 640) - - button:SetDrawBackground(false) - button:SetTall(24) - button:SetWide(285 + (extra / 2)) - button.NumVotes = 0 - + local button = self:CreateButton(k, v) self.mapList:AddItem(button) end end diff --git a/lua/mapvote/sv_mapvote.lua b/lua/mapvote/sv_mapvote.lua index f5e17ce..33028e3 100644 --- a/lua/mapvote/sv_mapvote.lua +++ b/lua/mapvote/sv_mapvote.lua @@ -13,7 +13,8 @@ net.Receive("RAM_MapVoteUpdate", function(len, ply) if(update_type == MapVote.UPDATE_VOTE) then local map_id = net.ReadUInt(32) - if(MapVote.CurrentMaps[map_id]) then + + if(MapVote.CurrentMaps[map_id] or map_id == MapVote.RandomMapID) then MapVote.Votes[ply:SteamID()] = map_id net.Start("RAM_MapVoteUpdate") @@ -155,9 +156,12 @@ function MapVote.Start(length, current, limit, prefix, callback) net.WriteUInt(winner, 32) net.Broadcast() + + if(winner == MapVote.RandomMapID) then + winner = math.random(#MapVote.CurrentMaps) + end local map = MapVote.CurrentMaps[winner] - local gamemode = nil if (autoGamemode) then