feat: lock the puzzles page outside of 9AM-11AM

This commit is contained in:
sBubshait 2025-08-07 09:51:21 +03:00
parent c9188a4e31
commit e4781c0aa2

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'dart:async';
import 'wordle_page.dart'; import 'wordle_page.dart';
import '../../services/puzzle_service.dart'; import '../../services/puzzle_service.dart';
import '../../services/user_service.dart'; import '../../services/user_service.dart';
@ -16,6 +17,8 @@ class _PuzzlesPageState extends State<PuzzlesPage> {
DailyChallenge? _dailyChallenge; DailyChallenge? _dailyChallenge;
bool _isLoading = false; bool _isLoading = false;
String? _errorMessage; String? _errorMessage;
Timer? _timer;
DateTime _currentTime = DateTime.now();
void _showLeaderboardView() { void _showLeaderboardView() {
setState(() { setState(() {
@ -33,6 +36,51 @@ class _PuzzlesPageState extends State<PuzzlesPage> {
void initState() { void initState() {
super.initState(); super.initState();
_loadDailyChallenge(); _loadDailyChallenge();
_updateCurrentTime();
// Update time every minute
_timer = Timer.periodic(Duration(minutes: 1), (timer) {
_updateCurrentTime();
});
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
void _updateCurrentTime() {
setState(() {
_currentTime = DateTime.now();
});
}
DateTime get _currentTimeUTC3 {
// Convert current time to UTC+3 (Riyadh timezone)
final utc = _currentTime.toUtc();
return utc.add(Duration(hours: 3));
}
bool get _isGameAvailable {
final timeUTC3 = _currentTimeUTC3;
final hour = timeUTC3.hour;
return hour >= 9 && hour < 11; // 9 AM to 11 AM UTC+3
}
String get _gameStatusText {
final timeUTC3 = _currentTimeUTC3;
final hour = timeUTC3.hour;
final minute = timeUTC3.minute;
final currentTimeFormatted =
'${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}';
if (_isGameAvailable) {
return 'Game Available Now!';
} else if (hour < 9) {
return 'Daily Challenge starts at 09:00';
} else {
return 'Daily Challenge ended at 11:00';
}
} }
Future<void> _loadDailyChallenge() async { Future<void> _loadDailyChallenge() async {
@ -56,6 +104,11 @@ class _PuzzlesPageState extends State<PuzzlesPage> {
} }
void _startGame() async { void _startGame() async {
if (!_isGameAvailable) {
_showGameNotAvailableDialog();
return;
}
if (_dailyChallenge == null) { if (_dailyChallenge == null) {
_loadDailyChallenge(); _loadDailyChallenge();
return; return;
@ -75,6 +128,35 @@ class _PuzzlesPageState extends State<PuzzlesPage> {
}); });
} }
void _showGameNotAvailableDialog() {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Game Not Available'),
content: Text(
'The Daily Challenge is only available between 09:00 and 11:00.\n\n'
'You can still view the leaderboard to see today\'s results!',
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text('OK', style: TextStyle(color: Color(0xFF6A4C93))),
),
TextButton(
onPressed: () {
Navigator.of(context).pop();
_showLeaderboardView();
},
child: Text(
'View Leaderboard',
style: TextStyle(color: Color(0xFF6A4C93)),
),
),
],
),
);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_currentView == 'leaderboard') { if (_currentView == 'leaderboard') {
@ -122,50 +204,83 @@ class _PuzzlesPageState extends State<PuzzlesPage> {
), ),
SizedBox(height: 16), SizedBox(height: 16),
// Challenge timing info // Challenge timing info with live status
Container( Container(
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12), padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.white.withOpacity(0.2), color: (_isGameAvailable ? Colors.green : Colors.orange)
.withOpacity(0.3),
borderRadius: BorderRadius.circular(20), borderRadius: BorderRadius.circular(20),
), border: Border.all(
child: Text( color: _isGameAvailable
'Daily Wordle Challenge Open Until 11 AM', ? Colors.green.shade300
style: TextStyle( : Colors.orange.shade300,
fontSize: 16, width: 1,
color: Colors.white,
fontWeight: FontWeight.w500,
), ),
), ),
child: Column(
children: [
Text(
'Daily Challenge: 09:00 - 11:00',
style: TextStyle(
fontSize: 16,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 4),
Text(
_gameStatusText,
style: TextStyle(
fontSize: 14,
color: Colors.white,
fontWeight: FontWeight.w500,
),
textAlign: TextAlign.center,
),
],
),
), ),
SizedBox(height: 60), SizedBox(height: 60),
// Big gamified PLAY button // Big gamified PLAY button with conditional availability
GestureDetector( GestureDetector(
onTap: _isLoading ? null : _startGame, onTap: _isLoading ? null : _startGame,
child: Container( child: Container(
padding: EdgeInsets.symmetric(horizontal: 40, vertical: 20), padding: EdgeInsets.symmetric(horizontal: 40, vertical: 20),
decoration: BoxDecoration( decoration: BoxDecoration(
gradient: LinearGradient( gradient: LinearGradient(
colors: [Colors.white, Colors.white.withOpacity(0.9)], colors: _isGameAvailable
? [Colors.white, Colors.white.withOpacity(0.9)]
: [Colors.grey[300]!, Colors.grey[400]!],
begin: Alignment.topLeft, begin: Alignment.topLeft,
end: Alignment.bottomRight, end: Alignment.bottomRight,
), ),
borderRadius: BorderRadius.circular(25), borderRadius: BorderRadius.circular(25),
boxShadow: [ boxShadow: _isGameAvailable
BoxShadow( ? [
color: Colors.black.withOpacity(0.3), BoxShadow(
blurRadius: 20, color: Colors.black.withOpacity(0.3),
offset: Offset(0, 8), blurRadius: 20,
), offset: Offset(0, 8),
BoxShadow( ),
color: Colors.white.withOpacity(0.5), BoxShadow(
blurRadius: 10, color: Colors.white.withOpacity(0.5),
offset: Offset(-5, -5), blurRadius: 10,
), offset: Offset(-5, -5),
], ),
]
: [
BoxShadow(
color: Colors.black.withOpacity(0.1),
blurRadius: 5,
offset: Offset(0, 2),
),
],
border: Border.all( border: Border.all(
color: Color(0xFF6A4C93).withOpacity(0.2), color: _isGameAvailable
? Color(0xFF6A4C93).withOpacity(0.2)
: Colors.grey.withOpacity(0.3),
width: 2, width: 2,
), ),
), ),
@ -173,9 +288,13 @@ class _PuzzlesPageState extends State<PuzzlesPage> {
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
Icon( Icon(
Icons.sports_esports, _isGameAvailable
? Icons.sports_esports
: Icons.access_time,
size: 32, size: 32,
color: Color(0xFF6A4C93), color: _isGameAvailable
? Color(0xFF6A4C93)
: Colors.grey[600],
), ),
SizedBox(width: 12), SizedBox(width: 12),
_isLoading _isLoading
@ -185,32 +304,42 @@ class _PuzzlesPageState extends State<PuzzlesPage> {
child: CircularProgressIndicator( child: CircularProgressIndicator(
strokeWidth: 2, strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>( valueColor: AlwaysStoppedAnimation<Color>(
Color(0xFF6A4C93), _isGameAvailable
? Color(0xFF6A4C93)
: Colors.grey[600]!,
), ),
), ),
) )
: Text( : Text(
_dailyChallenge?.attempted == true !_isGameAvailable
? 'GAME CLOSED'
: _dailyChallenge?.attempted == true
? 'VIEW RESULTS' ? 'VIEW RESULTS'
: 'PLAY NOW', : 'PLAY NOW',
style: TextStyle( style: TextStyle(
fontSize: 24, fontSize: 24,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
color: Color(0xFF6A4C93), color: _isGameAvailable
? Color(0xFF6A4C93)
: Colors.grey[600],
letterSpacing: 1.5, letterSpacing: 1.5,
), ),
), ),
SizedBox(width: 8), SizedBox(width: 8),
Icon( Icon(
Icons.arrow_forward_ios, _isGameAvailable
? Icons.arrow_forward_ios
: Icons.lock,
size: 20, size: 20,
color: Color(0xFF6A4C93), color: _isGameAvailable
? Color(0xFF6A4C93)
: Colors.grey[600],
), ),
], ],
), ),
), ),
), ),
// Show error message if any // Show error message if any
if (_errorMessage != null) ...[ if (_errorMessage != null) ...[
SizedBox(height: 20), SizedBox(height: 20),
@ -230,14 +359,18 @@ class _PuzzlesPageState extends State<PuzzlesPage> {
), ),
), ),
], ],
// Show completion status if attempted // Show completion status if attempted
if (_dailyChallenge?.attempted == true) ...[ if (_dailyChallenge?.attempted == true) ...[
SizedBox(height: 20), SizedBox(height: 20),
Container( Container(
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12), padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
decoration: BoxDecoration( decoration: BoxDecoration(
color: (_dailyChallenge!.solved ? Colors.green : Colors.orange).withOpacity(0.2), color:
(_dailyChallenge!.solved
? Colors.green
: Colors.orange)
.withOpacity(0.2),
borderRadius: BorderRadius.circular(20), borderRadius: BorderRadius.circular(20),
), ),
child: Text( child: Text(
@ -253,7 +386,28 @@ class _PuzzlesPageState extends State<PuzzlesPage> {
), ),
), ),
], ],
// Game availability help text
if (!_isGameAvailable) ...[
SizedBox(height: 20),
Container(
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
decoration: BoxDecoration(
color: Colors.blue.withOpacity(0.2),
borderRadius: BorderRadius.circular(20),
),
child: Text(
'The Daily Challenge is available every day from 09:00 to 11:00 Riyadh time. You can still view the leaderboard!',
style: TextStyle(
fontSize: 14,
color: Colors.white,
fontWeight: FontWeight.w500,
),
textAlign: TextAlign.center,
),
),
],
SizedBox(height: 40), SizedBox(height: 40),
// Leaderboard button // Leaderboard button
@ -374,11 +528,12 @@ class _LeaderboardContentState extends State<_LeaderboardContent> {
if (userResponse['success'] == true && userResponse['data'] != null) { if (userResponse['success'] == true && userResponse['data'] != null) {
_currentUsername = userResponse['data']['username']; _currentUsername = userResponse['data']['username'];
} }
if (leaderboardResponse.status) { if (leaderboardResponse.status) {
_leaderboardData = leaderboardResponse.data; _leaderboardData = leaderboardResponse.data;
} else { } else {
_errorMessage = leaderboardResponse.message ?? 'Failed to load leaderboard'; _errorMessage =
leaderboardResponse.message ?? 'Failed to load leaderboard';
} }
}); });
} catch (e) { } catch (e) {
@ -402,10 +557,7 @@ class _LeaderboardContentState extends State<_LeaderboardContent> {
SizedBox(height: 16), SizedBox(height: 16),
Text( Text(
'Loading leaderboard...', 'Loading leaderboard...',
style: TextStyle( style: TextStyle(fontSize: 16, color: Colors.grey[600]),
fontSize: 16,
color: Colors.grey[600],
),
), ),
], ],
), ),
@ -417,18 +569,11 @@ class _LeaderboardContentState extends State<_LeaderboardContent> {
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
Icon( Icon(Icons.error_outline, size: 64, color: Colors.grey[400]),
Icons.error_outline,
size: 64,
color: Colors.grey[400],
),
SizedBox(height: 16), SizedBox(height: 16),
Text( Text(
_errorMessage!, _errorMessage!,
style: TextStyle( style: TextStyle(fontSize: 16, color: Colors.grey[600]),
fontSize: 16,
color: Colors.grey[600],
),
textAlign: TextAlign.center, textAlign: TextAlign.center,
), ),
SizedBox(height: 16), SizedBox(height: 16),
@ -467,10 +612,7 @@ class _LeaderboardContentState extends State<_LeaderboardContent> {
SizedBox(height: 8), SizedBox(height: 8),
Text( Text(
'Be the first one!', 'Be the first one!',
style: TextStyle( style: TextStyle(fontSize: 16, color: Colors.grey[500]),
fontSize: 16,
color: Colors.grey[500],
),
), ),
], ],
), ),
@ -516,7 +658,9 @@ class _LeaderboardContentState extends State<_LeaderboardContent> {
margin: EdgeInsets.only(bottom: 12), margin: EdgeInsets.only(bottom: 12),
padding: EdgeInsets.all(16), padding: EdgeInsets.all(16),
decoration: BoxDecoration( decoration: BoxDecoration(
color: isCurrentUser ? Color(0xFF6A4C93).withOpacity(0.05) : Colors.white, color: isCurrentUser
? Color(0xFF6A4C93).withOpacity(0.05)
: Colors.white,
borderRadius: BorderRadius.circular(12), borderRadius: BorderRadius.circular(12),
boxShadow: [ boxShadow: [
BoxShadow( BoxShadow(
@ -528,8 +672,8 @@ class _LeaderboardContentState extends State<_LeaderboardContent> {
border: isCurrentUser border: isCurrentUser
? Border.all(color: Color(0xFF6A4C93), width: 2) ? Border.all(color: Color(0xFF6A4C93), width: 2)
: rank <= 3 : rank <= 3
? Border.all(color: rankColor.withOpacity(0.3), width: 2) ? Border.all(color: rankColor.withOpacity(0.3), width: 2)
: Border.all(color: Colors.grey[200]!, width: 1), : Border.all(color: Colors.grey[200]!, width: 1),
), ),
child: Row( child: Row(
children: [ children: [
@ -558,12 +702,13 @@ class _LeaderboardContentState extends State<_LeaderboardContent> {
CircleAvatar( CircleAvatar(
radius: 25, radius: 25,
backgroundColor: Color(0xFF6A4C93).withOpacity(0.1), backgroundColor: Color(0xFF6A4C93).withOpacity(0.1),
backgroundImage: entry.avatar != null && entry.avatar!.isNotEmpty backgroundImage:
entry.avatar != null && entry.avatar!.isNotEmpty
? NetworkImage(entry.avatar!) ? NetworkImage(entry.avatar!)
: null, : null,
child: entry.avatar == null || entry.avatar!.isEmpty child: entry.avatar == null || entry.avatar!.isEmpty
? Text( ? Text(
entry.displayName.isNotEmpty entry.displayName.isNotEmpty
? entry.displayName[0].toUpperCase() ? entry.displayName[0].toUpperCase()
: '?', : '?',
style: TextStyle( style: TextStyle(
@ -594,7 +739,10 @@ class _LeaderboardContentState extends State<_LeaderboardContent> {
if (isCurrentUser) ...[ if (isCurrentUser) ...[
SizedBox(width: 8), SizedBox(width: 8),
Container( Container(
padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2), padding: EdgeInsets.symmetric(
horizontal: 6,
vertical: 2,
),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Color(0xFF6A4C93), color: Color(0xFF6A4C93),
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),