forked from unimatrix/web
92 lines
10 KiB
JavaScript
92 lines
10 KiB
JavaScript
import {clanDelegateAuthority,lockClanVotingDeed} from '../node/clan-voting-authority.mjs';
|
|
import {connectionReader} from '../node/clan-deeds.mjs';
|
|
import {randomUUID} from 'node:crypto';
|
|
import {approvedForcedStart} from '../node/election-rallies.mjs';
|
|
import {ELECTION_RULES,electionWindow,electionScope,voteCapacity} from './public/election-rules.mjs';
|
|
import {currentNaib} from '../node/naib-membership.mjs';
|
|
const uuid=/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i;
|
|
export function validateBallot(input,context,now=Date.now(),roundWindow=null,approvedStart=null){
|
|
if(!input||!uuid.test(input.requestId||'')||!Object.hasOwn(ELECTION_RULES,input.kind)||input.confirmed!==true)throw Error('Confirm your definitive vote');
|
|
if(!context?.linked||(['house','imperial'].includes(input.kind)&&!context.faction)||!uuid.test(context.playerUuid||''))throw Error('Verified faction membership required');
|
|
const window=electionWindow(input.kind,input.start,now,approvedStart);if(roundWindow)window.phase=now>=Number(roundWindow.opens_at)&&now<Number(roundWindow.closes_at)?'voting':'closed';if(window.phase!=='voting')throw Error('Voting is not open');
|
|
const capacity=voteCapacity(input.kind,context);
|
|
if(!Array.isArray(input.allocations)||!input.allocations.length||input.allocations.length>capacity)throw Error('Choose eligible candidates');
|
|
const seen=new Set();let total=0;
|
|
const allocations=input.allocations.map(a=>{if(!a||!uuid.test(a.applicationId||'')||seen.has(a.applicationId.toLowerCase())||!Number.isInteger(a.votes)||a.votes<1||a.votes>capacity)throw Error('Invalid vote allocation');seen.add(a.applicationId.toLowerCase());total+=a.votes;return{applicationId:a.applicationId.toLowerCase(),votes:a.votes};}).sort((a,b)=>a.applicationId.localeCompare(b.applicationId));
|
|
if(total>capacity)throw Error('Vote allowance exceeded');
|
|
return {kind:input.kind,start:Date.parse(window.votingOpens),faction:electionScope(input.kind,context),capacity,total,allocations,requestId:input.requestId.toLowerCase()};
|
|
}
|
|
export const ballotSchema=[
|
|
`CREATE TABLE IF NOT EXISTS political_clan_votes(round_id CHAR(36) NOT NULL,clan_id CHAR(36) NOT NULL,ballot_id CHAR(36) NOT NULL UNIQUE,PRIMARY KEY(round_id,clan_id)) ENGINE=InnoDB`,
|
|
`CREATE TABLE IF NOT EXISTS political_voters (round_id CHAR(36) NOT NULL,player_uuid CHAR(36) NOT NULL,kind VARCHAR(16) NOT NULL,election_start BIGINT NOT NULL,user_id CHAR(36) NOT NULL,faction VARCHAR(32) NOT NULL,capacity INTEGER NOT NULL,spent INTEGER NOT NULL DEFAULT 0,PRIMARY KEY(player_uuid,round_id)) ENGINE=InnoDB`,
|
|
`CREATE TABLE IF NOT EXISTS political_ballots (id CHAR(36) PRIMARY KEY,round_id CHAR(36) NOT NULL,request_id CHAR(36) NOT NULL,player_uuid CHAR(36) NOT NULL,kind VARCHAR(16) NOT NULL,election_start BIGINT NOT NULL,payload TEXT NOT NULL,created_at BIGINT NOT NULL,UNIQUE KEY voter_request(player_uuid,request_id)) ENGINE=InnoDB`,
|
|
`CREATE TABLE IF NOT EXISTS political_ballot_allocations (ballot_id CHAR(36) NOT NULL,application_id CHAR(36) NOT NULL,votes INTEGER NOT NULL,PRIMARY KEY(ballot_id,application_id)) ENGINE=InnoDB`,
|
|
`CREATE TABLE IF NOT EXISTS political_ballot_events (id CHAR(36) PRIMARY KEY,ballot_id CHAR(36) NOT NULL UNIQUE,actor_id CHAR(36) NOT NULL,event VARCHAR(32) NOT NULL,created_at BIGINT NOT NULL) ENGINE=InnoDB`
|
|
];
|
|
export async function initializeBallots(db){for(const sql of ballotSchema)await db.prepare(sql).run();}
|
|
// Private append-only repository. Never expose player_uuid or audit rows to candidates.
|
|
// Not yet mounted: runoff scheduling and result finalization remain separate work.
|
|
export async function castBallot(db,{actor,context,input,now=Date.now(),clock=Date.now,makeId=randomUUID}){
|
|
if(!actor?.id)throw Error('Authenticated voter required');
|
|
if(!uuid.test(input?.roundId||'')||!uuid.test(context?.playerUuid||''))throw Error('Verified election round required');
|
|
const player=context.playerUuid.toLowerCase();
|
|
const [result]=await db.batch([{async run(connection){
|
|
const query=async(sql,args=[])=>(await connection.execute(sql,args))[0];
|
|
if(input.kind==='naib_chair'){const seat=await currentNaib({query},actor.id,{lock:true});if(!seat||seat.clan_id!==context.clanId)throw Error('Current Arrakduin sietch ownership is required');}
|
|
const [round]=await query('SELECT * FROM political_election_rounds WHERE id=? FOR UPDATE',[input.roundId]);
|
|
if(!round||round.kind!==input.kind||Number(round.election_start)!==Date.parse(input.start)||round.faction!==electionScope(input.kind,context)||round.status!=='open')throw Error('Election round is not open');
|
|
const approved=await approvedForcedStart({query,electionRalliesEnabled:db.electionRalliesEnabled},round.kind,round.election_start);const vote=validateBallot(input,context,clock(),round,approved?Number(round.election_start):null),clanBallot=vote.kind==='clan_delegate',payload=JSON.stringify({kind:vote.kind,start:vote.start,roundId:round.id,...(clanBallot?{clanId:context.clanId}:{}),allocations:vote.allocations});
|
|
if(clanBallot){
|
|
await lockClanVotingDeed(db,connection,context.clanId);
|
|
if(!await clanDelegateAuthority(connectionReader(db,connection),actor.id,context.clanId))throw Error('Current clan political authority is required');
|
|
}
|
|
await query('INSERT INTO political_voters(round_id,player_uuid,kind,election_start,user_id,faction,capacity,spent) VALUES(?,?,?,?,?,?,?,0) ON DUPLICATE KEY UPDATE player_uuid=VALUES(player_uuid)',[round.id,player,vote.kind,vote.start,actor.id,context.faction||vote.faction,vote.capacity]);
|
|
const [voter]=await query('SELECT user_id,faction,capacity,spent FROM political_voters WHERE player_uuid=? AND round_id=? FOR UPDATE',[player,round.id]);
|
|
if(voter.user_id!==actor.id||voter.faction!==(context.faction||vote.faction))throw Error('Voter identity or faction changed');
|
|
const [link]=await query('SELECT minecraft_uuid FROM site_game_links WHERE user_id=? FOR UPDATE',[actor.id]);if(link?.minecraft_uuid?.toLowerCase()!==player)throw Error('Linked account changed');
|
|
const [previous]=await query('SELECT id,payload FROM political_ballots WHERE player_uuid=? AND request_id=?',[player,vote.requestId]);
|
|
if(previous){
|
|
if(previous.payload!==payload){
|
|
const legacy=clanBallot?await query('SELECT ballot_id FROM political_clan_votes WHERE round_id=? AND clan_id=?',[round.id,context.clanId]):[];
|
|
const legacyPayload=JSON.stringify({kind:vote.kind,start:vote.start,roundId:round.id,allocations:vote.allocations});
|
|
if(!clanBallot||previous.payload!==legacyPayload||legacy[0]?.ballot_id!==previous.id)throw Error('Submission identifier already used');
|
|
}
|
|
return{id:previous.id,created:false,remaining:clanBallot?0:Number(voter.capacity)-Number(voter.spent)};}
|
|
validateBallot(input,context,clock(),round,approved?Number(round.election_start):null);
|
|
// Allowance is captured at the first successful ballot; later changes cannot
|
|
// manufacture additional votes. A lower current allowance also prevents spending.
|
|
const available=clanBallot?vote.capacity:Math.min(Number(voter.capacity),vote.capacity)-Number(voter.spent);if(vote.total>available)throw Error('No sufficient votes remain');
|
|
for(const allocation of vote.allocations){
|
|
const [entry]=await query('SELECT application_id FROM political_round_candidates WHERE round_id=? AND application_id=?',[round.id,allocation.applicationId]);if(!entry)throw Error('Candidate is not in this round');
|
|
const [candidate]=await query('SELECT kind,faction,election_start,status,user_id FROM political_applications WHERE id=? FOR UPDATE',[allocation.applicationId]);
|
|
if(!candidate||candidate.kind!==vote.kind||Number(candidate.election_start)!==vote.start||candidate.faction!==vote.faction||candidate.status!=='submitted')throw Error('Candidate is not eligible for this election');
|
|
if(vote.kind==='naib_chair'&&!await currentNaib({query},candidate.user_id,{lock:true}))throw Error('Candidate is no longer a Naib');
|
|
}
|
|
const id=makeId();
|
|
if(clanBallot){await query('INSERT INTO political_clan_votes(round_id,clan_id,ballot_id) VALUES(?,?,?)',[round.id,context.clanId,id]);}
|
|
else if(vote.kind==='naib_chair'){
|
|
const [clan]=await query("SELECT c.id FROM site_clans c JOIN site_clan_members m ON m.clan_id=c.id AND m.user_id=c.owner_id AND m.clan_role='owner' JOIN site_game_links l ON l.user_id=m.user_id AND l.minecraft_uuid=m.minecraft_uuid WHERE c.id=? AND c.owner_id=? AND c.active=1 FOR UPDATE",[context.clanId,actor.id]);
|
|
if(!clan)throw Error('Current linked clan leadership is required');
|
|
await query('INSERT INTO political_clan_votes(round_id,clan_id,ballot_id) VALUES(?,?,?)',[round.id,clan.id,id]);
|
|
}
|
|
await query('INSERT INTO political_ballots(id,round_id,request_id,player_uuid,kind,election_start,payload,created_at) VALUES(?,?,?,?,?,?,?,?)',[id,round.id,vote.requestId,player,vote.kind,vote.start,payload,now]);
|
|
for(const a of vote.allocations)await query('INSERT INTO political_ballot_allocations(ballot_id,application_id,votes) VALUES(?,?,?)',[id,a.applicationId,a.votes]);
|
|
await query('UPDATE political_voters SET spent=spent+? WHERE player_uuid=? AND round_id=?',[vote.total,player,round.id]);
|
|
await query('INSERT INTO political_ballot_events(id,ballot_id,actor_id,event,created_at) VALUES(?,?,?,?,?)',[makeId(),id,actor.id,'vote_received',now]);
|
|
return{id,created:true,remaining:clanBallot?0:Number(voter.capacity)-Number(voter.spent)-vote.total};
|
|
}}]);return result;
|
|
}
|
|
// A tied ballot delegates succession to a three-hour office auction.
|
|
export function electionOutcome(candidates){
|
|
const rows=candidates.filter(c=>Number.isSafeInteger(c.votes)&&c.votes>0);
|
|
if(!rows.length)return{status:'no_votes',winner:null,runoff:[]};
|
|
const high=Math.max(...rows.map(c=>c.votes)),leaders=rows.filter(c=>c.votes===high).map(c=>c.id).sort();
|
|
return leaders.length===1?{status:'winner',winner:leaders[0],runoff:[]}:{status:'auction_required',winner:null,runoff:leaders};
|
|
}
|
|
export function planRoundResult({kind,start,round=0,candidates},now=Date.now(),approvedStart=null){
|
|
if(!Number.isSafeInteger(round)||round<0||round>1000)throw Error('Invalid election round');
|
|
const base=electionWindow(kind,start,now,approvedStart),opens=Date.parse(base.votingOpens)+round*10800000,closes=opens+10800000;
|
|
if(now<closes)throw Error('Voting round has not closed');
|
|
const result=electionOutcome(candidates);
|
|
return {...result,round,opensAt:opens,closesAt:closes,nextRound:null,auction:result.status==='auction_required'?{durationMs:10800000,candidateIds:result.runoff}:null};
|
|
}
|

Staff