MC/website/control-panel/public/election-rules.mjs
2026-11-27 23:43:05 +00:00

46 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Owner-confirmed schedule, 8 November 2026. GMT is UTC, not local summer time.
export const ELECTION_RULES = Object.freeze({
house: Object.freeze({first:'2026-11-15T09:00:00.000Z',weeks:4}),
bene: Object.freeze({first:'2026-11-15T15:00:00.000Z',weeks:4}),
church: Object.freeze({first:'2026-11-15T15:00:00.000Z',weeks:4}),
guild: Object.freeze({first:'2026-11-15T15:00:00.000Z',weeks:4}),
clan_delegate: Object.freeze({first:'2026-11-15T15:00:00.000Z',weeks:4}),
naib_chair: Object.freeze({first:'2026-11-15T15:00:00.000Z',weeks:4}),
imperial: Object.freeze({first:'2026-11-15T15:00:00.000Z',weeks:4}),
});
export function electionDates(kind, now=Date.now(), count=4) {
const rule=ELECTION_RULES[kind];
if(!rule || !Number.isFinite(now) || !Number.isInteger(count) || count<1 || count>12) throw new Error('Invalid calendar request');
const first=Date.parse(rule.first),period=rule.weeks*7*86400000;
const offset=Math.max(0,Math.ceil((now-first)/period));
return Array.from({length:count},(_,i)=>new Date(first+(offset+i)*period).toISOString());
}
// Only a verified server-side faction and current office may establish eligibility.
export function imperialVoteAllowance({linked=false,faction=null,houseLeader=false}={}) {
return linked && faction ? houseLeader===true ? 5 : 1 : 0;
}
export function electionWindow(kind,start,now=Date.now(),approvedStart=null) {
const rule=ELECTION_RULES[kind],timestamp=Date.parse(start);
if(!rule||!Number.isFinite(timestamp)||!Number.isFinite(now))throw Error('Invalid election window');
const first=Date.parse(rule.first),period=rule.weeks*7*86400000;
if(timestamp<first||((timestamp-first)%period!==0&&!(kind==='imperial'&&approvedStart===timestamp)))throw Error('Election is outside the confirmed calendar');
const closes=timestamp+3*3600000;
return {applicationsClose:new Date(timestamp).toISOString(),votingOpens:new Date(timestamp).toISOString(),votingCloses:new Date(closes).toISOString(),phase:now<timestamp?'applications':now<closes?'voting':'closed'};
}
export function validateCandidature(input,context,now=Date.now(),approvedStart=null) {
if(!input||!Object.hasOwn(ELECTION_RULES,input.kind)||input.accepted!==true)throw Error('Choose an election and accept your candidature');
if(!context?.linked)throw Error('A verified Minecraft account is required');
if(input.kind==='house'&&!['atreides','harkonnen','corrino','ordos','fremen'].includes(context.faction))throw Error('A verified faction membership is required');
if(input.kind==='clan_delegate'&&!context.clanId)throw Error('Active clan membership is required');
if(input.kind==='naib_chair'&&(!context.naib||!context.clanId))throw Error('Current Arrakduin sietch ownership is required');
const window=electionWindow(input.kind,input.start,now,approvedStart);
if(window.phase!=='applications')throw Error('Applications for this election are closed');
if(window.votingOpens!==(approvedStart!==null&&input.kind==='imperial'?new Date(approvedStart).toISOString():electionDates(input.kind,now,1)[0]))throw Error('Only the next scheduled election accepts applications');
const motivation=typeof input.motivation==='string'?input.motivation.trim():'';
if(motivation.length<20||motivation.length>4000||/[\u0000-\u0008\u000b\u000c\u000e-\u001f]/.test(motivation))throw Error('Motivation must contain 20–4000 readable characters');
// Never take faction or office authority from the submitted form.
return {kind:input.kind,start:window.votingOpens,faction:electionScope(input.kind,context),motivation};
}
export function electionScope(kind,context={}){return kind==='house'?context.faction:({imperial:'imperium',bene:'veiled_concord',church:'muad_dib',guild:'spacing_guild',clan_delegate:'clans',naib_chair:'arrakis'})[kind];}
export function voteCapacity(kind,context={}){return kind==='imperial'?imperialVoteAllowance(context):kind==='naib_chair'?(context.naib&&context.clanId?1:0):kind==='clan_delegate'?(context.clanLeader&&context.clanId?1:0):1;}