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

77
backend/test-client.sh Executable file
View file

@ -0,0 +1,77 @@
#!/bin/bash
# UDP test client with interactive key controls
# Connects to game server and displays responses
HOST="localhost"
SERVER_PORT=9999
LISTEN_PORT=12345
echo "=========================================="
echo " Game Test Client"
echo "=========================================="
echo ""
echo "Controls:"
echo " 1 = REGISTER (join game)"
echo " 2 = Send position update (100, 200)"
echo " 3 = Send position update (300, 150)"
echo " q = quit"
echo ""
echo "Listening for server responses on port $LISTEN_PORT..."
echo "=========================================="
echo ""
# Create named pipe for responses
RESPONSE_PIPE=$(mktemp -d)/response
mkfifo "$RESPONSE_PIPE"
# Start UDP listener in background, output to FIFO
nc -u -l 127.0.0.1 $LISTEN_PORT > "$RESPONSE_PIPE" 2>/dev/null &
LISTENER_PID=$!
# Read responses in background and display them
(while IFS= read -r line; do
echo "[SERVER] $line"
done < "$RESPONSE_PIPE") &
READER_PID=$!
# Cleanup on exit
cleanup() {
kill $LISTENER_PID $READER_PID 2>/dev/null
rm -rf "$(dirname "$RESPONSE_PIPE")"
echo ""
echo "Disconnected."
}
trap cleanup EXIT
# Interactive input loop
echo -n "> "
while IFS= read -rsn1 key; do
case "$key" in
1)
echo ""
echo "[CLIENT] Sending: REGISTER"
echo -n "REGISTER" | nc -u -w1 -p $LISTEN_PORT "$HOST" "$SERVER_PORT" 2>/dev/null
echo -n "> "
;;
2)
echo ""
echo "[CLIENT] Sending: POS#100#200"
echo -n "POS#100#200" | nc -u -w1 -p $LISTEN_PORT "$HOST" "$SERVER_PORT" 2>/dev/null
echo -n "> "
;;
3)
echo ""
echo "[CLIENT] Sending: POS#300#150"
echo -n "POS#300#150" | nc -u -w1 -p $LISTEN_PORT "$HOST" "$SERVER_PORT" 2>/dev/null
echo -n "> "
;;
q|Q)
echo ""
break
;;
*)
# Don't print anything for invalid keys, just show prompt again
;;
esac
done

View file

@ -1,48 +1,61 @@
#!/bin/bash #!/bin/bash
# Simple UDP test script for the game server # Simple UDP test script for the game server
# Sends registration and position updates to simulate two players # Sends registration from two players on specific ports and receives responses
HOST="localhost" HOST="localhost"
PORT=9999 SERVER_PORT=9999
echo "Testing game server at $HOST:$PORT" echo "Testing game server at $HOST:$SERVER_PORT"
echo "" RESPONSE1=$(mktemp)
# RESPONSE2=$(mktemp)
# Helper to send UDP packet echo "=== Listener on port 12345 ==="
send_udp() { nc -u -l 127.0.0.1 12345 > "$RESPONSE1" &
local msg="$1" PID1=$!
echo "Sending: $msg" sleep 3
echo -n "$msg" | nc -u -w1 "$HOST" "$PORT"
}
echo "=== Registering Player 1 ===" trap "rm -f $RESPONSE1 $RESPONSE2; jobs -p | xargs kill -9 2>/dev/null" EXIT
send_udp "REGISTER" # echo ""
nc -u -l 0.0.0.0 12345
# # Create temp files for responses
# RESPONSE1=$(mktemp)
# RESPONSE2=$(mktemp)
# # Cleanup on exit
# trap "rm -f $RESPONSE1 $RESPONSE2; jobs -p | xargs kill -9 2>/dev/null" EXIT
# echo "=== Starting Player 1 listener on port 12345 ==="
# nc -u -l 127.0.0.1 12345 > "$RESPONSE1" 2>/dev/null &
# PID1=$!
# sleep 0.3
# echo "=== Starting Player 2 listener on port 12346 ==="
# nc -u -l 127.0.0.1 12346 > "$RESPONSE2" 2>/dev/null &
# PID2=$!
# sleep 0.3
# echo ""
# echo "=== Sending REGISTER from Player 1 (port 12345) ==="
# echo -n "REGISTER" | nc -u -w1 -p 12345 "$HOST" "$SERVER_PORT"
# sleep 0.5 # sleep 0.5
# echo "" # echo "=== Sending REGISTER from Player 2 (port 12346) ==="
# echo "=== Registering Player 2 ===" # echo -n "REGISTER" | nc -u -w1 -p 12346 "$HOST" "$SERVER_PORT"
# send_udp "register:2" # sleep 1
# sleep 0.5
# echo "" # echo ""
# echo "=== Player 1 moving around ===" # echo "=== Stopping listeners ==="
# for i in {1..3}; do # kill $PID1 $PID2 2>/dev/null
# x=$((100 + i * 10)) # sleep 0.2
# y=$((200 + i * 5))
# send_udp "1:$x:$y"
# sleep 0.3
# done
# echo "" # echo ""
# echo "=== Player 2 moving around ===" # echo "=== Results ==="
# for i in {1..3}; do # echo "Player 1 response:"
# x=$((300 - i * 10)) # cat "$RESPONSE1"
# y=$((150 + i * 20)) # echo ""
# send_udp "2:$x:$y" # echo "Player 2 response:"
# sleep 0.3 # cat "$RESPONSE2"
# done
echo "" # echo ""
echo "Done!" echo "Done!"