Nindel / Ice Apple Energy Helper

// ==UserScript==
// @name       Ice Apple Energy Helper
// @version    0.7
// @description  Energy helper para el juego online IceApple. Energy Helper for the online game Ice Apple.
// @match      http://*.ice-apple.com/game/*
// @exclude    http://ice-apple.com/game/index.php/*/profile/*
// @exclude    http://www.ice-apple.com/game/index.php/*/profile/*
// @require http://code.jquery.com/jquery-latest.js
// @namespace   5bb7a5c3bb81d226cb62c9d582de14e3
// @updateURL   https://openuserjs.org/meta/Nindel/Ice_Apple_Energy_Helper.meta.js
// @downloadURL https://openuserjs.org/install/Nindel/Ice_Apple_Energy_Helper.user.js
// ==/UserScript==
 
String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}
 
var scriptVersion = "0.7";
 
var buttons='';
$(document).ready(function() {
    var uId = getCookie('user_id');
    createInterface();
    foodTimer();
   
    if(getCookie("energyHelperDisplay") == "hide"){
        $('#foodBar').hide();
        $('#hideEnergyHelper').text("Show");
    }
   
    $( '#energyHelperUserEnergy' ).text($('#energy-now').text());
   
    $( '#foodBarContainer' ).css({'border-radius': '5px'});
    $( '#energyHelperTable' ).css({'table-layout': 'fixed', 'width': '100%', 'text-align': 'center'});
    $( '#energyHelperTable td' ).css({'width': '20%', 'background-color': '#fff', 'color': '#1C1C1C','vertical-align': 'top'});
    $( '#foodBarMsgComplete' ).css({'width': '100%', 'text-align': 'center', 'color': '#0080FF'});
    $( '#hiddenFoodDiv' ).load("/game/index.php/en/consume-food #panel", function(){
        var foodPath = '#hiddenFoodDiv #panel .items_holder .item_mask table tbody tr';
        var numberOfEnergyTypes = $( '#hiddenFoodDiv #panel .items_holder .item_mask table tbody tr' ).length;
        var energyButtons = '';
        for(var i = 0; i < numberOfEnergyTypes; i++){
            var energyType = $( '#hiddenFoodDiv #panel .items_holder .item_mask table tbody tr:eq( ' + i + ' ) td:eq( 1 )' ).text();
            var energyQuality = findQuality($( '#hiddenFoodDiv #panel .items_holder .item_mask table tbody tr:eq( ' + i + ' ) td:eq( 4 )' ).text(),energyType);
            var amount = $( '#hiddenFoodDiv #panel .items_holder .item_mask table tbody tr:eq( ' + i + ' ) td:eq( 2 )' ).text();
            if(energyQuality === false){
                alert("Something is wrong");
            }else{
                $( '#q' + energyQuality + energyType + 'Amount' ).text(amount);
            }
        }
    });
   
    $('#energyHelperFoodRow').hide();$('#energyHelperGiftRow').hide();$('#energyHelperHouseRow').hide();$('#energyHelperHospitalRow').hide();$('#energyHelperAppleRow').hide();
    if(getCookie("energyHelperEnergyOption") != ""){
        var d = getCookie("energyHelperEnergyOption");
        $('#energyHelperSelect').val( d );
        $( '#'+d ).show();
    }
   
    $("#energyHelperSelect").change(function(){
        var f = $(this).val();
        $('#energyHelperFoodRow').hide();$('#energyHelperGiftRow').hide();$('#energyHelperHouseRow').hide();$('#energyHelperHospitalRow').hide();$('#energyHelperAppleRow').hide();
        $( '#'+f ).show();
        setCookie("energyHelperEnergyOption",f,300);
    });
   
    $('#hideEnergyHelper').click(function(){
        if($(this).text() == "Show"){
            $('#foodBar').show();
            $(this).text("Hide");
            setCookie("energyHelperDisplay","show",300);
        }else{
            $('#foodBar').hide();
            $(this).text("Show");
            setCookie("energyHelperDisplay","hide",300);
        }
    });
   
    var doingSomething = false;
    $('.foodHelperEat').click(function(){
        if(doingSomething === false){
            var el = $(this);
            if($(this).data('t') == "food"){
                var ajaxcall;
                if($(this).data('q') == 1 || $(this).data('q') == 2){
                    ajaxcall = $.ajax({
                        type: 'POST',
                        url: 'http://ice-apple.com/game/index.php/en/consume-food',
                        data: {user: uId,
                               'collect-food': $(this).data('q')
                              }
                    });
                }else{
                    ajaxcall = $.ajax({
                        type: 'POST',
                        url: 'http://ice-apple.com/game/index.php/en/consume-food',
                        data: {user: uId,
                               'food': $(this).data('q')
                              }
                    });
                }
            }else if($(this).data('t') == "gift"){
                ajaxcall = $.ajax({
                    type: 'POST',
                    url: 'http://ice-apple.com/game/index.php/en/consume-food',
                    data: {user: uId,
                           'gift': $(this).data('q')
                          }
                });
            }else if($(this).data('t') == "apple"){
                ajaxcall = $.ajax({
                    type: 'POST',
                    url: 'http://ice-apple.com/game/index.php/en/consume-food',
                    data: {user: uId,
                           'apple': $(this).data('q')
                          }
                });
            }else if($(this).data('t') == "hospital"){
                ajaxcall = $.ajax({
                    type: 'POST',
                    url: 'http://ice-apple.com/game/index.php/en/consume-food',
                    data: {user: uId,
                           'hospital': $(this).data('q')
                          }
                });
            }else if($(this).data('t') == "house"){
                ajaxcall = $.ajax({
                    type: 'POST',
                    url: 'http://ice-apple.com/game/index.php/en/consume-food',
                    data: {user: uId,
                           'house': $(this).data('q')
                          }
                });
            }
                doingSomething = true;
            ajaxcall.done(function( data ) {
                ajaxDone( data , el );
                doingSomething = false;
            });
        }
        // NEED TO ADD FIELD NAMES FOR APPLES, HOSPITALS AND HOTELS.
    });
});
 
