From 96b53e11891363241f3e89edf8f712c3edd23e57 Mon Sep 17 00:00:00 2001 From: "baloh.matevz" Date: Fri, 12 Oct 2012 18:37:36 +0000 Subject: [PATCH] - Added numeric value polls. These allow multiple options to be present in each poll, each option has a drop-down list that goes from a low value (-3 in screenshot) to a high value (3 in screenshot), both of these can be set per-option. Along with text descriptors for the lowest value option, middle option and highest value option (In the screenshot 'Worst', 'no change' and 'Best') Example screenshot (if you ignore the poll text): http://www.kamletos.si/numval%20polls.PNG Also note that the screenshot above only has one option (or question). It can have an unlimited number. git-svn-id: http://tgstation13.googlecode.com/svn/trunk@4848 316c924e-a436-60f5-8080-3fe189b3f50e --- code/modules/mob/new_player/new_player.dm | 21 +++- code/modules/mob/new_player/poll.dm | 141 ++++++++++++++++++++++ 2 files changed, 161 insertions(+), 1 deletion(-) diff --git a/code/modules/mob/new_player/new_player.dm b/code/modules/mob/new_player/new_player.dm index 86d479c4b3..f166ff82cd 100644 --- a/code/modules/mob/new_player/new_player.dm +++ b/code/modules/mob/new_player/new_player.dm @@ -48,7 +48,7 @@ var/isadmin = 0 if(src.client && src.client.holder) isadmin = 1 - var/DBQuery/query = dbcon.NewQuery("SELECT id FROM erro_poll_question WHERE [(isadmin ? "" : "adminonly = false AND")] Now() BETWEEN starttime AND endtime AND id NOT IN (SELECT pollid FROM erro_poll_vote WHERE ckey = \"[ckey]\")") + var/DBQuery/query = dbcon.NewQuery("SELECT id FROM erro_poll_question WHERE [(isadmin ? "" : "adminonly = false AND")] Now() BETWEEN starttime AND endtime AND id NOT IN (SELECT pollid FROM erro_poll_vote WHERE ckey = \"[ckey]\") AND id NOT IN (SELECT pollid FROM erro_poll_textreply WHERE ckey = \"[ckey]\")") query.Execute() var/newpoll = 0 while(query.NextRow()) @@ -240,6 +240,25 @@ if("TEXT") var/replytext = href_list["replytext"] log_text_poll_reply(pollid, replytext) + if("NUMVAL") + var/id_min = text2num(href_list["minid"]) + var/id_max = text2num(href_list["maxid"]) + + if( (id_max - id_min) > 100 ) //Basic exploit prevention + usr << "The option ID difference is too big. Please contact administration or the database admin." + return + + for(var/optionid = id_min; optionid<= id_max; optionid++) + if(!isnull(href_list["o[optionid]"])) //Test if this optionid was replied to + var/rating + if(href_list["o[optionid]"] == "abstain") + rating = null + else + rating = text2num(href_list["o[optionid]"]) + if(!isnum(rating)) + return + + vote_on_numval_poll(pollid, optionid, rating) proc/IsJobAvailable(rank) var/datum/job/job = job_master.GetJob(rank) diff --git a/code/modules/mob/new_player/poll.dm b/code/modules/mob/new_player/poll.dm index 3303add8f7..70abd8d60a 100644 --- a/code/modules/mob/new_player/poll.dm +++ b/code/modules/mob/new_player/poll.dm @@ -229,6 +229,77 @@ output += "[vote_text]" src << browse(output,"window=playerpoll;size=500x500") + + //Polls with a text input + if("NUMVAL") + var/DBQuery/voted_query = dbcon.NewQuery("SELECT o.text, v.rating FROM erro_poll_option o, erro_poll_vote v WHERE o.pollid = [pollid] AND v.ckey = '[usr.ckey]' AND o.id = v.optionid") + voted_query.Execute() + + var/output = "
Player poll" + output +="
" + output += "Question: [pollquestion]
" + output += "Poll runs from [pollstarttime] until [pollendtime]

