fable/val Public Public: anyone can read this; nobody can change it.

forked from benjivers/val

workbenchthis machine
Files 03302de7b925b15e6e7d266e9a2ca7c5b4a55296
1
/**
2
 * The view angle belongs to the browser between snapshots, and the server
3
 * gets it back only when it has something to say that we did not.
4
 */
5
6
import { expect, test } from "bun:test";
7
import { makeYaw, settle, shortWay, told, turn, SENT_TOLERANCE } from "../static/yaw.js";
8
9
const me = (angle: number, extra: Partial<{ id: string; alive: boolean }> = {}) => ({
10
  id: "p1",
11
  alive: true,
12
  angle,
13
  ...extra,
14
});
15
16
// The server rounds an angle to a tenth of a radian on the way out.
17
const echoed = (angle: number) => Math.round(angle * 10) / 10;
18
19
test("the first snapshot is where you are looking: there is no view of your own yet", () => {
20
  const yaw = makeYaw();
21
  expect(yaw.ready).toBe(false);
22
  expect(settle(yaw, me(1.4), 10)).toBeCloseTo(1.4, 6);
23
  expect(yaw.ready).toBe(true);
24
});
25
26
test("a snapshot does not drag the camera back to the angle it was told", () => {
27
  const yaw = makeYaw();
28
  settle(yaw, me(0), 10);
29
  // A flick: the mouse moves at frame rate, and every angle is announced.
30
  for (let i = 1; i <= 10; i++) {
31
    turn(yaw, 0.12);
32
    told(yaw, yaw.angle, 10 + i * 0.016);
33
  }
34
  expect(yaw.angle).toBeCloseTo(1.2, 6);
35
  // The snapshot is the angle the server last heard — four frames ago.
36
  settle(yaw, me(echoed(0.72)), 10.17);
37
  expect(yaw.angle).toBeCloseTo(1.2, 6);
38
});
39
40
test("an angle we never sent is a correction, and is taken", () => {
41
  const yaw = makeYaw();
42
  settle(yaw, me(0), 10);
43
  told(yaw, 0, 10);
44
  turn(yaw, 0.5);
45
  told(yaw, 0.5, 10.05);
46
  // The round starts and the match points everybody at the site.
47
  settle(yaw, me(-2.1), 10.1);
48
  expect(yaw.angle).toBeCloseTo(-2.1, 6);
49
});
50
51
test("an echo just inside the rounding is ours, one just outside is not", () => {
52
  const near = makeYaw();
53
  settle(near, me(0), 10);
54
  told(near, 0.5, 10);
55
  turn(near, 0.4);
56
  settle(near, me(0.5 + SENT_TOLERANCE / 2), 10.05);
57
  expect(near.angle).toBeCloseTo(0.4, 6);
58
59
  const far = makeYaw();
60
  settle(far, me(0), 10);
61
  told(far, 0.5, 10);
62
  turn(far, 0.4);
63
  settle(far, me(0.5 + SENT_TOLERANCE * 2), 10.05);
64
  expect(far.angle).toBeCloseTo(0.5 + SENT_TOLERANCE * 2, 6);
65
});
66
67
test("an echo from a second ago has expired: only recent angles are ours", () => {
68
  const yaw = makeYaw();
69
  settle(yaw, me(0), 10);
70
  told(yaw, 0.5, 10);
71
  turn(yaw, 0.4);
72
  settle(yaw, me(0.5), 12);
73
  expect(yaw.angle).toBeCloseTo(0.5, 6);
74
});
75
76
test("a client that has not spoken yet keeps what it has", () => {
77
  const yaw = makeYaw();
78
  settle(yaw, me(0), 10);
79
  turn(yaw, 0.8);
80
  // No input has been sent between the two snapshots; the server can only be
81
  // repeating what it already knew, so there is nothing here to take.
82
  settle(yaw, me(0), 10.03);
83
  expect(yaw.angle).toBeCloseTo(0.8, 6);
84
});
85
86
test("a respawn and a seat change both point you where the server points you", () => {
87
  const respawn = makeYaw();
88
  settle(respawn, me(0, { alive: false }), 10);
89
  told(respawn, 0, 10);
90
  turn(respawn, 1);
91
  settle(respawn, me(0, { alive: true }), 10.1);
92
  expect(respawn.angle).toBeCloseTo(0, 6);
93
94
  const seat = makeYaw();
95
  settle(seat, me(0), 10);
96
  told(seat, 0, 10);
97
  turn(seat, 1);
98
  settle(seat, { id: "p2", alive: true, angle: 0 }, 10.1);
99
  expect(seat.angle).toBeCloseTo(0, 6);
100
});
101
102
test("the wrap is taken the short way round", () => {
103
  expect(shortWay(3.0, -3.0)).toBeCloseTo(0.2831853, 6);
104
  expect(shortWay(-3.0, 3.0)).toBeCloseTo(-0.2831853, 6);
105
  // Which means an echo either side of π is still ours.
106
  const yaw = makeYaw();
107
  settle(yaw, me(0), 10);
108
  told(yaw, Math.PI - 0.01, 10);
109
  turn(yaw, 0.3);
110
  settle(yaw, me(-Math.PI + 0.01), 10.05);
111
  expect(yaw.angle).toBeCloseTo(0.3, 6);
112
});
113
114
test("the sent log does not grow without bound", () => {
115
  const yaw = makeYaw();
116
  for (let i = 0; i < 500; i++) told(yaw, i * 0.01, 10 + i * 0.05);
117
  expect(yaw.sent.length).toBeLessThan(30);
118
});
↑↓ navigate ↵ open esc close
↑↓ navigate ↵ open esc close

Keyboard shortcuts