function createInterface(){
    $( '#zt-mainframe' ).prepend( '<div id="hiddenFoodDiv" style="display: none;"></div>' );
    buttons += '<table id="energyHelperTable">';
    buttons += '<tr id="energyHelperFoodRow"> <td> Q1 Food: <span id="q1FoodAmount" class="energyHelper_foodAmount"></span><span id="energyHelper_Q1FoodCountdown" style="display:block;"></span><input type="button" data-t="food" data-q="1" value="Eat" id="foodHelper_0" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q2 Food: <span id="q2FoodAmount" class="energyHelper_foodAmount"></span><span id="energyHelper_Q2FoodCountdown" style="display:block;"></span> <input type="button" data-t="food" data-q="2" value="Eat" id="foodHelper_1" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q3 Food: <span id="q3FoodAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="food" data-q="3" value="Eat" id="foodHelper_2" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q4 Food: <span id="q4FoodAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="food" data-q="4" value="Eat" id="foodHelper_3" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q5 Food: <span id="q5FoodAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="food" data-q="5" value="Eat" id="foodHelper_4" class="foodHelperEat" />';
    buttons += '</td> </tr> <tr id="energyHelperGiftRow"> <td>';
    buttons += 'Q1 Gifts: <span id="q1GiftAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="gift" data-q="1" value="Eat" id="foodHelper_5" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q2 Gifts: <span id="q2GiftAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="gift" data-q="2" value="Eat" id="foodHelper_6" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q3 Gifts: <span id="q3GiftAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="gift" data-q="3" value="Eat" id="foodHelper_7" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q4 Gifts: <span id="q4GiftAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="gift" data-q="4" value="Eat" id="foodHelper_8" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q5 Gifts: <span id="q5GiftAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="gift" data-q="5" value="Eat" id="foodHelper_9" class="foodHelperEat" />';
    buttons += '</td> </tr> <tr id="energyHelperHouseRow"> <td>';
    buttons += 'Q1 House: <span id="q1HouseAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="house" data-q="1" value="Eat" id="foodHelper_10" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q2 House: <span id="q2HouseAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="house" data-q="2" value="Eat" id="foodHelper_11" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q3 House: <span id="q3HouseAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="house" data-q="3" value="Eat" id="foodHelper_12" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q4 House: <span id="q4HouseAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="house" data-q="4" value="Eat" id="foodHelper_13" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q5 House: <span id="q5HouseAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="house" data-q="5" value="Eat" id="foodHelper_14" class="foodHelperEat" />';
    buttons += '</td></tr><tr id="energyHelperHospitalRow"><td>';
    buttons += 'Q1 Hospital: <span id="q1HospitalAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="hospital" data-q="1" value="Eat" id="foodHelper_15" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q2 Hospital: <span id="q2HospitalAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="hospital" data-q="2" value="Eat" id="foodHelper_16" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q3 Hospital: <span id="q3HospitalAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="hospital" data-q="3" value="Eat" id="foodHelper_17" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q4 Hospital: <span id="q4HospitalAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="hospital" data-q="4" value="Eat" id="foodHelper_18" class="foodHelperEat" /> ';
    buttons += '</td><td>';
    buttons += 'Q5 Hospital: <span id="q5HospitalAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="hospital" data-q="5" value="Eat" id="foodHelper_19" class="foodHelperEat" />';
    buttons += '</td></tr><tr id="energyHelperAppleRow"><td colspan="5">';
    buttons += 'Q5 Apple: <span id="q5AppleAmount" class="energyHelper_foodAmount"></span><input type="button" data-t="apple" data-q="5" value="Eat" id="foodHelper_20" class="foodHelperEat" />';
    buttons += '</td></tr></table>';
   
    var energySelectDiv = '<div id="energyHelperSelectCon" style="width: 90%: margin: auto; text-align: center;"><select id="energyHelperSelect"><option selected="selected"></option><option value="energyHelperFoodRow">Food</option><option value="energyHelperGiftRow">Gifts</option><option value="energyHelperHouseRow">Houses</house><option value="energyHelperHospitalRow">Hospitals</option><option value="energyHelperAppleRow">Apples</option></select></div>';
    $( '#zt-mainframe' ).prepend( '<div id="foodBarContainer" style="width: 960px; margin: auto; background-color: #2E2E2E; color: #fff; padding: 5px;"><div style="overflow: auto;"><div style="float: left;">iApple Energy Helper v' + scriptVersion + ' - Created by <a target="_blank" href="http://ice-apple.com/game/index.php/en/profile/3083">SuperLlama</a></div><div style="float: right;">Energy: <span id="energyHelperUserEnergy">0</span>/100</div></div><div id="foodBar">' + energySelectDiv + buttons + '</div><div id="hideEnergyHelperContainer" style="width: 100%; text-align: center; padding-top: 3px; border-top: 2px solid #fff;"><div id="foodBarMsgComplete"></div><button id="hideEnergyHelper">Hide</button></div></div>' );
    $( '.energyHelper_foodAmount' ).text('0');
    $( '.energyHelper_foodAmount' ).css({'display': 'block','color': '#0000FF', 'font-size': '1.2em'});
}
 
