Skip to content
Snippets Groups Projects

Some changes

Merged Max Wehmeier requested to merge some-changes into custom-server
All threads resolved!
+ 52
8
import SkribblClient from "../logic/SkribblClient.js";
import {CustomElement} from "../util/Util.js";
//TODO try and make SkribblMessage and SkribblGuess into one class
//TODO cosmetics (make the chat look good)
//TODO clean up code
/**
* Custom element `<skribbl-chat>` for the chat used to guess words and see others guesses.
* @todo better scroll functionality (stay stickied to bottom unless specifically scrolled up, subtle css effects where stuff is hidden from view)
* @todo allow cycling through previous guesses using the arrow keys, like in a terminal
* @todo write to the chat which player is drawing now
*/
@@ -73,24 +75,58 @@ export default class SkribblChat extends CustomElement {
});
client.onGuess(guessData=>{
let playerName = this._client.players.value[guessData.player].name;
this._messageDiv.appendChild(new SkribblGuess(playerName,guessData));
let skribblGuess = new SkribblGuess(playerName,guessData,client);
let shouldScroll = !(this._messageDiv.scrollHeight > this._messageDiv.scrollTop + this._messageDiv.clientHeight);
this._messageDiv.appendChild(skribblGuess);
if (shouldScroll) {
this._messageDiv.scrollTop = this._messageDiv.scrollHeight - this._messageDiv.clientHeight;
}
},{onlyWhen:this.connected});
client.onWordReveal(message=>{
// TODO reveal the word in the chat
this._messageDiv.appendChild(new SkribblMessage(1,message.word));
},{onlyWhen:this.connected});
//TODO Message which player is drawing now
}
}
export class SkribblMessage extends HTMLElement {
/**
* @param {number} type
* @param {string} identifier
*/
constructor(type,identifier) {
super();
this.attachShadow({mode:"open"});
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
}
</style>
<b id="player_name"></b><span id="message"></span><b id="word_reveal"></b>
`;
if (type == 1) { //Type 1 is a word reveal
this.shadowRoot.querySelector("#message").textContent = "The word was ";
this.shadowRoot.querySelector("#word_reveal").textContent = "'" + identifier + "'";
this.style.color = "#00bf00";
} else if (type == 2) { //Type 2 is a new drawer
this.shadowRoot.querySelector("#player_name").textContent = "'" + identifier + "'";
this.shadowRoot.querySelector("#message").textContent = " is drawing now!";
this.style.color = "#00bf00";
}
}
}
/**
* Custom element `<skribbl-guess>` that appears in the chat whenever someone tries to guess the word.
* @todo display message when the word was close
* @todo refer to the player with "you" when he guesses the word, not using his name
*/
export class SkribblGuess extends HTMLElement {
/**
* @param {string} playerName
* @param {import("../logic/SkribblClient.js").GuessResponse} guess
* @param {SkribblClient} client
*/
constructor(playerName,guess){
constructor(playerName,guess,client){
super();
this.attachShadow({mode:"open"});
this.shadowRoot.innerHTML = `
@@ -104,10 +140,17 @@ export class SkribblGuess extends HTMLElement {
`;
this._playerName = playerName;
this._guess = guess;
this._client = client;
this.shadowRoot.querySelector("#player_name").textContent = playerName;
// seems unnecessary, I know. but for some reason IntelliSense won't correctly infer the type in the else block without the `==true`
if (guess.correct==true){
this.shadowRoot.querySelector("#message").textContent = " has guessed the word!";
if (this._guess.player == this._client.state.value.playerIndex) {
this.shadowRoot.querySelector("#player_name").textContent = "";
this.shadowRoot.querySelector("#message").textContent = "You have guessed the word!"
} else {
this.shadowRoot.querySelector("#message").textContent = " has guessed the word!";
}
this.style.color = "#00bf00";
}else{
this.shadowRoot.querySelector("#message").textContent = `: ${guess.word}`;
@@ -130,4 +173,5 @@ export class SkribblGuess extends HTMLElement {
}
}
customElements.define("skribbl-chat",SkribblChat);
customElements.define("skribbl-guess",SkribblGuess);
\ No newline at end of file
customElements.define("skribbl-guess",SkribblGuess);
customElements.define("skribbl-message",SkribblMessage);
\ No newline at end of file