2024-12-03 16:42:54 +04:00
|
|
|
enum CharacterType { Survivor, Hunter }
|
|
|
|
|
|
|
|
class Character {
|
|
|
|
final String name;
|
|
|
|
final CharacterType type;
|
|
|
|
final String backstory;
|
|
|
|
final String imageUrl;
|
|
|
|
bool isLiked;
|
|
|
|
|
|
|
|
Character({
|
|
|
|
required this.name,
|
|
|
|
required this.type,
|
|
|
|
required this.backstory,
|
|
|
|
required this.imageUrl,
|
|
|
|
this.isLiked = false, // Значение по умолчанию - false
|
|
|
|
});
|
|
|
|
|
|
|
|
factory Character.fromJson(Map<String, dynamic> json) {
|
|
|
|
try {
|
|
|
|
CharacterType characterType;
|
|
|
|
if (json['type'] == 'Выживший') {
|
|
|
|
characterType = CharacterType.Survivor;
|
|
|
|
} else if (json['type'] == 'Охотник') {
|
|
|
|
characterType = CharacterType.Hunter;
|
|
|
|
} else {
|
|
|
|
throw Exception('Неизвестный тип персонажа: ${json['type']}');
|
|
|
|
}
|
|
|
|
|
|
|
|
return Character(
|
|
|
|
name: json['name'],
|
|
|
|
type: characterType,
|
|
|
|
backstory: json['backstory'],
|
|
|
|
imageUrl: json['image_url'], // Используем корректный URL
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
print("Ошибка при парсинге данных: $e");
|
|
|
|
throw Exception('Ошибка при парсинге персонажа');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Преобразуем тип обратно в строку
|
|
|
|
String get typeString {
|
|
|
|
switch (type) {
|
|
|
|
case CharacterType.Survivor:
|
|
|
|
return 'Выживший';
|
|
|
|
case CharacterType.Hunter:
|
|
|
|
return 'Охотник';
|
|
|
|
default:
|
|
|
|
return 'Неизвестно';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String getInfo() {
|
|
|
|
return '$typeString - $backstory';
|
|
|
|
}
|
|
|
|
|
2024-12-10 13:41:28 +04:00
|
|
|
// Для пустого объекта
|
|
|
|
static Character? empty() {
|
|
|
|
return null; // Возвращаем null для пустого объекта
|
2024-12-03 16:42:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Survivor extends Character {
|
|
|
|
Survivor({
|
|
|
|
required String name,
|
|
|
|
required String backstory,
|
|
|
|
required String imageUrl,
|
|
|
|
}) : super(
|
|
|
|
name: name,
|
|
|
|
backstory: backstory,
|
|
|
|
imageUrl: imageUrl,
|
|
|
|
type: CharacterType.Survivor,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class Hunter extends Character {
|
|
|
|
Hunter({
|
|
|
|
required String name,
|
|
|
|
required String backstory,
|
|
|
|
required String imageUrl,
|
|
|
|
}) : super(
|
|
|
|
name: name,
|
|
|
|
backstory: backstory,
|
|
|
|
imageUrl: imageUrl,
|
|
|
|
type: CharacterType.Hunter,
|
|
|
|
);
|
|
|
|
}
|
2024-12-10 13:41:28 +04:00
|
|
|
|
|
|
|
class CharacterDTO {
|
|
|
|
final String name;
|
|
|
|
final String characterType;
|
|
|
|
final String backstory;
|
|
|
|
final String imageUrl;
|
|
|
|
|
|
|
|
CharacterDTO({
|
|
|
|
required this.name,
|
|
|
|
required this.characterType,
|
|
|
|
required this.backstory,
|
|
|
|
required this.imageUrl,
|
|
|
|
});
|
|
|
|
|
|
|
|
CharacterDTO toDTO() {
|
|
|
|
return CharacterDTO(
|
|
|
|
name: name,
|
|
|
|
characterType: typeString,
|
|
|
|
backstory: backstory,
|
|
|
|
imageUrl: imageUrl,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Добавим метод fromJson
|
|
|
|
factory CharacterDTO.fromJson(Map<String, dynamic> json) {
|
|
|
|
return CharacterDTO(
|
|
|
|
name: json['name'],
|
|
|
|
characterType: json['type'], // 'type' - это то, что мы ожидаем от API
|
|
|
|
backstory: json['backstory'],
|
|
|
|
imageUrl: json['image_url'], // Используем корректное имя поля для URL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Геттер для typeString
|
|
|
|
String get typeString {
|
|
|
|
switch (characterType) {
|
|
|
|
case 'Выживший':
|
|
|
|
return 'Выживший';
|
|
|
|
case 'Охотник':
|
|
|
|
return 'Охотник';
|
|
|
|
default:
|
|
|
|
return 'Неизвестно';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Преобразуем DTO обратно в объект Character
|
|
|
|
Character toCharacter() {
|
|
|
|
return Character(
|
|
|
|
name: name,
|
|
|
|
type: characterType == 'Выживший' ? CharacterType.Survivor : CharacterType.Hunter,
|
|
|
|
backstory: backstory,
|
|
|
|
imageUrl: imageUrl,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Метод для преобразования в Map
|
|
|
|
Map<String, dynamic> toMap() {
|
|
|
|
return {
|
|
|
|
'name': name,
|
|
|
|
'type': characterType,
|
|
|
|
'backstory': backstory,
|
|
|
|
'image_url': imageUrl,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|