changes
This commit is contained in:
30
plurvote.html
Normal file
30
plurvote.html
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<title>Plurality Vote</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<form id="my-form" action="/plur_vote" method="post">
|
||||||
|
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.
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th> </th>
|
||||||
|
<th>Candidate</th>
|
||||||
|
</tr>
|
||||||
|
<!-- INJECT0 -->
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<br>
|
||||||
|
<button type="submit" value="submit">Submit</button>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
|
||||||
57
rangevote.js
57
rangevote.js
@@ -4,7 +4,6 @@ var fs=require('fs')
|
|||||||
|
|
||||||
var app = express()
|
var app = express()
|
||||||
|
|
||||||
|
|
||||||
var Candidates=[ "Chris Christie", "Ron DeSantis", "Nikki Haley", "Mike Pence",
|
var Candidates=[ "Chris Christie", "Ron DeSantis", "Nikki Haley", "Mike Pence",
|
||||||
"Vivek Ramaswamy", "Tim Scott", "Donald Trump" ];
|
"Vivek Ramaswamy", "Tim Scott", "Donald Trump" ];
|
||||||
|
|
||||||
@@ -17,12 +16,15 @@ function CanId(c)
|
|||||||
return(rval);
|
return(rval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Set up data structures for Range Votes and Plur Votes
|
// Set up data structures for Range Votes and Plur Votes
|
||||||
var RangeVotes=new Map();
|
var RangeVotes=new Map();
|
||||||
var RangePower=new Map();
|
var RangePower=new Map();
|
||||||
var RangeResult=new Map();
|
var RangeResult=new Map();
|
||||||
var PlurVotes=new Map();
|
var PlurVotes=new Map();
|
||||||
var PlurResult=new Map();
|
var PlurResult=new Map();
|
||||||
|
var PlurCount=0;
|
||||||
for (let c of Candidates)
|
for (let c of Candidates)
|
||||||
{
|
{
|
||||||
let k=CanId(c);
|
let k=CanId(c);
|
||||||
@@ -37,16 +39,28 @@ for (let c of Candidates)
|
|||||||
|
|
||||||
|
|
||||||
// Construct the static page for range voting
|
// Construct the static page for range voting
|
||||||
var TheHTML="";
|
var RangeHTML="";
|
||||||
for (let c of Candidates)
|
for (let c of Candidates)
|
||||||
{
|
{
|
||||||
TheHTML=TheHTML+' <tr>'+
|
RangeHTML=RangeHTML+' <tr>'+
|
||||||
' <td>'+c+'</td><td><input type="range" min="-1" max="100" value="-1" id="i-'+CanId(c)+'" name="n-'+CanId(c)+'" /></td><td><span id="o-'+CanId(c)+'">No Opinion</span></td></tr>\n';
|
' <td>'+c+'</td><td><input type="range" min="-1" max="100" value="-1" id="i-'+CanId(c)+'" name="n-'+CanId(c)+'" /></td><td><span id="o-'+CanId(c)+'">No Opinion</span></td></tr>\n';
|
||||||
}
|
}
|
||||||
var TemplateHTML=fs.readFileSync("rangevote.html").toString();
|
var TemplateHTML=fs.readFileSync("rangevote.html").toString();
|
||||||
var TemplateSearch="<!-- INJECT0 -->";
|
var TemplateSearch="<!-- INJECT0 -->";
|
||||||
var TemplateIndex=TemplateHTML.indexOf(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+' <tr>'+
|
||||||
|
' <td><input type="radio" value="v-'+CanId(c)+'" name="pick" /></td><td>'+c+'</td></tr>\n';
|
||||||
|
}
|
||||||
|
TemplateHTML=fs.readFileSync("plurvote.html").toString();
|
||||||
|
TemplateSearch="<!-- INJECT0 -->";
|
||||||
|
TemplateIndex=TemplateHTML.indexOf(TemplateSearch);
|
||||||
|
PlurHTML=TemplateHTML.substr(0,TemplateIndex)+PlurHTML+TemplateHTML.substr(TemplateIndex+TemplateSearch.length,1000000);
|
||||||
|
|
||||||
|
|
||||||
// create application/json parser
|
// create application/json parser
|
||||||
@@ -56,7 +70,8 @@ var jsonParser = bodyParser.json()
|
|||||||
var urlencodedParser = bodyParser.urlencoded({ extended: false })
|
var urlencodedParser = bodyParser.urlencoded({ extended: false })
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
res.send(TheHTML);
|
res.send(RangeHTML);
|
||||||
|
// res.send(PlurHTML);
|
||||||
})
|
})
|
||||||
|
|
||||||
function lerp(a,b,c,d,e)
|
function lerp(a,b,c,d,e)
|
||||||
@@ -70,26 +85,20 @@ function lerp(a,b,c,d,e)
|
|||||||
function PlurVote(ip,NewChoice)
|
function PlurVote(ip,NewChoice)
|
||||||
{
|
{
|
||||||
let OldChoice=PlurResult.get(ip);
|
let OldChoice=PlurResult.get(ip);
|
||||||
if (typeof(OldChoice)=='string') PlurResult.set(OldChoice,PlurResult.get(OldChoice)-1);
|
if (typeof(OldChoice)=='string') { PlurResult.set(OldChoice,PlurResult.get(OldChoice)-1); PlurCount=PlurCount-1; }
|
||||||
if (typeof(NewChoice)=='string') PlurResult.set(NewChoice,PlurResult.get(NewChoice)+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)
|
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)
|
for (let[key,val] of vote) if (key) if (val>=0)
|
||||||
{
|
{
|
||||||
RangeResult.set(key,RangeResult.get(key)+val*mult);
|
RangeResult.set(key,RangeResult.get(key)+val*mult);
|
||||||
RangePower.set(key,RangePower.get(key)+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;
|
let r0=null,r1=null;
|
||||||
for (let [key,val] of entries) if (typeof(key)=="string" && val>=0)
|
for (let [key,val] of entries) if (typeof(key)=="string" && val>=0)
|
||||||
{
|
{
|
||||||
if (r0) r0=Math.min(r0,val); else r0=val;
|
if (r0==null) { r0=val; r1=val; }
|
||||||
if (r1) r1=Math.max(r1,val); else r1=val;
|
r0=Math.min(r0,val);
|
||||||
|
r1=Math.max(r1,val);
|
||||||
}
|
}
|
||||||
if (r1<=r0) return;
|
if (r1<=r0) return;
|
||||||
let NewVote=new Map()
|
let NewVote=new Map()
|
||||||
for (let [key,val] of entries) if (typeof(key)=="string")
|
for (let [key,val] of entries) if (typeof(key)=="string")
|
||||||
{
|
{
|
||||||
let id=CanId(key);
|
let id=CanId(key);
|
||||||
console.log(key+' '+id);
|
|
||||||
let norm=Math.round(lerp(val,r0,r1,0,100));
|
let norm=Math.round(lerp(val,r0,r1,0,100));
|
||||||
if (val>=0) NewVote.set(id,norm);
|
if (val>=0) NewVote.set(id,norm);
|
||||||
}
|
}
|
||||||
let OldVote=RangeVotes.get(ip);
|
let OldVote=RangeVotes.get(ip);
|
||||||
// if (OldVote) AddToRangeResult(OldVote,-1);
|
// if (OldVote) AddToRangeResult(OldVote,-1);
|
||||||
console.log("NewVote");
|
|
||||||
console.log(NewVote);
|
|
||||||
RangeVotes.set(ip,NewVote);
|
RangeVotes.set(ip,NewVote);
|
||||||
AddToRangeResult(NewVote,1);
|
AddToRangeResult(NewVote,1);
|
||||||
}
|
}
|
||||||
@@ -132,8 +139,14 @@ app.post('/range_vote', urlencodedParser, function (req, res) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
app.post('/plur_vote', urlencodedParser, function (req, res) {
|
app.post('/plur_vote', urlencodedParser, function (req, res) {
|
||||||
// PlurVote(req.socket.remoteAddress,Object.entries(req.body));
|
PlurVote(req.socket.remoteAddress,req.body.pick);
|
||||||
res.send("Your Plurality Vote Counted!!!");
|
let str="Results:<br>";
|
||||||
|
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)+'%<br>';
|
||||||
|
}
|
||||||
|
res.send(str);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user