Refactor player command processing.
This commit is contained in:
@@ -488,6 +488,13 @@ img.card:hover {
|
||||
document.querySelectorAll(".supply-area .supply-pile")[index].classList.toggle("selected");
|
||||
}
|
||||
|
||||
function send_command(command, data) {
|
||||
var payload = data || {};
|
||||
payload.type = command;
|
||||
var msg = { type: "Command", "command": payload };
|
||||
webSocket.send(JSON.stringify(msg));
|
||||
}
|
||||
|
||||
function dialog_confirm(ev) {
|
||||
if (resolve_request.request.type == "GainCard") {
|
||||
var selected = document.querySelector(".supply-pile.selected");
|
||||
@@ -501,7 +508,7 @@ img.card:hover {
|
||||
|
||||
var reply = { type: "SupplyCardChosen", choice: selected };
|
||||
var msg = { type: "ResolveReply", reply: reply };
|
||||
webSocket.send(JSON.stringify(msg));
|
||||
send_command("ResolveReply", msg);
|
||||
enable_supply_selection = false;
|
||||
} else {
|
||||
var selected = [];
|
||||
@@ -513,7 +520,7 @@ img.card:hover {
|
||||
|
||||
var reply = { type: "HandCardsChosen", choice: selected };
|
||||
var msg = { type: "ResolveReply", reply: reply };
|
||||
webSocket.send(JSON.stringify(msg));
|
||||
send_command("ResolveReply", msg);
|
||||
enable_hand_selection = false;
|
||||
}
|
||||
document.querySelector("#dialog").style.visibility = "hidden";
|
||||
@@ -533,20 +540,15 @@ img.card:hover {
|
||||
|
||||
function handle_dnd(data) {
|
||||
if (data.source == "Hand" && data.dest == "InPlay") {
|
||||
var msg = { type: "PlayCard", name: "", index: data.index};
|
||||
webSocket.send(JSON.stringify(msg));
|
||||
send_command("PlayCard", { index: data.index });
|
||||
} else if (data.source == "Hand" && data.dest == "Discard") {
|
||||
var msg = { type: "Discard", index: data.index};
|
||||
webSocket.send(JSON.stringify(msg));
|
||||
send_command("Discard", { index: data.index });
|
||||
} else if (data.source == "Supply" && data.dest == "Discard") {
|
||||
var msg = { type: "GainCard", name: data.name, index: parseInt(data.index) };
|
||||
webSocket.send(JSON.stringify(msg));
|
||||
send_command("GainCard", { index: parseInt(data.index)});
|
||||
} else if (data.source == "DrawPile" && data.dest == "Hand") {
|
||||
var msg = { type: "DrawCard" };
|
||||
webSocket.send(JSON.stringify(msg));
|
||||
send_command("DrawCard", null);
|
||||
} else if (data.source == "Hand" && data.dest == "Trash") {
|
||||
var msg = { type: "TrashHand", index: data.index };
|
||||
webSocket.send(JSON.stringify(msg));
|
||||
send_command("TrashHand", { index: data.index });
|
||||
} else {
|
||||
console.log("handle_dnd: unhandled data", data);
|
||||
}
|
||||
@@ -600,12 +602,7 @@ img.card:hover {
|
||||
}
|
||||
|
||||
var doubleclick = function(ev) {
|
||||
let msg = {
|
||||
type: "BuyCard",
|
||||
index: parseInt(ev.srcElement.parentElement.dataset.index),
|
||||
}
|
||||
|
||||
webSocket.send(JSON.stringify(msg));
|
||||
send_command("BuyCard", { index: parseInt(ev.srcElement.parentElement.dataset.index) });
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -856,8 +853,7 @@ img.card:hover {
|
||||
|
||||
function PlayerArea(initialVnode) {
|
||||
var end_turn_click = function(e) {
|
||||
var msg = { type: "EndTurn" };
|
||||
webSocket.send(JSON.stringify(msg));
|
||||
send_command("EndTurn", null);
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -931,10 +927,9 @@ img.card:hover {
|
||||
|
||||
function SetupScreen(initialVnode) {
|
||||
var start_click = function(e) {
|
||||
let msg = { type: "StartGame" };
|
||||
let msg = { type: "Command", command: { type: "StartGame" }};
|
||||
initialVnode.attrs.socket.send(JSON.stringify(msg));
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
view: function(vnode) {
|
||||
@@ -1061,12 +1056,6 @@ img.card:hover {
|
||||
setup_state.active = false;
|
||||
game_state.active = true;
|
||||
}
|
||||
|
||||
if (last_player != game_state.active_player) {
|
||||
if (game_state.active_player == my_player_id) {
|
||||
turnStartSound.play();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var handle_resolve_request = function(request) {
|
||||
@@ -1099,6 +1088,44 @@ img.card:hover {
|
||||
modal.style.display = "block";
|
||||
m.mount(modal, EndScreen);
|
||||
}
|
||||
|
||||
var handle_notification = function(msg) {
|
||||
if (msg.event.type == "CardPlayed") {
|
||||
append_chat(player_name(msg.event.player) + " plays " + msg.event.name);
|
||||
} else if (msg.event.type == "CardBought") {
|
||||
append_chat(player_name(msg.event.player) + " buys " + msg.event.name);
|
||||
} else if (msg.event.type == "CardGained") {
|
||||
let card_name = game_state.supply[msg.event.index].name;
|
||||
append_chat(player_name(msg.event.player) + " gains " + card_name);
|
||||
} else if (msg.event.type == "CardDiscarded") {
|
||||
append_chat(player_name(msg.event.player) + " discards a card.");
|
||||
} else if (msg.event.type == "CardTrashed") {
|
||||
append_chat(player_name(msg.event.player) + " trashes " + msg.event.name);
|
||||
} else if (msg.event.type == "TurnStarted") {
|
||||
if (msg.event.player == my_player_id) {
|
||||
turnStartSound.play();
|
||||
}
|
||||
} else {
|
||||
console.log(msg);
|
||||
}
|
||||
}
|
||||
|
||||
var player_name = function(index) {
|
||||
return game_state.players[index].name;
|
||||
}
|
||||
|
||||
var append_chat = function(text) {
|
||||
let chatDiv = document.getElementById("chat");
|
||||
let last_element = document.querySelector("#chat li:last-child");
|
||||
if (last_element.innerText == text) {
|
||||
last_element.dataset.repeat = (parseInt(last_element.dataset.repeat || 1)) + 1;
|
||||
} else {
|
||||
let newmsg = document.createElement("li");
|
||||
newmsg.innerHTML = text;
|
||||
chatDiv.append(newmsg);
|
||||
newmsg.scrollIntoView();
|
||||
}
|
||||
}
|
||||
|
||||
webSocket.onopen = function(event) {
|
||||
console.log("ws open");
|
||||
@@ -1114,17 +1141,8 @@ img.card:hover {
|
||||
chatDiv.append(newmsg);
|
||||
newmsg.scrollIntoView();
|
||||
} else if (msg.type == "Notification") {
|
||||
let chatDiv = document.getElementById("chat");
|
||||
let last_element = document.querySelector("#chat li:last-child");
|
||||
if (last_element.innerText == msg.text) {
|
||||
last_element.dataset.repeat = (parseInt(last_element.dataset.repeat || 1)) + 1;
|
||||
} else {
|
||||
let newmsg = document.createElement("li");
|
||||
newmsg.innerHTML = msg.text;
|
||||
chatDiv.append(newmsg);
|
||||
newmsg.scrollIntoView();
|
||||
}
|
||||
} else if (msg.type == "PlayerJoined") {
|
||||
handle_notification(msg);
|
||||
} else if (msg.type == "PlayerJoined") {
|
||||
let chatDiv = document.getElementById("chat");
|
||||
let newmsg = document.createElement("li");
|
||||
newmsg.innerHTML = msg.player + " joined the game.";
|
||||
|
Reference in New Issue
Block a user