Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows voting for a random map #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lua/autorun/mapvote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
99 changes: 61 additions & 38 deletions lua/mapvote/cl_mapvote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lua/mapvote/sv_mapvote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down