Skip to content

Commit

Permalink
Added Voting Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tltneon committed Aug 17, 2015
1 parent c41e860 commit 8863c43
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 0 deletions.
27 changes: 27 additions & 0 deletions plugins/voting/entities/entities/nut_votingcomputer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
AddCSLuaFile()

local PLUGIN = PLUGIN

ENT.Type = "anim"
ENT.PrintName = "Voting Computer"
ENT.Author = "_FR_Starfox64"
ENT.Spawnable = true
ENT.AdminOnly = true
ENT.Category = "NutScript"

if (SERVER) then
function ENT:Initialize()
self:SetModel("models/props_downtown/atm.mdl")
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetUseType( SIMPLE_USE )
end

function ENT:AcceptInput(name, activator, ply, data)
if name == "Use" and IsValid(ply) and ply:IsPlayer() then
if PLUGIN:CanVote(ply) then
netstream.Start(ply, "nut_OpenVotingComputer", PLUGIN.votingList)
end
end
end
end
180 changes: 180 additions & 0 deletions plugins/voting/sh_plugin.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
local PLUGIN = PLUGIN
PLUGIN.name = "Voting"
PLUGIN.author = "_FR_Starfox64 (NS 1.0), Neon (NS 1.1)"
PLUGIN.desc = "Voting plugin for governor elections."

PLUGIN.votingList = PLUGIN.votingList or {}

nut.util.include("sv_plugin.lua")

nut.command.add("getvotes", {
superAdminOnly = true,
onRun = function(ply, arguments)
local toPrint = "EvoCity Voting Results:"
local total = 0
local results = {}

for _, vote in pairs(PLUGIN.votes) do
results[vote] = results[vote] or 0
total = total + 1
results[vote] = results[vote] + 1
end

for voteID, votes in pairs(results) do
local name = PLUGIN.votingList[voteID] or "BLANK"
toPrint = toPrint.."\n"..name.." -> "..votes.." ["..(votes / total * 100).."%]"
end

ply:notify("Printing results to your console...")
ply:PrintMessage(HUD_PRINTCONSOLE, toPrint)
end
})

nut.command.add("addvote", {
superAdminOnly = true,
syntax = "<string option>",
onRun = function(ply, arguments)
if not arguments[1] then
ply:notifyLocalized("missing_arg", 1)
return
end

table.insert(PLUGIN.votingList, arguments[1])
PLUGIN:SaveData()

ply:notify(arguments[1].." was added to the voting list.")
end
})

nut.command.add("resetvoting", {
superAdminOnly = true,
onRun = function(ply, arguments)
PLUGIN.votes = {}
PLUGIN.votingList = {}
PLUGIN:SaveData()

ply:notify("Voting list and votes are now empty.")
end
})

nut.command.add("setupvote", {
superAdminOnly = true,
syntax = "[int unixStart] [int unixEnd]",
onRun = function(ply, arguments)
local votingStart = tonumber(arguments[1])
local votingEnd = tonumber(arguments[2])

if arguments[1] and not arguments[2] then
ply:notify("Both timestamps are required if you wish to manually set the voting period.")
return
elseif votingStart and votingEnd then
PLUGIN.startTime = votingStart
PLUGIN.endTime = votingEnd
PLUGIN:SaveData()

ply:notify("Next voting session will start at UNIX-"..votingStart.." and will end at UNIX-"..votingEnd..".")
return
end

local timeData = os.date("*t", os.time())
local hours = 23 - timeData.hour
local minutes = 59 - timeData.min
local seconds = 60 - timeData.sec
local offset = hours * 60 * 60 + minutes * 60 + seconds -- Seconds left until midnight

votingStart = os.time() + offset
votingEnd = os.time() + offset + 24 * 60 * 60

PLUGIN.startTime = votingStart
PLUGIN.endTime = votingEnd
PLUGIN:SaveData()

ply:notify("Next voting session will start at UNIX-"..votingStart.." and will end at UNIX-"..votingEnd..".")
end
})

if CLIENT then
netstream.Hook("nut_OpenVotingComputer", function( votingList )
PLUGIN.votingList = votingList
vgui.Create("nut_Voting")
end)

