diff --git a/bluevsred_flutter/lib/main.dart b/bluevsred_flutter/lib/main.dart index be9486c..29fdffe 100644 --- a/bluevsred_flutter/lib/main.dart +++ b/bluevsred_flutter/lib/main.dart @@ -15,6 +15,7 @@ void main() async { WidgetsFlutterBinding.ensureInitialized(); await Flame.device.setLandscape(); await Flame.device.fullScreen(); + // runApp(const ProviderScope(child: MyGame())); runApp(const MyGame()); } diff --git a/bluevsred_flutter/lib/player/active_game_player.dart b/bluevsred_flutter/lib/player/active_game_player.dart new file mode 100644 index 0000000..76af62e --- /dev/null +++ b/bluevsred_flutter/lib/player/active_game_player.dart @@ -0,0 +1,12 @@ +import 'package:bluevsred_shared/bluesvsred_shared.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; + +part 'active_game_player.g.dart'; + +@riverpod +class ActiveGamePlayer extends _$ActiveGamePlayer { + @override + Future build(String creationName) async { + return GamePlayer(name: this.creationName); + } +} diff --git a/bluevsred_flutter/lib/player/active_game_player.g.dart b/bluevsred_flutter/lib/player/active_game_player.g.dart new file mode 100644 index 0000000..db0f4a1 --- /dev/null +++ b/bluevsred_flutter/lib/player/active_game_player.g.dart @@ -0,0 +1,175 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'active_game_player.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$activeGamePlayerHash() => r'6e6474c25bd208585712989d2b8d500cad66ced1'; + +/// Copied from Dart SDK +class _SystemHash { + _SystemHash._(); + + static int combine(int hash, int value) { + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + value); + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); + return hash ^ (hash >> 6); + } + + static int finish(int hash) { + // ignore: parameter_assignments + hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); + // ignore: parameter_assignments + hash = hash ^ (hash >> 11); + return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); + } +} + +abstract class _$ActiveGamePlayer + extends BuildlessAutoDisposeAsyncNotifier { + late final String creationName; + + FutureOr build( + String creationName, + ); +} + +/// See also [ActiveGamePlayer]. +@ProviderFor(ActiveGamePlayer) +const activeGamePlayerProvider = ActiveGamePlayerFamily(); + +/// See also [ActiveGamePlayer]. +class ActiveGamePlayerFamily extends Family> { + /// See also [ActiveGamePlayer]. + const ActiveGamePlayerFamily(); + + /// See also [ActiveGamePlayer]. + ActiveGamePlayerProvider call( + String creationName, + ) { + return ActiveGamePlayerProvider( + creationName, + ); + } + + @override + ActiveGamePlayerProvider getProviderOverride( + covariant ActiveGamePlayerProvider provider, + ) { + return call( + provider.creationName, + ); + } + + static const Iterable? _dependencies = null; + + @override + Iterable? get dependencies => _dependencies; + + static const Iterable? _allTransitiveDependencies = null; + + @override + Iterable? get allTransitiveDependencies => + _allTransitiveDependencies; + + @override + String? get name => r'activeGamePlayerProvider'; +} + +/// See also [ActiveGamePlayer]. +class ActiveGamePlayerProvider + extends AutoDisposeAsyncNotifierProviderImpl { + /// See also [ActiveGamePlayer]. + ActiveGamePlayerProvider( + String creationName, + ) : this._internal( + () => ActiveGamePlayer()..creationName = creationName, + from: activeGamePlayerProvider, + name: r'activeGamePlayerProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') + ? null + : _$activeGamePlayerHash, + dependencies: ActiveGamePlayerFamily._dependencies, + allTransitiveDependencies: + ActiveGamePlayerFamily._allTransitiveDependencies, + creationName: creationName, + ); + + ActiveGamePlayerProvider._internal( + super._createNotifier, { + required super.name, + required super.dependencies, + required super.allTransitiveDependencies, + required super.debugGetCreateSourceHash, + required super.from, + required this.creationName, + }) : super.internal(); + + final String creationName; + + @override + FutureOr runNotifierBuild( + covariant ActiveGamePlayer notifier, + ) { + return notifier.build( + creationName, + ); + } + + @override + Override overrideWith(ActiveGamePlayer Function() create) { + return ProviderOverride( + origin: this, + override: ActiveGamePlayerProvider._internal( + () => create()..creationName = creationName, + from: from, + name: null, + dependencies: null, + allTransitiveDependencies: null, + debugGetCreateSourceHash: null, + creationName: creationName, + ), + ); + } + + @override + AutoDisposeAsyncNotifierProviderElement + createElement() { + return _ActiveGamePlayerProviderElement(this); + } + + @override + bool operator ==(Object other) { + return other is ActiveGamePlayerProvider && + other.creationName == creationName; + } + + @override + int get hashCode { + var hash = _SystemHash.combine(0, runtimeType.hashCode); + hash = _SystemHash.combine(hash, creationName.hashCode); + + return _SystemHash.finish(hash); + } +} + +mixin ActiveGamePlayerRef on AutoDisposeAsyncNotifierProviderRef { + /// The parameter `creationName` of this provider. + String get creationName; +} + +class _ActiveGamePlayerProviderElement + extends AutoDisposeAsyncNotifierProviderElement with ActiveGamePlayerRef { + _ActiveGamePlayerProviderElement(super.provider); + + @override + String get creationName => (origin as ActiveGamePlayerProvider).creationName; +} +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/bluevsred_flutter/test/game_player_test.dart b/bluevsred_flutter/test/game_player_test.dart index 5cc74ea..5d692ef 100644 --- a/bluevsred_flutter/test/game_player_test.dart +++ b/bluevsred_flutter/test/game_player_test.dart @@ -1,20 +1,29 @@ // import 'package:bluevsred_shared/src/game_player/game_player.dart'; +import 'package:bluevsred_flutter/player/active_game_player.dart'; import 'package:test/test.dart'; +import 'provider_container.dart'; + void main() { - // const String gamePlayer1Name = 'first test player'; - // final List initialPlayerList = [GamePlayer(name: 'initial player 1'), GamePlayer(name: 'initial player 2'), GamePlayer(name: 'initial player 3'), GamePlayer(name: 'initial player 4'),]; group('player lifecycle', () { - test('when game player is created, he appear in the player list', () { - // Here comes the need of state management system - bool isPlayerInTheList = false; + test('When a player is created its name begin by given string ', () async { + final container = createContainer(); + const expectedName = 'given name'; + var state = container.read(activeGamePlayerProvider(expectedName).notifier); + expect((await state.future).name, equals(expectedName)); + }); + + test('when game player is created, he gets a commander troop', () { + // Here comes the need of state management system + bool isPlayerInTheList = false; + /// check player is not in inital list expect(isPlayerInTheList, equals(false)); -/// Add new player to the list - isPlayerInTheList = true; - expect(isPlayerInTheList, equals(true)); - } ); + /// Add new player to the list + isPlayerInTheList = true; + expect(isPlayerInTheList, equals(true)); + }); // test('when game player is created, his first commander is created', () { // bool isPlayerCommanderCreated = false; @@ -23,7 +32,7 @@ void main() { // } ); // test('when game player is created, its name must be unique', () { - // final gamePlayer = GamePlayer(name: gamePlayer1Name); + // final gamePlayer = GamePlayer(name: gamePlayer1Name); // } ); }); } diff --git a/bluevsred_flutter/test/provider_container.dart b/bluevsred_flutter/test/provider_container.dart new file mode 100644 index 0000000..259b512 --- /dev/null +++ b/bluevsred_flutter/test/provider_container.dart @@ -0,0 +1,22 @@ +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:test/test.dart'; + +/// A testing utility which creates a [ProviderContainer] and automatically +/// disposes it at the end of the test. +ProviderContainer createContainer({ + ProviderContainer? parent, + List overrides = const [], + List? observers, +}) { + // Create a ProviderContainer, and optionally allow specifying parameters. + final container = ProviderContainer( + parent: parent, + overrides: overrides, + observers: observers, + ); + + // When the test ends, dispose the container. + addTearDown(container.dispose); + + return container; +} \ No newline at end of file diff --git a/bluevsred_shared/lib/bluesvsred_shared.dart b/bluevsred_shared/lib/bluesvsred_shared.dart new file mode 100644 index 0000000..1cb5b6b --- /dev/null +++ b/bluevsred_shared/lib/bluesvsred_shared.dart @@ -0,0 +1,5 @@ +export 'src/battle/battle.dart'; +export 'src/battle_map/battle_map.dart'; +export 'src/game_player/game_player.dart'; +export 'src/team/team.dart'; +export 'src/troops/troop.dart';