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:
Orkun ÇAKILKAYA 2026-09-25 07:45:26 +03:00
parent 12172d7b75
commit 79ed7a335b
12 changed files with 707 additions and 0 deletions

View file

@ -0,0 +1,177 @@
# session_test.ludic - the roster, the roles, the nearest player, the shared night and votes,
# with a fake local player
import "ludic.session"
import "ludic.base"
program SessionTest {
numbers float
var me_x: float = 0.0
var me_z: float = 0.0
function fx() -> float { return me_x }
function fy() -> float { return 0.0 }
function fz() -> float { return me_z }
function fname() -> string { return "me" }
bind SessionLocal { x: fn fx, y: fn fy, z: fn fz, name: fn fname }
function whats() -> []int {
let fs = q_drain(ses_facts())
let out = new []int
for i in 0 .. len(fs) { push(out, fs[i].what) }
return out
}
function last_of(what: int) -> SessionFact {
let fs = q_drain(ses_facts())
var f: SessionFact = null
for i in 0 .. len(fs) { if fs[i].what == what { f = fs[i] } }
return f
}
# a host with n guests, pids 2.., facts drained
function party(n: int) -> void {
ply_init()
ses_set_role(SES_HOST)
for i in 0 .. n { ply_join(2 + i) }
q_clear(ses_facts())
}
test "slot 0 is the local player, read live; others join at its feet and leave" {
ply_init()
me_x = 5.0
expect(ply_on(0))
expect_near(ply_x(0), 5.0, 0.001)
expect(ply_name(0) == "me")
let p = ply_join(7)
expect_eq(p, 1)
expect_near(ply_x(p), 5.0, 0.001)
expect_eq(ply_slot_of(7), 1)
expect_eq(ply_count(), 2)
expect_eq(ply_join(0), -1)
ply_leave(p)
expect_eq(ply_slot_of(7), -1)
expect_eq(ply_count(), 1)
let w = whats()
expect_eq(len(w), 2)
expect_eq(w[0], SES_JOINED)
expect_eq(w[1], SES_LEFT)
}
test "the roster fills at twelve" {
ply_init()
for i in 0 .. PLY_MAX - 1 { expect(ply_join(10 + i) > 0) }
expect_eq(ply_join(99), -1)
expect_eq(ply_count(), PLY_MAX)
}
test "the nearest player, whoever it is" {
ply_init()
me_x = 100.0
let p = ply_join(2)
ply_report(p, 3.0, 0.0, 4.0, 0.0, 1.0)
expect_near(ply_near(0.0, 0.0), 5.0, 0.001)
expect_eq(ply_near_i(), p)
expect_near(ply_dist(0, 100.0, 0.0), 0.0, 0.001)
}
test "roles: alone owns the world, a guest does not, and a change is a fact" {
ply_init()
expect(ses_owns_world())
expect(not ses_live())
expect(ses_night_passes())
ses_set_role(SES_GUEST)
expect(not ses_owns_world())
expect(ses_live())
expect(not ses_night_passes())
let f = last_of(SES_ROLE)
expect_eq(f.role, SES_GUEST)
ses_set_role(SES_HOST)
expect(ses_owns_world())
expect(not ses_night_passes())
}
test "the party's size and door" {
ses_set_party(20, true)
expect_eq(ses_max(), PLY_MAX)
ses_set_party(3, true)
party(1)
expect(ses_room_for_one())
ply_join(9)
expect(not ses_room_for_one())
ses_set_party(12, false)
expect(not ses_room_for_one())
}
test "the night passes when the host and every guest have turned in" {
party(2)
expect(ses_sleep(true))
expect_eq(ses_sleep_waiting(), 2)
ses_sleep_ready(1, true)
expect(last_of(SES_NIGHT) == null)
ses_sleep_ready(2, true)
expect(last_of(SES_NIGHT) != null)
ses_night_passed()
expect(not ses_sleeping())
}
test "a guest's turning in is a fact for the host to hear; alone a bed just sleeps" {
ply_init()
expect(not ses_sleep(true))
ses_set_role(SES_GUEST)
q_clear(ses_facts())
expect(ses_sleep(true))
let f = last_of(SES_SLEEP)
expect(f.yes)
ses_sleep(false)
expect(not ses_sleeping())
}
test "a vote passes on a majority, the host's yes counted" {
party(2)
expect_eq(ses_may(4, 9, "set out"), SES_MAY_ASKED)
expect(ses_vote_live())
expect_eq(ses_may(4, 9, "set out"), SES_MAY_BUSY)
ses_vote_heard(ses_vote_id(), true)
let f = last_of(SES_VOTE_RESOLVED)
expect(f.yes)
expect_eq(f.param, 9)
expect(f.text == "set out")
expect(not ses_vote_live())
}
test "a vote fails on half saying no, or when the time runs out" {
party(1)
ses_vote_open(1, 0, "a")
ses_vote_heard(ses_vote_id(), false)
expect(not last_of(SES_VOTE_RESOLVED).yes)
party(3)
ses_vote_open(1, 0, "b")
ses_vote_tick(31.0)
let f = last_of(SES_VOTE_RESOLVED)
expect(f != null)
expect(not f.yes)
}
test "alone it may go; a guest may not, and its vote goes to the host" {
ply_init()
expect_eq(ses_may(1, 0, "x"), SES_MAY_GO)
ses_set_role(SES_GUEST)
expect_eq(ses_may(1, 0, "x"), SES_MAY_HOSTS)
ses_vote_told(5, 1, "x")
expect(ses_vote_live())
q_clear(ses_facts())
ses_vote_cast(true)
let f = last_of(SES_VOTE_CAST)
expect_eq(f.id, 5)
expect(f.yes)
ses_vote_ended(5, true)
expect_eq(ses_vote_passed(), 1)
}
test "a reset leaves everyone and stands alone" {
party(2)
ses_sleep(true)
ses_reset()
expect_eq(ply_count(), 1)
expect_eq(ses_role(), SES_SOLO)
expect(not ses_sleeping())
}
}