Skip to content

Commit

Permalink
Merge pull request #90 from daithihearn/auto-action3
Browse files Browse the repository at this point in the history
More autoplay fixes
  • Loading branch information
daithihearn authored Jan 19, 2023
2 parents 7ef1b8d + 80dae78 commit c675ca4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 60 deletions.
24 changes: 14 additions & 10 deletions src/components/Game/WebsocketManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useCallback, useEffect, useState } from "react"

import { StompSessionProvider, useSubscription } from "react-stomp-hooks"
import { useAppDispatch, useAppSelector } from "../../caches/hooks"
import { getGameId, updateGame } from "../../caches/GameSlice"
import { getGameId, getIsMyGo, updateGame } from "../../caches/GameSlice"
import { getAccessToken } from "../../caches/MyProfileSlice"
import { getPlayerProfiles } from "../../caches/PlayerProfilesSlice"
import { GameState } from "../../model/Game"
Expand Down Expand Up @@ -51,6 +51,7 @@ const WebsocketHandler = () => {
const dispatch = useAppDispatch()

const [autoActionEnabled, setAutoActionEnabled] = useState(false)
const isMyGo = useAppSelector(getIsMyGo)
const playerProfiles = useAppSelector(getPlayerProfiles)
const { enqueueSnackbar } = useSnackbar()

Expand All @@ -60,7 +61,7 @@ const WebsocketHandler = () => {
useEffect(() => {
setTimeout(() => {
if (!autoActionEnabled) setAutoActionEnabled(true)
}, 4000)
}, 2000)
}, [autoActionEnabled])

const handleWebsocketMessage = useCallback(
Expand Down Expand Up @@ -93,10 +94,13 @@ const WebsocketHandler = () => {
if (!autoActionEnabled) setAutoActionEnabled(true)
}

const reloadCards = (payload: unknown) => {
dispatch(clearSelectedCards())
dispatch(clearAutoPlay())
const reloadCards = (payload: unknown, clearSelected = false) => {
const gameState = payload as GameState

if (clearSelected) {
dispatch(clearSelectedCards())
dispatch(clearAutoPlay())
}
dispatch(updateMyCards(gameState.cards))
}

Expand All @@ -105,14 +109,14 @@ const WebsocketHandler = () => {
switch (type) {
case "DEAL":
shuffleSound()
reloadCards(payload)
reloadCards(payload, true)
break
case "CHOOSE_FROM_DUMMY":
case "BUY_CARDS":
case "LAST_CARD_PLAYED":
case "CARD_PLAYED":
playCardSound()
reloadCards(payload)
reloadCards(payload, isMyGo)
break
case "REPLAY":
break
Expand All @@ -139,15 +143,15 @@ const WebsocketHandler = () => {
break
case "CALL":
callSound()
reloadCards(payload)
reloadCards(payload, true)
break
case "PASS":
passSound()
reloadCards(payload)
reloadCards(payload, true)
break
}
},
[playerProfiles],
[playerProfiles, isMyGo],
)

useSubscription(["/game", "/user/game"], message =>
Expand Down
107 changes: 57 additions & 50 deletions src/components/Leaderboard/SinglesLeaderboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,56 +62,63 @@ const SinglesLeaderboard = () => {
return leaderboardData
}, [playerProfiles, game, getProfile, previousHand])

const columns: TableColumn<LeaderboardItem>[] = [
{
name: "Avatar",
cell: row => (
<img alt="Image Preview" src={row.picture} className="avatar" />
),
},
{ name: "Player", selector: row => row.name, sortable: true },
{
name: "Score",
selector: row => row.score,
sortable: true,
center: true,
},
{
name: "Rings",
selector: row => row.rings,
sortable: true,
center: true,
},
{
name: "Bought",
selector: row => row.cardsBought,
sortable: true,
center: true,
omit: gameOver,
},
{
cell: row => (
<div>
{row.winner ? (
<img src={TrophyImage} width="50px" height="50px" />
) : null}
</div>
),
center: true,
omit: !gameOver,
},
{
cell: row => (
<img
alt={row.previousCard}
src={"/cards/thumbnails/" + row.previousCard + ".png"}
className="thumbnail_size_small cardNotSelected"
/>
),
center: true,
omit: gameOver || !previousHand,
},
]
const columns: TableColumn<LeaderboardItem>[] = useMemo(
() => [
{
name: "Avatar",
cell: row => (
<img
alt="Image Preview"
src={row.picture}
className="avatar"
/>
),
},
{ name: "Player", selector: row => row.name, sortable: true },
{
name: "Score",
selector: row => row.score,
sortable: true,
center: true,
},
{
name: "Rings",
selector: row => row.rings,
sortable: true,
center: true,
},
{
name: "Bought",
selector: row => row.cardsBought,
sortable: true,
center: true,
omit: gameOver,
},
{
cell: row => (
<div>
{row.winner ? (
<img src={TrophyImage} width="50px" height="50px" />
) : null}
</div>
),
center: true,
omit: !gameOver,
},
{
cell: row => (
<img
alt={row.previousCard}
src={"/cards/thumbnails/" + row.previousCard + ".png"}
className="thumbnail_size_small cardNotSelected"
/>
),
center: true,
omit: gameOver || !previousHand,
},
],
[gameOver, previousHand],
)

if (
!game ||
Expand Down

0 comments on commit c675ca4

Please sign in to comment.