Browse Source

Adding card tag

crobi 10 years ago
parent
commit
8613cee9ae
4 changed files with 164 additions and 34 deletions
  1. 18
    10
      generator/data/card_data_example.js
  2. 72
    3
      generator/generate.html
  3. 13
    1
      generator/js/cards.js
  4. 61
    20
      generator/js/ui.js

+ 18
- 10
generator/data/card_data_example.js View File

@@ -18,7 +18,8 @@ var card_data_example = [
18 18
             "fill | 3",
19 19
             "section | At higher levels",
20 20
             "text | +1d6 damage for each slot above 1st"
21
-        ]
21
+        ],
22
+        "tags": ["spell", "mage"]
22 23
     },
23 24
     {
24 25
         "count": 1,
@@ -33,8 +34,9 @@ var card_data_example = [
33 34
             "text | You can take a <b>bonus action on each of your turns</b> in combat. This action can be used only to take the <b>Dash, Disengage, or Hide</b> action.",
34 35
             "fill | 2",
35 36
             "section | Fast hands (Thief 3rd)",
36
-            "text | You can also use the bonus action to make a Dexterity (<b>Sleight of Hand</b>) check, use your thieves' tools to <b>disarm a trap</b> or <b>open a lock</b>, or take the <b>Use an Object</b> action.",
37
-        ]
37
+            "text | You can also use the bonus action to make a Dexterity (<b>Sleight of Hand</b>) check, use your thieves' tools to <b>disarm a trap</b> or <b>open a lock</b>, or take the <b>Use an Object</b> action."
38
+        ],
39
+        "tags": ["feature", "rogue"]
38 40
     },
39 41
     {
40 42
         "count": 1,
@@ -52,7 +54,8 @@ var card_data_example = [
52 54
             "description | Heavy | Unless you have the required strength, your speed is reduced by 10 feet.",
53 55
             "description | Stealth | You have disadvantage on Dexterity (Stealth) checks.",
54 56
             "fill | 3"
55
-        ]
57
+        ],
58
+        "tags": ["item", "armor"]
56 59
     },
57 60
     {
58 61
         "count": 1,
@@ -71,7 +74,8 @@ var card_data_example = [
71 74
             "description | Light | When you attack while dual wielding light weapons, you may use a bonus action to attack with your off hand.",
72 75
             "description | Thrown | You can throw the weapon to make a ranged attack with the given range.",
73 76
             "fill | 3"
74
-        ]
77
+        ],
78
+        "tags": ["item", "weapon"]
75 79
     },
76 80
     {
77 81
         "count": 1,
@@ -90,7 +94,8 @@ var card_data_example = [
90 94
             "description | Finesse | Use your choice of Strength or Dexterity modifier for attack and damage.",
91 95
             "description | Light | When you attack while dual wielding light weapons, you may use a bonus action to attack with your off hand.",
92 96
             "fill | 3"
93
-        ]
97
+        ],
98
+        "tags": ["item", "weapon", "magic"]
94 99
     },
95 100
     {
96 101
         "count": 1,
@@ -111,7 +116,8 @@ var card_data_example = [
111 116
             "bullet | magic missile, 3rd level (3 charges)",
112 117
             "fill | 3",
113 118
             "boxes | 7 | 2.5"
114
-        ]
119
+        ],
120
+        "tags": ["item", "wondrous-item", "magic"]
115 121
     },
116 122
     {
117 123
         "count": 2,
@@ -128,7 +134,8 @@ var card_data_example = [
128 134
             "text | When you drink this potion, you regain 2d4+2 hitpoints.",
129 135
             "text | Drinking or administering a potion takes 1 action.",
130 136
             "fill | 3"
131
-        ]
137
+        ],
138
+        "tags": ["item", "consumable"]
132 139
     },
133 140
     {
134 141
         "count": 1,
@@ -150,6 +157,7 @@ var card_data_example = [
150 157
             "fill | 2",
151 158
             "section | Actions",
152 159
             "description | Scimitar | Melee Weapon Attack: +4 to hit, reach 5 ft., one target. Hit: 5 (1d6 + 2) slashing damage"
153
-        ]
160
+        ],
161
+        "tags": ["creature", "humanoid"]
154 162
     }
155
-];
163
+]

+ 72
- 3
generator/generate.html View File

@@ -67,7 +67,7 @@
67 67
                     </div>
68 68
                     <div class="form-group">
69 69
                         <div class="col-sm-6">
70
-                            <button type="button" class="btn btn-primary btn-block" id="button-sort-name">Sort by name</button>
70
+                            <button type="button" class="btn btn-primary btn-block" id="button-sort">Sort</button>
71 71
                         </div>
72 72
                         <div class="col-sm-6">
73 73
                             <button type="button" class="btn btn-primary btn-block" id="button-save" onclick="ui_save_file()">Save to file</button>
@@ -76,7 +76,7 @@
76 76
                     </div>
77 77
                     <div class="form-group">
78 78
                         <div class="col-sm-6">
79
-                            <button type="button" class="btn btn-primary btn-block" id="button-sort-icon">Sort by icon</button>
79
+                            <button type="button" class="btn btn-primary btn-block" id="button-filter">Map/Filter</button>
80 80
                         </div>
