rules.test.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { describe, expect, it } from 'vitest';
  2. import { loadCharacterStats, loadMap } from '../src/server/assets';
  3. import { Game } from '../src/server/game';
  4. import { MatchStats } from '../src/server/stats';
  5. import { Anim } from '../src/shared/characters';
  6. import { CHERRY_REGRAB_DELAY, FINAL_RUSH_S, RESPAWN_DELAY_S, SMARTIES_DURATION_S, TICK_RATE } from '../src/shared/constants';
  7. import { Button } from '../src/shared/input';
  8. import type { FxEvent } from '../src/shared/protocol';
  9. import { ASSETS, realMaterials } from './helpers';
  10. const materials = realMaterials();
  11. function newRound(duration = 180, seed = 1): Game {
  12. const { definition, terrain } = loadMap(ASSETS, 'gateau-etages', materials);
  13. return new Game({ map: definition, terrain, materials, seed, rules: { duration }, loadCharacter: (id) => loadCharacterStats(ASSETS, id) });
  14. }
  15. function run(game: Game, ticks: number): FxEvent[] {
  16. const fx: FxEvent[] = [];
  17. for (let i = 0; i < ticks; i++) fx.push(...game.step().fx);
  18. return fx;
  19. }
  20. const me = (game: Game, id: number) => game.snapshot().find((p) => p.id === id)!;
  21. describe('custard and the spoon', () => {
  22. it('rises during a round, faster in the final rush, and stays put during the warm-up', () => {
  23. const round = newRound(40);
  24. const start = round.custardLevel;
  25. run(round, 5 * TICK_RATE);
  26. const slow = start - round.custardLevel;
  27. expect(slow).toBeCloseTo(round.map.custard.speed * 5, 0);
  28. run(round, (40 - FINAL_RUSH_S) * TICK_RATE); // into the final rush
  29. const before = round.custardLevel;
  30. run(round, 5 * TICK_RATE);
  31. expect(before - round.custardLevel).toBeCloseTo(round.map.custard.finalRushSpeed * 5, 0);
  32. expect(round.roundState().finalRush).toBe(true);
  33. const { definition, terrain } = loadMap(ASSETS, 'gateau-etages', materials);
  34. const warmup = new Game({ map: definition, terrain, materials, seed: 1, loadCharacter: (id) => loadCharacterStats(ASSETS, id) });
  35. run(warmup, 5 * TICK_RATE);
  36. expect(warmup.custardLevel).toBe(definition.custard.startY);
  37. expect(warmup.roundState()).toMatchObject({ timeLeft: null, cherry: null });
  38. });
  39. it('a player falling in is out for 5 s, then the spoon launches them back from open air', () => {
  40. const game = newRound();
  41. const { id } = game.addPlayer('Plouf', 'generic');
  42. game.teleport(id, 100, 900); // left of the cake: nothing below but custard
  43. const fx = run(game, TICK_RATE);
  44. expect(fx).toContainEqual(expect.objectContaining({ kind: 'splash', id }));
  45. expect(me(game, id).anim & Anim.Dead).toBeTruthy();
  46. expect(game.stats.get(id, 'custard')).toBe(1);
  47. const later = run(game, RESPAWN_DELAY_S * TICK_RATE);
  48. const spoon = later.find((e) => e.kind === 'spoon');
  49. expect(spoon).toBeDefined();
  50. const p = me(game, id);
  51. expect(p.anim & Anim.Dead).toBe(0);
  52. expect(p.y).toBeLessThan(game.custardLevel);
  53. // ...and lands on the cake instead of falling straight back in.
  54. const flight = run(game, 3 * TICK_RATE);
  55. expect(flight.filter((e) => e.kind === 'splash' && e.id === id)).toEqual([]);
  56. expect(me(game, id).grounded).toBe(true);
  57. });
  58. it('the spoon lands players back on the cake whatever spawn it rolls (several seeds)', () => {
  59. for (const seed of [1, 2, 3, 4, 5, 6]) {
  60. const game = newRound(180, seed);
  61. const { id } = game.addPlayer('Plouf', 'generic');
  62. game.teleport(id, 100, 900);
  63. run(game, (RESPAWN_DELAY_S + 0.5) * TICK_RATE);
  64. const fx = run(game, 3 * TICK_RATE);
  65. expect(fx.filter((e) => e.kind === 'splash' && e.id === id), `seed ${seed}`).toEqual([]);
  66. }
  67. });
  68. it('the round ends when the timer runs out', () => {
  69. const game = newRound(2);
  70. run(game, 2 * TICK_RATE - 1);
  71. expect(game.roundOver).toBe(false);
  72. run(game, 2);
  73. expect(game.roundOver).toBe(true);
  74. });
  75. });
  76. describe('the cherry', () => {
  77. it('is picked up by touching it and scores 1 point per second', () => {
  78. const game = newRound();
  79. const { id } = game.addPlayer('Gourmand', 'generic');
  80. const [cx, cy] = game.map.cherrySpawn;
  81. run(game, 30); // let the cherry settle on the top tier
  82. const cherry = game.roundState().cherry!;
  83. game.teleport(id, cherry.x, cherry.y - 10);
  84. const fx = run(game, 10);
  85. expect(fx).toContainEqual(expect.objectContaining({ kind: 'pickup', id, item: 'cherry' }));
  86. expect(game.roundState().cherry!.holder).toBe(id);
  87. run(game, 3 * TICK_RATE);
  88. expect(me(game, id).score).toBeGreaterThanOrEqual(3);
  89. expect([cx, cy]).toEqual(game.map.cherrySpawn);
  90. });
  91. it('a hit holder drops it, and cannot grab it back right away', () => {
  92. const game = newRound();
  93. const holder = game.addPlayer('Porteur', 'generic');
  94. const biter = game.addPlayer('Castor', 'castor');
  95. run(game, 30);
  96. const cherry = game.roundState().cherry!;
  97. game.teleport(holder.id, cherry.x, cherry.y - 10);
  98. run(game, 10);
  99. expect(game.roundState().cherry!.holder).toBe(holder.id);
  100. const h = me(game, holder.id);
  101. game.teleport(biter.id, h.x - 45, h.y);
  102. game.setInput(biter.id, { buttons: Button.Attack, aimX: Math.round(h.x), aimY: Math.round(h.y) });
  103. const fx = run(game, 2);
  104. expect(fx).toContainEqual(expect.objectContaining({ kind: 'cherryDrop', id: holder.id }));
  105. expect(game.roundState().cherry!.holder).not.toBe(holder.id);
  106. expect(game.stats.get(biter.id, 'hits')).toBeGreaterThanOrEqual(1);
  107. // Still blocked for the holder for a moment.
  108. game.setInput(biter.id, { buttons: 0, aimX: 0, aimY: 0 });
  109. run(game, Math.floor(CHERRY_REGRAB_DELAY * TICK_RATE * 0.5));
  110. expect(game.roundState().cherry!.holder).not.toBe(holder.id);
  111. });
  112. });
  113. describe('items', () => {
  114. it('a candle is picked up, thrown with Use, and explodes: carves the cake and blows players away', () => {
  115. const game = newRound();
  116. const { id } = game.addPlayer('Artificier', 'generic');
  117. run(game, 90);
  118. const p = me(game, id);
  119. game.spawnItem('candle', p.x, p.y - 30);
  120. const fx = run(game, 20);
  121. expect(fx).toContainEqual(expect.objectContaining({ kind: 'pickup', id, item: 'candle' }));
  122. expect(me(game, id).item).toBe('candle');
  123. const eventsBefore = game.events.length;
  124. game.setInput(id, { buttons: Button.Use, aimX: Math.round(p.x + 60), aimY: Math.round(p.y + 80) });
  125. const after = run(game, 2 * TICK_RATE);
  126. expect(me(game, id).item).toBeNull();
  127. expect(after).toContainEqual(expect.objectContaining({ kind: 'explosion' }));
  128. expect(game.events.length).toBeGreaterThan(eventsBefore);
  129. expect(game.stats.get(id, 'candles')).toBe(1);
  130. });
  131. it('smarties boost the speed for a while', () => {
  132. const game = newRound();
  133. const { id } = game.addPlayer('Speedy', 'generic');
  134. run(game, 90);
  135. const p = me(game, id);
  136. game.spawnItem('smarties', p.x, p.y - 30);
  137. run(game, 20);
  138. expect(me(game, id).anim & Anim.Boosted).toBeTruthy();
  139. run(game, SMARTIES_DURATION_S * TICK_RATE);
  140. expect(me(game, id).anim & Anim.Boosted).toBe(0);
  141. });
  142. it('the electric whisk jolts the others and shakes the jelly', () => {
  143. const game = newRound();
  144. const a = game.addPlayer('Fouet', 'generic');
  145. const b = game.addPlayer('Victime', 'generic');
  146. run(game, 90);
  147. const p = me(game, a.id);
  148. const eventsBefore = game.events.length;
  149. game.spawnItem('whisk', p.x, p.y - 30);
  150. let whisked = false;
  151. for (let i = 0; i < 30 && !whisked; i++) whisked = game.step().fx.some((e) => e.kind === 'whisk' && e.id === a.id);
  152. expect(whisked).toBe(true);
  153. // Same tick: the other player is jolted and stunned.
  154. expect(me(game, b.id).anim & Anim.Stunned).toBeTruthy();
  155. run(game, 2);
  156. expect(game.events.length).toBeGreaterThan(eventsBefore + 5);
  157. expect(game.stats.get(a.id, 'eaten')).toBeGreaterThan(0);
  158. });
  159. it('items fall from the sky on their own during a round (seeded)', () => {
  160. const a = newRound(180, 7);
  161. const b = newRound(180, 7);
  162. // The first item drops within map.items.interval[1] seconds; both games drop the same ones.
  163. let seen = 0;
  164. for (let i = 0; i < a.map.items.interval[1] * TICK_RATE + 2; i++) {
  165. a.step();
  166. b.step();
  167. seen = Math.max(seen, a.itemStates().length);
  168. }
  169. expect(seen).toBeGreaterThan(0);
  170. expect(a.itemStates()).toEqual(b.itemStates());
  171. });
  172. });
  173. describe('trophies', () => {
  174. it('go to the best player of each stat, only if they did it', () => {
  175. const stats = new MatchStats();
  176. stats.add(1, 'eaten', 5000);
  177. stats.add(2, 'eaten', 300);
  178. stats.add(2, 'custard', 3);
  179. stats.add(3, 'crushed', 1);
  180. stats.add(3, 'hitTaken', 4);
  181. const t = stats.trophies([1, 2, 3]);
  182. expect(t).toContainEqual({ id: 'glutton', playerId: 1, value: 5000 });
  183. expect(t).toContainEqual({ id: 'kamikaze', playerId: 2, value: 3 });
  184. expect(t).toContainEqual({ id: 'notMyFault', playerId: 3, value: 5 });
  185. expect(t.find((x) => x.id === 'pyromaniac')).toBeUndefined();
  186. });
  187. });