summaryrefslogtreecommitdiff
path: root/server/lib/core/models/character_class.dart
blob: def035436f865e9ad83178ebb8b6d1747f6c4849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import 'package:x_haven_server/core/models/color.dart';
import 'package:x_haven_server/core/models/summon_model.dart';

class CharacterClass {
  final String id;
  final String name;
  final String edition;
  final List<int> healthByLevel;
  final Color color;
  final Color colorSecondary;
  final bool hidden;
  final List<SummonModel> summons;
  final List<PerkModel> perks;
  final List<PerkModel> perksFH;

  const CharacterClass(
    this.id,
    this.name,
    this.healthByLevel,
    this.edition,
    this.color,
    this.colorSecondary,
    this.hidden,
    this.summons,
    this.perks,
    this.perksFH,
  );

  factory CharacterClass.fromJson(Map<String, dynamic> data) {
    final edition = data['edition'] as String;
    final name = data['name'] as String;
    String id = name; //default to name if no id
    if (data.containsKey('id')) {
      id = data['id'] as String;
    }
    bool hidden = false;
    if (data.containsKey('hidden')) {
      hidden = data['hidden'] as bool;
    }
    //TODO: color array + gradiantType support
    final healthByLevel = (data['health'] as List<Object?>).cast<int>();
    final int radix = 16;
    final colorValue = data['color'];
    final Color color = (colorValue is List<Object?>)
        ? Color(int.parse(colorValue.first as String, radix: radix))
        : Color(int.parse(colorValue as String, radix: radix));

    Color colorSecondary = color;
    if (data.containsKey("colorSecondary")) {
      colorSecondary = (data["colorSecondary"] is List<Object?>)
          ? colorSecondary = Color(
              int.parse(data['colorSecondary'][0], radix: radix),
            )
          : Color(int.parse(data['colorSecondary'], radix: radix));
    }

    final List<SummonModel> summonList = [];
    if (data.containsKey('summons')) {
      final summons = data['summons'] as Map<dynamic, dynamic>;
      for (String key in summons.keys) {
        summonList.add(SummonModel.fromJson(summons[key], key));
      }
    }

    final List<PerkModel> perkList = [];
    if (data.containsKey('perks')) {
      final perks = (data['perks'] as List<Object?>)
          .cast<Map<String, dynamic>>();
      for (final item in perks) {
        perkList.add(PerkModel.fromJson(item));
      }
    }

    final List<PerkModel> perkFHList = [];
    if (data.containsKey('perks_fh')) {
      final perks = (data['perks_fh'] as List<Object?>)
          .cast<Map<String, dynamic>>();
      for (final item in perks) {
        perkFHList.add(PerkModel.fromJson(item));
      }
    }

    return CharacterClass(
      id,
      name,
      healthByLevel,
      edition,
      color,
      colorSecondary,
      hidden,
      summonList,
      perkList,
      perkFHList,
    );
  }
}

class PerkModel {
  final String text;
  final List<String> remove;
  final List<String> add;
  const PerkModel(this.text, this.remove, this.add);

  factory PerkModel.fromJson(Map<String, dynamic> data) {
    String text = "";
    if (data.containsKey('text')) {
      text = data['text'];
    }

    final List<String> addList = [];
    if (data.containsKey('adds')) {
      final adds = (data['adds'] as List<Object?>).cast<String>();
      for (String item in adds) {
        addList.add(item);
      }
    }

    final List<String> removeList = [];
    if (data.containsKey('removes')) {
      final removes = (data['removes'] as List<Object?>).cast<String>();
      for (String item in removes) {
        removeList.add(item);
      }
    }

    return PerkModel(text, removeList, addList);
  }
}