This commit is contained in:
2026-03-22 01:40:29 +00:00
parent 6c89b7ab8c
commit 00fee30792
23 changed files with 1717 additions and 2081 deletions

View File

@@ -4,51 +4,44 @@ import '../models/game_model.dart';
class GameController {
final _supabase = Supabase.instance.client;
// 👇 Atalho para apanhar o ID do utilizador logado
String get myUserId => _supabase.auth.currentUser?.id ?? '';
// 1. LER JOGOS (Stream em Tempo Real da tabela original)
// LER JOGOS
Stream<List<Game>> get gamesStream {
return _supabase
.from('games')
.stream(primaryKey: ['id'])
.eq('user_id', myUserId) // 🔒 SEGURANÇA: Ouve apenas os jogos deste utilizador
.eq('user_id', myUserId)
.asyncMap((event) async {
// Lê diretamente da tabela "games" e já não da "games_with_logos"
final data = await _supabase
.from('games')
.select()
.eq('user_id', myUserId) // 🔒 SEGURANÇA
.eq('user_id', myUserId)
.order('game_date', ascending: false);
// O Game.fromMap agora faz o trabalho sujo todo!
return data.map((json) => Game.fromMap(json)).toList();
});
}
// =========================================================================
// 👇 LER JOGOS COM FILTROS DE EQUIPA E TEMPORADA
// =========================================================================
// LER JOGOS COM FILTROS
Stream<List<Game>> getFilteredGames({required String teamFilter, required String seasonFilter}) {
return _supabase
.from('games')
.stream(primaryKey: ['id'])
.eq('user_id', myUserId) // 🔒 SEGURANÇA
.eq('user_id', myUserId)
.asyncMap((event) async {
// 1. Começamos a query na tabela principal "games"
var query = _supabase.from('games').select().eq('user_id', myUserId); // 🔒 SEGURANÇA
var query = _supabase.from('games').select().eq('user_id', myUserId);
// 2. Se a temporada não for "Todas", aplicamos o filtro AQUI
if (seasonFilter != 'Todas') {
query = query.eq('season', seasonFilter);
}
// 3. Executamos a query e ordenamos pela data
final data = await query.order('game_date', ascending: false);
List<Game> games = data.map((json) => Game.fromMap(json)).toList();
// 4. Filtramos a equipa em memória
if (teamFilter != 'Todas') {
games = games.where((g) => g.myTeam == teamFilter || g.opponentTeam == teamFilter).toList();
}
@@ -57,11 +50,11 @@ class GameController {
});
}
// 2. CRIAR JOGO
// CRIAR JOGO
Future<String?> createGame(String myTeam, String opponent, String season) async {
try {
final response = await _supabase.from('games').insert({
'user_id': myUserId, // 🔒 CARIMBA O JOGO COM O ID DO TREINADOR
'user_id': myUserId,
'my_team': myTeam,
'opponent_team': opponent,
'season': season,
@@ -69,16 +62,24 @@ class GameController {
'opponent_score': 0,
'status': 'Decorrer',
'game_date': DateTime.now().toIso8601String(),
// 👇 Preenchemos logo com os valores iniciais da tua Base de Dados
'remaining_seconds': 600, // Assume 10 minutos (600s)
'my_timeouts': 0,
'opp_timeouts': 0,
'current_quarter': 1,
'top_pts_name': '---',
'top_ast_name': '---',
'top_rbs_name': '---',
'top_def_name': '---',
'mvp_name': '---',
}).select().single();
return response['id'];
return response['id']?.toString();
} catch (e) {
print("Erro ao criar jogo: $e");
return null;
}
}
void dispose() {
// Não é necessário fechar streams do Supabase manualmente aqui
}
void dispose() {}
}