From 7dfe5795b25928b46a55302347f6d002a08eb47d Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Fri, 23 Oct 2020 23:02:58 +0100 Subject: [PATCH 1/2] Allows voting for a random map --- lua/autorun/mapvote.lua | 2 + lua/mapvote/cl_mapvote.lua | 84 +++++++++++++++++++++----------------- lua/mapvote/sv_mapvote.lua | 8 +++- 3 files changed, 55 insertions(+), 39 deletions(-) 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..ad594c2 100644 --- a/lua/mapvote/cl_mapvote.lua +++ b/lua/mapvote/cl_mapvote.lua @@ -247,47 +247,57 @@ function PANEL:Think() self.countDown:CenterHorizontal() end +function PANEL:CreateButton(ID, text, fullwidth) + 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") + + local width = 285 + (math.Clamp(300, 0, ScrW() - 640) / 2) + + if(fullwidth == true) then width = width * 2 + 5 end + + button:SetDrawBackground(false) + button:SetTall(24) + button:SetWide(width) + button.NumVotes = 0 + + return button +end + function PANEL:SetMaps(maps) self.mapList:Clear() + local randomButton = self:CreateButton(MapVote.RandomMapID, "Random Map", true) + self.mapList:AddItem(randomButton) + 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 From c537586231f139cfc4751b26fe5e0a15836c0ddb Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Sat, 24 Oct 2020 19:19:16 +0100 Subject: [PATCH 2/2] Centers random map button --- lua/mapvote/cl_mapvote.lua | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/lua/mapvote/cl_mapvote.lua b/lua/mapvote/cl_mapvote.lua index ad594c2..769f341 100644 --- a/lua/mapvote/cl_mapvote.lua +++ b/lua/mapvote/cl_mapvote.lua @@ -247,7 +247,15 @@ function PANEL:Think() self.countDown:CenterHorizontal() end -function PANEL:CreateButton(ID, text, fullwidth) +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) @@ -278,23 +286,28 @@ function PANEL:CreateButton(ID, text, fullwidth) button:SetTextInset(8, 0) button:SetFont("RAM_VoteFont") - local width = 285 + (math.Clamp(300, 0, ScrW() - 640) / 2) - - if(fullwidth == true) then width = width * 2 + 5 end - button:SetDrawBackground(false) - button:SetTall(24) - button:SetWide(width) + 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 randomButton = self:CreateButton(MapVote.RandomMapID, "Random Map", true) - self.mapList:AddItem(randomButton) + + 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 = self:CreateButton(k, v)