Skip to content

Commit

Permalink
added new import via URL to damage and defense calc
Browse files Browse the repository at this point in the history
  • Loading branch information
fquirin committed Aug 4, 2024
1 parent dc6d3bb commit e79e551
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 33 deletions.
4 changes: 2 additions & 2 deletions damage.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ <h3 id="main-headline" class="text-shadow-1">Damage Calculator</h3>
<label>Color style dark:</label><input name="option-dark-mode" type="checkbox">
</div>
<div class="group">
<label>Show detailed damage:</label><input name="option-detailed-info" type="checkbox">
<label>Show detailed result:</label><input name="option-detailed-info" type="checkbox">
</div>
<div class="group">
<label>Always single column:</label><input name="option-single-column" type="checkbox">
Expand Down Expand Up @@ -264,7 +264,7 @@ <h3>Welcome to the<br>D4 Damage Calculator</h3>
</div>
</div>
<div name="info-footer" class="section info-box" style="font-size: 12px; justify-content: center;">
Version: <script>document.write(d4cVersion);</script>
Version: <script>document.write(d4cVersion);</script> (S<script>document.write(d4season);</script>)
</div>
</div>
</div>
Expand Down
72 changes: 58 additions & 14 deletions damage.js
Original file line number Diff line number Diff line change
Expand Up @@ -960,22 +960,66 @@ noConImportDataSelector.addEventListener('change', function(ev){
const file = ev.target.files[0];
if (file){
importConfigurationFromFile(file, function(fileType, data){
if (fileType == "singleConfig"){
//create new calculator and add data
if (data){
var calc = addNewCalculator(false);
calc.restoreData(data);
openCalculatorsFromData(fileType, data, false);
});
}
});
function openCalculatorsFromData(fileType, data, openList, calculatorNames){
if (fileType == "singleConfig"){
//create new calculator and add data
if (data){
var calc = addNewCalculator(false);
calc.restoreData(data);
}
}else if (fileType == "configArray"){
//open calculators or import?
if (openList){
//open calculators
if (!calculatorNames?.length){
//fill up with IDs
calculatorNames = Object.keys(data);
}
calculatorNames.forEach(calcIdOrName => {
var calcData = data[calcIdOrName];
if (!calcData){
calcData = Object.values(data).find(itm => itm?.name == calcIdOrName);
}
}else if (fileType == "configArray"){
//add all configurations from file to storage
if (confirm("Warning: This will overwrite existing configurations with the same name! Continue?")){
var keepOld = true;
writeAllConfigsToLocalStorage(data, keepOld);
if (calcData?.calc == d4cType){
var calc = addNewCalculator(false);
calc.restoreData(calcData.data);
}
});
}else{
//add all configurations from file to storage
if (confirm("Warning: This will overwrite existing configurations with the same name! Continue?")){
var keepOld = true;
writeAllConfigsToLocalStorage(data, keepOld);
}
});
}
}
});
}

//Show a default calculator at start:
//addNewCalculator(true, true);
//Load calculators from file and or name?
checkUrlFileLoad(function(fileType, data){
//on load
console.log("Got data from file:", fileType, data);
//what now?
let promptText = "A file with calculators has been loaded.\nDo you want to open/import the data?";
if (confirm(promptText) == true){
//OK
openCalculatorsFromData(fileType, data, false);
}

}, function(fileType, data, calculatorNames){
//on load and show selected
console.log("Got data from file:", fileType, data);
console.log("Show calculators:", calculatorNames);
openCalculatorsFromData(fileType, data, true, calculatorNames);

}, function(){
//on skip
console.log("Ready");

//Show a default calculator at start:
//addNewCalculator(true, true);
});
4 changes: 2 additions & 2 deletions defense.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ <h3 id="main-headline" class="text-shadow-1">Defense Calculator</h3>
<label>Color style dark:</label><input name="option-dark-mode" type="checkbox">
</div>
<div class="group">
<label>Show detailed damage:</label><input name="option-detailed-info" type="checkbox">
<label>Show detailed result:</label><input name="option-detailed-info" type="checkbox">
</div>
<div class="group">
<label>Always single column:</label><input name="option-single-column" type="checkbox">
Expand Down Expand Up @@ -220,7 +220,7 @@ <h3>Welcome to the<br>D4 Defense Calculator</h3>
</div>
</div>
<div name="info-footer" class="section info-box" style="font-size: 12px; justify-content: center;">
Version: <script>document.write(d4cVersion);</script>
Version: <script>document.write(d4cVersion);</script> (S<script>document.write(d4season);</script>)
</div>
</div>
</div>
Expand Down
74 changes: 59 additions & 15 deletions defense.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ function buildCalculator(containerEle, options){
return (totalHp + addLife);
}
}, maximumLifeBase);
addResult("Total maximum life", maximumLifeMulti, undefined, lifeItemColor, "Total life after multiplicative modifiers.");
addResult("Total life + barrier", maximumLifeMulti, undefined, lifeItemColor, "Total max. life after multiplicative modifiers including barrier.");
addCustom("<hr>", "flat");
var maximumLife = maximumLifeMulti;

Expand Down Expand Up @@ -696,22 +696,66 @@ noConImportDataSelector.addEventListener('change', function(ev){
const file = ev.target.files[0];
if (file){
importConfigurationFromFile(file, function(fileType, data){
if (fileType == "singleConfig"){
//create new calculator and add data
if (data){
var calc = addNewCalculator(false);
calc.restoreData(data);
openCalculatorsFromData(fileType, data, false);
});
}
});
function openCalculatorsFromData(fileType, data, openList, calculatorNames){
if (fileType == "singleConfig"){
//create new calculator and add data
if (data){
var calc = addNewCalculator(false);
calc.restoreData(data);
}
}else if (fileType == "configArray"){
//open calculators or import?
if (openList){
//open calculators
if (!calculatorNames?.length){
//fill up with IDs
calculatorNames = Object.keys(data);
}
calculatorNames.forEach(calcIdOrName => {
var calcData = data[calcIdOrName];
if (!calcData){
calcData = Object.values(data).find(itm => itm?.name == calcIdOrName);
}
}else if (fileType == "configArray"){
//add all configurations from file to storage
if (confirm("Warning: This will overwrite existing configurations with the same name! Continue?")){
var keepOld = true;
writeAllConfigsToLocalStorage(data, keepOld);
if (calcData?.calc == d4cType){
var calc = addNewCalculator(false);
calc.restoreData(calcData.data);
}
});
}else{
//add all configurations from file to storage
if (confirm("Warning: This will overwrite existing configurations with the same name! Continue?")){
var keepOld = true;
writeAllConfigsToLocalStorage(data, keepOld);
}
});
}
}
});
}

//Show a default calculator at start:
//addNewCalculator(true, true);
//Load calculators from file and or name?
checkUrlFileLoad(function(fileType, data){
//on load
console.log("Got data from file:", fileType, data);
//what now?
let promptText = "A file with calculators has been loaded.\nDo you want to open/import the data?";
if (confirm(promptText) == true){
//OK
openCalculatorsFromData(fileType, data, false);
}

}, function(fileType, data, calculatorNames){
//on load and show selected
console.log("Got data from file:", fileType, data);
console.log("Show calculators:", calculatorNames);
openCalculatorsFromData(fileType, data, true, calculatorNames);

}, function(){
//on skip
console.log("Ready");

//Show a default calculator at start:
//addNewCalculator(true, true);
});

0 comments on commit e79e551

Please sign in to comment.