get join-game logic working
This commit is contained in:
parent
09bf48481a
commit
32ea10495e
3 changed files with 145 additions and 49 deletions
|
|
@ -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))]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue