-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanCraft.js
1739 lines (1518 loc) · 68.3 KB
/
PlanCraft.js
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Adds a row to the sequence tasks table with the given parameters.
* @param {string} name - The name of the task.
* @param {number} duration - The duration of the task in days.
* @param {array} dependencies - An array of task names that must be completed before this task can be completed.
* @param {array} extraDependencies - An array of task names that are not in the same sequence as the task, but must be completed before this task can be completed.
*/
function addSequenceTask(name="", duration=5, dependencies=[], extraDependencies=[]) {
const table = document.getElementById('sequenceTasksTable').getElementsByTagName('tbody')[0];
addTaskRow(table, "Sequence", name, duration, dependencies, extraDependencies);
// Call the function to adjust height
adjustCollapsibleHeight();
}
/**
* Adds a row to the appropriate asset tasks table based on the given type.
* @param {string} type - The type of asset task ("env", "prop", or "char").
* @param {string} name - The name of the task.
* @param {number} duration - The duration of the task in days.
* @param {array} dependencies - An array of task names that must be completed before this task can be completed.
*/
function addAssetTask(type="", name="", duration=5, dependencies=[]) {
if (type === "env") {
const table = document.getElementById('envAssetTasksTable').getElementsByTagName('tbody')[0];
addTaskRow(table, "Env", name, duration, dependencies);
} else if (type === "prop") {
const table = document.getElementById('propAssetTasksTable').getElementsByTagName('tbody')[0];
addTaskRow(table, "Prop", name, duration, dependencies);
} else if (type === "char") {
const table = document.getElementById('charAssetTasksTable').getElementsByTagName('tbody')[0];
addTaskRow(table, "Char", name, duration, dependencies);
}
// Call the function to adjust height
adjustCollapsibleHeight();
}
/**
* Adds a row to the appropriate shot tasks table based on the given type.
* @param {string} type - The type of shot task ("simple" or "complex").
* @param {string} name - The name of the task.
* @param {number} duration - The duration of the task in days.
* @param {array} dependencies - An array of task names that must be completed before this task can be completed.
* @param {array} firstExtraDependencies - A first level array of task names that are not in the same sequence as the task, but must be completed before this task can be completed.
* @param {array} secondExtraDependencies - A second level array of task names that are not in the same sequence as the task, but must be completed before this task can be completed.
*
* The difference between first and second extra dependencies is that first ones are for asset tasks dependencies, and second ones are for sequence tasks dependencies.
*/
function addShotTask(type="", name="", duration=5, dependencies=[], firstExtraDependencies=[], secondExtraDependencies=[]) {
if (type === "simple") {
const table = document.getElementById('simpleShotTasksTable').getElementsByTagName('tbody')[0];
addTaskRow(table, "SimpleShot", name, duration, dependencies, firstExtraDependencies, secondExtraDependencies);
} else if (type === "complex") {
const table = document.getElementById('complexShotTasksTable').getElementsByTagName('tbody')[0];
addTaskRow(table, "ComplexShot", name, duration, dependencies, firstExtraDependencies, secondExtraDependencies);
}
// Call the function to adjust height
adjustCollapsibleHeight();
}
/**
* Adds a new row to the specified table with input fields for task details.
* The function handles different types of tasks and their dependencies.
*
* @param {HTMLTableElement} table - The table to which the row should be added.
* @param {string} type - The type of task ("SimpleShot", "ComplexShot", "Sequence", etc.).
* @param {string} [name=""] - The name of the task. If not provided, a default name is generated.
* @param {number} [duration=5] - The duration of the task in days.
* @param {array} [dependencies=[]] - An array of task names that must be completed before this task.
* @param {array} [firstExtraDependencies=[]] - A first array of additional dependencies, as the table or type requires.
* @param {array} [secondExtraDependencies=[]] - A second array of additional dependencies, as the table or type requires.
*/
function addTaskRow(table, type, name="", duration=5, dependencies=[],
firstExtraDependencies=[], secondExtraDependencies=[]) {
const row = table.insertRow(-1);
// style the row by setting its class
row.className = "bg-white border-b dark:bg-gray-800 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600";
const nameCell = row.insertCell();
// set cell scope and class
nameCell.scope = "col";
nameCell.className = "px-6 py-3";
const durationCell = row.insertCell();
durationCell.scope = "col";
durationCell.className = "px-6 py-3";
const shotDependenciesCell = row.insertCell();
shotDependenciesCell.scope = "col";
shotDependenciesCell.className = "px-6 py-3";
if (!name) {
name = `Task${type}${table.rows.length}`;
}
if (dependencies.length > 0) {
dependencies = dependencies.join(",");
} else {
dependencies = "";
}
nameCell.innerHTML = `
<input
type="text"
class="taskName bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
placeholder="Task Name"
value="${name}"
>
`;
durationCell.innerHTML = `
<input
type="number"
class="taskDuration bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
placeholder="Days Duration"
value="${duration}"
>
`;
shotDependenciesCell.innerHTML = `
<input
type="text"
class="taskDependencies bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
placeholder="comma,separated"
value="${dependencies}"
>
`;
//console.log("type: " + type);
//console.log("firstExtraDependencies: " + firstExtraDependencies);
if (["SimpleShot", "ComplexShot", "Sequence"].includes(type)) {
//console.log("Doing first extra dependencies");
//console.log("firstExtraDependencies: " + firstExtraDependencies.length);
if (firstExtraDependencies.length > 0) {
firstExtraDependencies = firstExtraDependencies.join(",");
} else {
firstExtraDependencies = "";
}
const assetDependenciesCell = row.insertCell();
assetDependenciesCell.scope = "col";
assetDependenciesCell.className = "px-6 py-3";
assetDependenciesCell.innerHTML = `
<input
type="text"
class="asetTaskDependencies bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
placeholder="comma,separated"
value="${firstExtraDependencies}"
>
`;
}
if (["SimpleShot", "ComplexShot"].includes(type)) {
//console.log("Doing second extra dependencies");
//console.log("secondExtraDependencies: " + secondExtraDependencies.length);
if (secondExtraDependencies.length > 0) {
secondExtraDependencies = secondExtraDependencies.join(",");
} else {
secondExtraDependencies = "";
}
const seqDependenciesCell = row.insertCell();
seqDependenciesCell.scope = "col";
seqDependenciesCell.className = "px-6 py-3";
seqDependenciesCell.innerHTML = `
<input
type="text"
class="seqTaskDependencies bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
placeholder="comma,separated"
value="${secondExtraDependencies}"
>
`;
}
}
/**
* Adds some default asset tasks with appropriate dependencies.
* Environment ones: design -> model -> rig -> surface.
* Character ones: design -> model -> rig -> groom -> surface.
* Prop ones: design -> model -> rig -> surface.
*/
function addDefaultAssetTasks() {
// Environment ones
addAssetTask("env", "design", 15);
addAssetTask("env", "model", 15, ["design"]);
addAssetTask("env", "rig", 1, ["model"]);
addAssetTask("env", "surface", 15, ["model"]);
addAssetTask("char", "design", 10);
addAssetTask("char", "model", 10, ["design"]);
addAssetTask("char", "rig", 10, ["model"]);
addAssetTask("char", "groom", 10, ["model"]);
addAssetTask("char", "surface", 10, ["model", "groom"]);
addAssetTask("prop", "design", 5);
addAssetTask("prop", "model", 5, ["design"]);
addAssetTask("prop", "rig", 5, ["model"]);
addAssetTask("prop", "surface", 5, ["model"]);
}
/**
* Adds some default shot tasks with appropriate dependencies.
* Simple shots: animation -> simulation -> lighting -> comp.
* Complex shots: animation -> simulation -> fx -> lighting -> comp.
*/
function addDefaultShotTasks() {
addShotTask("simple", "animation", 5, [], ["rig"], ["layout"]);
addShotTask("simple", "simulation", 5, ["animation"], ["groom"]);
addShotTask("simple", "lighting", 5, ["animation", "simulation"], ["surface"]);
addShotTask("simple", "comp", 5, ["lighting"]);
addShotTask("complex", "animation", 10, [], ["rig"], ["layout"]);
addShotTask("complex", "simulation", 10, ["animation"], ["groom"]);
addShotTask("complex", "fx", 10, ["animation"]);
addShotTask("complex", "lighting", 10, ["animation", "simulation", "fx"], ["surface"]);
addShotTask("complex", "comp", 10, ["lighting"]);
}
/**
* Adds some default sequence tasks with appropriate dependencies.
* Sequence ones: story -> layout.
*/
function addDefaultSequenceTasks() {
addSequenceTask("story", 5, [], []);
addSequenceTask("layout", 5, ["story"], ["rig", "model"]);
}
/**
* Adjusts the height of all collapsible blocks that are currently expanded.
* This is needed because the initial height is set to 0, and we want to
* allow the content to expand to its natural height.
*/
function adjustCollapsibleHeight() {
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
if (coll[i].classList.contains("active")) { // Only adjust if the block is expanded
var content = coll[i].nextElementSibling;
content.style.maxHeight = content.scrollHeight + 20 + "px";
}
}
}
/**
* Shuffles an array in place, modifying the original array.
* The Fisher-Yates shuffle algorithm is used.
* @param {Array} array The array to shuffle.
*/
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
/**
* Generates an array of strings where each string is repeated a number of times
* given by its corresponding value in the input dictionary.
* The array is then shuffled in place.
* @param {Object} inputDict A dictionary where the keys are strings and the values
* are numbers representing the number of times each string should appear in the
* output array.
* @returns {Array<string>} An array of strings where each string is repeated a number
* of times given by its corresponding value in the input dictionary.
*/
function generateStringList(inputDict) {
let stringList = [];
for (const string in inputDict) {
for (let i = 0; i < inputDict[string]; i++) {
stringList.push(string);
}
}
// Shuffle the array
shuffleArray(stringList);
return stringList;
}
/**
* Randomly distributes items across multiple groups, ensuring each group receives
* a random number of items up to a specified maximum. After the initial distribution,
* any remaining items are assigned to groups in order until all items are distributed.
*
* @param {Array} items - The array of items to be distributed among groups.
* @param {Array} groups - The array of groups to which the items will be assigned.
* @param {number} [max_items_per_group=5] - The maximum number of items each group can initially receive.
* @returns {Object} An object where keys are group names and values are arrays of items assigned to each group.
*/
function randomlyDistributeItems(items, groups, max_items_per_group=5) {
const groupAssignments = {};
// Shuffle the items first
shuffleArray(items);
let itemIndex = 0;
// Assign initial items to groups
for (const group of groups) {
groupAssignments[group] = [];
const numItems = Math.floor(Math.random() * max_items_per_group) + 1;
for (let i = 0; i < numItems && itemIndex < items.length; i++) {
groupAssignments[group].push(items[itemIndex]);
itemIndex++;
}
}
// Assign remaining items to groups in order
while (itemIndex < items.length) {
for (const group of groups) {
if (itemIndex < items.length) {
groupAssignments[group].push(items[itemIndex]);
itemIndex++;
}
}
}
return groupAssignments;
}
/**
* Extracts tasks from HTML tables with a specified class name and organizes them into an object.
* Each task includes a name, duration, dependencies, and optionally additional dependencies
* based on the entity type (simple-shot, complex-shot, simple-sequence).
*
* @param {string} className - The class name of the tables from which tasks are extracted.
* @returns {Object} An object mapping entity types to arrays of task objects, where each task
* object contains details such as name, duration, dependencies, and any extra dependencies.
*/
function getTablesTasks(className) {
//console.log("Getting tasks from: " + className);
const tables = document.getElementsByClassName(className);
const allTablesTasks = {};
// Loop through each table
for (let i = 0; i < tables.length; i++) {
const table = tables[i];
//console.log(table);
const entityType = table.getAttribute('data-entity-type');
//console.log(entityType);
allTablesTasks[entityType] = [];
// Loop through each row
//console.log("table.rows: " + table.rows.length);
for (let j = 1; j < table.rows.length; j++) {
const row = table.rows[j];
//console.log(row);
// Extract task details
allTablesTasks[entityType].push({
name: row.cells[0].querySelector('.taskName').value,
duration: parseInt(row.cells[1].querySelector('.taskDuration').value),
dependencies: row.cells[2].querySelector('.taskDependencies').value.split(','),
// Only add first extra dependencies for shots and sequences
... ( ["simple-shot", "complex-shot", "simple-sequence"].includes(entityType) && {
firstExtraDependencies: row.cells[3].querySelector('.asetTaskDependencies').value.split(',')
}),
// Only add second extra dependencies for shots
... ( ["simple-shot", "complex-shot"].includes(entityType) && {
secondExtraDependencies: row.cells[4].querySelector('.seqTaskDependencies').value.split(',')
})
});
};
};
//console.log("allTablesTasks:")
//console.log(allTablesTasks);
return allTablesTasks
}
/**
* Adds a resource class to the OSF structure if it does not already exist.
* @param {Object} osf - The OSF structure.
* @param {string} taskName - The name of the task.
* @returns {Object} The resource class object created or found.
*/
function addResourceClass(osf, taskName) {
let resourceClass = getOsfResourceClass(osf, taskName);
//console.log(resourceClass);
if (resourceClass) {
return resourceClass;
}
const resourceId = `resource_${osf.snapshot.resourceClasses.length + 1}`;
resourceClass = {"id": resourceId,"name": taskName}
osf.snapshot.resourceClasses.push(resourceClass);
//console.log("Created resource class: " + resourceClass);
}
/**
* Finds the resource class with the given task name in the OSF structure.
* @param {Object} osf - The OSF structure.
* @param {string} taskName - The name of the task.
* @returns {string} The id of the resource class object if found, undefined otherwise.
*/
function getOsfResourceClass(osf, taskName) {
for (let i = 0; i < osf.snapshot.resourceClasses.length; i++) {
if (osf.snapshot.resourceClasses[i].name === taskName) {
return osf.snapshot.resourceClasses[i].id;
}
}
}
/**
* Generates asset activities for the OSF structure based on the given asset tasks.
* @param {Object} osf - The OSF structure.
* @param {number} numEnvAssets - The number of environment assets.
* @param {number} numCharAssets - The number of character assets.
* @param {number} numPropAssets - The number of prop assets.
* @param {Object} assetTasks - The templated asset tasks, with each key being an asset type
* and the value being an array of asset tasks.
* @returns {Object} The asset activities object with the generated activities.
*/
function generateAssetActivities(osf, numEnvAssets, numCharAssets, numPropAssets, assetTasks) {
let assetActivities = [];
const assetTypesCounts = {
"env": numEnvAssets,
"char": numCharAssets,
"prop": numPropAssets
};
//console.log(assetTypesCounts);
for (let x = 0; x < Object.keys(assetTypesCounts).length; x++) {
let assetType = Object.keys(assetTypesCounts)[x];
let assetTypeTasks = assetTasks[assetType];
let assetTypeCount = assetTypesCounts[assetType];
let assetTypeActivities = [];
//console.log(assetType);
//console.log(assetTypeCount);
for (let assetNum = 1; assetNum <= assetTypeCount; assetNum++) {
let assetTasksWithIds = [];
for (let j = 0; j < assetTypeTasks.length; j++) {
// Create a mapping of task names to their IDs for dependency resolution
const assetTaskNameToId = {};
for (let j = 0; j < assetTypeTasks.length; j++) {
assetTaskNameToId[assetTypeTasks[j].name] = `asset_${assetType}_${assetNum}_task_${assetTypeTasks[j].name}`;
}
let dependencies = assetTypeTasks[j].dependencies.map(dep => {
// Resolve dependency using the task name mapping
const depName = dep.trim();
// Return empty string if dependency not found
return assetTaskNameToId[depName] || "";
}).filter(dep => dep !== ""); // Remove invalid dependencies
assetTasksWithIds.push({
"id": `asset_${assetType}_${assetNum}_task_${assetTypeTasks[j].name}`,
"name": assetTypeTasks[j].name,
"task": {
"duration": assetTypeTasks[j].duration,
"resources": [
{
"resource": getOsfResourceClass(osf, assetTypeTasks[j].name),
"units": 1
}
]
},
"dependencies": dependencies
});
}
assetTypeActivities.push({
"id": `asset_${assetType}_${assetNum}`,
"name": `Asset ${assetType} ${assetNum}`,
"category": "Asset",
"summary": assetTasksWithIds
});
};
assetActivities.push({
"id": `${assetType}`,
"name": `${assetType}`,
"category": "Asset",
"summary": assetTypeActivities
});
};
return assetActivities
}
/**
* Calculates a random set of asset indexes for each asset type, given the
* max number of dependencies for each type and the total number of assets of
* each type.
* @param {Object} assetTypesCounts - An object with asset types as keys and
* the number of assets of that type as value.
* @param {Object} assetTypesDependencyCounts - An object with asset types as
* keys and the max number of dependencies for that type as value.
* @returns {Object} An object with asset types as keys and an array of random
* asset indexes of that type as value.
*
* @example
* const assetTypesCounts = {
* "env": 30,
* "char": 20,
* "prop": 10
* };
* const assetTypesDependencyCounts = {
* "env": 1,
* "char": 3,
* "prop": 2
* };
* const assetIndexes = calculateRandomAssetIndexes(
* assetTypesCounts, assetTypesDependencyCounts
* );
* //console.log(assetIndexes);
* {
* "env": [18],
* "char": [5, 12, 20],
* "prop": [1, 8]
* }
*/
function calculateRandomAssetIndexes(assetTypesCounts, assetTypesDependencyCounts) {
let assetIndexes = {};
for (let assetType in assetTypesCounts) {
let maxAssetCount = assetTypesDependencyCounts[assetType];
let assetTypeCount = assetTypesCounts[assetType];
// Get random number of asset dependencies between 0 and maxAssetCount
let numAssetDependencies = Math.min(1, maxAssetCount) +1 ;
let assetIndexesForType = [];
for (let l = 0; l < numAssetDependencies; l++) {
let randomAssetIndex;
do {
randomAssetIndex = Math.floor(Math.random() * assetTypeCount) + 1;
} while (
assetIndexesForType.includes(randomAssetIndex)
);
assetIndexesForType.push(randomAssetIndex);
}
assetIndexes[assetType] = assetIndexesForType;
}
return assetIndexes;
}
/**
* Resolves the asset dependencies for a given entity task.
* @param {object} entityTask - The entity task for which to resolve the asset dependencies.
* @param {object} assetTasks - The asset tasks, with each key being a type of asset
* and the value being an array of asset tasks.
* @param {object} assetTypesCounts - The count of each asset type, with each key being a type of asset
* and the value being the number of assets of that type.
* @param {object} assetTypesDependencyCounts - The number of dependencies for each asset type, with each key being a type of asset
* and the value being the number of dependencies for that type.
* @param {object} assetTypesIndexes - The indexes of the assets to use for each asset type, with each key being a type of asset
* and the value being an array of indexes.
* @returns {array} The resolved asset dependencies as an array of strings.
*/
function resolveAssetDependencies(
entityTask, assetTasks, assetTypesCounts=null, assetTypesDependencyCounts=null, assetTypesIndexes=null
) {
let assetDependencies = [];
// Validate that either assetTypesCounts and assetTypesIndexes
// is provided or that assetTypesIndexes is provided
if (!assetTypesCounts && !assetTypesIndexes) {
throw new Error(
"Either assetTypesCounts and assetTypesDependencyCounts, or assetTypesIndexes must be provided"
);
}
if (!assetTypesIndexes) {
// validate that assetTypesCounts and assetTypesDependencyCounts are provided
if (!assetTypesCounts || !assetTypesDependencyCounts) {
throw new Error(
"Both assetTypesCounts and assetTypesDependencyCounts must be provided"
);
}
assetTypesIndexes = calculateRandomAssetIndexes(assetTypesCounts, assetTypesDependencyCounts);
}
for (let assetType in assetTypesIndexes) {
let assetTypeTasks = assetTasks[assetType];
let assetIndexesForType = assetTypesIndexes[assetType];
for (let l = 0; l < assetIndexesForType.length; l++) {
let randomAssetIndex = assetIndexesForType[l];
// Get the asset dependencies specified in the UI
//console.log(entityTask);
let assetDepNames = entityTask.firstExtraDependencies;
//console.log(assetDepNames);
// Find matching asset tasks
assetDepNames.forEach(depName => {
depName = depName.trim();
for (let n = 0; n < assetTypeTasks.length; n++) {
// Check if the asset task name is the dependency name
if (assetTypeTasks[n].name == depName) {
assetDependencies.push(
`asset_${assetType}_${randomAssetIndex}_task_${assetTypeTasks[n].name}`
);
break; // Move to the next dependency name once a match is found
}
}
});
}
}
return assetDependencies;
}
/**
* Generates the activities for each episode based on the given parameters.
* @param {Object} osf - The OSF structure.
* @param {number} numEpisodes - The number of episodes.
* @param {number} shotsPerEpisode - The number of shots per episode.
* @param {number} complexShotsPercentagePerEpisode - The percentage number of complex shots per episode.
* @param {Object} shotTypesTasks - The templated shot tasks, with each key being a shot type
* and the value being an array of shot tasks.
* @param {number} sequencesPerEpisode - The number of sequences per episode.
* @param {Object} sequenceTypesTasks - The templated sequence tasks, with each key being a sequence type
* and the value being an array of sequence tasks.
* @param {number} numEnvAssets - The number of environment assets.
* @param {number} numCharAssets - The number of character assets.
* @param {number} numPropAssets - The number of prop assets.
* @param {Object} assetTasks - The templated asset tasks, with each key being an asset type
* and the value being an array of asset tasks.
* @returns {Object} The episode activities object with the generated activities.
*/
function generateEpisodeActivities(
osf, numEpisodes, shotsPerEpisode, complexShotsPercentagePerEpisode, shotTypesTasks,
sequencesPerEpisode, sequenceTypesTasks,
numEnvAssets, numCharAssets, numPropAssets, assetTasks
) {
let episodeActivities = [];
// Define a data structure to store the number of assets of each type
let assetTypesCounts = {
"env": numEnvAssets,
"char": numCharAssets,
"prop": numPropAssets
};
// Generate a radom number of complex shots per episode
// using the complexShotsPercentagePerEpisode input for the whole project
// the data will be a dictionary per episode number and the
// value will be the number of complex shots
let complexShotsPerEpisodeCounts = {};
for (let i = 1; i <= numEpisodes; i++) {
let randomPercentage = Math.floor(Math.random() * complexShotsPercentagePerEpisode) + 1;
complexShotsPerEpisodeCounts[i] = Math.round((shotsPerEpisode * randomPercentage)/100) +1;
}
//console.log("shotsPerEpisode: " + shotsPerEpisode);
//console.log("complexShotsPerEpisodeCounts:");
//console.log(complexShotsPerEpisodeCounts);
for (let epiNum = 1; epiNum <= numEpisodes; epiNum++) {
let episodeID = `episode_${epiNum}`;
//console.log("episodeID: " + episodeID);
// Define a list of simple and complex shots for this episode
let episodeSimpleShotsCount = shotsPerEpisode - complexShotsPerEpisodeCounts[epiNum];
//console.log("episodeSimpleShotsCount: " + episodeSimpleShotsCount);
//console.log("episodeComplexShotsCount: " + complexShotsPerEpisodeCounts[epiNum]);
let episodeShotsTypes = generateStringList(
{
"simple": episodeSimpleShotsCount,
"complex": complexShotsPerEpisodeCounts[epiNum]
},
);
//console.log("episodeShotsTypes:");
//console.log(episodeShotsTypes);
// Divide the shots among the episode sequences
//let shotsPerSequence = Math.floor(shotsPerEpisode / sequencesPerEpisode);
// Make a list of sequences with the same number of shots but with random
// assignment of simple and complex shots
let sequences = [];
for (let j = 1; j <= sequencesPerEpisode; j++) {
sequences.push("Sequence " + j);
}
//console.log("sequences:");
//console.log(sequences);
let sequencesAndShots = randomlyDistributeItems(episodeShotsTypes, sequences);
//console.log("sequencesAndShots:");
//console.log(sequencesAndShots);
let sequenceActivities = [];
// we know we only have one single sequence type at the moment
let sequenceTasks = sequenceTypesTasks["simple-sequence"];
// iterate over the sequencesAndShots dictionary
for (let seqNum = 1; seqNum < Object.keys(sequencesAndShots).length +1; seqNum++) {
let sequence = Object.keys(sequencesAndShots)[seqNum-1];
let sequenceShotsTypes = sequencesAndShots[sequence];
let sequenceID = `episode_${epiNum}_sequence_${seqNum}`;
//console.log("sequenceID: " + sequenceID);
//console.log("sequence: " + sequence);
//console.log("sequenceShotsTypes: ")
//console.log(sequenceShotsTypes);
sequenceContainerActivities = [];
// Define a data structure to store the number
// of assets dependencies of each type for this sequence
let assetTypesDependencyCounts = {
"env": 1,
"char": 5,
"prop": 5
};
// pre-calculate random asset indexes for each asset type for this
// sequence so that we can reuse them for both sequence and shots
sequenceAssetTypesIndexes = calculateRandomAssetIndexes(
assetTypesCounts, assetTypesDependencyCounts
);
let sequenceTasksWithIds = [];
// iterate over sequence tasks
//console.log("sequenceTasks.length: " + sequenceTasks.length);
for (let tskIndex = 0; tskIndex < sequenceTasks.length; tskIndex++) {
// Create a mapping of task names to their IDs for dependency resolution
const sequenceTaskNameToId = {};
for (let m = 0; m < sequenceTasks.length; m++) {
sequenceTaskNameToId[sequenceTasks[m].name] =
`episode_${epiNum}_sequence_${seqNum}_task_${sequenceTasks[m].name}`
;
}
let dependencies = sequenceTasks[tskIndex].dependencies.map(dep => {
// Resolve dependency using the task name mapping
const depName = dep.trim();
// Return empty string if dependency not found
return sequenceTaskNameToId[depName] || "";
}).filter(dep => dep !== ""); // Remove invalid dependencies
// resolve a list of asset dependencies for this sequence task
let sequenceAssetDependencies = resolveAssetDependencies(
sequenceTasks[tskIndex], assetTasks, null, null, sequenceAssetTypesIndexes
);
sequenceTasksWithIds.push({
"id": `episode_${epiNum}_sequence_${seqNum}_task_${sequenceTasks[tskIndex].name}`,
"name": sequenceTasks[tskIndex].name,
"task": {
"duration": sequenceTasks[tskIndex].duration,
"resources": [
{
"resource": getOsfResourceClass(osf, sequenceTasks[tskIndex].name),
"units": 1
}
]
},
// Combine shot and asset dependencies
"dependencies": dependencies.concat(sequenceAssetDependencies)
});
}
sequenceContainerActivities.push({
"id": `episode_${epiNum}_sequence_${seqNum}_tasks_container`,
"name": `Episode ${epiNum} Sequence ${seqNum} Tasks`,
"category": "SequenceTasksContainer",
"summary": sequenceTasksWithIds
});
// iterate over sequence shots
let shotActivities = [];
for (let shtNum = 1; shtNum <= sequenceShotsTypes.length; shtNum++) {
let shotTasksWithIds = [];
let shotType = sequenceShotsTypes[shtNum - 1];
let shotTasks;
let shotID = `episode_${epiNum}_sequence_${seqNum}_${shotType}_shot_${shtNum}`;
//console.log("shotID: " + shotID);
//console.log(shotType);
if (shotType == "complex") {
shotTasks = shotTypesTasks["complex-shot"];
} else {
shotTasks = shotTypesTasks["simple-shot"];
};
//console.log(shotTasks);
// iterate over shot tasks
for (let tskIndex = 0; tskIndex < shotTasks.length; tskIndex++) {
// Create a mapping of task names to their IDs for dependency resolution
const shotTaskNameToId = {};
for (let m = 0; m < shotTasks.length; m++) {
shotTaskNameToId[shotTasks[m].name] =
`episode_${epiNum}_sequence_${seqNum}_${shotType}_shot_${shtNum}_task_${shotTasks[m].name}`
;
}
let dependencies = shotTasks[tskIndex].dependencies.map(dep => {
// Resolve dependency using the task name mapping
const depName = dep.trim();
// Return empty string if dependency not found
return shotTaskNameToId[depName] || "";
}).filter(dep => dep !== ""); // Remove invalid dependencies
// resolve a list of asset dependencies for this shot task
// start from the pre calculated sequence asset types indexes
// but now randombly select a subset of indexes for each type
let shotAssetTypesIndexes = {
"env": shuffleArray(
sequenceAssetTypesIndexes["env"]
).slice(0, 1)
,
"char": shuffleArray(
sequenceAssetTypesIndexes["char"]
).slice(0, Math.floor(Math.random() * 3) + 1)
,
"prop": shuffleArray(
sequenceAssetTypesIndexes["prop"]
).slice(0, Math.floor(Math.random() * 3) + 1)
};
// resolve a list of asset dependencies for this shot task
let shotAssetDependencies = resolveAssetDependencies(
shotTasks[tskIndex], assetTasks, null, null, shotAssetTypesIndexes
);
// Get the sequence dependencies specified in the UI
let sequenceDependencies = [];
let seqDepNames = shotTasks[tskIndex].secondExtraDependencies;
//console.log(seqDepNames);
// Find matching asset tasks
seqDepNames.forEach(depName => {
depName = depName.trim();
for (let n = 0; n < sequenceTasks.length; n++) {
// Check if the asset task name is the dependency name
if (sequenceTasks[n].name == depName) {
sequenceDependencies.push(
`episode_${epiNum}_sequence_${seqNum}_task_${sequenceTasks[n].name}`
);
break; // Move to the next dependency name once a match is found
}
}
});
// Create the shot task
shotTasksWithIds.push({
"id": `episode_${epiNum}_sequence_${seqNum}_${shotType}_shot_${shtNum}_task_${shotTasks[tskIndex].name}`,
"name": shotTasks[tskIndex].name,
"task": {
"duration": shotTasks[tskIndex].duration,
"resources": [
{
"resource": getOsfResourceClass(osf, shotTasks[tskIndex].name),
"units": 1
}
]
},
// Combine shot, sequence and asset dependencies
"dependencies": dependencies.concat(sequenceDependencies).concat(shotAssetDependencies)
});
}
shotActivities.push({
"id": shotID,
"name": `Episode ${epiNum} Sequence ${seqNum} Shot ${shtNum}`,
"category": "Shot",
"summary": shotTasksWithIds
});
shotActivities
};
sequenceContainerActivities.push({
"id": `episode_${epiNum}_sequence_${seqNum}_shots_container`,
"name": `Episode ${epiNum} Sequence ${seqNum} Shots`,
"category": "SequenceShotsContainer",
"summary": shotActivities
});
sequenceActivities.push({
"id": sequenceID,
"name": `Episode ${epiNum} ${sequence}`,
"category": "Sequence",
"summary": sequenceContainerActivities
});
};
episodeActivities.push({
"id": episodeID,
"name": `Episode ${epiNum}`,
"category": "Episode",
"summary": sequenceActivities
});
};
return episodeActivities
}
function generateOSF() {
const numEpisodes = parseInt(document.getElementById("numEpisodes").value);
const shotsPerEpisode = parseInt(document.getElementById("shotsPerEpisode").value);
const complexShotsPerEpisode = parseInt(document.getElementById("complexShotsPerEpisode").value);
const sequencesPerEpisode = parseInt(document.getElementById("sequencesPerEpisode").value);
const numEnvAssets = parseInt(document.getElementById("numEnvAssets").value);
const numCharAssets = parseInt(document.getElementById("numCharAssets").value);
const numPropAssets = parseInt(document.getElementById("numPropAssets").value);
//console.log("numEpisodes: " + numEpisodes);
//console.log("shotsPerEpisode: " + shotsPerEpisode);
//console.log("complexShotsPerEpisode: " + complexShotsPerEpisode);
//console.log("sequencesPerEpisode: " + sequencesPerEpisode);
//console.log("numEnvAssets: " + numEnvAssets);
//console.log("numCharAssets: " + numCharAssets);
//console.log("numPropAssets: " + numPropAssets);
// Get Asset tasks
const assetTasks = getTablesTasks("assets-tasks-table");
// Get Shot tasks
const shotTasks = getTablesTasks("shots-tasks-table");
// Get Sequence tasks
const sequenceTasks = getTablesTasks("sequence-tasks-table");
// Create OSF structure
const osf = {
"snapshot": {
"description": "Generated OSF",
"dayDuration": 28800,
"calendars": [],
"calendar": {},
"resourceClasses": [],
"projects": [
{
"id": "project_1",
"name": "Generated Project",
"calendar": {},
"activities": []
}
]
}
};
// Generate the resources from task names for each type of the tables
// considering that the tasks objects are dictionaries of types
//console.log("Generating resource classes...");
//console.log("assetTasks: " + Object.keys(assetTasks).length);
//console.log("shotTasks: " + Object.keys(shotTasks).length);
//console.log("sequenceTasks: " + Object.keys(sequenceTasks).length);
for (let i = 0; i < Object.keys(assetTasks).length; i++) {
let assetType = Object.keys(assetTasks)[i];
assetTasks[assetType].forEach(task => addResourceClass(osf, task.name));
}
for (let i = 0; i < Object.keys(shotTasks).length; i++) {
let shotType = Object.keys(shotTasks)[i];
shotTasks[shotType].forEach(task => addResourceClass(osf, task.name));
}
for (let i = 0; i < Object.keys(sequenceTasks).length; i++) {
let sequenceType = Object.keys(sequenceTasks)[i];
sequenceTasks[sequenceType].forEach(task => addResourceClass(osf, task.name));
}
//console.log("resourceClasses: " + osf.snapshot.resourceClasses);
// Generate asset activities
//console.log("Generating asset activities...");
const assetActivities = generateAssetActivities(
osf, numEnvAssets, numCharAssets, numPropAssets, assetTasks
);
osf.snapshot.projects[0].activities.push({
"id": "assets",
"name": "Assets",
"summary": assetActivities
});
// Generate episodes shot activities
//console.log("Generating episode episode activities...");
const episodeActivities = generateEpisodeActivities(
osf, numEpisodes, shotsPerEpisode, complexShotsPerEpisode, shotTasks,
sequencesPerEpisode, sequenceTasks,
numEnvAssets, numCharAssets, numPropAssets, assetTasks
);
osf.snapshot.projects[0].activities.push({
"id": "episode_shots",
"name": "Episode Shots",
"summary": episodeActivities
});
// Global calendar settings
const globalCalendar = {
"id": "global_calendar",
"name": "Global Calendar",
"workweek": {
"monday": parseInt(document.getElementById("monday").value),
"tuesday": parseInt(document.getElementById("tuesday").value),
"wednesday": parseInt(document.getElementById("wednesday").value),
"thursday": parseInt(document.getElementById("thursday").value),
"friday": parseInt(document.getElementById("friday").value),
"saturday": parseInt(document.getElementById("saturday").value),
"sunday": parseInt(document.getElementById("sunday").value)
},
"exceptions": []
};