Advertisement
Guest User

VoteSystem VCMP

a guest
Jul 30th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. /*
  2. VoteSystem
  3. Developed by: Lucas
  4. Credits: Matheus/Takanaue (I did nothing but I'm here)
  5. */
  6.  
  7. VoteStarted <- false;
  8. VoteText <- null;
  9. VoteY <- 0;
  10. VoteN <- 0;
  11. VoteTime <- 0;
  12. VoteList <- {};
  13.  
  14. function onScriptLoad()
  15. {
  16. N <- BindKey( true, 0x4E, 0, 0 );
  17. Y <- BindKey( true, 0x59, 0, 0 );
  18. }
  19.  
  20. function onPlayerJoin( player )
  21. {
  22. VoteList.rawset( player.ID, "none" );
  23. }
  24.  
  25. function onPlayerPart( player, reason )
  26. {
  27. VoteList.rawdelete( player.ID );
  28. }
  29.  
  30. function onKeyDown( player, key )
  31. {
  32. if ( key == N )
  33. {
  34. if ( VoteStarted )
  35. {
  36. switch ( VoteList[ player.ID ] )
  37. {
  38. case "yes":
  39. VoteY > 0 ? VoteY -- : false;
  40. VoteN ++;
  41. VoteList.rawset( player.ID, "no" );
  42. PrivMessage( player, "Okay, you've changed your vote." );
  43. break;
  44.  
  45. case "none":
  46. VoteN ++;
  47. VoteList.rawset( player.ID, "no" );
  48. PrivMessage( player, "Feedback computed." );
  49. break;
  50. default: break;
  51. }
  52. }
  53. }
  54.  
  55. else if ( key == Y )
  56. {
  57. if ( VoteStarted )
  58. {
  59. switch ( VoteList[ player.ID ] )
  60. {
  61. case "no":
  62. VoteY ++;
  63. VoteN > 0 ? VoteN -- : false;
  64. VoteList.rawset( player.ID, "yes" );
  65. PrivMessage( player, "Okay, you've changed your vote." );
  66. break;
  67.  
  68. case "none":
  69. VoteY ++;
  70. VoteList.rawset( player.ID, "yes" );
  71. PrivMessage( player, "Feedback computed." );
  72. break;
  73. default: break;
  74. }
  75. }
  76. }
  77. }
  78.  
  79. function Result()
  80. {
  81. local y_calc, n_calc;
  82. AnnounceAll( "The time is over!" ,1 );
  83. Message( "[#DCDCDC]Result of the votes:" );
  84.  
  85. if ( VoteY == VoteN && VoteY > 0 ) y_calc = "50", n_calc = "50";
  86. else if ( VoteY > VoteN ) y_calc = ( !VoteY ? "0" : ( 100 - ( ( VoteN.tofloat() / VoteY ) * 100 ) ) ), n_calc = ( !VoteN ? "0" : ( ( VoteN.tofloat() / VoteY ) * 100 ) );
  87. else y_calc = ( !VoteY ? "0" : ( ( VoteY.tofloat() / VoteN ) * 100 ) ), n_calc = ( !VoteN ? "0" : ( 100 - ( VoteY.tofloat() / VoteN ) * 100 ) );
  88.  
  89. Message( "[#DCDCDC]- Number of votes yes: [#FF0000]" + VoteY + "[#DCDCDC](" + y_calc + "%)" );
  90. Message( "[#DCDCDC]- Number of votes no: [#FF0000]" + VoteN + "[#DCDCDC](" + n_calc + "%)" );
  91.  
  92. // Resetting the results
  93. VoteStarted = false;
  94. VoteText = null;
  95. VoteY = 0;
  96. VoteN = 0;
  97. foreach ( idx, val in VoteList ) VoteList.rawset( idx, "none" );
  98. }
  99.  
  100. function VoteClock()
  101. {
  102. AnnounceAll( format( "Ends in " + VoteTime + " second%s...", VoteTime == 1 ? "" : "s" ), 1 );
  103. VoteTime --;
  104. }
  105.  
  106. function onPlayerCommand( player, cmd, text )
  107. {
  108. if ( cmd == "vote" )
  109. {
  110. if ( PlayerInfo( player, cmd ) ) return 0;
  111. else if ( !text ) MessagePlayer( "/" + cmd + " <text/off>.", player );
  112. else
  113. {
  114. if ( text == "off" )
  115. {
  116. if ( !VoteStarted ) MessagePlayer( "No vote in progress." player );
  117. else
  118. {
  119. VoteStarted = false;
  120. VoteText = null;
  121. VoteY != 0 ? VoteY = 0 : false;
  122. VoteN != 0 ? VoteN = 0 : false;
  123. VoteTime = 0;
  124. _Timer.Destroy( _Result );
  125. _Timer.Destroy( _VoteClock );
  126. Message( "[#9B30FF]* Admin. [#B5B5B5]" + player.Name + " [#9B30FF]closed the poll." );
  127. }
  128. }
  129. else
  130. {
  131. if ( VoteStarted ) MessagePlayer( "There is a vote in progress.", player );
  132. else
  133. {
  134. VoteStarted = true;
  135. VoteText = text;
  136. VoteTime = 29;
  137. Message( "[#9B30FF]* Admin. [#B5B5B5]" + player.Name + " [#9B30FF]started a poll." );
  138. Message( "[#DCDCDC]~ " + text + " ~\nPress [#00FF00]\"Y\" [#DCDCDC]to vote yes or [#FF0000]\"N\" [#DCDCDC]to vote no." );
  139. AnnounceAll( "Ends in " + VoteTime + " seconds...", 1 );
  140. _Result <- _Timer.Create( this, Result, 30000, 1 );
  141. _VoteClock <- _Timer.Create( this, VoteClock, 1000, VoteTime );
  142. }
  143. }
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement