Skip to content

Commit ca2ced3

Browse files
committed
feat: add sounds to game
1 parent fc071f3 commit ca2ced3

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

components/Bet.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export function Bet({
147147
title='APOSTAR'
148148
onPress={() => handleBet(bet)}
149149
loading={betting}
150+
disabled={loading}
150151
/>
151152
</View>
152153

components/Table/TablePlayer.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface ITablePlayer {
1212
name: string | null;
1313
bet: string;
1414
dealer: boolean;
15+
current: boolean;
1516
}
1617

1718
interface TablePlayerProps {
@@ -37,6 +38,10 @@ const styles = StyleSheet.create({
3738
height: 40,
3839
borderRadius: 20,
3940
},
41+
current: {
42+
borderWidth: 2,
43+
borderColor: Colors.dark.success,
44+
},
4045
livesIndicator: {
4146
left: -5,
4247
bottom: 15,
@@ -131,7 +136,10 @@ function PlayerInfo({ player, isVertical }: PlayerInfoProps) {
131136
<ThemedView style={getFlexStyle(isVertical)}>
132137
<ThemedView>
133138
{player.playerPicture && (
134-
<Image source={{ uri: player.playerPicture }} style={styles.image} />
139+
<Image
140+
source={{ uri: player.playerPicture }}
141+
style={[styles.image, player.current && styles.current]}
142+
/>
135143
)}
136144

137145
<ThemedView style={styles.livesIndicator}>

components/Table/TableSeat.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function TableSeat({
7171
playerPicture,
7272
lives: player.lives,
7373
dealer: player.dealer,
74+
current: player.current,
7475
number,
7576
}}
7677
/>

hooks/useGame.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {
55
dealCardsMutation,
66
updateGame,
77
betMutation,
8-
playMutation,
98
getTrumps,
109
finishRoundMutation,
10+
usePlayMutation,
1111
} from '@/services/game';
1212
import { type Bet, type GamePlayer } from '@/types';
1313
import { NotificationFeedbackType } from 'expo-haptics';
@@ -25,7 +25,7 @@ export const useGame = (matchId: string) => {
2525
finishRoundMutation(session?.access_token || ''),
2626
);
2727
const { mutate: mutatePlay } = useMutation(
28-
playMutation(session?.access_token || ''),
28+
usePlayMutation(session?.access_token || ''),
2929
);
3030
const { mutate: mutateBet } = useMutation(betMutation());
3131
const [dealing, setDealing] = useState<boolean>(false);
@@ -113,9 +113,10 @@ export const useGame = (matchId: string) => {
113113
async (id: string) => {
114114
if (me?.current) {
115115
setPlaying(true);
116+
playSoundAsync({ type: 'card', volume: getVolume('effects') });
117+
116118
mutatePlay(id, {
117119
onSuccess: () => {
118-
playSoundAsync({ type: 'card', volume: getVolume('effects') });
119120
notification(NotificationFeedbackType.Success);
120121
setPlaying(false);
121122
},

services/game.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { type BetInsert, type Deck, type Game } from '@/types';
22
import { supabase } from '@/providers/supabase';
3+
import { UseMutationOptions, useQueryClient } from '@tanstack/react-query';
34
import { api } from './api';
45

5-
export const gameKey = (gameId: string) => {
6-
return ['game', gameId];
6+
export const gameKey = () => {
7+
return ['game'];
78
};
89

910
export const trumpKey = (gameId: string, roundNumber: number) => {
@@ -12,7 +13,7 @@ export const trumpKey = (gameId: string, roundNumber: number) => {
1213

1314
export const updateGame = (gameId: string, token: string) => {
1415
return {
15-
queryKey: gameKey(gameId),
16+
queryKey: gameKey(),
1617
queryFn: async (): Promise<Game> => {
1718
if (!gameId) return {} as Game;
1819

@@ -77,7 +78,11 @@ export const finishRoundMutation = (token: string) => {
7778
};
7879
};
7980

80-
export const playMutation = (token: string) => {
81+
export const usePlayMutation = (
82+
token: string,
83+
): UseMutationOptions<void, Error, string, unknown> => {
84+
const queryClient = useQueryClient();
85+
8186
return {
8287
mutationFn: async (id: string): Promise<void> => {
8388
try {
@@ -96,6 +101,45 @@ export const playMutation = (token: string) => {
96101
// ignore error
97102
}
98103
},
104+
onMutate: async (id: string) => {
105+
await queryClient.cancelQueries({ queryKey: gameKey() });
106+
107+
const previousGame = queryClient.getQueryData<Game>(gameKey());
108+
109+
if (previousGame) {
110+
const turn =
111+
previousGame.player_cards
112+
.sort((a, b) => a.turn - b.turn)
113+
.findLast((c) => c.turn)?.turn || 1;
114+
115+
const currentCard = previousGame.player_cards.find((c) => c.id === id);
116+
117+
const updatedGame = {
118+
...previousGame,
119+
player_cards: previousGame.player_cards.map((card) => {
120+
if (card.id === id) return { ...card, status: 'on table', turn };
121+
122+
if (
123+
card.user_id === currentCard?.user_id &&
124+
card.status === 'on table'
125+
)
126+
return { ...card, status: 'played', turn };
127+
128+
return card;
129+
}),
130+
};
131+
132+
queryClient.setQueryData(gameKey(), updatedGame);
133+
}
134+
135+
return previousGame;
136+
},
137+
onError: (err, newTodo, context) => {
138+
queryClient.setQueryData(gameKey(), context);
139+
},
140+
onSettled: () => {
141+
queryClient.invalidateQueries({ queryKey: gameKey() });
142+
},
99143
};
100144
};
101145

0 commit comments

Comments
 (0)