function foodTimer(){
    setInterval(function(){
        if(getCookie("EnergyHelper_Q1FoodTimer") != false){
            var target_date = new Date(getCookie("EnergyHelper_Q1FoodTimer")).getTime();
            var current_date = new Date().getTime();
            var seconds_left = (target_date - current_date) / 1000;
            var m = parseInt(seconds_left / 60);
            var s = parseInt(seconds_left % 60);
            if(s < 10){
                s = "0" + s;
            }
            $( '#foodHelper_0' ).val(m + ':' + s);
            $( '#foodHelper_0' ).attr("disabled", true);
        }else{
            $( '#foodHelper_0' ).val('Use');
            $( '#foodHelper_0' ).attr("disabled", false);
        }
        if(getCookie("EnergyHelper_Q2FoodTimer") != false){
            var target_date = new Date(getCookie("EnergyHelper_Q2FoodTimer")).getTime();
            var current_date = new Date().getTime();
            var seconds_left = (target_date - current_date) / 1000;
            var m = parseInt(seconds_left / 60);
            var s = parseInt(seconds_left % 60);
            if(s < 10){
                s = "0" + s;
            }
            $( '#foodHelper_1' ).val(m + ':' + s);
            $( '#foodHelper_1' ).attr("disabled", true);
        }else{
            $( '#foodHelper_1' ).val('Use');
            $( '#foodHelper_1' ).attr("disabled", false);
        }
    },1000);
}
 
