feat(m5): client-server sync, offline queue and replay playback
This commit is contained in:
@@ -44,5 +44,15 @@ abstract class MatchSource {
|
||||
int fire({required int angleMdeg, required int powerQ16, int weapon});
|
||||
int endTurn();
|
||||
int tick(int ticks);
|
||||
|
||||
/// Current state hash (FNV chain over the whole state).
|
||||
int stateHash();
|
||||
|
||||
/// Replay JSON (ReplayV1) of the match played so far.
|
||||
String replayJson();
|
||||
|
||||
/// True when the handle plays back a recording.
|
||||
bool get isPlayback;
|
||||
|
||||
void dispose();
|
||||
}
|
||||
|
||||
@@ -54,12 +54,44 @@ typedef _ProjectileDart = int Function(Pointer<Void>, Pointer<Int32>);
|
||||
typedef _ImpactC = Uint32 Function(Pointer<Void>, Pointer<Int32>);
|
||||
typedef _ImpactDart = int Function(Pointer<Void>, Pointer<Int32>);
|
||||
|
||||
typedef _StateHashC = Uint64 Function(Pointer<Void>);
|
||||
typedef _StateHashDart = int Function(Pointer<Void>);
|
||||
|
||||
typedef _ReplayJsonC = Pointer<Utf8> Function(Pointer<Void>);
|
||||
typedef _ReplayJsonDart = Pointer<Utf8> Function(Pointer<Void>);
|
||||
|
||||
typedef _StringFreeC = Void Function(Pointer<Utf8>);
|
||||
typedef _StringFreeDart = void Function(Pointer<Utf8>);
|
||||
|
||||
typedef _PlaybackC = Pointer<Void> Function(Pointer<Utf8>);
|
||||
typedef _PlaybackDart = Pointer<Void> Function(Pointer<Utf8>);
|
||||
|
||||
typedef _IsPlaybackC = Int32 Function(Pointer<Void>);
|
||||
typedef _IsPlaybackDart = int Function(Pointer<Void>);
|
||||
|
||||
/// Thin typed wrapper around the Rust match handle.
|
||||
///
|
||||
/// The handle owns the simulation; Dart only pulls snapshots for rendering.
|
||||
class NativeMatch extends MatchSource {
|
||||
NativeMatch(int seed) : _handle = _newMatch(seed);
|
||||
|
||||
/// Creates a handle that replays a recorded match (see [replayJson]).
|
||||
NativeMatch.playback(String replayJson)
|
||||
: _handle = _createPlayback(replayJson) {
|
||||
if (_handle.address == 0) {
|
||||
throw StateError('replay rejected (content or sim version mismatch)');
|
||||
}
|
||||
}
|
||||
|
||||
static Pointer<Void> _createPlayback(String json) {
|
||||
final encoded = json.toNativeUtf8();
|
||||
try {
|
||||
return _newPlayback(encoded);
|
||||
} finally {
|
||||
calloc.free(encoded);
|
||||
}
|
||||
}
|
||||
|
||||
final Pointer<Void> _handle;
|
||||
bool _disposed = false;
|
||||
|
||||
@@ -102,6 +134,16 @@ class NativeMatch extends MatchSource {
|
||||
_lib.lookupFunction<_ProjectileC, _ProjectileDart>('iron_arc_projectile');
|
||||
static final _ImpactDart _lastImpact =
|
||||
_lib.lookupFunction<_ImpactC, _ImpactDart>('iron_arc_last_impact');
|
||||
static final _StateHashDart _stateHash =
|
||||
_lib.lookupFunction<_StateHashC, _StateHashDart>('iron_arc_state_hash');
|
||||
static final _ReplayJsonDart _replayJson =
|
||||
_lib.lookupFunction<_ReplayJsonC, _ReplayJsonDart>('iron_arc_replay_json');
|
||||
static final _StringFreeDart _stringFree =
|
||||
_lib.lookupFunction<_StringFreeC, _StringFreeDart>('iron_arc_string_free');
|
||||
static final _PlaybackDart _newPlayback = _lib
|
||||
.lookupFunction<_PlaybackC, _PlaybackDart>('iron_arc_new_match_playback');
|
||||
static final _IsPlaybackDart _isPlayback =
|
||||
_lib.lookupFunction<_IsPlaybackC, _IsPlaybackDart>('iron_arc_is_playback');
|
||||
|
||||
/// Phase: 0 aiming, 1 flying, 2 finished.
|
||||
@override
|
||||
@@ -176,6 +218,26 @@ class NativeMatch extends MatchSource {
|
||||
}
|
||||
}
|
||||
|
||||
/// Current state hash (desync detection against the server).
|
||||
@override
|
||||
int stateHash() => BigInt.from(_stateHash(_handle)).toSigned(64).toInt();
|
||||
|
||||
/// Serialised replay (ReplayV1 JSON) of everything played so far.
|
||||
@override
|
||||
String replayJson() {
|
||||
final ptr = _replayJson(_handle);
|
||||
if (ptr.address == 0) return '';
|
||||
try {
|
||||
return ptr.toDartString();
|
||||
} finally {
|
||||
_stringFree(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
/// True when this handle plays back a recorded match.
|
||||
@override
|
||||
bool get isPlayback => _isPlayback(_handle) == 1;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
if (_disposed) return;
|
||||
|
||||
Reference in New Issue
Block a user