From 7396eeb40a9b367cc9a0c5e16d5ffddb1da6ab90 Mon Sep 17 00:00:00 2001 From: Remie Richards Date: Thu, 30 Jun 2016 02:37:29 +0100 Subject: [PATCH] This is bad, this doesn't work, Stop looking at it --- code/__DEFINES/misc.dm | 1 + code/modules/admin/create_poll.dm | 5 ++ code/modules/mob/new_player/new_player.dm | 17 +++++ code/modules/mob/new_player/poll.dm | 86 ++++++++++++++++++++++- 4 files changed, 107 insertions(+), 2 deletions(-) diff --git a/code/__DEFINES/misc.dm b/code/__DEFINES/misc.dm index 7169390f124f5b..294cdaa739cbce 100644 --- a/code/__DEFINES/misc.dm +++ b/code/__DEFINES/misc.dm @@ -302,6 +302,7 @@ var/list/bloody_footprints_cache = list() #define POLLTYPE_TEXT "TEXT" #define POLLTYPE_RATING "NUMVAL" #define POLLTYPE_MULTI "MULTICHOICE" +#define POLLTYPE_IRV "INSTANTRUNOFFVOTING" //lighting area defines #define DYNAMIC_LIGHTING_DISABLED 0 //dynamic lighting disabled (area stays at full brightness) diff --git a/code/modules/admin/create_poll.dm b/code/modules/admin/create_poll.dm index 23c07c4228e4c2..c816420b344b47 100644 --- a/code/modules/admin/create_poll.dm +++ b/code/modules/admin/create_poll.dm @@ -48,6 +48,11 @@ choice_amount = input("How many choices should be allowed?","Select choice amount") as num|null if(!choice_amount) return + if("Instant Run-off") + polltype = POLLTYPE_IRV + choice_amount = input("How many candidates are running?", "Select number of running candidates") as num|null + if(!choice_amount) + return var/starttime = SQLtime() var/endtime = input("Set end time for poll as format YYYY-MM-DD HH:MM:SS. All times in server time. HH:MM:SS is optional and 24-hour. Must be later than starting time for obvious reasons.", "Set end time", SQLtime()) as text if(!endtime) diff --git a/code/modules/mob/new_player/new_player.dm b/code/modules/mob/new_player/new_player.dm index b9fa1070e6675d..a0ccb7bae8aa90 100644 --- a/code/modules/mob/new_player/new_player.dm +++ b/code/modules/mob/new_player/new_player.dm @@ -257,6 +257,23 @@ usr << "Maximum replies reached." break usr << "Vote successful." + if(POLLTYPE_IRV) + var/id_max = text2num(href_list["maxoptionid"]) + for(var/optionid = 1 to id_max) + if(!isnull(href_list["option_[optionid]"])) + var/i = vote_on_irv_poll(pollid, optionid) + switch(i) + if(0) + continue + if(1) + usr << "Vote failed, please try again or contact an administrator." + return + if(2) + usr << "You have voted on all candidates." + break + if(3) + usr << "No fuck you." + usr << "Vote successful." /mob/new_player/proc/IsJobAvailable(rank) var/datum/job/job = SSjob.GetJob(rank) diff --git a/code/modules/mob/new_player/poll.dm b/code/modules/mob/new_player/poll.dm index 64fc6aac5324a6..d9cbfa6d0ee651 100644 --- a/code/modules/mob/new_player/poll.dm +++ b/code/modules/mob/new_player/poll.dm @@ -1,6 +1,7 @@ /datum/polloption var/optionid var/optiontext + var/optionrating /mob/new_player/proc/handle_player_polling() if(!dbcon.IsConnected()) @@ -224,7 +225,48 @@ output += "