function ajaxDone(data, el){
    var msg = $(data).find('.alert-box.error');
    if(msg.text() != ''){
        alert(msg.text());
    }else{
        if(el.data('t') == "food" && el.data('q') == "1"){
            var date = new Date();
            date.setTime(date.getTime()+(5*1000*60));
            setCookieMinutes('EnergyHelper_Q1FoodTimer', date, 5);
        }
        if(el.data('t') == "food" && el.data('q') == "2"){
            var date = new Date();
            date.setTime(date.getTime()+(15*1000*60));
            setCookieMinutes('EnergyHelper_Q2FoodTimer', date, 15);
        }
        var t = el.data('t').capitalize();
        var amount = parseInt($( '#q' + el.data('q') + t + 'Amount' ).text());
        amount -= 1;
        $( '#q' + el.data('q') + t + 'Amount' ).text(amount)
        $( '#foodBarMsgComplete' ).html( '<span>' + t + ' used.');
        $( '#foodBarMsgComplete span' ).fadeOut(2000);
        var energy = parseInt($('#energy-now').text());
        if(el.data('t') == "food" || el.data('t') == "gift"){
            energy += parseInt(el.data('q')) * 2;
        }else if(el.data('t') == "house"){
            energy += parseInt(el.data('q')) * 20;
        }else if(el.data('t') == "hospital"){
            energy += parseInt(el.data('q')) * 10;
        }else if(el.data('t') == "apple"){
            energy += 100;
        }
            if(energy > 100){
                energy = 100;
            }
        $('.progress-w-in').css('width', energy + '%');
        $('#energy-now').text(energy);
        $('.progress-w-val').text(energy + '%');
        $( '#energyHelperUserEnergy' ).text(energy);
    }
}
 
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires + '; path=/';
}
 
function setCookieMinutes(cname, cvalue, exminutes) {
    var date = new Date();
    date.setTime(date.getTime()+(exminutes*1000*60));
    var expires = "; expires="+date.toGMTString();
    document.cookie = cname+"="+cvalue+expires+"; path=/";
}
 
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length,c.length);
    }
    return false;
}
 
function findQuality(energyRestore, type){
    if(energyRestore == "Restores 2 Energy"){
        return 1;
    }
    else if(energyRestore == "Restores 4 Energy"){
        return 2;
    }
        else if(energyRestore == "Restores 6 Energy"){
            return 3;
        }
        else if(energyRestore == "Restores 8 Energy"){
            return 4;
        }
            else if(energyRestore == "Restores 10 Energy" && type != "hospital"){
                return 5;
            }
            else if(energyRestore == "Restores 10 Energy" && type == "hospital"){
                return 1;
            }
                else if(energyRestore == "Restores 20 Energy" && type == "hospital"){
                    return 2;
                }
                else if(energyRestore == "Restores 20 Energy" && type == "house"){
                    return 1;
                }
                    else if(energyRestore == "Restores 30 Energy" && type == "hospital"){
                        return 3;
                    }
                    else if(energyRestore == "Restores 40 Energy" && type == "hospital"){
                        return 4;
                    }
                        else if(energyRestore == "Restores 40 Energy" && type == "house"){
                            return 2;
                        }
                        else if(energyRestore == "Restores 50 Energy" && type == "hospital"){
                            return 5;
                        }
                            else if(energyRestore == "Restores 60 Energy"){
                                return 3;
                            }
                            else if(energyRestore == "Restores 80 Energy"){
                                return 4;
                            }
                                else if(energyRestore == "Restores 100 Energy"){
                                    return 5;
                                }
                                else{
                                    return false;
                                }
}