Skip to content

Commit

Permalink
Use list instead of map for packsignatures
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Nov 19, 2024
1 parent 7a3594f commit 858ad9c
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 108 deletions.
12 changes: 6 additions & 6 deletions api/lib/src/event/event.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ class WorldInitializedMapper extends SubClassMapperBase<WorldInitialized> {
static int? _$id(WorldInitialized v) => v.id;
static const Field<WorldInitialized, int> _f$id =
Field('id', _$id, opt: true);
static Map<String, SignatureMetadata>? _$packsSignature(WorldInitialized v) =>
static List<SignatureMetadata>? _$packsSignature(WorldInitialized v) =>
v.packsSignature;
static const Field<WorldInitialized, Map<String, SignatureMetadata>>
static const Field<WorldInitialized, List<SignatureMetadata>>
_f$packsSignature = Field('packsSignature', _$packsSignature, opt: true);

@override
Expand Down Expand Up @@ -305,7 +305,7 @@ abstract class WorldInitializedCopyWith<$R, $In extends WorldInitialized, $Out>
GameInfoCopyWith<$R, GameInfo, GameInfo>? get info;
MapCopyWith<$R, String, Set<int>, ObjectCopyWith<$R, Set<int>, Set<int>>>?
get teamMembers;
MapCopyWith<$R, String, SignatureMetadata,
ListCopyWith<$R, SignatureMetadata,
SignatureMetadataCopyWith<$R, SignatureMetadata, SignatureMetadata>>?
get packsSignature;
@override
Expand All @@ -314,7 +314,7 @@ abstract class WorldInitializedCopyWith<$R, $In extends WorldInitialized, $Out>
GameInfo? info,
Map<String, Set<int>>? teamMembers,
int? id,
Map<String, SignatureMetadata>? packsSignature});
List<SignatureMetadata>? packsSignature});
WorldInitializedCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}
Expand Down Expand Up @@ -342,10 +342,10 @@ class _WorldInitializedCopyWithImpl<$R, $Out>
(v) => call(teamMembers: v))
: null;
@override
MapCopyWith<$R, String, SignatureMetadata,
ListCopyWith<$R, SignatureMetadata,
SignatureMetadataCopyWith<$R, SignatureMetadata, SignatureMetadata>>?
get packsSignature => $value.packsSignature != null
? MapCopyWith($value.packsSignature!, (v, t) => v.copyWith.$chain(t),
? ListCopyWith($value.packsSignature!, (v, t) => v.copyWith.$chain(t),
(v) => call(packsSignature: v))
: null;
@override
Expand Down
6 changes: 4 additions & 2 deletions api/lib/src/event/process/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ ServerResponse? processClientEvent(
table: state.protectTable(channel),
info: state.info,
id: channel,
packsSignature:
assetManager.createSignature(state.info.packs.toSet()),
packsSignature: assetManager
.createSignature(state.info.packs.toSet())
.values
.toList(),
teamMembers: state.teamMembers,
),
channel);
Expand Down
17 changes: 9 additions & 8 deletions api/lib/src/event/process/server.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:dart_leap/dart_leap.dart';
import 'package:networker/networker.dart';
import 'package:setonix_api/setonix_api.dart';
Expand All @@ -6,8 +7,8 @@ bool isValidServerEvent(ServerWorldEvent event, WorldState state) =>
switch (event) {
WorldInitialized() =>
event.info?.packs.length == event.packsSignature?.length &&
(event.info?.packs.every(
(e) => event.packsSignature?.containsKey(e) ?? false) ??
(event.info?.packs.every((e) =>
event.packsSignature?.any((p) => p.id == e) ?? false) ??
true),
TeamJoined() => state.info.teams.containsKey(event.team),
TeamLeft() => state.info.teams.containsKey(event.team),
Expand Down Expand Up @@ -52,7 +53,7 @@ bool isValidServerEvent(ServerWorldEvent event, WorldState state) =>
sealed class FatalServerEventError {}

final class InvalidPacksError extends FatalServerEventError {
final Map<String, SignatureMetadata> signature;
final List<SignatureMetadata> signature;

InvalidPacksError({required this.signature});

Expand All @@ -61,11 +62,11 @@ final class InvalidPacksError extends FatalServerEventError {
'Server requested packs, that are not available on the client (or is empty): $signature';
}

bool isServerSupported(Map<String, SignatureMetadata> mySignature,
Map<String, SignatureMetadata> serverSignature) {
for (final entry in serverSignature.entries) {
final current = mySignature[entry.key];
if (current == null || !current.supports(entry.value)) {
bool isServerSupported(List<SignatureMetadata> mySignature,
List<SignatureMetadata> serverSignature) {
for (final entry in serverSignature) {
final current = mySignature.firstWhereOrNull((e) => e.id == entry.id);
if (current == null || !current.supports(entry)) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/lib/src/event/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class WorldInitialized extends ServerWorldEvent
final GameInfo? info;
final Map<String, Set<Channel>>? teamMembers;
final Channel? id;
final Map<String, SignatureMetadata>? packsSignature;
final List<SignatureMetadata>? packsSignature;

WorldInitialized({
this.table,
Expand Down
2 changes: 2 additions & 0 deletions api/lib/src/models/meta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ final class FileMetadata with FileMetadataMappable {
final class SignatureMetadata with SignatureMetadataMappable {
final String id;
final FileMetadata metadata;
final List<String> downloadUrls;

SignatureMetadata({
required this.id,
required this.metadata,
this.downloadUrls = const [],
});

bool supports(SignatureMetadata other) {
Expand Down
28 changes: 23 additions & 5 deletions api/lib/src/models/meta.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,22 @@ class SignatureMetadataMapper extends ClassMapperBase<SignatureMetadata> {
static FileMetadata _$metadata(SignatureMetadata v) => v.metadata;
static const Field<SignatureMetadata, FileMetadata> _f$metadata =
Field('metadata', _$metadata);
static List<String> _$downloadUrls(SignatureMetadata v) => v.downloadUrls;
static const Field<SignatureMetadata, List<String>> _f$downloadUrls =
Field('downloadUrls', _$downloadUrls, opt: true, def: const []);

@override
final MappableFields<SignatureMetadata> fields = const {
#id: _f$id,
#metadata: _f$metadata,
#downloadUrls: _f$downloadUrls,
};

static SignatureMetadata _instantiate(DecodingData data) {
return SignatureMetadata(
id: data.dec(_f$id), metadata: data.dec(_f$metadata));
id: data.dec(_f$id),
metadata: data.dec(_f$metadata),
downloadUrls: data.dec(_f$downloadUrls));
}

@override
Expand Down Expand Up @@ -317,7 +323,8 @@ extension SignatureMetadataValueCopy<$R, $Out>
abstract class SignatureMetadataCopyWith<$R, $In extends SignatureMetadata,
$Out> implements ClassCopyWith<$R, $In, $Out> {
FileMetadataCopyWith<$R, FileMetadata, FileMetadata> get metadata;
$R call({String? id, FileMetadata? metadata});
ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get downloadUrls;
$R call({String? id, FileMetadata? metadata, List<String>? downloadUrls});
SignatureMetadataCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}
Expand All @@ -334,12 +341,23 @@ class _SignatureMetadataCopyWithImpl<$R, $Out>
FileMetadataCopyWith<$R, FileMetadata, FileMetadata> get metadata =>
$value.metadata.copyWith.$chain((v) => call(metadata: v));
@override
$R call({String? id, FileMetadata? metadata}) => $apply(FieldCopyWithData(
{if (id != null) #id: id, if (metadata != null) #metadata: metadata}));
ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>>
get downloadUrls => ListCopyWith(
$value.downloadUrls,
(v, t) => ObjectCopyWith(v, $identity, t),
(v) => call(downloadUrls: v));
@override
$R call({String? id, FileMetadata? metadata, List<String>? downloadUrls}) =>
$apply(FieldCopyWithData({
if (id != null) #id: id,
if (metadata != null) #metadata: metadata,
if (downloadUrls != null) #downloadUrls: downloadUrls
}));
@override
SignatureMetadata $make(CopyWithData data) => SignatureMetadata(
id: data.get(#id, or: $value.id),
metadata: data.get(#metadata, or: $value.metadata));
metadata: data.get(#metadata, or: $value.metadata),
downloadUrls: data.get(#downloadUrls, or: $value.downloadUrls));

@override
SignatureMetadataCopyWith<$R2, SignatureMetadata, $Out2> $chain<$R2, $Out2>(
Expand Down
2 changes: 1 addition & 1 deletion api/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ packages:
source: hosted
version: "4.10.1"
collection:
dependency: transitive
dependency: "direct main"
description:
name: collection
sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76"
Expand Down
1 change: 1 addition & 0 deletions api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies:
path: packages/dart_leap
ref: dfda6c2e3cef2e29511f97e9470fd4deb8e0c573
crypto: ^3.0.5
collection: ^1.18.0
# path: ^1.8.0

dev_dependencies:
Expand Down
3 changes: 2 additions & 1 deletion app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,6 @@
"zoomOut": "Zoom out",
"resetZoom": "Reset zoom",
"thirdPartyContent": "This content is provided by the third party server. Please make sure you don't share any personal information that you don't want to be shared with the server owner. If you don't trust the server, please disconnect using the top left corner menu.",
"showDuplicates": "Show duplicates"
"showDuplicates": "Show duplicates",
"downloadAll": "Download all"
}
136 changes: 136 additions & 0 deletions app/lib/pages/game/error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:go_router/go_router.dart';
import 'package:material_leap/material_leap.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';
import 'package:setonix/bloc/multiplayer.dart';
import 'package:setonix/pages/home/background.dart';
import 'package:setonix_api/setonix_api.dart';

class GameErrorView extends StatelessWidget {
final MultiplayerDisconnectedState state;
final VoidCallback onReconnect;

const GameErrorView({
super.key,
required this.state,
required this.onReconnect,
});

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final error = state.error;
var message = AppLocalizations.of(context).disconnectedMessage;
Widget? content;
if (error is FatalServerEventError) {
message = switch (error) {
InvalidPacksError() => AppLocalizations.of(context).invalidPacks,
};
content = switch (error) {
InvalidPacksError() => _PacksGameErrorView(error: error),
};
}
return Scaffold(
body: Stack(
alignment: Alignment.center,
children: [
const DotsBackground(),
Card.filled(
child: Container(
constraints: const BoxConstraints(
maxWidth: LeapBreakpoints.large,
),
padding: const EdgeInsets.all(8.0),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
AppLocalizations.of(context).disconnected,
textAlign: TextAlign.center,
style: theme.textTheme.headlineSmall,
),
Text(
message,
textAlign: TextAlign.center,
style: theme.textTheme.bodySmall,
),
if (content != null) ...[
const SizedBox(height: 16),
content,
],
const SizedBox(height: 16),
Row(
mainAxisSize: MainAxisSize.min,
children: [
FilledButton(
onPressed: onReconnect,
child: Text(AppLocalizations.of(context).reconnect),
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: () => GoRouter.of(context).go('/'),
child: Text(AppLocalizations.of(context).home),
),
],
),
if (state.error != null) ...[
const SizedBox(height: 16),
Text(state.error.toString()),
],
],
),
),
),
),
],
),
);
}
}

class _PacksGameErrorView extends StatefulWidget {
final InvalidPacksError error;
const _PacksGameErrorView({required this.error});

@override
State<_PacksGameErrorView> createState() => _PacksGameErrorViewState();
}

class _PacksGameErrorViewState extends State<_PacksGameErrorView> {
final List<int> _selectedUrls = [];

@override
Widget build(BuildContext context) {
final packs = widget.error.signature;
return ListView.builder(
shrinkWrap: true,
itemCount: packs.length + 1,
itemBuilder: (context, index) {
if (index == packs.length) {
return Wrap(
children: [
if (packs.any((e) => e.downloadUrls.isNotEmpty))
FilledButton(
onPressed: () {},
child: Text(AppLocalizations.of(context).downloadAll),
),
],
);
}
final pack = packs[index];
final currentDownloadUrl = pack.downloadUrls
.elementAtOrNull(_selectedUrls.elementAtOrNull(index) ?? 0);
return ListTile(
title: Text(pack.metadata.name),
subtitle: Text(currentDownloadUrl ?? ''),
trailing: IconButton(
onPressed: () {},
icon: Icon(PhosphorIconsLight.downloadSimple),
),
);
},
);
}
}
Loading

0 comments on commit 858ad9c

Please sign in to comment.