81 81
                         <div class="col-sm-6">
82 82
                             <button type="button" class="btn btn-success btn-block" id="button-generate">Generate</button>
@@ -266,6 +266,12 @@
266 266
                             <input type="number" id="card-count" class="form-control" placeholder="Count" data-property="count" value="1">
267 267
                         </div>
268 268
                     </div>
269
+                    <div class="form-group">
270
+                        <label for="card-tags" class="col-sm-2 control-label">Tags</label>
271
+                        <div class="col-sm-10">
272
+                            <input type="text" id="card-tags" class="form-control" placeholder="Tags">
273
+                        </div>
274
+                    </div>
269 275
                     <div class="form-group">
270 276
                         <label for="card-icon" class="col-sm-2 control-label">Icon</label>
271 277
                         <div class="col-sm-10">
@@ -341,7 +347,7 @@
341 347
             </div>
342 348
         </div>
343 349
     </div>
344
-    <!-- Modal -->
350
+    <!-- Modal (Info)-->
345 351
     <div class="modal" id="help-modal" tabindex="-1" role="dialog">
346 352
         <div class="modal-dialog">
347 353
             <div class="modal-content">
@@ -378,5 +384,68 @@
378 384
             </div>
379 385
         </div>
380 386
     </div>
387
+    <!-- Modal (Sort)-->
388
+    <div class="modal" id="sort-modal" tabindex="-1" role="dialog">
389
+        <div class="modal-dialog">
390
+            <div class="modal-content">
391
+                <div class="modal-header">
392
+                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
393
+                    <h4 class="modal-title" id="myModalLabel">Sort cards</h4>
394
+                </div>
395
+                <div class="modal-body">
396
+                    <p>
397
+                        Enter the comparison function below.
398
+                        The comparison function is a block of javascript code.
399
+                        Return -1 if card_a should appear before card_b.
400
+                    </p>
401
+                    <textarea class="form-control" rows="10" id="sort-function" wrap="off">
402
+if (card_a.title < card_b.title) {
403
+    return -1;
404
+}
405
+else if (card_a.title > card_b.title) {
406
+    return 1;
407
+}
408
+else {
409
+    return 0;
410
+}
411
+                    </textarea>
412
+                </div>
413
+                <div class="modal-footer">
414
+                    <button type="button" class="btn btn-default" id="sort-execute">Sort</button>
415
+                </div>
416
+            </div>
417
+        </div>
418
+    </div>
419
+    <!-- Modal (Filter)-->
420
+    <div class="modal" id="filter-modal" tabindex="-1" role="dialog">
421
+        <div class="modal-dialog">
422
+            <div class="modal-content">
423
+                <div class="modal-header">
424
+                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
425
+                    <h4 class="modal-title" id="myModalLabel">Filter/map cards</h4>
426
+                </div>
427
+                <div class="modal-body">
428
+                    <p>
429
+                        Enter code below that will be executed for each card in the deck.
430
+                        Return false to remove the current card from the deck.
431
+                    </p>
432
+                    <textarea class="form-control" rows="10" id="filter-function" wrap="off">
433
+// Color all spell cards yellow
434
+if (card_has_tag(card, "spell")) {
435
+    card.color = "yellow";
436
+}
437
+
438
+// Remove all creature cards
439
+if (card_has_tag(card, "creature")) {
440
+    return false;
441
+}
442
+                    </textarea>
443
+                </div>
444
+                <div class="modal-footer">
445
+                    <button type="button" class="btn btn-default" id="filter-execute">Filter</button>
446
+                </div>
447
+            </div>
448
+        </div>
449
+    </div>
381 450
 </body>
382 451
 </html>

+ 13
- 1
generator/js/cards.js View File

@@ -22,10 +22,22 @@ function card_default_data() {
22 22
     return {
23 23
         count: 1,
24 24
         title: "New card",
25
-        contents: []
25
+        contents: [],
26
+        tags: []
26 27
     }
27 28
 }
28 29
 
30
+function card_init(card) {
31
+    card.title = card.title || "";
32
+    card.contents = card.contents || [];
33
+    card.tags = card.tags || [];
34
+}
35
+
36
+function card_has_tag(card, tag) {
37
+    var index = card.tags.indexOf(tag.toLowerCase());
38
+    return index > -1;
39
+}
40
+
29 41
 // ============================================================================
30 42
 // Card definition related functions
31 43
 // ============================================================================

+ 61
- 20
generator/js/ui.js View File

