Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
klydra committed Mar 1, 2023
1 parent 693cc61 commit 56a3738
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 28 deletions.
24 changes: 20 additions & 4 deletions server/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,31 @@ func nextPlayer(name string, players []Player, globals Globals) (int, *apis.ApiE

if globals.Direction {
if index == len(players)-1 {
return 0, nil
if players[0].Cards == 0 {
return nextPlayer(players[0].Name, players, globals)
} else {
return 0, nil
}
} else {
return index + 1, nil
if players[index+1].Cards == 0 {
return nextPlayer(players[index+1].Name, players, globals)
} else {
return index + 1, nil
}
}
} else {
if index == 0 {
return len(players) - 1, nil
if players[len(players)-1].Cards == 0 {
return nextPlayer(players[len(players)-1].Name, players, globals)
} else {
return len(players) - 1, nil
}
} else {
return index - 1, nil
if players[index-1].Cards == 0 {
return nextPlayer(players[index-1].Name, players, globals)
} else {
return index - 1, nil
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ func main() {

// Check how many players are still playing
for i := 0; i < len(players); i++ {
if players[0].Cards > 0 {
if players[i].Cards > 0 {
playing++
}
if playing > 1 {
Expand Down Expand Up @@ -1115,7 +1115,7 @@ func main() {

// Check how many players are still playing
for i := 0; i < len(players); i++ {
if players[0].Cards > 0 {
if players[i].Cards > 0 {
playing++
}
if playing > 1 {
Expand Down
10 changes: 5 additions & 5 deletions src/components/game/lobby/LobbyPlayers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { SessionType } from "../../../models/Session";
export default function LobbyPlayers(props: { session: SessionType }) {
return (
<div className="h-full max-h-full max-w-[20rem] flex flex-grow flex-col flex-wrap justify-center items-center gap-4 overflow-hidden">
<Avatar avatar={props.session.me.avatar} />
{props.session.enemies.map(({ avatar }) => (
<Avatar avatar={avatar} />
<Avatar avatar={props.session.me.avatar} name={props.session.me.name} />
{props.session.enemies.map(({ avatar, name }) => (
<Avatar avatar={avatar} name={name} />
))}
</div>
);
}

function Avatar(props: { avatar: string }) {
function Avatar(props: { avatar: string; name: string }) {
return (
<div className="h-16 w-16 rounded-2xl overflow-hidden">
<div className="h-16 w-16 rounded-2xl overflow-hidden" key={props.name}>
<div dangerouslySetInnerHTML={{ __html: props.avatar }}></div>
</div>
);
Expand Down
4 changes: 4 additions & 0 deletions src/components/game/session/SessionRows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ export function EnemyCardRotated() {
</div>
);
}

export function calledAvatar(avatar: string) {
return avatar.replaceAll('fill="#ffffff"', 'fill="gold"');
}
6 changes: 5 additions & 1 deletion src/components/game/session/rows/RowBottom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { showNotification } from "@mantine/notifications";
import { PlayArrow } from "@mui/icons-material";
import CardFront from "../../../card/front/CardFront";
import React from "react";
import { calledAvatar } from "../SessionRows";

export default function RowBottom(props: {
session: SessionType;
Expand Down Expand Up @@ -60,13 +61,16 @@ export default function RowBottom(props: {
<div
className="h-24 w-24 rounded-xl overflow-hidden absolute z-10"
dangerouslySetInnerHTML={{
__html: props.session.me.avatar,
__html: props.session.me.called
? calledAvatar(props.session.me.avatar)
: props.session.me.avatar,
}}
></div>
<div
className="m-2 h-20 w-20 rounded-xl bg-contrast duration-300 absolute animate-ping"
style={{
display: props.session.me.live ? "" : "none",
backgroundColor: props.session.me.called ? "gold" : "",
}}
></div>
</div>
Expand Down
18 changes: 14 additions & 4 deletions src/components/game/session/rows/RowLeft.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { showNotification } from "@mantine/notifications";
import { SwapHoriz } from "@mui/icons-material";
import React from "react";
import { EnemyCardRotated } from "../SessionRows";
import { calledAvatar, EnemyCardRotated } from "../SessionRows";

export default function RowLeft(props: {
session: SessionType;
Expand All @@ -17,7 +17,9 @@ export default function RowLeft(props: {

const full =
props.session.enemies.length === 2 || props.session.enemies.length === 3;
const enemy = full ? props.session.enemies[0] : null;
const enemy = full
? props.session.enemies.find((item) => item.spot === 1)
: null;
const callable = full
? !enemy!.called &&
enemy!.cards < 2 &&
Expand Down Expand Up @@ -67,14 +69,22 @@ export default function RowLeft(props: {
<div
className="h-20 w-20 rounded-xl overflow-hidden absolute z-10 rotate-180"
dangerouslySetInnerHTML={{
__html: enemy!.avatar,
__html: enemy!.called
? calledAvatar(enemy!.avatar)
: enemy!.avatar,
}}
></div>
<div
className="m-2 h-16 w-16 rounded-xl bg-contrast duration-300 absolute animate-ping"
style={{
display: enemy!.live || swapping || callable! ? "" : "none",
backgroundColor: swapping ? "orange" : callable! ? "red" : "",
backgroundColor: enemy!.called
? "gold"
: swapping
? "orange"
: callable!
? "red"
: "",
}}
></div>
</div>
Expand Down
20 changes: 16 additions & 4 deletions src/components/game/session/rows/RowRight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { showNotification } from "@mantine/notifications";
import { SwapHoriz } from "@mui/icons-material";
import React from "react";
import { EnemyCardRotated } from "../SessionRows";
import { calledAvatar, EnemyCardRotated } from "../SessionRows";

export default function RowRight(props: {
session: SessionType;
Expand All @@ -17,7 +17,11 @@ export default function RowRight(props: {

const full =
props.session.enemies.length === 2 || props.session.enemies.length === 3;
const enemy = full ? props.session.enemies[1] : null;
const enemy = full
? props.session.enemies.find(
(item) => item.spot === (props.session.enemies.length === 2 ? 2 : 3)
)
: null;
const callable = full
? !enemy!.called &&
enemy!.cards < 2 &&
Expand Down Expand Up @@ -80,14 +84,22 @@ export default function RowRight(props: {
<div
className="h-20 w-20 rounded-xl overflow-hidden absolute z-10 rotate-180"
dangerouslySetInnerHTML={{
__html: enemy!.avatar,
__html: enemy!.called
? calledAvatar(enemy!.avatar)
: enemy!.avatar,
}}
></div>
<div
className="m-2 h-16 w-16 rounded-xl bg-contrast duration-300 absolute animate-ping"
style={{
display: enemy!.live || swapping || callable! ? "" : "none",
backgroundColor: swapping ? "orange" : callable! ? "red" : "",
backgroundColor: enemy!.called
? "gold"
: swapping
? "orange"
: callable!
? "red"
: "",
}}
></div>
</div>
Expand Down
18 changes: 14 additions & 4 deletions src/components/game/session/rows/RowTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import {
import { showNotification } from "@mantine/notifications";
import { SwapHoriz } from "@mui/icons-material";
import React from "react";
import { EnemyCard } from "../SessionRows";
import { calledAvatar, EnemyCard } from "../SessionRows";

export default function RowTop(props: { session: SessionType; scale: number }) {
const swapping = props.session.me.live && props.session.globals.swapping;

const full =
props.session.enemies.length === 1 || props.session.enemies.length === 3;
const enemy = full
? props.session.enemies[props.session.enemies.length === 1 ? 0 : 2]
? props.session.enemies.find(
(item) => item.spot === (props.session.enemies.length === 1 ? 1 : 2)
)
: null;
const callable = full
? !enemy!.called &&
Expand Down Expand Up @@ -65,14 +67,22 @@ export default function RowTop(props: { session: SessionType; scale: number }) {
<div
className="h-20 w-20 rounded-xl overflow-hidden absolute z-10"
dangerouslySetInnerHTML={{
__html: enemy!.avatar,
__html: enemy!.called
? calledAvatar(enemy!.avatar)
: enemy!.avatar,
}}
></div>
<div
className="m-2 h-16 w-16 rounded-xl bg-contrast duration-300 absolute animate-ping"
style={{
display: enemy!.live || swapping || callable! ? "" : "none",
backgroundColor: swapping ? "orange" : callable! ? "red" : "",
backgroundColor: enemy!.called
? "gold"
: swapping
? "orange"
: callable!
? "red"
: "",
}}
></div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/models/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export type CardType = {
};

function codeToFace(card: string) {
if (card.length <= 0) return CardFace.NUMBER_0;

switch (card.charAt(0)) {
default:
case "0":
Expand Down Expand Up @@ -74,6 +76,8 @@ function codeToFace(card: string) {
}

function codeToColor(card: string) {
if (card.length <= 1) return CardColor.DARK;

switch (card.charAt(1)) {
default:
case "d":
Expand Down Expand Up @@ -103,7 +107,7 @@ export function codeToType(card: string) {
return {
face,
color,
order: Object.keys(CardColor).length * colorOrder + faceOrder,
order: Object.keys(CardFace).length * colorOrder + faceOrder,
} as CardType;
}

Expand Down
9 changes: 6 additions & 3 deletions src/models/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export type SessionMe = {
called: boolean;
host: boolean;
live: boolean;
spot: number;
};

export type SessionEnemy = {
Expand All @@ -35,6 +34,7 @@ function avatar(name: string) {

export function sessionConstruct(player: PlayerType, game: GameType) {
const me = game.players.find((item) => item.name === player.name);
const offset = game.players.findIndex((item) => item.name === me!.name);
const enemies = game.players.filter((item) => item.name !== me!.name);

return {
Expand All @@ -45,16 +45,19 @@ export function sessionConstruct(player: PlayerType, game: GameType) {
called: me!.called ?? false,
host: game.players[0].name === player.name,
live: me!.name === game.globals.live,
spot: game.players.findIndex((item) => item.name === me!.name),
},
enemies: enemies.map((enemy) => {
let spot =
game.players.findIndex((item) => item.name === enemy.name) - offset;
spot = spot > 0 ? spot : spot + game.players.length + offset - 1;

return {
name: enemy.name,
avatar: avatar(enemy.name),
cards: enemy.cards,
called: enemy.called,
live: enemy.name === game.globals.live,
spot: game.players.findIndex((item) => item.name === enemy.name),
spot,
};
}),
order: game.players.map((item) => item.name),
Expand Down
16 changes: 16 additions & 0 deletions src/pages/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ export default class Game extends Component<GameProps, GameState> {
color: "violet",
icon: <PlayArrow />,
});

if (
this.state.game &&
prevState.game &&
this.state.game.globals.live === "" &&
prevState.game.globals.live !== ""
)
showNotification({
autoClose: API_NOTIFICATION_NOTICE_TIMEOUT,
message:
"The game has ended. Congratulations to the winner" +
(this.state.game.rules.king ? "" : "s") +
"!",
color: "violet",
icon: <PlayArrow />,
});
}

render() {
Expand Down

0 comments on commit 56a3738

Please sign in to comment.