-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonGenerator.php
253 lines (204 loc) · 8.26 KB
/
JsonGenerator.php
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/**
* Created by PhpStorm.
* User: mikefuller
* Date: 5/31/15
* Time: 1:00 PM
*/
include "CsvImporter.php";
class JsonGenerator {
const RUNNING_ON_MAC = false; // set this to false if not running on a MAC OS
const DATA_FILE_NAME = "card_data.txt"; // name of tab delimited file this script looks for
const ICON_ABERRATION = 'alien-skull'; // icon to use for monsters of type aberration
const ICON_BEAST = 'boar-tusks'; // icon to use for monsters of type beast
const ICON_CELESTIAL = 'angel-wings'; // icon to use for monsters of type celestial
const ICON_CONSTRUCT = 'robot-golem'; // icon to use for monsters of type construct
const ICON_DRAGON = 'dragon-head'; // icon to use for monsters of type dragon
const ICON_ELEMENTAL = 'pyromaniac'; // icon to use for monsters of type elemental
const ICON_FEY = 'fairy-wand'; // icon to use for monsters of type fey
const ICON_FIEND = 'imp'; // icon to use for monsters of type fiend
const ICON_GIANT = 'muscle-fat'; // icon to use for monsters of type giant
const ICON_HUMANOID = 'run'; // icon to use for monsters of type humanoid
const ICON_MONSTROSITY = 'gluttonous-smile'; // icon to use for monsters of type monstrosity
const ICON_OOZE = 'gloop'; // icon to use for monsters of type ooze
const ICON_PLANT = 'beanstalk'; // icon to use for monsters of type plant
const ICON_UNDEAD = 'crowned-skull'; // icon to use for monsters of type undead
// Field names used in first row of tab delimited file
const FLD_MONSTER_NAME = 'Monster'; // name of monster
const FLD_TYPE = 'Type';
const FLD_SUBTYPE = 'Subtype';
const FLD_SIZE = 'Size';
const FLD_ALIGNMENT = 'Align';
const FLD_STR = 'Str';
const FLD_DEX = 'Dex';
const FLD_CON = 'Con';
const FLD_INT = 'Int';
const FLD_WIS = 'Wis';
const FLD_CHA = 'Cha';
const FLD_AC = 'AC';
const FLD_HP = 'HP';
const FLD_SPEED = 'Speed';
const FLD_OTHER_SPEED = 'Other Speed';
// Those fields below affects label on cards
const FLD_SAVES = 'Saves';
const FLD_SKILLS = 'Skills';
const FLD_DAMAGE_VULNER = 'Damage Vulnerabilities';
const FLD_DAMAGE_RESIST = 'Damage Resistances';
const FLD_DAMAGE_IMMUNE = 'Damage Immunities';
const FLD_CONDITION_IMMUNE = 'Condition Immunities';
const FLD_SENSES = 'Senses';
const FLD_LANGUAGES = 'Languages';
/* ======================================================================================= */
public function format( $data ) {
$newData = [ ];
foreach ( $data as $record ) {
$newData[] = [
'count' => 1,
'title' => $record[ self::FLD_MONSTER_NAME ],
'contents' => $this->contents( $record ),
'tags' => [ ],
'color' => '',
'icon' => $this->icon( $record[ self::FLD_TYPE ] )
];
}
return json_encode( $newData );
}
private function contents( $record ) {
$data = $this->sizeTypeAlign( $record );
$data = $this->baseCombat( $record, $data );
$data = $this->attributes( $record, $data );
$data = $this->miscProperties( $record, $data );
$data = $this->traits( $record, $data );
$data = $this->actions( $record, $data );
$data = $this->reactions( $record, $data );
return $data;
}
private function sizeTypeAlign( $record ) {
$sizeTypeAlign = "subtitle| {$record[ self::FLD_SIZE ]} {$record[ self::FLD_TYPE ]}";
if ( ! empty( $record[ self::FLD_SUBTYPE ] ) ) {
$sizeTypeAlign .= ' (' . $record[ self::FLD_SUBTYPE ] . ')';
}
$sizeTypeAlign .= ' ' . $record[ self::FLD_ALIGNMENT ];
return [ $sizeTypeAlign, 'rule' ];
}
private function attributes( $record, $data ) {
$attrs = [ self::FLD_STR, self::FLD_DEX, self::FLD_CON, self::FLD_INT, self::FLD_WIS, self::FLD_CHA ];
$attributeValues = 'dndstats';
foreach ( $attrs as $attr ) {
$attributeValues .= '|' . $record[ $attr ];
}
return array_merge( $data, [ $attributeValues, 'rule' ] );
}
private function baseCombat( $record, $data ) {
$fields = [
self::FLD_AC,
self::FLD_HP,
];
$data = array_merge( $data, $this->properties( $fields, $record ) );
$data[] = $this->speedProperty( $record );
$data[] = 'rule';
return $data;
}
private function miscProperties( $record, $data ) {
$fields = [
self::FLD_SAVES,
self::FLD_SKILLS,
self::FLD_DAMAGE_VULNER,
self::FLD_DAMAGE_RESIST,
self::FLD_DAMAGE_IMMUNE,
self::FLD_CONDITION_IMMUNE,
self::FLD_SENSES,
self::FLD_LANGUAGES
];
$data = array_merge( $data, $this->properties( $fields, $record ) );
$data[] = 'fill|1';
return $data;
}
private function traits( $record, $data ) {
return array_merge( $data, $this->conditionalSections( 'Trait', $record ) );
}
private function actions( $record, $data ) {
return array_merge( $data, $this->conditionalSections( 'Action', $record ) );
}
private function reactions( $record, $data ) {
return array_merge( $data, $this->conditionalSections( 'Reaction', $record ) );
}
private function properties( $fields, $record ) {
$data = [ ];
foreach ( $fields as $field ) {
if ( ! empty( $record[ $field ] ) ) {
$data[] = "property|$field|$record[$field]";
}
}
return $data;
}
private function conditionalSections( $property, $record ) {
$fields = $this->sequentialFields( $property );
$data = [ ];
$empty = true;
for ( $i = 0; $i < count( $fields ); $i ++ ) {
if ( ! empty( $record[ $fields[ $i ] ] ) ) {
$empty = false;
$data [] = 'text|' . $record[ $fields[ $i ] ];
}
}
if ( ! $empty ) {
$data = array_merge( [ 'section|' . $property . 's' ], $data );
$data[] = 'fill|1';
}
return $data;
}
private function sequentialFields( $propertyPrefix ) {
$fields = [ ];
for ( $i = 1; $i <= 5; $i ++ ) {
$fields[] = $propertyPrefix . $i;
}
return $fields;
}
private function speedProperty( $record ) {
$speed = 'property|Speed|' . $record[ self::FLD_SPEED ];
if ( ! empty( $record[ self::FLD_OTHER_SPEED ] ) ) {
$speed .= '; ' . $record[ self::FLD_OTHER_SPEED ];
return $speed;
}
return $speed;
}
private function icon( $type ) {
switch ( strtolower( trim($type ) ) ) {
case 'aberration' :
return self::ICON_ABERRATION;
case 'beast' :
return self::ICON_BEAST;
case 'celestial' :
return self::ICON_CELESTIAL;
case 'construct' :
return self::ICON_CONSTRUCT;
case 'dragon' :
return self::ICON_DRAGON;
case 'elemental' :
return self::ICON_ELEMENTAL;
case 'fey' :
return self::ICON_FEY;
case 'fiend' :
return self::ICON_FIEND;
case 'giant' :
return self::ICON_GIANT;
case 'humanoid' :
return self::ICON_HUMANOID;
case 'monstrosity' :
return self::ICON_MONSTROSITY;
case 'ooze' :
return self::ICON_OOZE;
case 'plant' :
return self::ICON_PLANT;
case 'undead' :
return self::ICON_UNDEAD;
}
return '';
}
}
ini_set('auto_detect_line_endings', JsonGenerator::RUNNING_ON_MAC);
$importer = new CsvImporter( JsonGenerator::DATA_FILE_NAME, true );
$data = $importer->get();
$jsonGenerator = new JsonGenerator();
print_r( $jsonGenerator->format( $data ) );