Skip to content

Commit

Permalink
Fix performance
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Sep 27, 2024
1 parent 8f46f2c commit 46382eb
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 218 deletions.
2 changes: 1 addition & 1 deletion api/lib/quokka_api.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// The Linwood Quokka API
library quokka_api;
library;

export 'models.dart';
export 'event.dart';
Expand Down
47 changes: 26 additions & 21 deletions api/lib/src/event/process/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ WorldState? processServerEvent(
case ObjectsChanged():
return state.mapTableOrDefault(event.cell.table, (table) {
final cell = table.cells[event.cell.position] ?? TableCell();
return table.copyWith.cells.replace(
event.cell.position, cell.copyWith(objects: event.objects));
final newCell = cell.copyWith(objects: event.objects);
if (newCell.isEmpty) {
return table.copyWith.cells.remove(event.cell.position);
}
return table.copyWith.cells.replace(event.cell.position, newCell);
});
case CellShuffled(positions: final positions):
return state.mapTableOrDefault(event.cell.table, (table) {
Expand Down Expand Up @@ -172,32 +175,30 @@ WorldState? processServerEvent(
event.cell.position,
cell.copyWith.objects.replace(objectIndex,
object.copyWith(hidden: event.hide ?? !object.hidden)));
} else {
final hidden =
!(event.hide ?? cell.objects.firstOrNull?.hidden ?? false);
return table.copyWith.cells.replace(
event.cell.position,
cell.copyWith(
objects: cell.objects
.map((e) => e.copyWith(hidden: hidden))
.toList(),
));
}
final hidden =
!(event.hide ?? cell.objects.firstOrNull?.hidden ?? false);
return table.copyWith.cells.replace(
event.cell.position,
cell.copyWith(
objects:
cell.objects.map((e) => e.copyWith(hidden: hidden)).toList(),
));
});
case CellItemsCleared():
return state.mapTableOrDefault(event.cell.table, (table) {
final cell = table.cells[event.cell.position] ?? TableCell();
final objectIndex = event.object;
var newCell = cell;
if (objectIndex != null) {
return table.copyWith.cells.replace(
event.cell.position, cell.copyWith.objects.removeAt(objectIndex));
newCell = cell.copyWith.objects.removeAt(objectIndex);
} else {
return table.copyWith.cells.replace(
event.cell.position,
cell.copyWith(
objects: [],
));
newCell = cell.copyWith(objects: []);
}
if (newCell.isEmpty) {
return table.copyWith.cells.remove(event.cell.position);
}
return table.copyWith.cells.replace(event.cell.position, newCell);
});
case ObjectIndexChanged():
return state.mapTableOrDefault(event.cell.table, (table) {
Expand Down Expand Up @@ -256,8 +257,12 @@ WorldState? processServerEvent(
return state.mapTableOrDefault(event.table, (table) {
final cells = Map<VectorDefinition, TableCell>.from(table.cells);
for (final entry in event.tiles.entries) {
cells[entry.key] =
table.getCell(entry.key).copyWith(tiles: entry.value);
final newCell = table.getCell(entry.key).copyWith(tiles: entry.value);
if (newCell.isEmpty) {
cells.remove(entry.key);
} else {
cells[entry.key] = newCell;
}
}
return table.copyWith(cells: cells);
});
Expand Down
2 changes: 1 addition & 1 deletion api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ dependencies:
# path: ^1.8.0

dev_dependencies:
lints: ^4.0.0
lints: ^5.0.0
dart_mappable_builder: ^4.2.3
build_runner: ^2.4.12
26 changes: 13 additions & 13 deletions app/android/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ GEM
artifactory (3.0.17)
atomos (0.1.3)
aws-eventstream (1.3.0)
aws-partitions (1.971.0)
aws-sdk-core (3.203.0)
aws-partitions (1.980.0)
aws-sdk-core (3.209.1)
aws-eventstream (~> 1, >= 1.3.0)
aws-partitions (~> 1, >= 1.651.0)
aws-sigv4 (~> 1.9)
jmespath (~> 1, >= 1.6.1)
aws-sdk-kms (1.89.0)
aws-sdk-core (~> 3, >= 3.203.0)
aws-sdk-kms (1.94.0)
aws-sdk-core (~> 3, >= 3.207.0)
aws-sigv4 (~> 1.5)
aws-sdk-s3 (1.160.0)
aws-sdk-core (~> 3, >= 3.203.0)
aws-sdk-s3 (1.166.0)
aws-sdk-core (~> 3, >= 3.207.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.5)
aws-sigv4 (1.9.1)
aws-sigv4 (1.10.0)
aws-eventstream (~> 1, >= 1.0.2)
babosa (1.0.4)
base64 (0.2.0)
Expand All @@ -39,7 +39,7 @@ GEM
dotenv (2.8.1)
emoji_regex (3.2.3)
excon (0.111.0)
faraday (1.10.3)
faraday (1.10.4)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
Expand All @@ -65,10 +65,10 @@ GEM
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
faraday_middleware (1.2.0)
faraday_middleware (1.2.1)
faraday (~> 1.0)
fastimage (2.3.1)
fastlane (2.222.0)
fastlane (2.223.1)
CFPropertyList (>= 2.3, < 4.0.0)
addressable (>= 2.8, < 3.0.0)
artifactory (~> 3.0)
Expand Down Expand Up @@ -152,7 +152,7 @@ GEM
httpclient (2.8.3)
jmespath (1.6.2)
json (2.7.2)
jwt (2.8.2)
jwt (2.9.1)
base64
mini_magick (4.13.2)
mini_mime (1.1.5)
Expand Down Expand Up @@ -195,7 +195,7 @@ GEM
tty-spinner (0.9.3)
tty-cursor (~> 0.7)
uber (0.1.0)
unicode-display_width (2.5.0)
unicode-display_width (2.6.0)
word_wrap (1.0.0)
xcodeproj (1.25.0)
CFPropertyList (>= 2.3.3, < 4.0)
Expand All @@ -220,4 +220,4 @@ DEPENDENCIES
screengrab

BUNDLED WITH
2.5.18
2.5.20
14 changes: 12 additions & 2 deletions app/lib/board/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,29 @@ import 'package:quokka_api/quokka_api.dart';
class GameBoardBackground extends PositionComponent
with FlameBlocListenable<WorldBloc, ClientWorldState> {
SpriteComponent? _sprite;
bool _isDirty = true;

GameBoardBackground({super.size});

@override
void onInitialState(ClientWorldState state) => updateBackground(state);
void onInitialState(ClientWorldState state) => _isDirty = true;

@override
bool listenWhen(ClientWorldState previousState, ClientWorldState newState) =>
previousState.table.background != newState.table.background ||
previousState.info.packs != newState.info.packs;

@override
void onNewState(ClientWorldState state) => updateBackground(state);
void onNewState(ClientWorldState state) => _isDirty = true;

@override
void update(double dt) {
super.update(dt);
if (_isDirty) {
_isDirty = false;
updateBackground(bloc.state);
}
}

Future<Sprite?> _loadSprite(
ClientWorldState state, PackItem<BackgroundDefinition>? item) {
Expand Down
58 changes: 29 additions & 29 deletions app/lib/board/cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ class GameCell extends PositionComponent
previousState.table.cells[definition] !=
newState.table.cells[definition] ||
previousState.teamMembers != newState.teamMembers ||
previousState.info.teams != newState.info.teams ||
previousState.colorScheme != newState.colorScheme;
}

Expand Down Expand Up @@ -161,7 +160,7 @@ class GameCell extends PositionComponent

GameObject? _currentTop;
BoardTile? _currentTile;
bool _currentVisible = false;
bool _currentVisible = true;

@override
void onNewState(ClientWorldState state) {
Expand Down Expand Up @@ -215,39 +214,40 @@ class GameCell extends PositionComponent
_currentTop = top;
_currentVisible = visible;
_currentTile = tile;
_cardComponent?.removeFromParent();
_boardComponent?.removeFromParent();
_cardComponent = null;
_boardComponent = null;
final paint = Paint()..isAntiAlias = false;
if (tile != null) {
_boardComponent = SpriteComponent(
sprite:
await state.assetManager.loadBoardSprite(tile.asset, tile.tile) ??
game.blankSprite,
size: size);
await add(_boardComponent!);
_boardComponent ??= SpriteComponent(
size: size,
paint: paint,
);
_boardComponent?.sprite =
await state.assetManager.loadBoardSprite(tile.asset, tile.tile) ??
game.blankSprite;
if (!_boardComponent!.isMounted) {
add(_boardComponent!);
}
} else {
_boardComponent?.removeFromParent();
}
if (top != null) {
_cardComponent = SpriteComponent(
sprite: await state.assetManager.loadFigureSprite(
top.asset,
top.hidden || !state.isCellVisible(toGlobalDefinition(state))
? null
: top.variation) ??
game.blankSprite,
size: size);
await add(_cardComponent!);
_cardComponent ??= SpriteComponent(
size: size,
paint: paint,
);
_cardComponent?.sprite = await state.assetManager.loadFigureSprite(
top.asset,
top.hidden || !state.isCellVisible(toGlobalDefinition(state))
? null
: top.variation) ??
game.blankSprite;
if (!_cardComponent!.isMounted) {
add(_cardComponent!);
}
} else {
_cardComponent?.removeFromParent();
}
}

late double startZoom;

@override
void onDragStart(DragStartEvent event) {
super.onDragStart(event);
startZoom = game.camera.viewfinder.zoom;
}

@override
void onDragUpdate(DragUpdateEvent event) {
super.onDragUpdate(event);
Expand Down
Loading

0 comments on commit 46382eb

Please sign in to comment.