get join-game logic working

This commit is contained in:
Travis Shears 2026-04-16 22:33:33 +02:00
parent 09bf48481a
commit 32ea10495e
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469
3 changed files with 145 additions and 49 deletions

View file

@ -65,6 +65,7 @@
[player-id message]
(if-let [player (get @players player-id)]
(try
(t/log! {:level :info :data {:player-id player-id :message message}} "Sending to player")
(s/put! @server-socket
{:host (:host player)
:port (:port player)
@ -74,28 +75,33 @@
(t/log! {:level :warn :data {:player-id player-id}} "Player not found")))
(defn join-game! [player-id]
(let [waiting-game
(let [existing-game (get-in @games [:by-id player-id])
waiting-game
(some-> (first (filter (fn [game] (nil? (:player-2 game))) (@games :all)))
(assoc :player-2 player-id))
other-player-id (:player-1 waiting-game)
new-game {:player-1 player-id :player-2 nil}]
(if waiting-game
(do
(swap! games (fn [state]
{:by-id
(-> (:by-id state)
(assoc player-id waiting-game)
(assoc other-player-id waiting-game))
:all (map (fn [game] (if (= (:player-1 game) other-player-id)
waiting-game
game)) (:all state))}))
(send-to-player other-player-id "READY_TO_PLAY")
(cond
existing-game
(if (nil? (:player-2 existing-game))
(send-to-player player-id "WAIT")
(send-to-player player-id "READY_TO_PLAY"))
(do
(swap! games (fn [state]
{:by-id (assoc (:by-id state) player-id new-game)
:all (conj (:all state) new-game)}))
(send-to-player player-id "WAIT")))))
waiting-game (do
(swap! games (fn [state]
{:by-id
(-> (:by-id state)
(assoc player-id waiting-game)
(assoc other-player-id waiting-game))
:all (map (fn [game] (if (= (:player-1 game) other-player-id)
waiting-game
game)) (:all state))}))
(send-to-player other-player-id "READY_TO_PLAY")
(send-to-player player-id "READY_TO_PLAY"))
:else (do
(swap! games (fn [state]
{:by-id (assoc (:by-id state) player-id new-game)
:all (conj (:all state) new-game)}))
(send-to-player player-id "WAIT")))))
(defn register-player! [host port]
(let [player-id (md5 (str host port))]