-- Voting Menu --
local PANEL = {}
function PANEL:Init()
self:SetSize(400, 130)
self:Center()
self:MakePopup()
self:SetTitle("EvoCity Voting Computer")

self.button = vgui.Create("DButton", self)
self.button:SetText("Select Candidate")
self.button:Dock(BOTTOM)
self.button:DockPadding(4, 2, 4, 2)
self.button:DockMargin(4,2,4,2)
self.button.DoClick = function()
local dMenu = DermaMenu()

local blank = dMenu:AddOption("Blank", function()
Derma_Query("Are you sure you wish to vote blank?",
"Voting Confirmation",
"Yes",
function()
netstream.Start("nut_Vote", 0)
self:Close()
end,
"No"
)
end)

blank:SetIcon("icon16/page_white.png")

dMenu:AddSpacer()

for voteID, name in pairs(PLUGIN.votingList) do
local vote = dMenu:AddOption(name, function()
Derma_Query("Are you sure you wish to vote for "..name.."?",
"Voting Confirmation",
"Yes",
function()
netstream.Start("nut_Vote", voteID)
self:Close()
end,
"No"
)
end)

vote:SetIcon("icon16/user_suit.png")
end

dMenu:Open()
end

self.panel = vgui.Create("DPanel", self)
self.panel:Dock(FILL)
self.panel:DockPadding(4, 2, 4, 2)
self.panel:DockMargin(4,2,4,2)

self.panel.label = vgui.Create("DLabel", self.panel)
self.panel.label:SetText("Welcome to the EvoCity Voting Center. You are about to vote for your next Governor. Please select carefully who you want to vote for as you may only vote for one person, one time.")
self.panel.label:SetTextColor(Color(210, 210, 210))
self.panel.label:SetWrap(self.panel:GetWide() - 8)
self.panel.label:Dock(FILL)
end

vgui.Register("nut_Voting", PANEL, "DFrame")

function PLUGIN:DrawTargetID(entity, x, y, alpha)
if (entity:GetClass() == "nut_votingcomputer") then
local mainColor = nut.config.mainColor
local color = Color(mainColor.r, mainColor.g, mainColor.b, alpha)
nut.util.drawText(x, y, "Voting Computer", color)
end
end

function PLUGIN:ShouldDrawTargetEntity( entity )
if entity:GetClass() == "nut_votingcomputer" then
return true
end
end
end
68 changes: 68 additions & 0 deletions plugins/voting/sv_plugin.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
local PLUGIN = PLUGIN
PLUGIN.startTime = PLUGIN.startTime or 0
PLUGIN.endTime = PLUGIN.endTime or 0
PLUGIN.votes = PLUGIN.votes or {}

function PLUGIN:LoadData()
local votingData = self:getData() or {}

self.startTime = votingData.startTime or 0
self.endTime = votingData.endTime or 0
self.votingList = votingData.votingList or {}
self.votes = votingData.votes or {}
end

function PLUGIN:SaveData()
local votingData = {
startTime = self.startTime,
endTime = self.endTime,
votingList = self.votingList,
votes = self.votes
}
self:setData(votingData)
end

function PLUGIN:CanVote( ply, silent )
if self.votes[ply:SteamID64()] then
if silent then return false end

ply:notify("You can only vote once!")
return false
end

if os.time() < self.startTime then
if silent then return false end

ply:notify("You cannot vote yet!")
return false
end

if os.time() > self.endTime then
if silent then return false end

ply:notify("You can no longer vote!")
return false
end

return true
end

function PLUGIN:PlayerLoadedChar( ply )
if self:CanVote(ply, true) then
ply:notify("It is time to vote for the Governor, head on to the City Hall to vote!")
end
end

netstream.Hook("nut_Vote", function( ply, vote )
if not PLUGIN:CanVote(ply) then return end

if PLUGIN.votingList[vote] or vote == 0 then
PLUGIN.votes[ply:SteamID64()] = vote
PLUGIN:SaveData()

local name = PLUGIN.votingList[vote] or "BLANK"
ply:notify("Your vote for "..name.." was successfully recorded.")
else
ply:notify("Invalid vote, please try again.")
end
end)

0 comments on commit 8863c43

Please sign in to comment.