" + + var/voted = 0 + while(voted_query.NextRow()) + voted = 1 + + var/optiontext = voted_query.item[1] + var/rating = voted_query.item[2] + + output += "
[optiontext] - [rating]" + + if(!voted) //Only make this a form if we have not voted yet + output += "

" + output += "" + output += "" + output += "" + + var/minid = 999999 + var/maxid = 0 + + var/DBQuery/option_query = dbcon.NewQuery("SELECT id, text, minval, maxval, descmin, descmid, descmax FROM erro_poll_option WHERE pollid = [pollid]") + option_query.Execute() + while(option_query.NextRow()) + var/optionid = text2num(option_query.item[1]) + var/optiontext = option_query.item[2] + var/minvalue = text2num(option_query.item[3]) + var/maxvalue = text2num(option_query.item[4]) + var/descmin = option_query.item[5] + var/descmid = option_query.item[6] + var/descmax = option_query.item[7] + + if(optionid < minid) + minid = optionid + if(optionid > maxid) + maxid = optionid + + var/midvalue = round( (maxvalue + minvalue) / 2) + + if(isnull(minvalue) || isnull(maxvalue) || (minvalue == maxvalue)) + continue + + output += "
[optiontext]: " + + output += "" + output += "" + + output += "

" + output += "

" + + src << browse(output,"window=playerpoll;size=500x500") return /mob/new_player/proc/vote_on_poll(var/pollid = -1, var/optionid = -1) @@ -364,4 +435,74 @@ insert_query.Execute() usr << "\blue Feedback logging successful." + usr << browse(null,"window=playerpoll") + + +/mob/new_player/proc/vote_on_numval_poll(var/pollid = -1, var/optionid = -1, var/rating = null) + if(pollid == -1 || optionid == -1) + return + + if(!isnum(pollid) || !isnum(optionid)) + return + + var/user = sqlfdbklogin + var/pass = sqlfdbkpass + var/db = sqlfdbkdb + var/address = sqladdress + var/port = sqlport + + var/DBConnection/dbcon = new() + dbcon.Connect("dbi:mysql:[db]:[address]:[port]","[user]","[pass]") + if(dbcon.IsConnected()) + + var/DBQuery/select_query = dbcon.NewQuery("SELECT starttime, endtime, question, polltype FROM erro_poll_question WHERE id = [pollid] AND Now() BETWEEN starttime AND endtime") + select_query.Execute() + + var/validpoll = 0 + + while(select_query.NextRow()) + if(select_query.item[4] != "NUMVAL") + return + validpoll = 1 + break + + if(!validpoll) + usr << "\red Poll is not valid." + return + + var/DBQuery/select_query2 = dbcon.NewQuery("SELECT id FROM erro_poll_option WHERE id = [optionid] AND pollid = [pollid]") + select_query2.Execute() + + var/validoption = 0 + + while(select_query2.NextRow()) + validoption = 1 + break + + if(!validoption) + usr << "\red Poll option is not valid." + return + + var/alreadyvoted = 0 + + var/DBQuery/voted_query = dbcon.NewQuery("SELECT id FROM erro_poll_vote WHERE optionid = [optionid] AND ckey = '[usr.ckey]'") + voted_query.Execute() + + while(voted_query.NextRow()) + alreadyvoted = 1 + break + + if(alreadyvoted) + usr << "\red You already voted in this poll." + return + + var/adminrank = "Player" + if(usr && usr.client && usr.client.holder) + adminrank = usr.client.holder.rank + + + var/DBQuery/insert_query = dbcon.NewQuery("INSERT INTO erro_poll_vote (id ,datetime ,pollid ,optionid ,ckey ,ip ,adminrank, rating) VALUES (null, Now(), [pollid], [optionid], '[usr.ckey]', '[usr.client.address]', '[adminrank]', [(isnull(rating)) ? "null" : rating])") + insert_query.Execute() + + usr << "\blue Vote successful." usr << browse(null,"window=playerpoll") \ No newline at end of file