" output += "" src << browse(output,"window=playerpoll;size=500x250") - return + if(POLLTYPE_IRV) + var/DBQuery/voted_query = dbcon.NewQuery("SELECT o.id, o.text, v.rating FROM [format_table_name("poll_option")] o, [format_table_name("poll_vote")] v WHERE o.pollid = [pollid] AND v.ckey = '[ckey]' AND o.id = v.optionid") + if(!voted_query.Execute()) + var/err = voted_query.ErrorMsg() + log_game("SQL ERROR obtaining o.id, o.text, v.rating from poll_option and poll_vote tables. Error : \[[err]\]\n") + return + var/list/votedfor = list() + while(voted_query.NextRow()) + votedfor.Add(text2num(voted_query.item[1])) + var/list/datum/polloption/options = list() + while(voted_query.NextRow()) + var/datum/polloption/O = new() + O.optionid = text2num(voted_query.item[1]) + O.optiontext = voted_query.item[2] //optiontext stores the Candidate's name + O.optionrating = voted_query.item[3] //how we rate Candidate, 1st, 2nd, 3rd choice etc. + options += O + shuffle(options) + var/output = "

Player poll
" + output += "Question: [pollquestion]
" + output += "Poll runs from [pollstarttime] until [pollendtime]

" + if(!votedfor.len) + output += "

" + output += "" + output += "" + output += "" + output += "" + output += "
" + for(var/datum/polloption/O in options) + if(O.optionid && O.optiontext && O.optionrating) + if(votedfor.len) + if(O.optionid in votedfor) + output += "[O.optiontext] - \[[O.optionrating]\]
" + else + output += "[O.optiontext] - \[0\]
" + else + output += "[O.optiontext] - \[ \]\
" + output += "
" + if(!votedfor.len) + output += "

" + output += "
" + src << browse(output,"window=playerpoll;size=500x250") + /mob/new_player/proc/poll_check_voted(pollid, table) if(!dbcon.IsConnected()) @@ -338,4 +380,44 @@ log_game("SQL ERROR adding vote to table. Error : \[[err]\]\n") return 1 usr << browse(null,"window=playerpoll") - return 0 \ No newline at end of file + return 0 + +/mob/new_player/proc/vote_on_irv_poll(pollid, optionid) + if(!pollid || !optionid) + return 1 + if(!dbcon.IsConnected()) + usr << "Failed to establish database connection." + return 1 + var/DBQuery/query_get_candidates = dbcon.NewQuery("SELECT multiplechoiceoptions FROM [format_table_name("poll_question")] WHERE id = [pollid]") + if(!query_get_candidates.Execute()) + var/err = query_get_candidates.ErrorMsg() + log_game("SQL ERROR obtaining multiplechoiceoptions from poll_question table. Error : \[[err]\]\n") + return 1 + var/i + if(query_get_candidates.NextRow()) + i = text2num(query_get_candidates.item[1]) + var/DBQuery/query_hasvoted = dbcon.NewQuery("SELECT id FROM [format_table_name("poll_vote")] WHERE pollid = [pollid] AND ckey = '[ckey]'") + if(!query_hasvoted.Execute()) + var/err = query_hasvoted.ErrorMsg() + log_game("SQL ERROR obtaining id from poll_vote table. Error : \[[err]\]\n") + return 1 + while(i) + if(query_hasvoted.NextRow()) + i-- + else + break + if(!i) + return 2 + var/adminrank = "Player" + if(client.holder) + adminrank = client.holder.rank + var/rating = input(usr, "How preferable is this candidate? Available options are above [WHAT_YOU_VOTED_LAST] (Based on previous votes)", "Preference Number") as num + if(rating <= WHAT_YOU_VOTED_LAST) //I don't want to put this variable on the mob, maybe the DB, idk + return 3 + var/DBQuery/query_insert = dbcon.NewQuery("INSERT INTO [format_table_name("poll_vote")] (datetime, pollid, optionid, ckey, ip, adminrank, rating) VALUES (Now(), [pollid], [optionid], '[ckey]', '[client.address]', '[adminrank]', [rating])") + if(!query_insert.Execute()) + var/err = query_insert.ErrorMsg() + log_game("SQL ERROR adding vote to table. Error : \[[err]\]\n") + return 1 + usr << browse(null,"window=playerpoll") + return 0