feat(packages): ludic.session - a party's roster (slot 0 a port, the others as reported), the roles and who owns the world, the shared night, votes and a world-changing act asking the party; joins, leaves, roles, votes and the night as facts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
12172d7b75
commit
79ed7a335b
12 changed files with 707 additions and 0 deletions
111
packages/ludic.session/votes.ludic
Normal file
111
packages/ludic.session/votes.ludic
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
# votes.ludic - what changes the world for everyone is the host's to put to the party and the
|
||||
# party's to agree: a majority of the players, the host included, within thirty seconds. A guest
|
||||
# only hears of a vote (ses_vote_told), casts its own (SES_VOTE_CAST) and hears how it ended.
|
||||
export const SES_MAY_GO: int = 0 # alone, or nobody else here: go ahead
|
||||
export const SES_MAY_ASKED: int = 1 # the party has been asked; the act waits for SES_VOTE_RESOLVED
|
||||
export const SES_MAY_BUSY: int = 2 # a vote is already open
|
||||
export const SES_MAY_HOSTS: int = 3 # a guest: it is for the host to ask
|
||||
|
||||
const SESSION_VOTE_SECS: float = 30.0
|
||||
|
||||
var session__vote_id: int = 0
|
||||
var session__vote_kind: int = 0
|
||||
var session__vote_param: int = 0
|
||||
var session__vote_text: string = ""
|
||||
var session__vote_t: float = 0.0
|
||||
var session__vote_yes: int = 0
|
||||
var session__vote_no: int = 0
|
||||
var session__vote_mine: int = -1 # -1 not voted, 0 no, 1 yes
|
||||
var session__vote_passed: int = -1 # the last vote: -1 none, 0 failed, 1 passed
|
||||
|
||||
# a world-changing act: may it go ahead now, or does the party have to be asked first?
|
||||
export function ses_may(kind: int, param: int, text: string) -> int {
|
||||
if not ses_live() { return SES_MAY_GO }
|
||||
if session__role == SES_GUEST { return SES_MAY_HOSTS }
|
||||
if ply_count() <= 1 { return SES_MAY_GO }
|
||||
if not ses_vote_open(kind, param, text) { return SES_MAY_BUSY }
|
||||
return SES_MAY_ASKED
|
||||
}
|
||||
|
||||
# the host puts it to the party, voting yes itself
|
||||
export function ses_vote_open(kind: int, param: int, text: string) -> bool {
|
||||
if session__role != SES_HOST or ses_vote_live() { return false }
|
||||
session__vote_id += 1
|
||||
session__vote_kind = kind
|
||||
session__vote_param = param
|
||||
session__vote_text = text
|
||||
session__vote_t = SESSION_VOTE_SECS
|
||||
session__vote_yes = 1
|
||||
session__vote_no = 0
|
||||
session__vote_mine = 1
|
||||
session__vote_passed = -1
|
||||
let f = session__vote_fact(SES_VOTE_OPENED)
|
||||
f.yes = true
|
||||
session__vote_check()
|
||||
return true
|
||||
}
|
||||
|
||||
# this player's answer; a guest's goes to the host as SES_VOTE_CAST
|
||||
export function ses_vote_cast(yes: bool) -> void {
|
||||
if not ses_vote_live() or session__vote_mine >= 0 { return }
|
||||
if yes { session__vote_mine = 1 } else { session__vote_mine = 0 }
|
||||
if session__role == SES_GUEST {
|
||||
let f = session__vote_fact(SES_VOTE_CAST)
|
||||
f.yes = yes
|
||||
return
|
||||
}
|
||||
ses_vote_heard(session__vote_id, yes)
|
||||
}
|
||||
|
||||
# the host: a player's answer to vote `id`
|
||||
export function ses_vote_heard(id: int, yes: bool) -> void {
|
||||
if session__role != SES_HOST or id != session__vote_id or not ses_vote_live() { return }
|
||||
if yes { session__vote_yes += 1 } else { session__vote_no += 1 }
|
||||
session__vote_check()
|
||||
}
|
||||
|
||||
# a guest: the host opened vote `id`, or ended it
|
||||
export function ses_vote_told(id: int, kind: int, text: string) -> void {
|
||||
session__vote_id = id
|
||||
session__vote_kind = kind
|
||||
session__vote_text = text
|
||||
session__vote_t = SESSION_VOTE_SECS
|
||||
session__vote_mine = -1
|
||||
}
|
||||
|
||||
export function ses_vote_ended(id: int, passed: bool) -> void {
|
||||
if id != session__vote_id { return }
|
||||
session__vote_t = 0.0
|
||||
if passed { session__vote_passed = 1 } else { session__vote_passed = 0 }
|
||||
}
|
||||
|
||||
# the vote's thirty seconds; on the host, running out decides it
|
||||
export function ses_vote_tick(dt: float) -> void {
|
||||
if not ses_vote_live() { return }
|
||||
session__vote_t = session__vote_t - dt
|
||||
if session__role == SES_HOST and not ses_vote_live() { session__vote_finish() }
|
||||
}
|
||||
|
||||
function session__vote_check() -> void {
|
||||
let n = ply_count()
|
||||
let yes = session__vote_yes
|
||||
let no = session__vote_no
|
||||
if yes * 2 > n or no * 2 >= n or yes + no >= n { session__vote_finish() }
|
||||
}
|
||||
|
||||
function session__vote_finish() -> void {
|
||||
let passed = session__vote_yes * 2 > ply_count()
|
||||
session__vote_t = 0.0
|
||||
if passed { session__vote_passed = 1 } else { session__vote_passed = 0 }
|
||||
let f = session__vote_fact(SES_VOTE_RESOLVED)
|
||||
f.yes = passed
|
||||
}
|
||||
|
||||
function session__vote_fact(what: int) -> SessionFact {
|
||||
let f = session__fact(what, -1, 0)
|
||||
f.id = session__vote_id
|
||||
f.kind = session__vote_kind
|
||||
f.param = session__vote_param
|
||||
f.text = session__vote_text
|
||||
return f
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue