From 11e3941fec6d01c220e7af4513af4af89b03a5d4 Mon Sep 17 00:00:00 2001 From: Teppy Date: Thu, 28 Sep 2023 16:36:00 -0400 Subject: [PATCH] changes --- plurvote.html | 30 +++++++++++++++++++++++++++ rangevote.js | 57 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 65 insertions(+), 22 deletions(-) create mode 100644 plurvote.html diff --git a/plurvote.html b/plurvote.html new file mode 100644 index 0000000..a354f42 --- /dev/null +++ b/plurvote.html @@ -0,0 +1,30 @@ + + + + + + Plurality Vote + + + +
+ This page is an experiment in voting. Half of the people who visit this site vote using Plurality Voting + where a voter selects their top candidate, and the winner is the candidate with the most votes. The other + half use Range Voting where voters score all familiar candidates based on how happy they would be should + that candidate win. You have been randomly selected to vote using Plurality Voting. +
+
+ + + + + + + +
Candidate
+ +
+ +
+ + diff --git a/rangevote.js b/rangevote.js index 2007b1c..64e688c 100644 --- a/rangevote.js +++ b/rangevote.js @@ -4,7 +4,6 @@ var fs=require('fs') var app = express() - var Candidates=[ "Chris Christie", "Ron DeSantis", "Nikki Haley", "Mike Pence", "Vivek Ramaswamy", "Tim Scott", "Donald Trump" ]; @@ -17,12 +16,15 @@ function CanId(c) return(rval); } + + // Set up data structures for Range Votes and Plur Votes var RangeVotes=new Map(); var RangePower=new Map(); var RangeResult=new Map(); var PlurVotes=new Map(); var PlurResult=new Map(); +var PlurCount=0; for (let c of Candidates) { let k=CanId(c); @@ -37,16 +39,28 @@ for (let c of Candidates) // Construct the static page for range voting -var TheHTML=""; +var RangeHTML=""; for (let c of Candidates) { - TheHTML=TheHTML+' '+ + RangeHTML=RangeHTML+' '+ ' '+c+'No Opinion\n'; } var TemplateHTML=fs.readFileSync("rangevote.html").toString(); var TemplateSearch=""; var TemplateIndex=TemplateHTML.indexOf(TemplateSearch); -TheHTML=TemplateHTML.substr(0,TemplateIndex)+TheHTML+TemplateHTML.substr(TemplateIndex+TemplateSearch.length,1000000); +RangeHTML=TemplateHTML.substr(0,TemplateIndex)+RangeHTML+TemplateHTML.substr(TemplateIndex+TemplateSearch.length,1000000); + +// Construct the static page for Plurality Voting +var PlurHTML=""; +for (let c of Candidates) +{ + PlurHTML=PlurHTML+' '+ + ' '+c+'\n'; +} +TemplateHTML=fs.readFileSync("plurvote.html").toString(); +TemplateSearch=""; +TemplateIndex=TemplateHTML.indexOf(TemplateSearch); +PlurHTML=TemplateHTML.substr(0,TemplateIndex)+PlurHTML+TemplateHTML.substr(TemplateIndex+TemplateSearch.length,1000000); // create application/json parser @@ -56,7 +70,8 @@ var jsonParser = bodyParser.json() var urlencodedParser = bodyParser.urlencoded({ extended: false }) app.get('/', (req, res) => { - res.send(TheHTML); + res.send(RangeHTML); +// res.send(PlurHTML); }) function lerp(a,b,c,d,e) @@ -70,26 +85,20 @@ function lerp(a,b,c,d,e) function PlurVote(ip,NewChoice) { let OldChoice=PlurResult.get(ip); - if (typeof(OldChoice)=='string') PlurResult.set(OldChoice,PlurResult.get(OldChoice)-1); - if (typeof(NewChoice)=='string') PlurResult.set(NewChoice,PlurResult.get(NewChoice)+1); + if (typeof(OldChoice)=='string') { PlurResult.set(OldChoice,PlurResult.get(OldChoice)-1); PlurCount=PlurCount-1; } + if (!NewChoice) return; + NewChoice=CanId(NewChoice); + if (typeof(NewChoice)=='string') { PlurResult.set(NewChoice,PlurResult.get(NewChoice)+1); PlurCount=PlurCount+1; } } function AddToRangeResult(vote,mult=1) { - console.log("RangeResult Before "+mult); - console.log(RangeResult); - console.log("RangePower Before "+mult); - console.log(RangePower); for (let[key,val] of vote) if (key) if (val>=0) { RangeResult.set(key,RangeResult.get(key)+val*mult); RangePower.set(key,RangePower.get(key)+mult); } - console.log("RangeResult After "+mult); - console.log(RangeResult); - console.log("RangePower After "+mult); - console.log(RangePower); } @@ -98,22 +107,20 @@ function RangeVote(ip,entries) let r0=null,r1=null; for (let [key,val] of entries) if (typeof(key)=="string" && val>=0) { - if (r0) r0=Math.min(r0,val); else r0=val; - if (r1) r1=Math.max(r1,val); else r1=val; + if (r0==null) { r0=val; r1=val; } + r0=Math.min(r0,val); + r1=Math.max(r1,val); } if (r1<=r0) return; let NewVote=new Map() for (let [key,val] of entries) if (typeof(key)=="string") { let id=CanId(key); - console.log(key+' '+id); let norm=Math.round(lerp(val,r0,r1,0,100)); if (val>=0) NewVote.set(id,norm); } let OldVote=RangeVotes.get(ip); // if (OldVote) AddToRangeResult(OldVote,-1); - console.log("NewVote"); - console.log(NewVote); RangeVotes.set(ip,NewVote); AddToRangeResult(NewVote,1); } @@ -132,8 +139,14 @@ app.post('/range_vote', urlencodedParser, function (req, res) { }) app.post('/plur_vote', urlencodedParser, function (req, res) { -// PlurVote(req.socket.remoteAddress,Object.entries(req.body)); - res.send("Your Plurality Vote Counted!!!"); + PlurVote(req.socket.remoteAddress,req.body.pick); + let str="Results:
"; + for (let c of Candidates) + { + let key=CanId(c); + if (PlurResult.get(key)>0) str=str+c+': '+Math.round(100.0*PlurResult.get(key)/PlurCount)+'%
'; + } + res.send(str); })