Skip to content

Commit

Permalink
start understanding riverpod: emergency commit, more a backup than a …
Browse files Browse the repository at this point in the history
…version (docker desktop crashes)
  • Loading branch information
hawkbee1 committed Apr 16, 2024
1 parent be62477 commit 69abd6e
Show file tree
Hide file tree
Showing 38 changed files with 4,955 additions and 293 deletions.
14 changes: 14 additions & 0 deletions bluevsred_client/lib/src/protocol/protocol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import 'battle_map_db.dart' as _i3;
import 'game_player_db.dart' as _i4;
import 'team_db.dart' as _i5;
import 'troop_db.dart' as _i6;
import 'troop_type.dart' as _i7;
export 'battle_db.dart';
export 'battle_map_db.dart';
export 'game_player_db.dart';
export 'team_db.dart';
export 'troop_db.dart';
export 'troop_type.dart';
export 'client.dart';

class Protocol extends _i1.SerializationManager {
Expand Down Expand Up @@ -55,6 +57,9 @@ class Protocol extends _i1.SerializationManager {
if (t == _i6.TroopDb) {
return _i6.TroopDb.fromJson(data, this) as T;
}
if (t == _i7.TroopType) {
return _i7.TroopType.fromJson(data) as T;
}
if (t == _i1.getType<_i2.BattleDb?>()) {
return (data != null ? _i2.BattleDb.fromJson(data, this) : null) as T;
}
Expand All @@ -70,6 +75,9 @@ class Protocol extends _i1.SerializationManager {
if (t == _i1.getType<_i6.TroopDb?>()) {
return (data != null ? _i6.TroopDb.fromJson(data, this) : null) as T;
}
if (t == _i1.getType<_i7.TroopType?>()) {
return (data != null ? _i7.TroopType.fromJson(data) : null) as T;
}
return super.deserialize<T>(data, t);
}

Expand All @@ -90,6 +98,9 @@ class Protocol extends _i1.SerializationManager {
if (data is _i6.TroopDb) {
return 'TroopDb';
}
if (data is _i7.TroopType) {
return 'TroopType';
}
return super.getClassNameForObject(data);
}

Expand All @@ -110,6 +121,9 @@ class Protocol extends _i1.SerializationManager {
if (data['className'] == 'TroopDb') {
return deserialize<_i6.TroopDb>(data['data']);
}
if (data['className'] == 'TroopType') {
return deserialize<_i7.TroopType>(data['data']);
}
return super.deserializeByClassName(data);
}
}
34 changes: 24 additions & 10 deletions bluevsred_client/lib/src/protocol/troop_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:serverpod_client/serverpod_client.dart' as _i1;
import 'protocol.dart' as _i2;

abstract class TroopDb extends _i1.SerializableEntity {
TroopDb._({
this.id,
required this.path,
required this.troopType,
required this.actionPoints,
});

factory TroopDb({
int? id,
required String path,
required _i2.TroopType troopType,
required double actionPoints,
}) = _TroopDbImpl;

factory TroopDb.fromJson(
Expand All @@ -27,7 +30,10 @@ abstract class TroopDb extends _i1.SerializableEntity {
) {
return TroopDb(
id: serializationManager.deserialize<int?>(jsonSerialization['id']),
path: serializationManager.deserialize<String>(jsonSerialization['path']),
troopType: serializationManager
.deserialize<_i2.TroopType>(jsonSerialization['troopType']),
actionPoints: serializationManager
.deserialize<double>(jsonSerialization['actionPoints']),
);
}

Expand All @@ -36,17 +42,21 @@ abstract class TroopDb extends _i1.SerializableEntity {
/// the id will be null.
int? id;

String path;
_i2.TroopType troopType;

double actionPoints;

TroopDb copyWith({
int? id,
String? path,
_i2.TroopType? troopType,
double? actionPoints,
});
@override
Map<String, dynamic> toJson() {
return {
if (id != null) 'id': id,
'path': path,
'troopType': troopType.toJson(),
'actionPoints': actionPoints,
};
}
}
Expand All @@ -56,20 +66,24 @@ class _Undefined {}
class _TroopDbImpl extends TroopDb {
_TroopDbImpl({
int? id,
required String path,
required _i2.TroopType troopType,
required double actionPoints,
}) : super._(
id: id,
path: path,
troopType: troopType,
actionPoints: actionPoints,
);

@override
TroopDb copyWith({
Object? id = _Undefined,
String? path,
_i2.TroopType? troopType,
double? actionPoints,
}) {
return TroopDb(
id: id is int? ? id : this.id,
path: path ?? this.path,
troopType: troopType ?? this.troopType,
actionPoints: actionPoints ?? this.actionPoints,
);
}
}
29 changes: 29 additions & 0 deletions bluevsred_client/lib/src/protocol/troop_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* AUTOMATICALLY GENERATED CODE DO NOT MODIFY */
/* To generate run: "serverpod generate" */

// ignore_for_file: library_private_types_in_public_api
// ignore_for_file: public_member_api_docs
// ignore_for_file: implementation_imports
// ignore_for_file: use_super_parameters
// ignore_for_file: type_literal_in_constant_pattern

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:serverpod_client/serverpod_client.dart' as _i1;

enum TroopType with _i1.SerializableEntity {
commander;

static TroopType? fromJson(String name) {
switch (name) {
case 'commander':
return commander;
default:
return null;
}
}

@override
String toJson() => name;
@override
String toString() => toJson();
}
23 changes: 23 additions & 0 deletions bluevsred_flutter/lib/player/active_game_player_troops.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:bluevsred_shared/bluesvsred_shared.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'active_game_player_troops.g.dart';

@riverpod
class ActiveGamePlayerTroops extends _$ActiveGamePlayerTroops {
// ActiveGamePlayerTroops._();
@override
Future<Set<Troop>> build({required Set<Troop> troops}) async {
return troops;
}

void addTroops({required List<Troop> newTroops}) {
this.
state
if (state.value != null) {
state.value!.addAll(newTroops);
} else {
state.value => newTroops;
}
}
}
177 changes: 177 additions & 0 deletions bluevsred_flutter/lib/player/active_game_player_troops.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions bluevsred_flutter/lib/player/create_player.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:bluevsred_client/bluevsred_client.dart';
import 'package:bluevsred_flutter/player/active_game_player_troops.dart';
import 'package:bluevsred_shared/bluesvsred_shared.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'active_game_player.dart';

Future<void> createGamePlayer(
{required String gamePlayerName,
required ProviderContainer container}) async {
final gamePlayerProvider = activeGamePlayerProvider(gamePlayerName);
// container.listen(gamePlayerProvider, (previous, next) {});
final troopsProvider = activeGamePlayerTroopsProvider(troops: const {});
// container.listen(troopsProvider, (previous, next) {});

container.read(troopsProvider).value!.addTroops(newTroops: [
Troop(
troopDb: TroopDb(troopType: TroopType.commander, actionPoints: 10),
gamePlayer: container.read(gamePlayerProvider).value!),
]);
}
Loading

0 comments on commit 69abd6e

Please sign in to comment.