feat: add resolver functions to class names

This commit is contained in:
2023-12-16 18:23:27 +02:00
parent 50c8012df0
commit 13c0281292
5 changed files with 78 additions and 10 deletions

View File

@@ -1,5 +1,9 @@
# CHANGELOG
## v3.1.0
- Added `registerEntityTypeResolver` and `resetEntityTypeResolver`
## v3.0.4
- Fixed "Cast A Spell" Wizard move dice

View File

@@ -128,13 +128,13 @@ class DungeonWorldRepository extends RepositoryMap<String, dynamic> {
Map<String, Tag> get tags => getItem('Tags').cast<String, Tag>();
/// Dump all the data as a JSON map. Same as `toJsonAsMaps()`
/// @see toJsonAsMaps()
/// @see toJsonAsLists()
/// See [toJsonAsMaps]
/// See [toJsonAsLists]
Map<String, dynamic> toJson() => toJsonAsMaps();
/// Dump all the data as a JSON map. Same as `toJson()`
/// @see toJson()
/// @see toJsonAsLists()
/// See [toJson]
/// See [toJsonAsLists]
Map<String, dynamic> toJsonAsMaps() => {
"CharacterClasses": characterClasses.map(((key, value) => MapEntry(key, value.toJson()))),
"Items": items.map(((key, value) => MapEntry(key, value.toJson()))),
@@ -146,8 +146,8 @@ class DungeonWorldRepository extends RepositoryMap<String, dynamic> {
};
/// Dump all the data as a JSON lists, instead of key => value maps.
/// @see toJson()
/// @see toJsonAsMaps()
/// See [toJson]
/// See [toJsonAsMaps]
Map<String, dynamic> toJsonAsLists() => {
"CharacterClasses": characterClasses.values.map(((value) => value.toJson())).toList(),
"Items": items.values.map(((value) => value.toJson())).toList(),

View File

@@ -12,6 +12,65 @@ mixin KeyMixin {
EntityReference get reference => EntityReference(
key: key,
name: displayName,
type: runtimeType.toString(),
type: _typeResolver?.call(runtimeType) ??
defaultTypeResolver(runtimeType),
);
}
String defaultTypeResolver(Type type) {
switch (type) {
case Move:
return 'Move';
case Spell:
return 'Spell';
case Item:
return 'Item';
case CharacterClass:
return 'CharacterClass';
case Race:
return 'Race';
case GearSelection:
return 'GearSelection';
case MoveCategory:
return 'MoveCategory';
case Tag:
return 'Tag';
case Dice:
return 'Dice';
default:
final typeString = type.toString();
final match = RegExp(r'\w+', caseSensitive: false).firstMatch(typeString);
return match?.group(0) ?? typeString;
}
}
String? Function(Type)? _typeResolver;
/// Register a type resolver for a given type to be used with EntityReference.
///
/// A type resolver is a function that takes a type and returns a string representing that type, to be used for
/// serialization/deserialization.
/// It is only mandatory in minified environments like Flutter web, where entity names are mangled.
///
/// If no type resolver is registered for a given type, or the resolver returns null,
/// the default type resolver will be used.
///
/// See [resetEntityTypeResolver]
void registerEntityTypeResolver(String Function(Type) typeResolver) {
_typeResolver = typeResolver;
}
/// Resets the type resolver to the default.
///
/// A type resolver is a function that takes a type and returns a string representing that type, to be used for
/// serialization/deserialization.
/// It is only mandatory in minified environments like Flutter web, where entity names are mangled.
///
/// If no type resolver is registered for a given type, or the resolver returns null,
/// the default type resolver will be used.
///
/// See [registerEntityTypeResolver]
resetEntityTypeResolver() {
_typeResolver = null;
}

View File

@@ -4,6 +4,7 @@ import 'dart:convert';
/// This is useful for storing the name & key of an entity without having to store the entire entity,
/// possibly for use in a dropdown or other UI element, or linking between entities in a database.
class EntityReference {
/// The key of the entity.
final String key;
@@ -19,9 +20,11 @@ class EntityReference {
required this.type,
});
factory EntityReference.fromRawJson(String str) => EntityReference.fromJson(json.decode(str));
factory EntityReference.fromRawJson(String str) =>
EntityReference.fromJson(json.decode(str));
factory EntityReference.fromJson(Map<String, dynamic> json) => EntityReference(
factory EntityReference.fromJson(Map<String, dynamic> json) =>
EntityReference(
key: json['key'],
name: json['name'],
type: json['type'],
@@ -46,6 +49,7 @@ class EntityReference {
type: type ?? this.type,
);
@override
bool operator ==(Object? other) =>
other is EntityReference &&
@@ -65,3 +69,4 @@ class EntityReference {
@override
String toString() => 'EntityReference(key: $key, name: $name, type: $type)';
}

View File

@@ -1,7 +1,7 @@
name: dungeon_world_data
homepage: https://github.com/DungeonPaper/dungeon_world_data
description: Data dump of Dungeon World classes, moves, equipment, and more. Also mirrored as NPM package.
version: 3.0.4
version: 3.1.0
environment:
sdk: ">=2.17.0 <4.0.0"