//Funtions function onSubmitAnimal(form){ var data = {}; $.map($(form).serializeArray(), function(n, i){ if(n['value']!= "") data[n['name']] = n['value']; else data[n['name']] = null; }); if(data._id){ $.ajax({ type: "PUT", url: "/animal/"+data._id, data: JSON.stringify(data), success: function() { showAnimal(data._id) }, contentType : "application/json" }); }else{ $.ajax({ type: "POST", url: "/animal", data: JSON.stringify(data), success: function() { $("#animals").click() }, contentType : "application/json" }); } return false; }; function editAnimal(id){ $("#load").fadeIn(0); $( "#body" ).load( "/templates/animal-form.html", function(){ var animal; $.get( "/animal/"+id, function( data ) { animal = data.data for(key in animal) { if(animal.hasOwnProperty(key)){ if(key == "father" || key == "mother" || key == "specie" || key == "sex"){ let a = key; setTimeout(function(){ $('#a-'+a+' option[value='+animal[a]+']').attr('selected','selected'); }, 200) }else{ $('input[name='+key+']').val( animal[key] ?animal[key].$numberDecimal ? animal[key].$numberDecimal : animal[key] : ""); } } } set_animal_include(animal._id); paint_male_female(animal.specie, animal._id); $("#load").fadeOut("fast"); }); }); } function showAnimal(id){ $("#load").fadeIn(0); $( "#body" ).load( "/templates/animal-show.html", function(){ var text = "
Stats
"; $.get( "/animal/"+ id, function( data ) { var animal = data.data, j = 1; // $.each(animal, function(i, val) { // if(i == "specie") // text += ""; // else if(i != "_id" && i != "__v" && i != "father" && i != "mother" && i != "name" && i.indexOf("initial") < 0 && i != "specie" && i != "level") // text += ""; // j++; // }); // text += "
"+i+"" + species[val].name + "
"+i+""+(val ? val.$numberDecimal ? val.$numberDecimal : val : "")+"
"; $("#button").html('') $(".a-name").html( "" + animal.name + "
" + " Level: " + animal.level + " - " + species[animal.specie].name ); $("#data").append( text ); get_parrents(animal); get_childrens(animal); $("#chart").append(get_animal(animal)); $("#load").fadeOut("fast"); }); }); } function removeAnimal(id){ if(confirm("¿Surely you want to erase this dino?")){ $.ajax({ url: '/animal/'+id, type: 'DELETE', success: function() { $("#animals").click() } }); } } function paint_male_female(specie, id){ $(".option").remove(); $.get( "/animal/"+specie+"/Male", function( data ) { data.data.map(function(item){ if(item._id != id) $("#a-father").append(""); }) }); $.get( "/animal/"+specie+"/Female", function( data ) { data.data.map(function(item){ if(item._id != id) $("#a-mother").append(""); }) }); } function get_parrents(animal){ $.get( "/animal/parrents/" + animal._id, function( data ) { data.data.map(function(item){ $(item.sex == "Male" ? "#a-father" : "#a-mother").html(paint_animal(item)); }); }); } function get_childrens(animal){ $.get( "/animal/childrens/" + animal._id, function( data ) { data.data.map(function(item){ $("#a-childrens").append("
" + paint_animal(item) + "
"); }) }); } function set_animal_include(id){ $.get( "/species", function( data ) { data.data.map(function(item){ $("#a-specie").append(""); }); }); $("#a-specie").change(function(e){ paint_male_female($( this ).val(), id); }); } function get_info(){ $.get("/info", function( data ) { var text = "" $.each(data, function(i, obj) { text += ""; }); text += "
"+i+" "+obj+"
" $( "#info" ).html( text ); $("#load").fadeOut("slow"); }); } function paint_animal(a){ return "" + a.name+" - "+ a.level + "" + get_animal(a) } function safe(s){ return s ? s.$numberDecimal ? s.$numberDecimal : s : "" } function get_prct(a, b){ return(safe(a) * 100) / safe(b) } function get_animal(a){ return '
' + '
' + ' Uuid: ' + safe(a.uuid) + '' + '
' + '
' + '
' + ' Breeder: ' + safe(a.breeder) + '' + '
' + '
' + '
' + ' Location: ' + safe(a.server) + ' / ' + safe(a.base) + '' + '
' + '
' + '
' + ' Sex: ' + safe(a.sex) + '' + '
' + '
' + '
' + ' Level: ' + safe(a.initial_level) + ' / ' + safe(a.level) + '' + '
' + '
' + '
' + ' Health: ' + safe(a.initial_health) + ' / ' + safe(a.health) + '' + '
' + '
' + '
' + ' Stamina: ' + safe(a.initial_energy) + ' / ' + safe(a.energy) + '' + '
' + '
' + '
' + ' Food: ' + safe(a.initial_food) + ' / ' + safe(a.food) + '' + '
' + '
' + '
' + ' Damage: ' + safe(a.initial_damage) + ' / ' + safe(a.damage) + '' + '
' + '
' + '
' + ' Speed: ' + safe(a.initial_velocity) + ' / ' + safe(a.velocity) + '' + '
' + '
' + '
' + ' Inconsistency: ' + safe(a.inconsistency) + '' + '
' + '
' + '
' + ' Imprint: ' + safe(a.imprint) + '' + '
';}