• Print

Author Topic: Help with a custom ULX vote  (Read 5182 times)

0 Members and 1 Guest are viewing this topic.

Offline Rex744

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Help with a custom ULX vote
« on: April 05, 2017, 07:42:46 pm »
Code: [Select]
function ulx.difficulty( calling_ply, title, ... )
if ulx.voteInProgress then
ULib.tsayError( calling_ply, "There is already a vote in progress. Please wait for the current one to end.", true )
return
end

ulx.doVote( title, { ... }, voteDone )
ulx.fancyLogAdmin( calling_ply, "#A started a vote to set the server difficulty (#s)", title )
end

local vote = ulx.command( CATEGORY_NAME, "ulx difficulty", ulx.vote, "!difficulty" )
vote:addParam{ type=ULib.cmds.StringArg, "title" }
vote:addParam{ type=ULib.cmds.StringArg, "options", ULib.cmds.takeRestOfLine, repeat_min=2, repeat_max=10 }
vote:defaultAccess( ULib.ACCESS_ADMIN )
vote:help( "Starts a public vote to change the server difficulty." )

Is there a way to make this have a predifined title, predifined options and if option succeeds then, so when someone does ulx difficulty a vote starts with the predifined stuff I want?

I cannot find any tutorial on the entire internet, and to make things worse there is no gmod lua custom vote tutorial online either, when I search it up all I see are map vote addons.

Edit: I posted a job on scriptfodder for this task:
https://www.gmodstore.com/jobs/view/16689
« Last Edit: April 06, 2017, 04:16:47 pm by Rex744 »

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Help with a custom ULX vote
« Reply #1 on: April 07, 2017, 09:43:54 am »
To make a vote: define a title, some vote options and a callback function that will process the vote result.
Code: Lua
  1. local voteTitle = "Set difficulty to.."
  2. local voteOptions = {"Easy", "Intermediate", "Hard"}
  3. local voteCallback = function ( t )
  4.   -- Code in this function will run after the vote finishes
  5.   -- TODO: Write callback code
  6. end

The vote callback has a parameter t. It’s a table that contains all data related to the vote.

Here's an example of what t looks like, if 1 person votes for the second option.
Code: [Select]
args:
callback        =       function: 0xe4448f48
options:
                1       =       Easy
                2       =       Intermediate
                3       =       Hard
results:
                2       =       1
title   =       Set game difficulty to...
voters  =       1
votes   =       1

Let's update the vote callback function so it uses the data in t.results to calculate the winning vote option and display the result.
Code: Lua
  1. local voteCallback = function ( t )
  2.   -- Calculate winning option
  3.   local winningOptionName
  4.   local winningOptionVotes = 0
  5.   for key, votes in pairs( t.results ) do
  6.     if votes > winningOptionVotes then
  7.       winningOptionName = voteOptions[ key ]
  8.       winningOptionVotes = votes
  9.     end
  10.   end
  11.  
  12.   -- Display the result
  13.   local str
  14.   if not winningOptionName then
  15.     str = "Vote results: No option won because no one voted!"
  16.   else
  17.     str = "Vote results: Game difficulty set to \"" .. winningOptionName .. "\""
  18.   end
  19.   ULib.tsay( _, str ) -- Show str to players in chat and console
  20.   ulx.logString( str ) -- Log str in logfile
  21.   Msg( str .. "\n" ) -- Show str in the server console
  22. end

The title, options and callback are defined. A simple ULX command can be created to trigger the vote.
Code: Lua
  1. function ulx.myvote( calling_ply )
  2.   if ulx.voteInProgress then
  3.     ULib.tsayError( calling_ply, "There is already a vote in progress. Please wait for the current one to end.", true )
  4.     return
  5.   end
  6.  
  7.   ulx.doVote( voteTitle, voteOptions, voteCallback ) -- Trigger the vote
  8.   ulx.fancyLogAdmin( calling_ply, "#A started a vote (#s)", voteTitle )
  9. end
  10. local myvote = ulx.command( "Voting", "ulx myvote", ulx.myvote, "!myvote" )
  11. myvote:defaultAccess( ULib.ACCESS_ADMIN )
  12. myvote:help( "Starts my vote." )

You can now append the code that changes the server difficulty to the vote callback.

I have attached a working example that you can pop in your server’s addons/ directory. You can download it below.
« Last Edit: April 07, 2017, 09:53:27 am by Timmy »

  • Print