Skip to content

Commit

Permalink
Fixed czar related issues
Browse files Browse the repository at this point in the history
Worked on sidebar
  • Loading branch information
F1nnM committed Mar 28, 2021
1 parent b0d9d19 commit 9fd918f
Show file tree
Hide file tree
Showing 15 changed files with 551 additions and 483 deletions.
932 changes: 466 additions & 466 deletions backend/src/cards.ts

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions backend/src/rooms/GameRoom/GameRoomState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ export class Player extends Schema {
@type("boolean")
isOwner: boolean = false;

@type("boolean")
played: boolean = false;

timesCzar: number = 0;

@type("string")
id: string;
}

Expand All @@ -88,6 +92,9 @@ export class GameRoomState extends Schema {
@type("boolean")
gameRunning: boolean = false;

@type("string")
roundWinner: string;

@type("string")
winner: string;

Expand Down
4 changes: 3 additions & 1 deletion backend/src/rooms/GameRoom/commands/CzarVoteCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ export class CzarVoteCommand extends Command<GameRoomState, {sessionId: string,
this.state.cardsPlayed[index].chosenByCzar = true;
this.state.czarDidVote = true;
let winnerId: string = this.state.cardsPlayed[index].playedBy;
this.state.roundWinner = winnerId;
let winner = this.state.players.get(winnerId);
winner.score ++;
if (winner.score >= this.state.pointsToWin)
return [new DisplayWinnerCommand().setPayload({wait: 5000, winnerId: winnerId})]
else
else{
return [new NewRoundCommand().setPayload({wait: 5000})]
}

}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/rooms/GameRoom/commands/FinishRoundCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class FinishRoundCommand extends Command<GameRoomState> {
this.state.players.forEach((player, id) => {
if (!player.connected || player.isCzar)
return
everybodyOnlinePlayed = everybodyOnlinePlayed && ( this.state.cardsPlayed.some(cardStack => (cardStack.playedBy === id && cardStack.cards.length === this.state.blackCard.blanks)) || player.cards.length === 0);
everybodyOnlinePlayed = everybodyOnlinePlayed && player.played;
})
return everybodyOnlinePlayed
}
Expand Down
3 changes: 0 additions & 3 deletions backend/src/rooms/GameRoom/commands/NewOwnerCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ export class NewOwnerCommand extends Command<GameRoomState, {lastOwnerId: string
execute({lastOwnerId} = this.payload) {
if(this.state.players.get(lastOwnerId).connected) // owner returned
return;
console.log("didn't return, searching new")
let newOwner: string;
this.state.players.forEach((player, id) => {
console.log(id +" : "+ player.connected)
if(player.connected)
newOwner = id
})
console.log(newOwner)
if (newOwner){
console.log("found new owner: "+newOwner)
this.state.players.get(newOwner).isOwner = true;
this.state.players.get(lastOwnerId).isOwner = false;
}
Expand Down
4 changes: 4 additions & 0 deletions backend/src/rooms/GameRoom/commands/NewRoundCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class NewRoundCommand extends Command<GameRoomState, {wait: number}> {
this.state.czarsTurn = false;
this.state.czarDidVote = false;

this.state.roundWinner = null;

//determine new card czar. Not allowed to be the same as last round
var possibleCzars: Player[] = [];

Expand All @@ -30,6 +32,8 @@ export class NewRoundCommand extends Command<GameRoomState, {wait: number}> {
player.isCzar = false;

minTimesCzar = Math.min(minTimesCzar, player.timesCzar);

player.played = false;
})

possibleCzars = possibleCzars.filter(player => player.timesCzar === minTimesCzar);
Expand Down
1 change: 1 addition & 0 deletions backend/src/rooms/GameRoom/commands/PlayCardCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class PlayCardCommand extends Command<GameRoomState, {sessionId: string,
card.mark = handCard.mark;
playedCardStack.cards.push(card)
this.state.players.get(sessionId).cards.deleteAt(index);
this.state.players.get(sessionId).played = true;

return [new FinishRoundCommand()]
}
Expand Down
7 changes: 4 additions & 3 deletions backend/src/rooms/GameRoom/commands/ReplaceHandCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ export class ReplaceHandCommand extends Command<GameRoomState, {sessionId: strin

validate({sessionId} = this.payload){
return this.state.gameRunning && !this.state.players.get(sessionId).isCzar &&
!this.state.czarsTurn && !this.state.cardsPlayed.some(cardStack => cardStack.playedBy == sessionId)
!this.state.czarsTurn && !this.state.cardsPlayed.some(cardStack => cardStack.playedBy == sessionId);
}

execute({sessionId} = this.payload) {
this.state.players.get(sessionId).cards.clear()
return [new FinishRoundCommand()]
this.state.players.get(sessionId).cards.clear();
this.state.players.get(sessionId).played = true;
return [new FinishRoundCommand()];
}

}
2 changes: 2 additions & 0 deletions frontend/src/components/Card.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
outline: none;
transition-duration: .1s;
overflow-wrap: break-word;
word-break: break-word;
word-wrap: break-word;
hyphens: auto;
}
.card.white {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import './Card.css'
export class Card extends React.Component {

render() {
let text = this.props.text? this.props.text.replace('[[BLANK]]', '________'):null
let text = this.props.text? this.props.text.replaceAll('[[BLANK]]', '____'):null
return (
<button className={`card aspect-ratio-box ${this.props.type} ${this.props.chosen ? 'chosen' : ''} ${this.props.sticky ? 'sticky' : ''}`} onClick={this.props.onclick}>
<div className='aspect-ratio-box-inner'>
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/components/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ class Game extends React.Component {

let playedCards = this.state.game.winner ? [{cards: [{content: this.state.game.players[this.state.game.winner].name, mark: "WIN"}], chosenByCzar: true}] : this.state.game.cardsPlayed

console.log("Winners")
console.log(this.state.game.winner)
console.log(this.state.game.roundWinner)

return (

<CardArea
Expand All @@ -156,9 +160,9 @@ class Game extends React.Component {
whiteCardClicked={ index => this.playCard(this, index)}
playedCards={playedCards}
playedCardsClicked={index => this.czarVote(this, index)}
sidebar={<Scoreboard players={Array.from(this.state.game.players.values())}/>}
sidebar={<Scoreboard players={Array.from(this.state.game.players.values())} gameRunning={this.state.game.gameRunning} winner={this.state.game.winner || this.state.game.roundWinner}/>}
trustPlayedCards={!this.state.game.winner}
additionalSpace={<button hidden={!this.state.game.gameRunning} className='replaceHandButton' onClick={_=>this.replaceHand()} disabled={this.state.game.czarsTurn || player.isCzar || player.cards.length == 0}>Replace hand instead of playing a card</button>}/>
additionalSpace={<button hidden={!this.state.game.gameRunning} className='replaceHandButton' onClick={_=>this.replaceHand()} disabled={this.state.game.czarsTurn || player.isCzar || player.cards.length === 0}>Replace hand instead of playing a card</button>}/>
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Header extends React.Component {
<div className="header">
{this.props.showHome &&
<div className="homeContainer" onClick={_ => this.props.history.push("/")}>
<img className="home" src={home}></img>
<img alt="Home" className="home" src={home}></img>
</div>
}
<span>Yet another <a href="https://cardsagainsthumanity.com/">Cards against Humanity</a> clone.</span>
Expand Down
16 changes: 13 additions & 3 deletions frontend/src/components/MainMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class MainMenu extends React.Component {

constructor(props) {
super(props);
this.state = { name: localStorage.getItem("name") || "Anonymous"};
this.state = { name: localStorage.getItem("name") || "Anonymous", displayGreeting: false};
}

whiteCardClicked(me, index){
Expand Down Expand Up @@ -41,16 +41,26 @@ class MainMenu extends React.Component {
}
}

saveNameToState(me, name) {
me.setState({...this.state, name: name});
}

saveName(me) {
me.setState({...this.state, displayGreeting: true});
localStorage.setItem("name", this.state.name)
}

render() {

let whiteCards = [{content: "Join a public game"}, {content: "Join a private game"}, {content: "Create a new game"}]
let whiteCards = [{content: "Join a public ga fa a conversationist ga"}, {content: "Join a private game"}, {content: "Create a new game"}]
if (localStorage.getItem("roomId"))
whiteCards.push({content: "Rejoin the last game (might be closed already)"})

let sidebar = (
<div>
<span>Set your name:</span>
<input value={this.state.name} onChange={e => {this.setState({...this.state, name: e.target.value})}}/><button onClick={_=>localStorage.setItem("name", this.state.name)}>Set</button>
<input value={this.state.name} onChange={e => {this.saveNameToState(this, e.target.value)}}/><button onClick={_=>this.saveName(this)}>Set</button>
{this.state.displayGreeting && <span className="greeting">Hi, {this.state.name}!</span>}
</div>
)

Expand Down
17 changes: 17 additions & 0 deletions frontend/src/components/Scoreboard.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
.scoreboard {
display: flex;
flex-direction: column;
}

.player {
padding: 0.5%;
border-bottom: 1px solid black;
}

.player:last-child {
border-bottom: unset;
}

.name {
font-weight: bold;
}

.winner {
background-color: gold;
}
27 changes: 25 additions & 2 deletions frontend/src/components/Scoreboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,36 @@ import React from 'react';
import './Scoreboard.css'
export default class Scoreboard extends React.Component {

getAnnotation(me, player){
let annotations = ""
if (player.isOwner)
annotations += "(Owner) "
if (!player.connected)
if (player.left)
annotations += "(Left) "
else
annotations += "(Disconnected) "
if (me.props.gameRunning && !player.played && !player.isCzar)
annotations += "Waiting "
if (player.isCzar)
annotations += "Card-Czar "
return annotations
}

render() {
console.log(this.props.players)
console.log(this.props.winner)
return (
<div className="scoreboard">
{this.props.players.map((player, index) => {
return (
<div className="player" key={player.name+index}>{player.name}{player.isOwner?" (Owner)": ""}: {player.score} {!player.connected?player.left?'(Left)':'(Disconnected)':''}</div>
<div className={`player ${(player.id === this.props.winner) ? 'winner': ''}`} key={player.name+index}>
<span className="name">
{player.name}: {player.score}
</span>
<span>
{this.getAnnotation(this, player)}
</span>
</div>
)
})}
</div>
Expand Down

0 comments on commit 9fd918f

Please sign in to comment.