@@ -59,6 +59,7 @@ function ui_generate() {
59 59
 
60 60
 function ui_load_sample() {
61 61
     card_data = card_data_example;
62
+    ui_init_cards(card_data);
62 63
     ui_update_card_list();
63 64
 }
64 65
 
@@ -87,7 +88,14 @@ function ui_load_files(evt) {
87 88
     $("#file-load-form")[0].reset();
88 89
 }
89 90
 
91
+function ui_init_cards(data) {
92
+    data.forEach(function (card) {
93
+        card_init(card);
94
+    });
95
+}
96
+
90 97
 function ui_add_cards(data) {
98
+    ui_init_cards(data);
91 99
     card_data = card_data.concat(data);
92 100
     ui_update_card_list();
93 101
 }
@@ -169,6 +177,7 @@ function ui_update_selected_card() {
169 177
         $("#card-icon").val(card.icon);
170 178
         $("#card-icon-back").val(card.icon_back);
171 179
         $("#card-contents").val(card.contents.join("\n"));
180
+        $("#card-tags").val(card.tags.join(", "));
172 181
         $("#card-color").val(card.color).change();
173 182
     } else {
174 183
         $("#card-title").val("");
@@ -177,6 +186,7 @@ function ui_update_selected_card() {
177 186
         $("#card-icon").val("");
178 187
         $("#card-icon-back").val("");
179 188
         $("#card-contents").val("");
189
+        $("#card-tags").val("");
180 190
         $("#card-color").val("").change();
181 191
     }
182 192
 
@@ -333,6 +343,22 @@ function ui_change_card_contents() {
333 343
     }
334 344
 }
335 345
 
346
+function ui_change_card_tags() {
347
+    var value = $(this).val();
348
+
349
+    var card = ui_selected_card();
350
+    if (card) {
351
+        if (value.trim().length == 0) {
352
+            card.tags = [];
353
+        } else {
354
+            card.tags = value.split(",").map(function (val) {
355
+                return val.trim().toLowerCase();
356
+            });
357
+        }
358
+        ui_render_selected_card();
359
+    }
360
+}
361
+
336 362
 function ui_change_default_title_size() {
337 363
     card_options.default_title_size = $(this).val();
338 364
     ui_render_selected_card();
@@ -343,29 +369,40 @@ function ui_change_default_icon_size() {
343 369
     ui_render_selected_card();
344 370
 }
345 371
 
346
-function ui_sort_by_name() {
347
-    card_data = mergeSort(card_data, function (a, b) {
348
-        if (a.title > b.title) {
349
-            return 1;
350
-        }
351
-        if (a.title < b.title) {
352
-            return -1;
353
-        }
354
-        return 0;
372
+function ui_sort() {
373
+    $("#sort-modal").modal('show');
374
+}
375
+
376
+function ui_sort_execute() {
377
+    $("#sort-modal").modal('hide');
378
+
379
+    var fn_code = $("#sort-function").val();
380
+    var fn = new Function("card_a", "card_b", fn_code);
381
+
382
+    card_data = card_data.sort(function (card_a, card_b) {
383
+        var result = fn(card_a, card_b);
384
+        return result;
355 385
     });
386
+
356 387
     ui_update_card_list();
357 388
 }
358 389
 
359
-function ui_sort_by_icon() {
360
-    card_data = mergeSort(card_data, function (a, b) {
361
-        if (a.icon > b.icon) {
362
-            return 1;
363
-        }
364
-        if (a.icon < b.icon) {
365
-            return -1;
366
-        }
367
-        return 0;
390
+function ui_filter() {
391
+    $("#filter-modal").modal('show');
392
+}
393
+
394
+function ui_filter_execute() {
395
+    $("#filter-modal").modal('hide');
396
+
397
+    var fn_code = $("#filter-function").val();
398
+    var fn = new Function("card", fn_code);
399
+
400
+    card_data = card_data.filter(function (card) {
401
+        var result = fn(card);
402
+        if (result === undefined) return true;
403
+        else return false;
368 404
     });
405
+
369 406
     ui_update_card_list();
370 407
 }
371 408
 
@@ -400,8 +437,8 @@ $(document).ready(function () {
400 437
     $("#button-clear").click(ui_clear_all);
401 438
     $("#button-load-sample").click(ui_load_sample);
402 439
     //$("#button-save").click(ui_save_file);
403
-    $("#button-sort-name").click(ui_sort_by_name);
404
-    $("#button-sort-icon").click(ui_sort_by_icon);
440
+    $("#button-sort").click(ui_sort);
441
+    $("#button-filter").click(ui_filter);
405 442
     $("#button-add-card").click(ui_add_new_card);
406 443
     $("#button-duplicate-card").click(ui_duplicate_card);
407 444
     $("#button-delete-card").click(ui_delete_card);
@@ -419,6 +456,7 @@ $(document).ready(function () {
419 456
     $("#card-icon-back").change(ui_change_card_property);
420 457
     $("#card-color").change(ui_change_card_color);
421 458
     $("#card-contents").change(ui_change_card_contents);
459
+    $("#card-tags").change(ui_change_card_tags);
422 460
 
423 461
     $("#page-size").change(ui_change_option);
424 462
     $("#page-rows").change(ui_change_option);
@@ -433,6 +471,9 @@ $(document).ready(function () {
433 471
     $("#small-icons").change(ui_change_default_icon_size);
434 472
 
435 473
     $(".icon-select-button").click(ui_select_icon);
474
+
475
+    $("#sort-execute").click(ui_sort_execute);
476
+    $("#filter-execute").click(ui_filter_execute);
436 477
     
437 478
     ui_update_card_list();
438 479
 });

Loading…
Cancel
Save