BlueGit

projects-timfalken-com meteors

feat: expose shot_flights so every client can draw others' arcs (t1125)

recordShots includes from_id. viewFor returns recent shots in the
flight window with a server-computed age, on both me and poll.

timfa Grok Bot · pushed by timfa

Authored 2026-11-13 · 4 days ago

Hash
2559ff6033adc37609eb5f0e19a0f089e2c76f65
Parent
fa6526a9
2 files changed +49 −1
server/api.php +3 −1
@@ -17,7 +17,7 @@ try {
17 17 $user = currentUser();
18 18
19 19 if ($action === 'me' || $action === '') {
20 $view = ['lobby' => null, 'incoming' => [], 'shot_results' => []];
20 $view = ['lobby' => null, 'incoming' => [], 'shot_results' => [], 'shot_flights' => []];
21 21 if ($user) {
22 22 $view = Store::open()->viewFor($user['id']);
23 23 }
@@ -30,6 +30,7 @@ try {
30 30 'lobby' => $view['lobby'],
31 31 'incoming' => $view['incoming'],
32 32 'shot_results' => $view['shot_results'],
33 'shot_flights' => $view['shot_flights'] ?? [],
33 34 'miss_chance_default' => METEORS_DEFAULT_MISS,
34 35 'flight_duration' => METEORS_FLIGHT_DURATION,
35 36 'login_url' => 'auth.php',
@@ -103,6 +104,7 @@ try {
103 104 'lobby' => $view['lobby'],
104 105 'incoming' => $view['incoming'],
105 106 'shot_results' => $view['shot_results'],
107 'shot_flights' => $view['shot_flights'] ?? [],
106 108 ]);
107 109
108 110 default:
@@ -17,7 +17,7 @@ try {
17 $user = currentUser();
17 $user = currentUser();
18
18
19 if ($action === 'me' || $action === '') {
19 if ($action === 'me' || $action === '') {
20 $view = ['lobby' => null, 'incoming' => [], 'shot_results' => []];
20 $view = ['lobby' => null, 'incoming' => [], 'shot_results' => [], 'shot_flights' => []];
21 if ($user) {
21 if ($user) {
22 $view = Store::open()->viewFor($user['id']);
22 $view = Store::open()->viewFor($user['id']);
23 }
23 }
@@ -30,6 +30,7 @@ try {
30 'lobby' => $view['lobby'],
30 'lobby' => $view['lobby'],
31 'incoming' => $view['incoming'],
31 'incoming' => $view['incoming'],
32 'shot_results' => $view['shot_results'],
32 'shot_results' => $view['shot_results'],
33 'shot_flights' => $view['shot_flights'] ?? [],
33 'miss_chance_default' => METEORS_DEFAULT_MISS,
34 'miss_chance_default' => METEORS_DEFAULT_MISS,
34 'flight_duration' => METEORS_FLIGHT_DURATION,
35 'flight_duration' => METEORS_FLIGHT_DURATION,
35 'login_url' => 'auth.php',
36 'login_url' => 'auth.php',
@@ -103,6 +104,7 @@ try {
103 'lobby' => $view['lobby'],
104 'lobby' => $view['lobby'],
104 'incoming' => $view['incoming'],
105 'incoming' => $view['incoming'],
105 'shot_results' => $view['shot_results'],
106 'shot_results' => $view['shot_results'],
107 'shot_flights' => $view['shot_flights'] ?? [],
106 ]);
108 ]);
107
109
108 default:
110 default:
server/lib/Store.php +46 −0
@@ -413,12 +413,15 @@ SQL);
413 413 $results[] = [
414 414 'id' => (int) $this->pdo->lastInsertId(),
415 415 'client_id' => $clientId,
416 'from_id' => $userId,
417 'from_user' => $userId,
416 418 'miss' => $miss,
417 419 'target_id' => $to,
418 420 'target_name' => $toName,
419 421 'deliver_at' => $deliver,
420 422 'color' => $color,
421 423 'flight_duration' => $flight,
424 'age' => 0.0,
422 425 ];
423 426 }
424 427 return $results;
@@ -568,6 +571,43 @@ SQL);
568 571 $this->pdo->prepare('DELETE FROM shots WHERE lobby_id = ?')->execute([$lobbyId]);
569 572 }
570 573
574 /**
575 * Shots still (or just) in flight, for every client in the lobby (t1125).
576 * `age` is seconds since `created`, so a late poll can join mid-arc.
577 * Window is flight_duration + 0.8 s (one poll slack).
578 *
579 * @return list<array>
580 */
581 public function recentFlights(string $lobbyId, ?float $flightDuration = null): array
582 {
583 $flight = $flightDuration !== null ? (float) $flightDuration : (float) METEORS_FLIGHT_DURATION;
584 $now = $this->nowf();
585 $window = $flight + 0.8;
586 $stmt = $this->pdo->prepare(
587 'SELECT * FROM shots
588 WHERE lobby_id = ? AND created >= ?
589 ORDER BY id ASC'
590 );
591 $stmt->execute([$lobbyId, $now - $window]);
592 $out = [];
593 foreach ($stmt->fetchAll() as $r) {
594 $to = $r['to_user'];
595 $out[] = [
596 'id' => (int) $r['id'],
597 'client_id' => $r['client_id'],
598 'from_id' => $r['from_user'],
599 'from_user' => $r['from_user'],
600 'target_id' => $to,
601 'miss' => $to === null || $to === '',
602 'color' => (int) $r['color'],
603 'flight_duration' => $flight,
604 'age' => $now - (float) $r['created'],
605 'deliver_at' => (float) $r['deliver_at'],
606 ];
607 }
608 return $out;
609 }
610
571 611 /** Public view of a lobby for the given user, including incoming shots. */
572 612 public function viewFor(string $userId, ?array $snapshot = null, ?int $score = null, bool $eliminated = false, array $outgoing = []): array
573 613 {
@@ -595,16 +635,22 @@ SQL);
595 635 'lobby' => null,
596 636 'incoming' => [],
597 637 'shot_results' => $shotResults,
638 'shot_flights' => [],
598 639 ];
599 640 }
600 641 $incoming = [];
601 642 if ($lobby['status'] === 'playing') {
602 643 $incoming = $this->takeIncoming($lobby['id'], $userId);
603 644 }
645 $flights = [];
646 if ($lobby['status'] === 'playing') {
647 $flights = $this->recentFlights($lobby['id'], (float) $lobby['flight_duration']);
648 }
604 649 return [
605 650 'lobby' => $this->presentLobby($lobby, $userId),
606 651 'incoming' => $incoming,
607 652 'shot_results' => $shotResults,
653 'shot_flights' => $flights,
608 654 ];
609 655 });
610 656 }
@@ -413,12 +413,15 @@ SQL);
413 $results[] = [
413 $results[] = [
414 'id' => (int) $this->pdo->lastInsertId(),
414 'id' => (int) $this->pdo->lastInsertId(),
415 'client_id' => $clientId,
415 'client_id' => $clientId,
416 'from_id' => $userId,
417 'from_user' => $userId,
416 'miss' => $miss,
418 'miss' => $miss,
417 'target_id' => $to,
419 'target_id' => $to,
418 'target_name' => $toName,
420 'target_name' => $toName,
419 'deliver_at' => $deliver,
421 'deliver_at' => $deliver,
420 'color' => $color,
422 'color' => $color,
421 'flight_duration' => $flight,
423 'flight_duration' => $flight,
424 'age' => 0.0,
422 ];
425 ];
423 }
426 }
424 return $results;
427 return $results;
@@ -568,6 +571,43 @@ SQL);
568 $this->pdo->prepare('DELETE FROM shots WHERE lobby_id = ?')->execute([$lobbyId]);
571 $this->pdo->prepare('DELETE FROM shots WHERE lobby_id = ?')->execute([$lobbyId]);
569 }
572 }
570
573
574 /**
575 * Shots still (or just) in flight, for every client in the lobby (t1125).
576 * `age` is seconds since `created`, so a late poll can join mid-arc.
577 * Window is flight_duration + 0.8 s (one poll slack).
578 *
579 * @return list<array>
580 */
581 public function recentFlights(string $lobbyId, ?float $flightDuration = null): array
582 {
583 $flight = $flightDuration !== null ? (float) $flightDuration : (float) METEORS_FLIGHT_DURATION;
584 $now = $this->nowf();
585 $window = $flight + 0.8;
586 $stmt = $this->pdo->prepare(
587 'SELECT * FROM shots
588 WHERE lobby_id = ? AND created >= ?
589 ORDER BY id ASC'
590 );
591 $stmt->execute([$lobbyId, $now - $window]);
592 $out = [];
593 foreach ($stmt->fetchAll() as $r) {
594 $to = $r['to_user'];
595 $out[] = [
596 'id' => (int) $r['id'],
597 'client_id' => $r['client_id'],
598 'from_id' => $r['from_user'],
599 'from_user' => $r['from_user'],
600 'target_id' => $to,
601 'miss' => $to === null || $to === '',
602 'color' => (int) $r['color'],
603 'flight_duration' => $flight,
604 'age' => $now - (float) $r['created'],
605 'deliver_at' => (float) $r['deliver_at'],
606 ];
607 }
608 return $out;
609 }
610
571 /** Public view of a lobby for the given user, including incoming shots. */
611 /** Public view of a lobby for the given user, including incoming shots. */
572 public function viewFor(string $userId, ?array $snapshot = null, ?int $score = null, bool $eliminated = false, array $outgoing = []): array
612 public function viewFor(string $userId, ?array $snapshot = null, ?int $score = null, bool $eliminated = false, array $outgoing = []): array
573 {
613 {
@@ -595,16 +635,22 @@ SQL);
595 'lobby' => null,
635 'lobby' => null,
596 'incoming' => [],
636 'incoming' => [],
597 'shot_results' => $shotResults,
637 'shot_results' => $shotResults,
638 'shot_flights' => [],
598 ];
639 ];
599 }
640 }
600 $incoming = [];
641 $incoming = [];
601 if ($lobby['status'] === 'playing') {
642 if ($lobby['status'] === 'playing') {
602 $incoming = $this->takeIncoming($lobby['id'], $userId);
643 $incoming = $this->takeIncoming($lobby['id'], $userId);
603 }
644 }
645 $flights = [];
646 if ($lobby['status'] === 'playing') {
647 $flights = $this->recentFlights($lobby['id'], (float) $lobby['flight_duration']);
648 }
604 return [
649 return [
605 'lobby' => $this->presentLobby($lobby, $userId),
650 'lobby' => $this->presentLobby($lobby, $userId),
606 'incoming' => $incoming,
651 'incoming' => $incoming,
607 'shot_results' => $shotResults,
652 'shot_results' => $shotResults,
653 'shot_flights' => $flights,
608 ];
654 ];
609 });
655 });
610 }
656 }