+import { card_name, pretty_suit } from "./card.js";
+
export class Chatroom {
constructor(container, summary, actions, username, send) {
this.container = container;
this.chatroom_input.value = "";
};
this.chatroom.append(this.chatroom_input);
- const close = document.createElement("button");
- close.classList.add("chatroom-close");
- close.innerText = "✗";
- close.onclick = () => this.send({type: "TakeAction", action: {action: "Leave"}});
- this.chatroom.append(close);
this.container.append(this.chatroom);
for (const user_action of this.actions) {
}
}
+ set_error_text(text) {
+ // TODO
+ }
+
take_action(user_action, initialising) {
const is_at_end = this.chat.scrollTop + this.chat.clientHeight == this.chat.scrollHeight;
switch (user_action.action.action) {
case "Join":
this.seats.set(user_action.username, user_action.action.seat);
- this.chat.append(this.join_element(user_action.username));
+ this.chat.append(this.join_element(user_action.username, "Joined"));
break;
case "Message":
this.chat.append(this.message_element(user_action.username, user_action.action.message));
break;
+ case "PostBlind":
+ if (user_action.action.chips === 0) {
+ this.chat.append(this.info_element(user_action.username, "Posts ghost blind"));
+ } else {
+ this.chat.append(this.info_element(user_action.username, "Posts blind of " + user_action.action.chips));
+ }
+ break;
+ case "Bet":
+ this.chat.append(this.info_element(user_action.username, "Bets " + user_action.action.chips));
+ break;
+ case "Fold":
+ this.chat.append(this.info_element(user_action.username, "Folds"));
+ break;
+ case "TimeoutFold":
+ this.chat.append(this.info_element(user_action.username, "Folds due to timeout"));
+ break;
+ case "NextToDeal":
+ this.chat.append(this.info_element(user_action.username, "Next to deal"));
+ break;
+ case "ReceiveCard":
+ if (user_action.action.card === null) {
+ this.chat.append(this.info_element(user_action.username, "Receives card"));
+ } else {
+ this.chat.append(this.info_element(user_action.username, "Receives " + card_name(user_action.action.card)));
+ }
+ break;
+ case "CommunityCard":
+ this.chat.append(this.info_element(user_action.username, "Deals " + card_name(user_action.action.card)));
+ break;
+ case "EndDeal":
+ this.chat.append(this.info_element(user_action.username, "Ends deal"));
+ break;
+ case "RevealCard":
+ this.chat.append(this.info_element(user_action.username, "Reveals " + card_name(user_action.action.card)));
+ case "PlayCard":
+ this.chat.append(this.info_element(user_action.username, "Plays " + card_name(user_action.action.card)));
+ break;
+ case "CutCard":
+ this.chat.append(this.info_element(user_action.username, "Cuts " + card_name(user_action.action.card)));
+ break;
+ case "ChooseTrumps":
+ this.chat.append(this.info_element(user_action.username, "Chooses " + pretty_suit(user_action.action.suit) + " as trumps"));
+ break;
+ case "WinTrick":
+ this.chat.append(this.info_element(user_action.username, "Wins trick"));
+ break;
+ case "WinCall":
+ this.chat.append(this.info_element(user_action.username, "Wins the call"));
+ break;
+ case "WinHand":
+ this.chat.append(this.info_element(
+ user_action.username,
+ "Wins " + user_action.action.chips + " chips" +
+ (user_action.action.hand === null ? "" : " with " + user_action.action.hand)
+ ));
+ break;
+ case "WinGame":
+ this.chat.append(this.win_game_element(user_action.username, "Wins the game"));
+ break;
+ case "KnockedOut":
+ this.chat.append(this.leave_element(user_action.username, "Knocked out"));
+ break;
case "Leave":
this.seats.delete(user_action.username);
- this.chat.append(this.leave_element(user_action.username));
+ this.chat.append(this.leave_element(user_action.username, "Left"));
if (!initialising && user_action.username === this.username) {
this.send({type: "JoinLobby", filter: ""});
}
}
}
- join_element(username) {
- const join_element = document.createElement("div");
- join_element.classList.add("chatroom-join");
+ chat_element(message_class, username, message) {
+ const chat_element = document.createElement("div");
+ chat_element.classList.add(message_class);
const username_element = document.createElement("div");
username_element.classList.add("chatroom-username");
username_element.innerText = username;
- join_element.append(username_element);
+ chat_element.append(username_element);
const text_element = document.createElement("div");
text_element.classList.add("chatroom-text");
- text_element.innerText = "Joined";
- join_element.append(text_element);
- return join_element;
+ text_element.innerText = message;
+ chat_element.append(text_element);
+ return chat_element;
+ }
+
+ join_element(username, message) {
+ return this.chat_element("chatroom-join", username, message);
}
message_element(username, message) {
- const message_element = document.createElement("div");
- message_element.classList.add("chatroom-message");
- const username_element = document.createElement("div");
- username_element.classList.add("chatroom-username");
- username_element.innerText = username;
- message_element.append(username_element);
- const text_element = document.createElement("div");
- text_element.classList.add("chatroom-text");
- text_element.innerText = message;
- message_element.append(text_element);
- return message_element;
+ return this.chat_element("chatroom-message", username, message);
}
- leave_element(username) {
- const leave_element = document.createElement("div");
- leave_element.classList.add("chatroom-leave");
- const username_element = document.createElement("div");
- username_element.classList.add("chatroom-username");
- username_element.innerText = username;
- leave_element.append(username_element);
- const text_element = document.createElement("div");
- text_element.classList.add("chatroom-text");
- text_element.innerText = "Left";
- leave_element.append(text_element);
- return leave_element;
+ info_element(username, message) {
+ return this.chat_element("chatroom-info", username, message);
+ }
+
+ win_game_element(username, message) {
+ return this.chat_element("chatroom-win-game", username, message);
+ }
+
+ leave_element(username, message) {
+ return this.chat_element("chatroom-leave", username, message);
}
}
this.bet_size_area.setAttribute("width", "38");
this.bet_size_area.setAttribute("height", "16");
this.bet_size_area.classList.add("bet-size-area");
+ this.bet_size_area.onclick = () => {
+ const chips = prompt("Chips to bet", this.bet_size_text.textContent);
+ if (chips !== null && !isNaN(Number(chips))) {
+ const bet = Math.max(this.min_bet(), Math.min(this.all_in_bet(), Number(chips)));
+ this.bet_size_text.textContent = Number(chips);
+ const p = (bet - this.min_bet()) / (this.all_in_bet() - this.min_bet());
+ const x = Math.max(360, Math.min(475, Math.sqrt(p) * 115 + 360));
+ this.bet_slider_thumb.setAttribute("x", x);
+ }
+ };
this.svg.append(this.bet_size_area);
this.bet_size = document.createElementNS(svgns, "text");
this.fold_control.classList.toggle("active", this.active === this.username && this.chips_to_call() > 0);
this.call_control.classList.toggle("active", this.active === this.username);
this.call_control_text.textContent = this.active === this.username && this.chips_to_call() == 0 ? "Check" : "Call";
- this.bet_control.classList.toggle("active", this.active === this.username);
- this.bet_size_area.classList.toggle("active", this.active === this.username);
- this.bet_slider.classList.toggle("active", this.active === this.username);
+ const can_bet = this.active === this.username && this.chips_to_call() < this.all_in_bet();
+ this.bet_control.classList.toggle("active", can_bet);
+ this.bet_size_area.classList.toggle("active", can_bet);
+ this.bet_slider.classList.toggle("active", can_bet);
this.bet_size_text.textContent = this.min_bet();
this.bet_slider_thumb.setAttribute("x", 360);
for (const [username, [user, stack, active, bet]] of this.user_icons) {
}
min_bet() {
- return this.chips_to_call(); /* + this.min_raise() TODO */
+ return Math.min(this.all_in_bet(), this.chips_to_call() + this.min_raise());
}
all_in_bet() {
this.socket.onmessage = (msg) => this.onmessage(msg);
this.socket.onclose = () => this.onclose();
this.state = "Connecting";
+ this.last_filter = "";
this.scheduled_actions = [];
this.schedule_timeout = null;
this.state = "Connected";
break;
case "LoginSuccess":
+ case "LeaveLobbySuccess":
this.hide_login();
this.game = new MainMenu(this.container, message => this.send(message));
+ this.container.append(this.close_button());
this.state = "LoggedIn";
break;
case "JoinLobbyFailure":
this.state = "LoggedIn";
break;
case "JoinLobbySuccess":
+ this.last_filter = message.filter;
this.game = new GameList(this.container, message.filter, message.games, message => this.send(message))
+ this.container.append(this.close_button());
this.state = "InLobby";
break;
case "NewGame":
};
}
+ close_button() {
+ const close = document.createElement("button");
+ close.classList.add("game-close");
+ close.innerText = "✗";
+ close.onclick = () => {
+ switch (this.state) {
+ case "LoggedIn":
+ this.send({type: "Logout"});
+ break;
+ case "InLobby":
+ this.send({type: "LeaveLobby"});
+ break;
+ case "InGame":
+ this.send({type: "TakeAction", action: {action: "Leave"}});
+ this.send({type: "LeaveGame"});
+ this.send({type: "JoinLobby", filter: this.last_filter});
+ break;
+ }
+ };
+ return close;
+ }
+
create_game_display(summary, actions) {
this.container.textContent = "";
let Format;
return;
}
this.game = new Format(this.container, summary, actions, this.auth.username, message => this.send(message));
+ this.container.append(this.close_button());
}
onclose() {