add html/jsonapi armor builder

- make decoration calculator use 1-slot decorations multiple times
This commit is contained in:
Bryce Allen committed 2015-04-29 02:16:01 -05:00
1 parent 784299dd53
commit 6329e7028b
11 files changed
+662 -38

No files matched your search

+402
View File
@@ -0,0 +1,402 @@
<html>
<head>
<title>Poogie Outfitters</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/ejs_production.js"></script>
<style>
label {
font-weight: bold;
}
body {
font-family: sans, sans-serif;
}
td.num {
text-align: right;
}
/*@media (max-width: 600) {*/
#flexbox {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
</style>
<script type="text/javascript">
var DATA_PATH = get_base_path() + "/jsonapi/";
var TYPES = ["Head", "Body", "Arms", "Waist", "Legs"];
var ELEMENTS = ["fire", "water", "thunder", "ice", "dragon"];
// dict mapping armor name to list containing id
var armor_ids = {};
// dict mapping skill name to object
var skill_trees = {};
// dict mapping type to armor name
var armors = {};
var decorations = { "Head": [], "Body": [], "Arms": [],
"Waist": [], "Legs": [] };
var slots_left = {};
$(document).ready(function(){
$.getJSON(DATA_PATH + "armor/_index_name.json",
function(data) {
armor_ids = data;
setup_autocomplete();
$.getJSON(DATA_PATH + "skilltree/_all.json",
function(data) {
skill_trees = data;
load_armors();
load_decorations();
// if nothing is loaded update won't be
// called, so force an update
update_skills();
});
});
$("#armor_table").on("click", "button.add_decoration",
function (evt) {
add_decoration_by_element(evt.target.id);
});
$("#armor_table").on("click", "button.remove_decoration",
function (evt) {
remove_decoration_by_element(evt.target.id);
});
// TODO: update skills
});
function load_armors() {
armors_saved = localStorage.getItem("armor_names");
if (armors_saved != null) {
armor_names = JSON.parse(armors_saved);
$.each(armor_names, function(type, name) {
if (name) {
$("#" + type).val(name);
set_armor(type, name);
}
});
}
}
function load_decorations() {
decorations_saved = localStorage.getItem("decoration_name_lists");
if (decorations_saved != null) {
dname_list = JSON.parse(decorations_saved);
$.each(dname_list, function(type, names) {
$.each(names, function(i, name) {
add_decoration(type, name);
});
});
}
}
function add_decoration_by_element(element_id) {
parts = element_id.split("_");
type = parts[0];
value = $("#" + type + "_decoration").val();
add_decoration(type, value);
}
function remove_decoration_by_element(element_id) {
parts = element_id.split("_");
type = parts[0];
index = parts[3];
remove_decoration(type, index);
}
function setup_decoration_autocomplete(type, max_slots) {
options = [];
$.each(skill_trees, function(skill_name, stree) {
$.each(stree["decoration_values"], function(i, points) {
nslots = i + 1;
if (nslots <= max_slots && points > 0) {
option_name = stree["name"] + " +" + points;
options.push(option_name);
}
});
});
$("#" + type + "_decoration").autocomplete({ source: options });
$("#" + type + "_decoration").keypress(function(e) {
if (e.which == 13) {
value = $("#" + type + "_decoration").val();
add_decoration(type, value);
}
});
}
function save_armors() {
var armor_names = {};
$.each(armors, function(type, armor) {
armor_names[type] = armor["name"];
});
localStorage.setItem("armor_names", JSON.stringify(armor_names));
}
function save_decorations() {
var decoration_name_lists = {};
$.each(decorations, function(type, dlist) {
$.each(dlist, function(i, d) {
if (! (type in decoration_name_lists)) {
decoration_name_lists[type] = [];
}
decoration_name_lists[type].push(d["display_name"]);
});
});
localStorage.setItem("decoration_name_lists",
JSON.stringify(decoration_name_lists));
}
function get_base_path() {
var path = document.location.pathname;
return path.substring(0, path.lastIndexOf('/'));
}
function setup_autocomplete() {
$.getJSON(DATA_PATH + "armor/_index_slot.json",
function(data) {
$.each(TYPES,
function(i, type) {
$("#" + type).autocomplete(
{ source: data[type],
change: function (event, ui) {
set_armor(this.id, ui.item["value"]);
}
}
);
$("#" + type).keypress(function(e) {
if (e.which == 13) {
set_armor(this.id, $("#" + this.id).val());
}
});
});
}
);
}
function add_decoration(type, value) {
parts = value.split(" +");
skill = parts[0]
points = parts[1]
slots = 0;
for (i=0; i < 3; i++) {
if (skill_trees[skill]["decoration_values"][i] == points) {
slots = i + 1;
break;
}
}
decoration_id = skill_trees[skill]["decoration_ids"][slots-1];
$.getJSON(DATA_PATH + "decoration/" + decoration_id + ".json",
function (data) {
if (! (type in decorations)) {
decorations[type] = []
}
data["display_name"] = value;
decorations[type].push(data);
slots_left[type] -= data["num_slots"];
save_decorations();
update_slots(type);
update_skills();
}
);
}
function remove_decoration(type, index) {
d = decorations[type][index];
slots_left[type] += d["num_slots"];
decorations[type].splice(index, 1);
save_decorations();
update_slots(type);
update_skills();
}
function set_armor(type, name, noupdate) {
armor_id = armor_ids[name][0];
$.getJSON(DATA_PATH + "armor/" + armor_id + ".json",
function(data) {
armors[type] = data;
if (noupdate == undefined) {
save_armors();
decorations[type] = [];
slots_left[type] = data["num_slots"];
var skill_names = Object.keys(data["skills"]);
skill_names.sort();
var slist = "";
$.each(skill_names, function(i, sname) {
if (i != 0) {
slist += ", ";
}
slist += sname + " " + data["skills"][sname];
});
$("#" + type + "_skills").html(slist);
update_slots(type);
update_skills();
}
}
);
}
function update_slots(type) {
var free_slots = slots_left[type];
var slots_html = new EJS(
{ url: "templates/decorations.ejs" }
).render({ decorations: decorations[type],
free_slots: free_slots,
type: type });
$("#" + type + "_slots").html(slots_html);
if (free_slots > 0) {
setup_decoration_autocomplete(type, free_slots);
}
}
function update_skills() {
// map of skill name to total
var skills = {};
// map of type to dict map of skill name to total including decoration
var tskills_by_type = {};
// map of type to dict map of skill name to total from decorations only
var dskills_by_type = {};
var element_resist = {};
var slots = 0;
var slots_free = 0;
$.each(armors, function(type, armor) {
dskills_by_type[type] = {};
tskills_by_type[type] = {};
$.each(armor["skills"], function(skill_name, skill_value) {
if (! (skill_name in skills)) {
skills[skill_name] = 0;
}
skills[skill_name] += skill_value;
if (! (skill_name in tskills_by_type[type])) {
tskills_by_type[type][skill_name] = 0;
}
tskills_by_type[type][skill_name] += skill_value;
})
$.each(decorations[type], function(i, decoration) {
$.each(decoration["skills"],
function(skill_name, skill_value) {
if (! (skill_name in skills)) {
skills[skill_name] = 0;
}
skills[skill_name] += skill_value;
if (! (skill_name in dskills_by_type[type])) {
dskills_by_type[type][skill_name] = 0;
}
dskills_by_type[type][skill_name] += skill_value;
if (! (skill_name in tskills_by_type[type])) {
tskills_by_type[type][skill_name] = 0;
}
tskills_by_type[type][skill_name] += skill_value;
}
);
})
$.each(ELEMENTS, function(i, element) {
key = element + "_res";
if (! (element in element_resist)) {
element_resist[element] = 0;
}
element_resist[element] += armor[key];
$("#" + type + "_" + element).html(armor[key]);
})
slots += armor["num_slots"];
slots_free += slots_left[type];
//$("#" + type + "_slots").html(armor["num_slots"]);
});
$("#Total_slots").html((slots - slots_free)
+ " used / " + slots + " slots");
var skill_names = Object.keys(skills);
skill_names.sort(function(a, b) {
sa = skills[a] ? skills[a] : 0;
sb = skills[b] ? skills[b] : 0;
return sb - sa;
});
var skills_table = new EJS(
{ url: "templates/skills.ejs" }
).render({ TYPES: TYPES,
skills: skills,
dskills_by_type: dskills_by_type,
tskills_by_type: tskills_by_type,
active_skills: skill_names,
armors: armors });
$("#skills_div").html(skills_table);
var resist_table = new EJS(
{ url: "templates/resistance.ejs" }
).render({ TYPES: TYPES,
ELEMENTS: ELEMENTS,
armors: armors,
element_resist: element_resist });
$("#resist_div").html(resist_table);
}
</script>
</head>
<body>
<table id="armor_table" cellpadding="2" cellspacing="2">
<tr>
<td><label for="Head">Head:</label></td>
<td><input id="Head" type="text" size="15" /></td>
<td id="Head_slots">&nbsp;</td>
<td id="Head_skills">&nbsp;</td>
</tr>
<tr>
<td><label for="Body">Body:</label></td>
<td><input id="Body" type="text" size="15" /></td>
<td id="Body_slots">&nbsp;</td>
<td id="Body_skills">&nbsp;</td>
</tr>
<tr>
<td><label for="Arms">Arms:</label></td>
<td><input id="Arms" type="text" size="15" /></td>
<td id="Arms_slots">&nbsp;</td>
<td id="Arms_skills">&nbsp;</td>
</tr>
<tr>
<td><label for="Waist">Waist:</label></td>
<td><input id="Waist" type="text" size="15" /></td>
<td id="Waist_slots">&nbsp;</td>
<td id="Waist_skills">&nbsp;</td>
</tr>
<tr>
<td><label for="Legs">Legs:</label></td>
<td><input id="Legs" type="text" size="15" /></td>
<td id="Legs_slots">&nbsp;</td>
<td id="Legs_skills">&nbsp;</td>
</tr>
<tr>
<th>&nbsp;</th>
<td>&nbsp;</td>
<td id="Total_slots">0</td>
<td>&nbsp;</td>
</tr>
</table>
<div id="flexbox">
<fieldset>
<legend>Skills</legend>
<div id="skills_div"></div>
</fieldset>
<fieldset>
<legend>Elemental Resistance</legend>
<div id="resist_div"></div>
</fieldset>
</div>
</body>
</body>
+1
View File
@@ -0,0 +1 @@
(function(){var rsplit=function(string,regex){var result=regex.exec(string),retArr=new Array(),first_idx,last_idx,first_bit;while(result!=null){first_idx=result.index;last_idx=regex.lastIndex;if((first_idx)!=0){first_bit=string.substring(0,first_idx);retArr.push(string.substring(0,first_idx));string=string.slice(first_idx)}retArr.push(result[0]);string=string.slice(result[0].length);result=regex.exec(string)}if(!string==""){retArr.push(string)}return retArr},chop=function(string){return string.substr(0,string.length-1)},extend=function(d,s){for(var n in s){if(s.hasOwnProperty(n)){d[n]=s[n]}}};EJS=function(options){options=typeof options=="string"?{view:options}:options;this.set_options(options);if(options.precompiled){this.template={};this.template.process=options.precompiled;EJS.update(this.name,this);return }if(options.element){if(typeof options.element=="string"){var name=options.element;options.element=document.getElementById(options.element);if(options.element==null){throw name+"does not exist!"}}if(options.element.value){this.text=options.element.value}else{this.text=options.element.innerHTML}this.name=options.element.id;this.type="["}else{if(options.url){options.url=EJS.endExt(options.url,this.extMatch);this.name=this.name?this.name:options.url;var url=options.url;var template=EJS.get(this.name,this.cache);if(template){return template}if(template==EJS.INVALID_PATH){return null}try{this.text=EJS.request(url+(this.cache?"":"?"+Math.random()))}catch(e){}if(this.text==null){throw ({type:"EJS",message:"There is no template at "+url})}}}var template=new EJS.Compiler(this.text,this.type);template.compile(options,this.name);EJS.update(this.name,this);this.template=template};EJS.prototype={render:function(object,extra_helpers){object=object||{};this._extra_helpers=extra_helpers;var v=new EJS.Helpers(object,extra_helpers||{});return this.template.process.call(object,object,v)},update:function(element,options){if(typeof element=="string"){element=document.getElementById(element)}if(options==null){_template=this;return function(object){EJS.prototype.update.call(_template,element,object)}}if(typeof options=="string"){params={};params.url=options;_template=this;params.onComplete=function(request){var object=eval(request.responseText);EJS.prototype.update.call(_template,element,object)};EJS.ajax_request(params)}else{element.innerHTML=this.render(options)}},out:function(){return this.template.out},set_options:function(options){this.type=options.type||EJS.type;this.cache=options.cache!=null?options.cache:EJS.cache;this.text=options.text||null;this.name=options.name||null;this.ext=options.ext||EJS.ext;this.extMatch=new RegExp(this.ext.replace(/\./,"."))}};EJS.endExt=function(path,match){if(!path){return null}match.lastIndex=0;return path+(match.test(path)?"":this.ext)};EJS.Scanner=function(source,left,right){extend(this,{left_delimiter:left+"%",right_delimiter:"%"+right,double_left:left+"%%",double_right:"%%"+right,left_equal:left+"%=",left_comment:left+"%#"});this.SplitRegexp=left=="["?/(\[%%)|(%%\])|(\[%=)|(\[%#)|(\[%)|(%\]\n)|(%\])|(\n)/:new RegExp("("+this.double_left+")|(%%"+this.double_right+")|("+this.left_equal+")|("+this.left_comment+")|("+this.left_delimiter+")|("+this.right_delimiter+"\n)|("+this.right_delimiter+")|(\n)");this.source=source;this.stag=null;this.lines=0};EJS.Scanner.to_text=function(input){if(input==null||input===undefined){return""}if(input instanceof Date){return input.toDateString()}if(input.toString){return input.toString()}return""};EJS.Scanner.prototype={scan:function(block){scanline=this.scanline;regex=this.SplitRegexp;if(!this.source==""){var source_split=rsplit(this.source,/\n/);for(var i=0;i<source_split.length;i++){var item=source_split[i];this.scanline(item,regex,block)}}},scanline:function(line,regex,block){this.lines++;var line_split=rsplit(line,regex);for(var i=0;i<line_split.length;i++){var token=line_split[i];if(token!=null){try{block(token,this)}catch(e){throw {type:"EJS.Scanner",line:this.lines}}}}}};EJS.Buffer=function(pre_cmd,post_cmd){this.line=new Array();this.script="";this.pre_cmd=pre_cmd;this.post_cmd=post_cmd;for(var i=0;i<this.pre_cmd.length;i++){this.push(pre_cmd[i])}};EJS.Buffer.prototype={push:function(cmd){this.line.push(cmd)},cr:function(){this.script=this.script+this.line.join("; ");this.line=new Array();this.script=this.script+"\n"},close:function(){if(this.line.length>0){for(var i=0;i<this.post_cmd.length;i++){this.push(pre_cmd[i])}this.script=this.script+this.line.join("; ");line=null}}};EJS.Compiler=function(source,left){this.pre_cmd=["var ___ViewO = [];"];this.post_cmd=new Array();this.source=" ";if(source!=null){if(typeof source=="string"){source=source.replace(/\r\n/g,"\n");source=source.replace(/\r/g,"\n");this.source=source}else{if(source.innerHTML){this.source=source.innerHTML}}if(typeof this.source!="string"){this.source=""}}left=left||"<";var right=">";switch(left){case"[":right="]";break;case"<":break;default:throw left+" is not a supported deliLine truncated
+18
View File
@@ -0,0 +1,18 @@
<table>
<% for(var i=0; i<decorations.length; i++) { %>
<tr>
<td><%= Array(decorations[i]["num_slots"] + 1).join("\u26ab") %></td>
<td><%= decorations[i]["display_name"] %>
<button id="<%= type %>_remove_decoration_<%= i %>"
class="remove_decoration">-</button></td>
</tr>
<% } %>
<% if (free_slots > 0) { %>
<tr>
<td><%= Array(free_slots + 1).join("\u26aa") %><%= Array(3- free_slots + 1).join("&nbsp;") %></td>
<td><input id="<%= type %>_decoration" type="text" size="12">
<button id="<%= type %>_add_decoration"
class="add_decoration">+</button></td>
</tr>
<% } %>
</table>
+21
View File
@@ -0,0 +1,21 @@
<table id="resist_table" cellpadding="2" border="0">
<tr>
<th>&nbsp;</th>
<th>Total</th>
<th>Head</th>
<th>Body</th>
<th>Arms</th>
<th>Waist</th>
<th>Legs</th>
</tr>
<% for(var i=0; i<ELEMENTS.length; i++) { %>
<tr>
<th><%= ELEMENTS[i] %></th>
<td class="num"><%= element_resist[ELEMENTS[i]] %></td>
<% for(var j=0; j<TYPES.length; j++) { %>
<td class="num">
<%= armors[TYPES[j]]
? armors[TYPES[j]][ELEMENTS[i] + "_res"] : "&nbsp;" %></td>
<% } %>
<% } %>
</table>
+25
View File
@@ -0,0 +1,25 @@
<table id="skills_table" cellpadding="2" border="0">
<tr>
<th>&nbsp;</th>
<th>Total</th>
<% for(var i=0; i<TYPES.length; i++) { %>
<td><%= TYPES[i] %></td>
<% } %>
</tr>
<% for(var i=0; i<active_skills.length; i++) { %>
<tr>
<td><%= active_skills[i] %></td>
<td class="num"><%= skills[active_skills[i]] %></td>
<% for(var j=0; j<TYPES.length; j++) { %>
<td class="num">
<%= (armors[TYPES[j]] && tskills_by_type[TYPES[j]][active_skills[i]])
? tskills_by_type[TYPES[j]][active_skills[i]]
: "&nbsp;" %>
<%= 0 && (armors[TYPES[j]] && dskills_by_type[TYPES[j]][active_skills[i]])
? " (" + dskills_by_type[TYPES[j]][active_skills[i]] + ")"
: "&nbsp;" %>
</td>
<% } %>
</tr>
<% } %>
</table>
+24
View File
@@ -0,0 +1,24 @@
<table id="skills_table" cellpadding="2">
<tr>
<th>Skill</th>
<th>Total</th>
<% for(var i=0; i<TYPES.length; i++) { %>
<th><%= TYPES[i] %></th>
<% } %>
</tr>
<% for(var i=0; i<active_skills.length; i++) { %>
<tr>
<td><%= active_skills[i] %></td>
<td><%= skills[active_skills[i]] %></td>
<% for(var j=0; j<TYPES.length; j++) { %>
<td>
<%= (armors[TYPES[j]] && armors[TYPES[j]]["skills"][active_skills[i]])
? armors[TYPES[j]]["skills"][active_skills[i]] : "&nbsp;" %>
<%= (armors[TYPES[j]] && dskills_by_type[TYPES[j]][active_skills[i]])
? " (" + dskills_by_type[TYPES[j]][active_skills[i]] + ")"
: "&nbsp;" %>
</td>
<% } %>
</tr>
<% } %>
</table>