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