From 4a53dc7e4665ddad5f7f777189d484cc56a3e22e Mon Sep 17 00:00:00 2001 From: Oliver Brunsmann Date: Fri, 3 Mar 2023 08:35:44 +0100 Subject: [PATCH] Added presences to match Closed #59 --- CHANGELOG.md | 1 + lib/src/models/match.dart | 18 +- lib/src/models/match.freezed.dart | 453 ++++++++++++++++++++++++++++- lib/src/models/match.g.dart | 28 ++ lib/src/models/status.dart | 4 + lib/src/models/status.freezed.dart | 21 +- lib/src/models/status.g.dart | 25 ++ 7 files changed, 543 insertions(+), 7 deletions(-) create mode 100644 lib/src/models/status.g.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 930348c..4f0c98f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * Added creating a match by match name * Added missing `vars` to session model +* Added `presences` to match ## 1.0.0-dev.5 diff --git a/lib/src/models/match.dart b/lib/src/models/match.dart index c56e7df..359899f 100644 --- a/lib/src/models/match.dart +++ b/lib/src/models/match.dart @@ -19,22 +19,36 @@ class Match with _$Match { String? handlerName, }) = _Match; + factory Match.realtime({ + required String matchId, + required bool authoritative, + required String label, + required int size, + int? tickRate, + String? handlerName, + required List presences, + }) = RealtimeMatch; + factory Match.fromJson(Map json) => _$MatchFromJson(json); - factory Match.fromDto(api.Match dto) => Match( + factory Match.fromDto(api.Match dto) => Match.realtime( matchId: dto.matchId, authoritative: dto.authoritative, handlerName: dto.handlerName, label: dto.label.value, size: dto.size, tickRate: dto.tickRate, + presences: [], ); - factory Match.fromRtpb(rtpb.Match dto) => Match( + factory Match.fromRtpb(rtpb.Match dto) => Match.realtime( matchId: dto.matchId, authoritative: dto.authoritative, label: dto.label.value, size: dto.size, + presences: dto.presences + .map((e) => UserPresence.fromDto(e)) + .toList(growable: false), ); } diff --git a/lib/src/models/match.freezed.dart b/lib/src/models/match.freezed.dart index e3a335c..4116023 100644 --- a/lib/src/models/match.freezed.dart +++ b/lib/src/models/match.freezed.dart @@ -15,7 +15,16 @@ final _privateConstructorUsedError = UnsupportedError( 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); Match _$MatchFromJson(Map json) { - return _Match.fromJson(json); + switch (json['runtimeType']) { + case 'default': + return _Match.fromJson(json); + case 'realtime': + return RealtimeMatch.fromJson(json); + + default: + throw CheckedFromJsonException(json, 'runtimeType', 'Match', + 'Invalid union type "${json['runtimeType']}"!'); + } } /// @nodoc @@ -26,7 +35,68 @@ mixin _$Match { int get size => throw _privateConstructorUsedError; int? get tickRate => throw _privateConstructorUsedError; String? get handlerName => throw _privateConstructorUsedError; - + @optionalTypeArgs + TResult when( + TResult Function(String matchId, bool authoritative, String label, int size, + int? tickRate, String? handlerName) + $default, { + required TResult Function( + String matchId, + bool authoritative, + String label, + int size, + int? tickRate, + String? handlerName, + List presences) + realtime, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult? whenOrNull( + TResult? Function(String matchId, bool authoritative, String label, + int size, int? tickRate, String? handlerName)? + $default, { + TResult? Function( + String matchId, + bool authoritative, + String label, + int size, + int? tickRate, + String? handlerName, + List presences)? + realtime, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult maybeWhen( + TResult Function(String matchId, bool authoritative, String label, int size, + int? tickRate, String? handlerName)? + $default, { + TResult Function(String matchId, bool authoritative, String label, int size, + int? tickRate, String? handlerName, List presences)? + realtime, + required TResult orElse(), + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult map( + TResult Function(_Match value) $default, { + required TResult Function(RealtimeMatch value) realtime, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult? mapOrNull( + TResult? Function(_Match value)? $default, { + TResult? Function(RealtimeMatch value)? realtime, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult maybeMap( + TResult Function(_Match value)? $default, { + TResult Function(RealtimeMatch value)? realtime, + required TResult orElse(), + }) => + throw _privateConstructorUsedError; Map toJson() => throw _privateConstructorUsedError; @JsonKey(ignore: true) $MatchCopyWith get copyWith => throw _privateConstructorUsedError; @@ -164,8 +234,10 @@ class _$_Match extends _Match { required this.label, required this.size, this.tickRate, - this.handlerName}) - : super._(); + this.handlerName, + final String? $type}) + : $type = $type ?? 'default', + super._(); factory _$_Match.fromJson(Map json) => _$$_MatchFromJson(json); @@ -183,6 +255,9 @@ class _$_Match extends _Match { @override final String? handlerName; + @JsonKey(name: 'runtimeType') + final String $type; + @override String toString() { return 'Match(matchId: $matchId, authoritative: $authoritative, label: $label, size: $size, tickRate: $tickRate, handlerName: $handlerName)'; @@ -215,6 +290,94 @@ class _$_Match extends _Match { _$$_MatchCopyWith<_$_Match> get copyWith => __$$_MatchCopyWithImpl<_$_Match>(this, _$identity); + @override + @optionalTypeArgs + TResult when( + TResult Function(String matchId, bool authoritative, String label, int size, + int? tickRate, String? handlerName) + $default, { + required TResult Function( + String matchId, + bool authoritative, + String label, + int size, + int? tickRate, + String? handlerName, + List presences) + realtime, + }) { + return $default(matchId, authoritative, label, size, tickRate, handlerName); + } + + @override + @optionalTypeArgs + TResult? whenOrNull( + TResult? Function(String matchId, bool authoritative, String label, + int size, int? tickRate, String? handlerName)? + $default, { + TResult? Function( + String matchId, + bool authoritative, + String label, + int size, + int? tickRate, + String? handlerName, + List presences)? + realtime, + }) { + return $default?.call( + matchId, authoritative, label, size, tickRate, handlerName); + } + + @override + @optionalTypeArgs + TResult maybeWhen( + TResult Function(String matchId, bool authoritative, String label, int size, + int? tickRate, String? handlerName)? + $default, { + TResult Function(String matchId, bool authoritative, String label, int size, + int? tickRate, String? handlerName, List presences)? + realtime, + required TResult orElse(), + }) { + if ($default != null) { + return $default( + matchId, authoritative, label, size, tickRate, handlerName); + } + return orElse(); + } + + @override + @optionalTypeArgs + TResult map( + TResult Function(_Match value) $default, { + required TResult Function(RealtimeMatch value) realtime, + }) { + return $default(this); + } + + @override + @optionalTypeArgs + TResult? mapOrNull( + TResult? Function(_Match value)? $default, { + TResult? Function(RealtimeMatch value)? realtime, + }) { + return $default?.call(this); + } + + @override + @optionalTypeArgs + TResult maybeMap( + TResult Function(_Match value)? $default, { + TResult Function(RealtimeMatch value)? realtime, + required TResult orElse(), + }) { + if ($default != null) { + return $default(this); + } + return orElse(); + } + @override Map toJson() { return _$$_MatchToJson( @@ -253,6 +416,288 @@ abstract class _Match extends Match { throw _privateConstructorUsedError; } +/// @nodoc +abstract class _$$RealtimeMatchCopyWith<$Res> implements $MatchCopyWith<$Res> { + factory _$$RealtimeMatchCopyWith( + _$RealtimeMatch value, $Res Function(_$RealtimeMatch) then) = + __$$RealtimeMatchCopyWithImpl<$Res>; + @override + @useResult + $Res call( + {String matchId, + bool authoritative, + String label, + int size, + int? tickRate, + String? handlerName, + List presences}); +} + +/// @nodoc +class __$$RealtimeMatchCopyWithImpl<$Res> + extends _$MatchCopyWithImpl<$Res, _$RealtimeMatch> + implements _$$RealtimeMatchCopyWith<$Res> { + __$$RealtimeMatchCopyWithImpl( + _$RealtimeMatch _value, $Res Function(_$RealtimeMatch) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? matchId = null, + Object? authoritative = null, + Object? label = null, + Object? size = null, + Object? tickRate = freezed, + Object? handlerName = freezed, + Object? presences = null, + }) { + return _then(_$RealtimeMatch( + matchId: null == matchId + ? _value.matchId + : matchId // ignore: cast_nullable_to_non_nullable + as String, + authoritative: null == authoritative + ? _value.authoritative + : authoritative // ignore: cast_nullable_to_non_nullable + as bool, + label: null == label + ? _value.label + : label // ignore: cast_nullable_to_non_nullable + as String, + size: null == size + ? _value.size + : size // ignore: cast_nullable_to_non_nullable + as int, + tickRate: freezed == tickRate + ? _value.tickRate + : tickRate // ignore: cast_nullable_to_non_nullable + as int?, + handlerName: freezed == handlerName + ? _value.handlerName + : handlerName // ignore: cast_nullable_to_non_nullable + as String?, + presences: null == presences + ? _value._presences + : presences // ignore: cast_nullable_to_non_nullable + as List, + )); + } +} + +/// @nodoc +@JsonSerializable() +class _$RealtimeMatch extends RealtimeMatch { + _$RealtimeMatch( + {required this.matchId, + required this.authoritative, + required this.label, + required this.size, + this.tickRate, + this.handlerName, + required final List presences, + final String? $type}) + : _presences = presences, + $type = $type ?? 'realtime', + super._(); + + factory _$RealtimeMatch.fromJson(Map json) => + _$$RealtimeMatchFromJson(json); + + @override + final String matchId; + @override + final bool authoritative; + @override + final String label; + @override + final int size; + @override + final int? tickRate; + @override + final String? handlerName; + final List _presences; + @override + List get presences { + if (_presences is EqualUnmodifiableListView) return _presences; + // ignore: implicit_dynamic_type + return EqualUnmodifiableListView(_presences); + } + + @JsonKey(name: 'runtimeType') + final String $type; + + @override + String toString() { + return 'Match.realtime(matchId: $matchId, authoritative: $authoritative, label: $label, size: $size, tickRate: $tickRate, handlerName: $handlerName, presences: $presences)'; + } + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$RealtimeMatch && + (identical(other.matchId, matchId) || other.matchId == matchId) && + (identical(other.authoritative, authoritative) || + other.authoritative == authoritative) && + (identical(other.label, label) || other.label == label) && + (identical(other.size, size) || other.size == size) && + (identical(other.tickRate, tickRate) || + other.tickRate == tickRate) && + (identical(other.handlerName, handlerName) || + other.handlerName == handlerName) && + const DeepCollectionEquality() + .equals(other._presences, _presences)); + } + + @JsonKey(ignore: true) + @override + int get hashCode => Object.hash( + runtimeType, + matchId, + authoritative, + label, + size, + tickRate, + handlerName, + const DeepCollectionEquality().hash(_presences)); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$RealtimeMatchCopyWith<_$RealtimeMatch> get copyWith => + __$$RealtimeMatchCopyWithImpl<_$RealtimeMatch>(this, _$identity); + + @override + @optionalTypeArgs + TResult when( + TResult Function(String matchId, bool authoritative, String label, int size, + int? tickRate, String? handlerName) + $default, { + required TResult Function( + String matchId, + bool authoritative, + String label, + int size, + int? tickRate, + String? handlerName, + List presences) + realtime, + }) { + return realtime( + matchId, authoritative, label, size, tickRate, handlerName, presences); + } + + @override + @optionalTypeArgs + TResult? whenOrNull( + TResult? Function(String matchId, bool authoritative, String label, + int size, int? tickRate, String? handlerName)? + $default, { + TResult? Function( + String matchId, + bool authoritative, + String label, + int size, + int? tickRate, + String? handlerName, + List presences)? + realtime, + }) { + return realtime?.call( + matchId, authoritative, label, size, tickRate, handlerName, presences); + } + + @override + @optionalTypeArgs + TResult maybeWhen( + TResult Function(String matchId, bool authoritative, String label, int size, + int? tickRate, String? handlerName)? + $default, { + TResult Function(String matchId, bool authoritative, String label, int size, + int? tickRate, String? handlerName, List presences)? + realtime, + required TResult orElse(), + }) { + if (realtime != null) { + return realtime(matchId, authoritative, label, size, tickRate, + handlerName, presences); + } + return orElse(); + } + + @override + @optionalTypeArgs + TResult map( + TResult Function(_Match value) $default, { + required TResult Function(RealtimeMatch value) realtime, + }) { + return realtime(this); + } + + @override + @optionalTypeArgs + TResult? mapOrNull( + TResult? Function(_Match value)? $default, { + TResult? Function(RealtimeMatch value)? realtime, + }) { + return realtime?.call(this); + } + + @override + @optionalTypeArgs + TResult maybeMap( + TResult Function(_Match value)? $default, { + TResult Function(RealtimeMatch value)? realtime, + required TResult orElse(), + }) { + if (realtime != null) { + return realtime(this); + } + return orElse(); + } + + @override + Map toJson() { + return _$$RealtimeMatchToJson( + this, + ); + } +} + +abstract class RealtimeMatch extends Match { + factory RealtimeMatch( + {required final String matchId, + required final bool authoritative, + required final String label, + required final int size, + final int? tickRate, + final String? handlerName, + required final List presences}) = _$RealtimeMatch; + RealtimeMatch._() : super._(); + + factory RealtimeMatch.fromJson(Map json) = + _$RealtimeMatch.fromJson; + + @override + String get matchId; + @override + bool get authoritative; + @override + String get label; + @override + int get size; + @override + int? get tickRate; + @override + String? get handlerName; + List get presences; + @override + @JsonKey(ignore: true) + _$$RealtimeMatchCopyWith<_$RealtimeMatch> get copyWith => + throw _privateConstructorUsedError; +} + /// @nodoc mixin _$Party { /// Unique party identifier. diff --git a/lib/src/models/match.g.dart b/lib/src/models/match.g.dart index 0f52bb4..6b67d70 100644 --- a/lib/src/models/match.g.dart +++ b/lib/src/models/match.g.dart @@ -13,6 +13,7 @@ _$_Match _$$_MatchFromJson(Map json) => _$_Match( size: json['size'] as int, tickRate: json['tickRate'] as int?, handlerName: json['handlerName'] as String?, + $type: json['runtimeType'] as String?, ); Map _$$_MatchToJson(_$_Match instance) => { @@ -22,4 +23,31 @@ Map _$$_MatchToJson(_$_Match instance) => { 'size': instance.size, 'tickRate': instance.tickRate, 'handlerName': instance.handlerName, + 'runtimeType': instance.$type, + }; + +_$RealtimeMatch _$$RealtimeMatchFromJson(Map json) => + _$RealtimeMatch( + matchId: json['matchId'] as String, + authoritative: json['authoritative'] as bool, + label: json['label'] as String, + size: json['size'] as int, + tickRate: json['tickRate'] as int?, + handlerName: json['handlerName'] as String?, + presences: (json['presences'] as List) + .map((e) => UserPresence.fromJson(e as Map)) + .toList(), + $type: json['runtimeType'] as String?, + ); + +Map _$$RealtimeMatchToJson(_$RealtimeMatch instance) => + { + 'matchId': instance.matchId, + 'authoritative': instance.authoritative, + 'label': instance.label, + 'size': instance.size, + 'tickRate': instance.tickRate, + 'handlerName': instance.handlerName, + 'presences': instance.presences, + 'runtimeType': instance.$type, }; diff --git a/lib/src/models/status.dart b/lib/src/models/status.dart index 9e1a658..2538cf0 100644 --- a/lib/src/models/status.dart +++ b/lib/src/models/status.dart @@ -2,6 +2,7 @@ import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:nakama/src/api/rtapi.dart' as rtpb; part 'status.freezed.dart'; +part 'status.g.dart'; @freezed class UserPresence with _$UserPresence { @@ -33,6 +34,9 @@ class UserPresence with _$UserPresence { persistence: dto.persistence, status: dto.status.value.isNotEmpty ? dto.status.value : null, ); + + factory UserPresence.fromJson(Map json) => + _$UserPresenceFromJson(json); } @freezed diff --git a/lib/src/models/status.freezed.dart b/lib/src/models/status.freezed.dart index 634b143..dcb07e3 100644 --- a/lib/src/models/status.freezed.dart +++ b/lib/src/models/status.freezed.dart @@ -14,6 +14,10 @@ T _$identity(T value) => value; final _privateConstructorUsedError = UnsupportedError( 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); +UserPresence _$UserPresenceFromJson(Map json) { + return _UserPresence.fromJson(json); +} + /// @nodoc mixin _$UserPresence { /// The user this presence belongs to. @@ -33,6 +37,7 @@ mixin _$UserPresence { /// A user-set status message for this stream, if applicable. String? get status => throw _privateConstructorUsedError; + Map toJson() => throw _privateConstructorUsedError; @JsonKey(ignore: true) $UserPresenceCopyWith get copyWith => throw _privateConstructorUsedError; @@ -155,7 +160,7 @@ class __$$_UserPresenceCopyWithImpl<$Res> } /// @nodoc - +@JsonSerializable() class _$_UserPresence extends _UserPresence { const _$_UserPresence( {required this.userId, @@ -165,6 +170,9 @@ class _$_UserPresence extends _UserPresence { this.status}) : super._(); + factory _$_UserPresence.fromJson(Map json) => + _$$_UserPresenceFromJson(json); + /// The user this presence belongs to. @override final String userId; @@ -207,6 +215,7 @@ class _$_UserPresence extends _UserPresence { (identical(other.status, status) || other.status == status)); } + @JsonKey(ignore: true) @override int get hashCode => Object.hash( runtimeType, userId, sessionId, username, persistence, status); @@ -216,6 +225,13 @@ class _$_UserPresence extends _UserPresence { @pragma('vm:prefer-inline') _$$_UserPresenceCopyWith<_$_UserPresence> get copyWith => __$$_UserPresenceCopyWithImpl<_$_UserPresence>(this, _$identity); + + @override + Map toJson() { + return _$$_UserPresenceToJson( + this, + ); + } } abstract class _UserPresence extends UserPresence { @@ -227,6 +243,9 @@ abstract class _UserPresence extends UserPresence { final String? status}) = _$_UserPresence; const _UserPresence._() : super._(); + factory _UserPresence.fromJson(Map json) = + _$_UserPresence.fromJson; + @override /// The user this presence belongs to. diff --git a/lib/src/models/status.g.dart b/lib/src/models/status.g.dart new file mode 100644 index 0000000..fb09549 --- /dev/null +++ b/lib/src/models/status.g.dart @@ -0,0 +1,25 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'status.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$_UserPresence _$$_UserPresenceFromJson(Map json) => + _$_UserPresence( + userId: json['userId'] as String, + sessionId: json['sessionId'] as String, + username: json['username'] as String, + persistence: json['persistence'] as bool, + status: json['status'] as String?, + ); + +Map _$$_UserPresenceToJson(_$_UserPresence instance) => + { + 'userId': instance.userId, + 'sessionId': instance.sessionId, + 'username': instance.username, + 'persistence': instance.persistence, + 'status': instance.status, + };