commit 1a453145330647ef67a95bea03f13f022dc8a0c5 (patch)
parent 45f10f5188825f84c08d27a1c8da6e04de022c22
Author: Alex Karle <alex@karle.co>
Date: Sat, 25 Apr 2020 20:29:34 -0400
Euchre::Dealer: Add chat message support
A stateless chat between clients is rather easy to support, if the
clients want to.
Adding server support because I think this is a pretty cool feature for
people who might not have voice chat running!
Diffstat:
2 files changed, 28 insertions(+), 0 deletions(-)
diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm
@@ -141,6 +141,7 @@ sub handle_msg {
order => [\&order, 'vote', "Not time for a vote", 1],
dealer_swap => [\&dealer_swap, 'dealer_swap', "Can't swap with the Kitty now", 1],
play_card => [\&play_card, 'play', "Can't play cards yet", 1],
+ chat => [\&chat],
);
@@ -668,4 +669,20 @@ sub stats {
return $msg;
}
+# Simple stateless broadcast to all clients in game
+sub chat {
+ my ($p, $msg) = @_;
+
+ my $game = $p->{game};
+ for my $player (@{$game->{players}}, @{$game->{spectators}}) {
+ next unless defined $player;
+
+ my $json = {
+ msg_type => 'chat',
+ msg => "$p->{name}: $msg->{msg}"
+ };
+ $player->{ws}->send({ json => $json });
+ }
+}
+
1;
diff --git a/public/debug.html b/public/debug.html
@@ -57,6 +57,8 @@
document.getElementById('game').innerHTML = gameState
} else if (msg.msg_type === 'error') {
document.getElementById('error').innerHTML += msg.msg + '<br>'
+ } else if (msg.msg_type === 'chat') {
+ document.getElementById('chat_box').innerHTML += msg.msg + '<br>'
//} else if (msg.msg_type === 'pong') {
// console.log('pong!');
}
@@ -94,6 +96,10 @@
ws.send(JSON.stringify({action:'play_card', card: card}))
}
}
+ function chat() {
+ ws.send(JSON.stringify({action:'chat', msg: document.getElementById('chat').value }))
+ document.getElementById('chat').value = '';
+ }
window.setInterval(() => { ws.send(JSON.stringify({action:'ping'})) }, 5000);
</script>
@@ -134,5 +140,10 @@
<button onclick="pass()">Pass</button>
<br><br>
<div id="error" style="color:red"> </div>
+ <br><br>
+ <label for="chat">Chat:</label>
+ <input type="text" id="chat">
+ <button onclick="chat()">Send</button>
+ <div id="chat_box"></div>
</body>
</html>