After correcting the countdown on mobile version
This commit is contained in:
parent
15e860c6db
commit
39c393b50e
@ -20,6 +20,8 @@ export type PlayerRoundDto = {
|
||||
secondsToBettingEnd: number;
|
||||
secondsToBettingStart: number;
|
||||
secondsToClose: number;
|
||||
bettingEndMs?: number;
|
||||
bettingStartMs?: number;
|
||||
shortPoolPercent?: number;
|
||||
longPoolPercent?: number;
|
||||
version?: number;
|
||||
@ -63,6 +65,8 @@ export async function publishRoundState(input: {
|
||||
secondsToBettingEnd: input.round.secondsToBettingEnd,
|
||||
secondsToBettingStart: input.round.secondsToBettingStart,
|
||||
secondsToClose: input.round.secondsToClose,
|
||||
bettingEndMs: input.round.bettingEndMs ?? input.round.candleOpenMs,
|
||||
bettingStartMs: input.round.bettingStartMs,
|
||||
version: (input.round as CachedRoundDto).version ?? 1,
|
||||
};
|
||||
|
||||
|
||||
@ -45,6 +45,8 @@ export type RoundCacheHash = {
|
||||
secondsToBettingEnd: string;
|
||||
secondsToBettingStart: string;
|
||||
secondsToClose: string;
|
||||
bettingEndMs: string;
|
||||
bettingStartMs: string;
|
||||
version: string;
|
||||
};
|
||||
|
||||
|
||||
@ -22,6 +22,10 @@ export type CachedRoundDto = {
|
||||
secondsToBettingEnd: number;
|
||||
secondsToBettingStart: number;
|
||||
secondsToClose: number;
|
||||
/** Absolute ms when the open (betting) window ends (= candleOpenMs). */
|
||||
bettingEndMs?: number;
|
||||
/** Absolute ms when the open window starts. */
|
||||
bettingStartMs?: number;
|
||||
version: number;
|
||||
};
|
||||
|
||||
@ -57,6 +61,8 @@ export function serializeRoundToHash(
|
||||
secondsToBettingEnd: String(round.secondsToBettingEnd),
|
||||
secondsToBettingStart: String(round.secondsToBettingStart),
|
||||
secondsToClose: String(round.secondsToClose),
|
||||
bettingEndMs: String(round.bettingEndMs ?? round.candleOpenMs),
|
||||
bettingStartMs: empty(round.bettingStartMs != null ? String(round.bettingStartMs) : ''),
|
||||
version: String(round.version ?? 1),
|
||||
};
|
||||
}
|
||||
@ -82,6 +88,8 @@ export function parseRoundFromHash(hash: Record<string, string>): CachedRoundDto
|
||||
secondsToBettingEnd: Number(hash.secondsToBettingEnd ?? 0),
|
||||
secondsToBettingStart: Number(hash.secondsToBettingStart ?? 0),
|
||||
secondsToClose: Number(hash.secondsToClose ?? 0),
|
||||
bettingEndMs: Number(hash.bettingEndMs ?? hash.candleOpenMs),
|
||||
bettingStartMs: parseOptionalNumber(hash.bettingStartMs) ?? undefined,
|
||||
version: Number(hash.version ?? 1),
|
||||
};
|
||||
}
|
||||
|
||||
@ -198,6 +198,7 @@ class RoundModel {
|
||||
this.shortPoolPercent,
|
||||
this.longPoolPercent,
|
||||
this.serverNowMs,
|
||||
this.candleOpenMs,
|
||||
this.bettingEndMs,
|
||||
this.bettingStartMs,
|
||||
});
|
||||
@ -212,26 +213,34 @@ class RoundModel {
|
||||
final double? longPoolPercent;
|
||||
final int? serverNowMs;
|
||||
|
||||
/// Candle open time (ms). Also equals [bettingEndMs] on the server.
|
||||
final int? candleOpenMs;
|
||||
|
||||
/// Absolute ms when the open (betting) window ends — preferred for smooth countdown.
|
||||
final int? bettingEndMs;
|
||||
final int? bettingStartMs;
|
||||
|
||||
bool get isBetting => status == 'betting';
|
||||
|
||||
factory RoundModel.fromJson(Map<String, dynamic> json) => RoundModel(
|
||||
id: json['id'] as String,
|
||||
status: (json['status'] ?? 'waiting').toString(),
|
||||
secondsToBettingEnd: (json['secondsToBettingEnd'] as num?)?.toInt() ?? 0,
|
||||
secondsToBettingStart:
|
||||
(json['secondsToBettingStart'] as num?)?.toInt() ?? 0,
|
||||
secondsToClose: (json['secondsToClose'] as num?)?.toInt() ?? 0,
|
||||
outcome: json['outcome'] as String?,
|
||||
shortPoolPercent: (json['shortPoolPercent'] as num?)?.toDouble(),
|
||||
longPoolPercent: (json['longPoolPercent'] as num?)?.toDouble(),
|
||||
serverNowMs: (json['serverNowMs'] as num?)?.toInt(),
|
||||
bettingEndMs: (json['bettingEndMs'] as num?)?.toInt(),
|
||||
bettingStartMs: (json['bettingStartMs'] as num?)?.toInt(),
|
||||
);
|
||||
factory RoundModel.fromJson(Map<String, dynamic> json) {
|
||||
final candleOpenMs = (json['candleOpenMs'] as num?)?.toInt();
|
||||
return RoundModel(
|
||||
id: json['id'] as String,
|
||||
status: (json['status'] ?? 'waiting').toString(),
|
||||
secondsToBettingEnd: (json['secondsToBettingEnd'] as num?)?.toInt() ?? 0,
|
||||
secondsToBettingStart:
|
||||
(json['secondsToBettingStart'] as num?)?.toInt() ?? 0,
|
||||
secondsToClose: (json['secondsToClose'] as num?)?.toInt() ?? 0,
|
||||
outcome: json['outcome'] as String?,
|
||||
shortPoolPercent: (json['shortPoolPercent'] as num?)?.toDouble(),
|
||||
longPoolPercent: (json['longPoolPercent'] as num?)?.toDouble(),
|
||||
serverNowMs: (json['serverNowMs'] as num?)?.toInt(),
|
||||
candleOpenMs: candleOpenMs,
|
||||
// WS Redis cache may omit bettingEndMs; candle open is the same deadline.
|
||||
bettingEndMs: (json['bettingEndMs'] as num?)?.toInt() ?? candleOpenMs,
|
||||
bettingStartMs: (json['bettingStartMs'] as num?)?.toInt(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class PredictionModel {
|
||||
|
||||
@ -8,6 +8,7 @@ import '../../../data/models/prediction_models.dart';
|
||||
import '../../../data/repositories/prediction_repository_impl.dart';
|
||||
import '../../../data/repositories/wallet_repository_impl.dart';
|
||||
import '../../../domain/repositories/prediction_repository.dart';
|
||||
import '../utils/server_adjusted_countdown.dart';
|
||||
|
||||
part 'prediction_event.dart';
|
||||
part 'prediction_state.dart';
|
||||
@ -573,7 +574,11 @@ class PredictionBloc extends Bloc<PredictionEvent, PredictionState> {
|
||||
gameState.autoSettings?.lastAutoBetAt != prevLastAuto;
|
||||
|
||||
emit(state.copyWith(
|
||||
gameState: gameState,
|
||||
gameState: _stabilizeGameStateRound(
|
||||
previous: state.gameState,
|
||||
incoming: gameState,
|
||||
forceRound: event.forceGameState,
|
||||
),
|
||||
mainBalanceLabel: labels.mainLabel,
|
||||
bonusBalanceLabel: labels.bonusLabel,
|
||||
testBalanceLabel: labels.testLabel,
|
||||
@ -636,7 +641,10 @@ class PredictionBloc extends Bloc<PredictionEvent, PredictionState> {
|
||||
}
|
||||
final snapshot = msg['snapshot'];
|
||||
if (snapshot is Map<String, dynamic>) {
|
||||
final next = GameStateModel.fromJson(snapshot);
|
||||
final next = _stabilizeGameStateRound(
|
||||
previous: state.gameState,
|
||||
incoming: GameStateModel.fromJson(snapshot),
|
||||
);
|
||||
var history = state.history;
|
||||
if (next.userMultiBet != null) {
|
||||
history = _historyWithMultiProgress(history, next.userMultiBet!);
|
||||
@ -672,9 +680,13 @@ class PredictionBloc extends Bloc<PredictionEvent, PredictionState> {
|
||||
}
|
||||
final round = msg['round'];
|
||||
if (round is Map<String, dynamic>) {
|
||||
final parsed = RoundModel.fromJson(round);
|
||||
final parsed = mergeRound(gs.round, RoundModel.fromJson(round));
|
||||
final prevStatus = gs.round?.status;
|
||||
final prevRoundId = gs.round?.id;
|
||||
// Timer-only WS ticks must not remount countdown / overwrite UI state.
|
||||
if (!roundUiChanged(gs.round, parsed)) {
|
||||
break;
|
||||
}
|
||||
final nextBet = _userBetForRound(gs.userBet, parsed);
|
||||
emit(state.copyWith(
|
||||
gameState: gs.copyWith(
|
||||
@ -969,6 +981,23 @@ class PredictionBloc extends Bloc<PredictionEvent, PredictionState> {
|
||||
return config.minStakeAmount;
|
||||
}
|
||||
|
||||
/// Keep countdown stable: merge absolute deadlines and ignore timer-only round ticks.
|
||||
GameStateModel _stabilizeGameStateRound({
|
||||
required GameStateModel? previous,
|
||||
required GameStateModel incoming,
|
||||
bool forceRound = false,
|
||||
}) {
|
||||
final incomingRound = incoming.round;
|
||||
if (incomingRound == null) return incoming;
|
||||
final merged = mergeRound(previous?.round, incomingRound);
|
||||
if (!forceRound &&
|
||||
previous?.round != null &&
|
||||
!roundUiChanged(previous!.round, merged)) {
|
||||
return incoming.copyWith(round: previous.round);
|
||||
}
|
||||
return incoming.copyWith(round: merged);
|
||||
}
|
||||
|
||||
List<String> _initialMultiSequence(GameStateModel gs) {
|
||||
final level = gs.multiLevels.isNotEmpty ? gs.multiLevels.first : null;
|
||||
if (level == null) return const ['long', 'long'];
|
||||
|
||||
@ -85,24 +85,29 @@ class PredictionState extends Equatable {
|
||||
}
|
||||
|
||||
bool get canBetSingle {
|
||||
final round = gameState?.round;
|
||||
return playMode == PredictionPlayMode.single &&
|
||||
round?.isBetting == true &&
|
||||
isBettingWindowOpen &&
|
||||
!isPlacingBet &&
|
||||
!hasActiveSingleBet &&
|
||||
selectedStakeValid;
|
||||
}
|
||||
|
||||
bool get canBetMulti {
|
||||
final round = gameState?.round;
|
||||
return playMode == PredictionPlayMode.multi &&
|
||||
round?.isBetting == true &&
|
||||
isBettingWindowOpen &&
|
||||
!isPlacingBet &&
|
||||
gameState?.userMultiBet == null &&
|
||||
selectedMultiLevel != null &&
|
||||
selectedStakeValid;
|
||||
}
|
||||
|
||||
/// Live open-window check using server clock skew (not raw status alone).
|
||||
bool get isBettingWindowOpen {
|
||||
final round = gameState?.round;
|
||||
if (round == null) return false;
|
||||
return serverAdjustedCountdown(round).bettingOpen;
|
||||
}
|
||||
|
||||
bool get selectedStakeValid {
|
||||
final config = gameState?.config;
|
||||
if (config == null) return false;
|
||||
|
||||
@ -11,6 +11,7 @@ import '../../../../shared/widgets/pred_text_field.dart';
|
||||
import '../../../../shared/widgets/adaptive_space_between_row.dart';
|
||||
import '../../auth/bloc/auth_bloc.dart';
|
||||
import '../bloc/prediction_bloc.dart';
|
||||
import '../utils/server_adjusted_countdown.dart';
|
||||
import 'widgets/candle_chart_widget.dart';
|
||||
import 'widgets/multi_sequence_progress.dart';
|
||||
import 'widgets/prediction_bet_panel.dart';
|
||||
@ -25,16 +26,22 @@ class PredictionScreen extends StatefulWidget {
|
||||
|
||||
class _PredictionScreenState extends State<PredictionScreen> {
|
||||
late final PredictionBloc _bloc;
|
||||
Timer? _uiClock;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_bloc = context.read<PredictionBloc>();
|
||||
_bloc.add(const PredictionResumed());
|
||||
// Rebuild bet gating as the open window expires (canBet uses DateTime.now).
|
||||
_uiClock = Timer.periodic(const Duration(seconds: 1), (_) {
|
||||
if (mounted) setState(() {});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_uiClock?.cancel();
|
||||
_bloc.add(const PredictionPaused());
|
||||
super.dispose();
|
||||
}
|
||||
@ -148,12 +155,16 @@ class _PredictionScreenState extends State<PredictionScreen> {
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
_StatusChip(round?.status ?? 'waiting'),
|
||||
_StatusChip(
|
||||
round != null
|
||||
? serverAdjustedCountdown(round).effectiveStatus
|
||||
: 'waiting',
|
||||
),
|
||||
const Spacer(),
|
||||
if (round != null)
|
||||
_LiveCountdown(
|
||||
key: ValueKey(
|
||||
'${state.selectedIntervalMinutes}-${round.id}-${round.bettingEndMs ?? round.secondsToBettingEnd}',
|
||||
'${state.selectedIntervalMinutes}-${round.id}',
|
||||
),
|
||||
round: round,
|
||||
openLabel: s.timeLeft,
|
||||
@ -747,11 +758,11 @@ class _StatusChip extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
/// Smooth local countdown.
|
||||
/// Smooth local countdown using serverNowMs skew (web parity).
|
||||
///
|
||||
/// Locks a client-clock deadline once per round phase so WS/poll refreshes that
|
||||
/// only update `secondsToBettingEnd` / `serverNowMs` cannot make the UI jump
|
||||
/// (e.g. 30 → 25). Uses absolute `bettingEndMs` when available.
|
||||
/// Relative `secondsToBetting*` values are frozen at publish time; subtracting
|
||||
/// elapsed client time since [RoundModel.serverNowMs] keeps the UI monotonic
|
||||
/// across WS/poll refreshes.
|
||||
class _LiveCountdown extends StatefulWidget {
|
||||
const _LiveCountdown({
|
||||
super.key,
|
||||
@ -770,13 +781,16 @@ class _LiveCountdown extends StatefulWidget {
|
||||
|
||||
class _LiveCountdownState extends State<_LiveCountdown> {
|
||||
Timer? _timer;
|
||||
int? _deadlineMs;
|
||||
int _nowMs = DateTime.now().millisecondsSinceEpoch;
|
||||
|
||||
/// Anchored snapshot so timer-only WS ticks (if any reach the widget) cannot
|
||||
/// jump the displayed seconds. Relocked only on round id / phase changes.
|
||||
RoundModel? _anchor;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_lockDeadline(widget.round);
|
||||
_anchor = widget.round;
|
||||
_timer = Timer.periodic(const Duration(milliseconds: 200), (_) {
|
||||
if (!mounted) return;
|
||||
setState(() => _nowMs = DateTime.now().millisecondsSinceEpoch);
|
||||
@ -788,14 +802,8 @@ class _LiveCountdownState extends State<_LiveCountdown> {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
final old = oldWidget.round;
|
||||
final next = widget.round;
|
||||
// Re-lock when the prediction window changes (interval switch, new round,
|
||||
// phase change). Ignore per-tick WS refreshes of the same deadline.
|
||||
if (old.id != next.id ||
|
||||
old.status != next.status ||
|
||||
old.bettingEndMs != next.bettingEndMs ||
|
||||
old.bettingStartMs != next.bettingStartMs ||
|
||||
_deadlineMs == null) {
|
||||
_lockDeadline(next);
|
||||
if (old.id != next.id || old.status != next.status || _anchor == null) {
|
||||
_anchor = next;
|
||||
}
|
||||
}
|
||||
|
||||
@ -805,25 +813,13 @@ class _LiveCountdownState extends State<_LiveCountdown> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _lockDeadline(RoundModel round) {
|
||||
final now = DateTime.now().millisecondsSinceEpoch;
|
||||
final serverNow = round.serverNowMs ?? now;
|
||||
final skew = now - serverNow;
|
||||
final isOpen = round.status == 'betting';
|
||||
|
||||
if (isOpen && round.bettingEndMs != null) {
|
||||
_deadlineMs = round.bettingEndMs! + skew;
|
||||
} else {
|
||||
final secs = isOpen ? round.secondsToBettingEnd : round.secondsToBettingStart;
|
||||
_deadlineMs = now + secs * 1000;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isOpen = widget.round.status == 'betting';
|
||||
final deadline = _deadlineMs ?? _nowMs;
|
||||
final seconds = ((deadline - _nowMs) / 1000).ceil().clamp(0, 1 << 30);
|
||||
final round = _anchor ?? widget.round;
|
||||
final adjusted = serverAdjustedCountdown(round, clientNowMs: _nowMs);
|
||||
final isOpen = adjusted.bettingOpen;
|
||||
final seconds =
|
||||
isOpen ? adjusted.secondsToBettingEnd : adjusted.secondsToBettingStart;
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
|
||||
@ -0,0 +1,115 @@
|
||||
/// Live countdown using [RoundModel.serverNowMs] skew — same idea as web
|
||||
/// `useServerAdjustedCountdown`. Avoids stale WS/cache windows jumping the UI.
|
||||
library;
|
||||
|
||||
import 'dart:math' as math;
|
||||
|
||||
import '../../../data/models/prediction_models.dart';
|
||||
|
||||
/// Snapshot of the open / wait windows after applying client–server clock skew.
|
||||
class ServerAdjustedCountdown {
|
||||
const ServerAdjustedCountdown({
|
||||
required this.secondsToBettingEnd,
|
||||
required this.secondsToBettingStart,
|
||||
required this.bettingOpen,
|
||||
required this.effectiveStatus,
|
||||
});
|
||||
|
||||
final int secondsToBettingEnd;
|
||||
final int secondsToBettingStart;
|
||||
|
||||
/// True when predictions can still be placed for this round snapshot.
|
||||
final bool bettingOpen;
|
||||
|
||||
/// Status after treating an expired betting window as locked.
|
||||
final String effectiveStatus;
|
||||
}
|
||||
|
||||
/// Computes a stable countdown from a round payload and the current client clock.
|
||||
ServerAdjustedCountdown serverAdjustedCountdown(
|
||||
RoundModel round, {
|
||||
int? clientNowMs,
|
||||
}) {
|
||||
final now = clientNowMs ?? DateTime.now().millisecondsSinceEpoch;
|
||||
final serverNow = round.serverNowMs ?? now;
|
||||
final elapsedSec = math.max(0, (now - serverNow) ~/ 1000);
|
||||
|
||||
final rawStatus = round.status;
|
||||
final secondsToBettingEnd = rawStatus == 'betting'
|
||||
? math.max(0, round.secondsToBettingEnd - elapsedSec)
|
||||
: 0;
|
||||
final secondsToBettingStart = rawStatus != 'betting'
|
||||
? math.max(0, round.secondsToBettingStart - elapsedSec)
|
||||
: 0;
|
||||
|
||||
final bettingOpen = rawStatus == 'betting' && secondsToBettingEnd > 0;
|
||||
final effectiveStatus =
|
||||
rawStatus == 'betting' && secondsToBettingEnd <= 0 ? 'locked' : rawStatus;
|
||||
|
||||
return ServerAdjustedCountdown(
|
||||
secondsToBettingEnd: secondsToBettingEnd,
|
||||
secondsToBettingStart: secondsToBettingStart,
|
||||
bettingOpen: bettingOpen,
|
||||
effectiveStatus: effectiveStatus,
|
||||
);
|
||||
}
|
||||
|
||||
/// True when UI-relevant round fields changed (ignore ticking countdown fields).
|
||||
bool roundUiChanged(RoundModel? prev, RoundModel? next) {
|
||||
if (identical(prev, next)) return false;
|
||||
if (prev == null || next == null) return true;
|
||||
return prev.id != next.id ||
|
||||
prev.status != next.status ||
|
||||
prev.outcome != next.outcome ||
|
||||
prev.candleOpenMs != next.candleOpenMs ||
|
||||
prev.shortPoolPercent != next.shortPoolPercent ||
|
||||
prev.longPoolPercent != next.longPoolPercent;
|
||||
}
|
||||
|
||||
/// Merges an incoming WS/HTTP round onto [prev], preserving absolute deadlines
|
||||
/// when the payload omits them (Redis cache historically stripped them).
|
||||
RoundModel mergeRound(RoundModel? prev, RoundModel incoming) {
|
||||
if (prev == null) return _withEffectiveStatus(incoming);
|
||||
|
||||
final sameRound = prev.id == incoming.id;
|
||||
final merged = RoundModel(
|
||||
id: incoming.id,
|
||||
status: incoming.status,
|
||||
secondsToBettingEnd: incoming.secondsToBettingEnd,
|
||||
secondsToBettingStart: incoming.secondsToBettingStart,
|
||||
secondsToClose: incoming.secondsToClose,
|
||||
outcome: incoming.outcome ?? (sameRound ? prev.outcome : null),
|
||||
shortPoolPercent: incoming.shortPoolPercent ??
|
||||
(sameRound ? prev.shortPoolPercent : null),
|
||||
longPoolPercent: incoming.longPoolPercent ??
|
||||
(sameRound ? prev.longPoolPercent : null),
|
||||
serverNowMs: incoming.serverNowMs ?? prev.serverNowMs,
|
||||
candleOpenMs: incoming.candleOpenMs ?? (sameRound ? prev.candleOpenMs : null),
|
||||
bettingEndMs: incoming.bettingEndMs ??
|
||||
(sameRound ? prev.bettingEndMs : null) ??
|
||||
incoming.candleOpenMs ??
|
||||
(sameRound ? prev.candleOpenMs : null),
|
||||
bettingStartMs: incoming.bettingStartMs ??
|
||||
(sameRound ? prev.bettingStartMs : null),
|
||||
);
|
||||
return _withEffectiveStatus(merged);
|
||||
}
|
||||
|
||||
RoundModel _withEffectiveStatus(RoundModel round) {
|
||||
final adjusted = serverAdjustedCountdown(round);
|
||||
if (adjusted.effectiveStatus == round.status) return round;
|
||||
return RoundModel(
|
||||
id: round.id,
|
||||
status: adjusted.effectiveStatus,
|
||||
secondsToBettingEnd: round.secondsToBettingEnd,
|
||||
secondsToBettingStart: round.secondsToBettingStart,
|
||||
secondsToClose: round.secondsToClose,
|
||||
outcome: round.outcome,
|
||||
shortPoolPercent: round.shortPoolPercent,
|
||||
longPoolPercent: round.longPoolPercent,
|
||||
serverNowMs: round.serverNowMs,
|
||||
candleOpenMs: round.candleOpenMs,
|
||||
bettingEndMs: round.bettingEndMs,
|
||||
bettingStartMs: round.bettingStartMs,
|
||||
);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user