|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { View, StyleSheet, ActivityIndicator, Text, SafeAreaView, TouchableOpacity } from 'react-native'; |
| 3 | +import { useLocalSearchParams, useRouter } from 'expo-router'; |
| 4 | +import { useSession } from '../../../hooks/use-session'; |
| 5 | +import { useAuth } from '../../../hooks/use-auth'; |
| 6 | +import { AccusationFormScreen } from '../../../features/murder-mystery/screens/AccusationFormScreen'; |
| 7 | +import { AwardsVotingScreen } from '../../../features/murder-mystery/screens/AwardsVotingScreen'; |
| 8 | +import { MurderMysteryData } from '../../../features/murder-mystery/types/murder-mystery'; |
| 9 | +import { supabase } from '../../../lib/supabase'; |
| 10 | + |
| 11 | +export default function MurderMysteryAccusation() { |
| 12 | + const { id } = useLocalSearchParams<{ id: string }>(); |
| 13 | + const router = useRouter(); |
| 14 | + const { getSession, updateSession } = useSession(); |
| 15 | + const { session: authSession } = useAuth(); |
| 16 | + const [scenario, setScenario] = useState<MurderMysteryData | null>(null); |
| 17 | + const [loading, setLoading] = useState(true); |
| 18 | + const [step, setStep] = useState<'accusation' | 'voting' | 'waiting'>('accusation'); |
| 19 | + |
| 20 | + const [hostId, setHostId] = useState<string | null>(null); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + if (!id) return; |
| 24 | + |
| 25 | + const channel = supabase.channel(`public:sessions:id=eq.${id}`) |
| 26 | + .on( |
| 27 | + 'postgres_changes', |
| 28 | + { event: 'UPDATE', schema: 'public', table: 'sessions', filter: `id=eq.${id}` }, |
| 29 | + (payload) => { |
| 30 | + const newConfig = payload.new.config as MurderMysteryData; |
| 31 | + setScenario(newConfig); |
| 32 | + |
| 33 | + if (newConfig?.game_night?.phase === 'EPHEMERA') { |
| 34 | + setTimeout(() => { |
| 35 | + router.push(`/murder-mystery/${id}/ephemera`); |
| 36 | + }, 0); |
| 37 | + } |
| 38 | + } |
| 39 | + ) |
| 40 | + .subscribe(); |
| 41 | + |
| 42 | + return () => { |
| 43 | + supabase.removeChannel(channel); |
| 44 | + }; |
| 45 | + }, [id, router]); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + const fetchSessionData = async () => { |
| 49 | + if (id) { |
| 50 | + const session = await getSession(id); |
| 51 | + if (session) { |
| 52 | + setHostId(session.host_id); |
| 53 | + if (session.config) { |
| 54 | + setScenario(session.config as MurderMysteryData); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + setLoading(false); |
| 59 | + }; |
| 60 | + fetchSessionData(); |
| 61 | + }, [id]); |
| 62 | + |
| 63 | + if (loading || !authSession) { |
| 64 | + return <View style={styles.centered}><ActivityIndicator size="large" /></View>; |
| 65 | + } |
| 66 | + |
| 67 | + if (!scenario) { |
| 68 | + return <View style={styles.centered}><Text>Scenario not found</Text></View>; |
| 69 | + } |
| 70 | + |
| 71 | + const currentPlayerId = authSession.user.id; |
| 72 | + const isHost = currentPlayerId === hostId; |
| 73 | + const currentPlayerCharacter = scenario.characters.find(c => c.assigned_to === currentPlayerId); |
| 74 | + |
| 75 | + const handleTriggerReveal = async () => { |
| 76 | + await updateSession(id as string, { |
| 77 | + config: { |
| 78 | + ...scenario, |
| 79 | + game_night: { ...scenario.game_night, phase: 'EPHEMERA' } |
| 80 | + } as any |
| 81 | + }); |
| 82 | + router.push(`/murder-mystery/${id}/ephemera`); |
| 83 | + }; |
| 84 | + |
| 85 | + if (step === 'waiting') { |
| 86 | + return ( |
| 87 | + <SafeAreaView style={styles.waitingContainer}> |
| 88 | + <Text style={styles.waitingTitle}>Submissions Locked</Text> |
| 89 | + <Text style={styles.waitingText}> |
| 90 | + Your accusation and votes have been sealed. Please wait for the Host to begin the Reveal! |
| 91 | + </Text> |
| 92 | + {isHost && ( |
| 93 | + <TouchableOpacity style={styles.hostButton} onPress={handleTriggerReveal}> |
| 94 | + <Text style={styles.hostButtonText}>The Reveal (Host Only)</Text> |
| 95 | + </TouchableOpacity> |
| 96 | + )} |
| 97 | + </SafeAreaView> |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return ( |
| 102 | + <View style={styles.container}> |
| 103 | + {step === 'accusation' && ( |
| 104 | + <AccusationFormScreen |
| 105 | + sessionId={id as string} |
| 106 | + scenario={scenario} |
| 107 | + currentPlayerId={currentPlayerId} |
| 108 | + onBack={() => router.back()} |
| 109 | + onSubmitSuccess={() => setStep('voting')} |
| 110 | + /> |
| 111 | + )} |
| 112 | + {step === 'voting' && ( |
| 113 | + <AwardsVotingScreen |
| 114 | + sessionId={id as string} |
| 115 | + scenario={scenario} |
| 116 | + currentPlayerId={currentPlayerId} |
| 117 | + currentPlayerCharacterId={currentPlayerCharacter?.id} |
| 118 | + onBack={() => setStep('accusation')} |
| 119 | + onSubmitSuccess={() => setStep('waiting')} |
| 120 | + /> |
| 121 | + )} |
| 122 | + </View> |
| 123 | + ); |
| 124 | +} |
| 125 | + |
| 126 | +const styles = StyleSheet.create({ |
| 127 | + container: { |
| 128 | + flex: 1, |
| 129 | + backgroundColor: '#111827', |
| 130 | + }, |
| 131 | + centered: { |
| 132 | + flex: 1, |
| 133 | + justifyContent: 'center', |
| 134 | + alignItems: 'center', |
| 135 | + backgroundColor: '#111827', |
| 136 | + }, |
| 137 | + waitingContainer: { |
| 138 | + flex: 1, |
| 139 | + justifyContent: 'center', |
| 140 | + alignItems: 'center', |
| 141 | + backgroundColor: '#111827', |
| 142 | + padding: 20, |
| 143 | + }, |
| 144 | + waitingTitle: { |
| 145 | + fontSize: 24, |
| 146 | + fontWeight: 'bold', |
| 147 | + color: '#fcd34d', |
| 148 | + marginBottom: 16, |
| 149 | + }, |
| 150 | + waitingText: { |
| 151 | + fontSize: 16, |
| 152 | + color: '#d1d5db', |
| 153 | + textAlign: 'center', |
| 154 | + lineHeight: 24, |
| 155 | + }, |
| 156 | + hostButton: { |
| 157 | + marginTop: 32, |
| 158 | + backgroundColor: '#dc2626', |
| 159 | + paddingVertical: 12, |
| 160 | + paddingHorizontal: 24, |
| 161 | + borderRadius: 8, |
| 162 | + }, |
| 163 | + hostButtonText: { |
| 164 | + color: '#fff', |
| 165 | + fontWeight: 'bold', |
| 166 | + fontSize: 16, |
| 167 | + } |
| 168 | +}); |
0 commit comments