diff --git a/luckychit/lib/shared/widgets/alternative_draw_animations.dart b/luckychit/lib/shared/widgets/alternative_draw_animations.dart index 8a5260c..4994046 100644 --- a/luckychit/lib/shared/widgets/alternative_draw_animations.dart +++ b/luckychit/lib/shared/widgets/alternative_draw_animations.dart @@ -401,7 +401,9 @@ class _SlotMachineDrawAnimationState extends State bool _isAnimating = false; bool _isComplete = false; String? _winnerId; + final List _baseNames = []; List _displayNames = []; + int _currentIndex = 0; Timer? _slotTimer; @override @@ -434,8 +436,7 @@ class _SlotMachineDrawAnimationState extends State curve: Curves.easeInOut, )); - // Prepare display names - _displayNames = widget.members.map((m) => m['name']?.toString() ?? 'Unknown').toList(); + _prepareDisplayNames(); // Auto-start after delay Future.delayed(const Duration(milliseconds: 500), () { @@ -451,6 +452,50 @@ class _SlotMachineDrawAnimationState extends State super.dispose(); } + void _prepareDisplayNames() { + _baseNames.clear(); + _baseNames.addAll( + widget.members.map((m) => m['name']?.toString() ?? 'Unknown'), + ); + + if (_baseNames.isEmpty) { + _baseNames.add('Unknown'); + } + + // Ensure we have at least 7 entries to fill the slot windows + if (_baseNames.length < 7) { + final original = List.from(_baseNames); + while (_baseNames.length < 7) { + _baseNames.addAll(original); + } + } + + _shuffleBaseNames(); + _currentIndex = 0; + _updateDisplayWindow(); + } + + void _shuffleBaseNames() { + final seedSource = DateTime.now().millisecondsSinceEpoch.toString(); + final hash = _generateHash(seedSource); + final randomValue = _hashToNumber(hash); + final random = math.Random(randomValue); + + for (var i = _baseNames.length - 1; i > 0; i--) { + final j = random.nextInt(i + 1); + final temp = _baseNames[i]; + _baseNames[i] = _baseNames[j]; + _baseNames[j] = temp; + } + } + + void _updateDisplayWindow() { + _displayNames = List.generate( + 7, + (index) => _baseNames[(_currentIndex + index) % _baseNames.length], + ); + } + void _startAnimation() { if (_isAnimating) return; @@ -488,8 +533,8 @@ class _SlotMachineDrawAnimationState extends State } setState(() { - // Shuffle the display names for effect - _displayNames.shuffle(); + _currentIndex = (_currentIndex + 1) % _baseNames.length; + _updateDisplayWindow(); }); _slotController.forward().then((_) { @@ -512,7 +557,8 @@ class _SlotMachineDrawAnimationState extends State } setState(() { - _displayNames.shuffle(); + _currentIndex = (_currentIndex + 1) % _baseNames.length; + _updateDisplayWindow(); }); _slotController.forward().then((_) { @@ -528,20 +574,23 @@ class _SlotMachineDrawAnimationState extends State void _completeAnimation() { final winnerName = widget.members.firstWhere((m) => m['id'] == _winnerId)['name']; + final winnerIndex = _baseNames.indexOf(winnerName); setState(() { _isAnimating = false; _isComplete = true; - // Show winner in center position (index 3) with padding names for 7 slots - _displayNames = [ - _displayNames.isNotEmpty ? _displayNames[0] : '', - _displayNames.length > 1 ? _displayNames[1] : '', - _displayNames.length > 2 ? _displayNames[2] : '', - winnerName, // Center (index 3) - the winner! - _displayNames.length > 4 ? _displayNames[4] : '', - _displayNames.length > 5 ? _displayNames[5] : '', - _displayNames.length > 6 ? _displayNames[6] : '', - ]; + if (winnerIndex != -1) { + final List window = []; + for (int offset = -3; offset <= 3; offset++) { + final index = (winnerIndex + offset) % _baseNames.length; + final adjustedIndex = index < 0 ? index + _baseNames.length : index; + window.add(_baseNames[adjustedIndex]); + } + _displayNames = window; + } else { + _displayNames = List.filled(7, '', growable: false); + _displayNames[3] = winnerName; + } }); _pulseController.stop();