battles/js/kicke.js
2018-01-28 18:40:49 +02:00

57 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function Dialog(t){
function e(t, e) {
var i = l.outerWidth(!0), n = l.outerHeight(!0), o = $(window).width(), c = $(window).height();
switch(t) {
case"center":
if("t"==e) return c/2-n/2;
if("l"==e)return o/2-i/2;
break;
case"top":
case"left":return 0;
case"bottom":return c-n;
case"right":return o-i
}
}
function i() {
var t = s.position.split(","), i = $.trim(t[0]), n = $.trim(t[1]);
l.css({position:"absolute", top:parseInt(i)?i:e(i, "t"), left:parseInt(n)?n:e(n, "l")})
}
function n() {
l.draggable({containment:"window", handle:".dialog-header"})
}
function o() {
$.each(s.buttons, function(t, e) {
e = $.isFunction(e)?{click:e, value:t}:e, e = $.extend({type:"button"}, e);
var i = e.click;
e.click = function() {
i.apply(this, r, l, arguments)
}, $("<input />", e).appendTo(p)
})
}
function c(t) {
var e, i = ["", 0, null, void 0];
for(e in i)
if(t == i[e])return!0; return!1
}
if(!jQuery)return alert("Для работы диалогов требуется jQuery библиотека."), !1;
var r = this, a = {position:"center,center", style:{}, title:"", content:"", template:['<div class="dialog">','<div class="dialog-header">','<span class="dialog-title"></span>','<a href="#" class="dialog-close-button">[x]</a>',"</div>",'<div class="dialog-content"></div>','<div class="dialog-buttons"></div>',"</div>"], onopen:$.noop, onclose:$.noop, buttons:!1, autoopen:!0, draggable:!0}, s = $.extend(a, t), l = $(s.template.join("")), d = l.find(".dialog-header"), u = d.find(".dialog-title"), k = l.find(".dialog-content"), p = l.find(".dialog-buttons"), f = d.find(".dialog-close-button"); return 0 == u.length || 0 == d.length || 0 == k.length || 0 == p.length || 0 == f.length ? void alert("Error css classes: dialog-title, dialog-header, dialog-content, dialog-buttons, dialog-close-button not found!"):(this.open =!1,
this.set = function(t, e) {
switch(t) {
case"content":
s.content = e, k.html(e);
break;
case"title":
s.title=e, u.text(e);
break;
case"position":
s.position = e, this.rePosition();
break;
case"onopen":
$.isFunction(e) && (s.onopen = e);
break;
case"onclose":$.isFunction(e)&&(s.onclose=e);break;case"draggable":"enable"==e?n():"disable"==e&&l.draggable("destroy");break;case"buttons":$.isPlainObject(e)&&!$.isEmptyObject(e)&&(p.show(),s.buttons=e,o())}},this.get=function(t){return s[t]||null},this.rePosition=function(){i()},this.close=function(){r.open=!1,s.onclose.call(r,l),l.hide()},this.show=function(){r.open=!0,s.onopen.call(r,l),l.show()},$("body").append(l),l.hide(),l.css(s.style),c(s.title)||u.html(s.title),f.click(function(){return r.close(),!1}),c(s.content)||k.html(s.content),($.isPlainObject(s.buttons)&&!$.isEmptyObject(s.buttons)||$.isArray(s.buttons)&&s.buttons.length>0)&&o(),$.isEmptyObject(s.buttons)&&p.hide(),i(),s.draggable&&void 0!==$.fn.draggable&&n(),void(s.autoopen&&this.show()))}jQuery&&($.extend({isEmpty:function(t){var e,i=["",0,null,void 0,!1,"null","undefined","false"];for(e in i)if(t==i[e])return!0;return!1},random:function(t,e){return void 0==t||void 0==e?Math.random():Math.ceil(Math.random()*(e-t))+t},isTrue:function(t){return t>$.random(0,1e4)/100},parseTime:function(t){var e=t.split("-");if(e.length>1){var i=e[0]<1?1:e[0],n=e[1]<1?1:e[1];return n=i>n?i+1:n,1e3*$.random(parseInt(i),parseInt(n))}return 1e3*parseInt(e[0]<1?1:e[0])},getButton:function(t,e,i){return e=e||document,i?i&&$(t,e).length>1?$(t,e).eq(i):!1:$(t,e)},getLink:function(t,e,i){return e=e||document,i?i&&$('a:contains("'+t+'")').length>1?$('a:contains("'+t+'")').eq(i):!1:$('a:contains("'+t+'")')}}),$.fn.findInput=function(t){var e=null;return this.each(function(){var i=$(this),n=i.val();-1!==n.search(t)&&(e=i)}),e});var Kicker=function(){function t(){return-1!=top.frames.main.document.location.href.search("battle")}function e(){var t=$("#"+x+"kick-r").is(":checked"),e=$("#"+x+"block-r").is(":checked"),n=0,o={},c=0,r={},a="",s=i($("#"+x+"waittime").val());t?o[0]="r":($("."+x+"kick:checked").each(function(){var t=$(this),e=t.attr("id").split("-").pop(),i=parseInt($("#"+x+"kick-perc-"+e).val());o[e]=i,n+=i}),n>=0&&100>n?a+='<div class="red">'+m.err_low_perc_kick+"</div>":n>100&&(a+='<div class="red">'+m.err_big_perc_kick+"</div>")),e?r[0]="r":($("."+x+"block:checked").each(function(){var t=$(this),e=t.attr("id").split("-").pop(),i=parseInt($("#"+x+"block-perc-"+e).val());r[e]=i,c+=i}),c>=0&&100>c?a+='<div class="red">'+m.err_low_perc_block+"</div>":c>100&&(a+='<div class="red">'+m.err_big_perc_block+"</div>")),$.isEmpty(s)&&(a+='<div class="red">'+m.err_no_waittime+"</div>"),$.isEmpty(a)?(O.waittime=s,O.kick=o,O.block=r,O.ontimeout=$("#"+x+"ontimeout").val(),f[x+"setting"]=JSON.stringify(O),$("#"+x+"errors").show().html('<div class="green">Настройки сохранены</div>').fadeOut(5e3)):$("#"+x+"errors").show().html(a).fadeOut(3e3)}function i(t){var e=t.split("-");if(e.length>1){var i=Math.ceil(e[0])||1,n=Math.ceil(e[1])||i+1;return n=i>=n?i+1:n,i+"-"+n}var i=Math.ceil(e[0])||1;return""+i}function n(){if($.isPlainObject(O.kick)&&!$.isEmpty(O.kick[0])&&($("#"+x+"kick-r").attr("checked",!0),$("."+x+"kick").attr("disabled",!0)),$.isPlainObject(O.block)&&!$.isEmpty(O.block[0])&&($("#"+x+"block-r").attr("checked",!0),$("."+x+"block").attr("disabled",!0)),$.isPlainObject(O.kick)&&!$.isEmptyObject(O.kick)&&$.isEmpty(O.kick[0])){$("#"+x+"kick-r").attr("checked",!1),$("."+x+"kick").attr("disabled",!1);for(var t in O.kick){var e=O.kick[t];$("#"+x+"kick-"+t).attr("checked",!0),$("#"+x+"kick-perc-"+t).val(e)}}if($.isPlainObject(O.block)&&!$.isEmptyObject(O.block)&&$.isEmpty(O.block[0])){$("#"+x+"block-r").attr("checked",!1),$("."+x+"block").attr("disabled",!1);for(var t in O.block){var e=O.block[t];$("#"+x+"block-"+t).attr("checked",!0),$("#"+x+"block-perc-"+t).val(e)}}$("#"+x+"ontimeout").val(O.ontimeout)}function o(){for(var t,e="off"==h.power?"red":"green",i='<div><input type="checkbox" class="'+x+'kick-r" id="'+x+'kick-r"> <label for="'+x+'kick-r">Случайно</label></div>',n='<div><input type="checkbox" class="'+x+'block-r" id="'+x+'block-r"> <label for="'+x+'block-r">Случайно</label></div>',o="",c=0;c<v.length;c++)i+='<div><input type="checkbox" class="'+x+'kick" id="'+x+"kick-"+(c+1)+'"><input type="text" size="3" maxlenght="3" class="'+x+'kick-perc" id="'+x+"kick-perc-"+(c+1)+'" value="0"> <label for="'+x+"kick-"+(c+1)+'">'+v[c]+"</label></div>",n+='<div><input type="checkbox" class="'+x+'block" id="'+x+"block-"+(c+1)+'"><input type="text" size="3" maxlenght="3" class="'+x+'block-perc" id="'+x+"block-perc-"+(c+1)+'" value="0"> <label for="'+x+"block-"+(c+1)+'">'+g[c]+"</label></div>";for(t in w)o+='<option value="'+w[t]+'">'+t+"</option>";var r=['<div>Состояние: <span id="'+x+'status" class="hand bold '+e+'">'+m["stat_"+h.power]+"</span></div>",'<hr width="80%">','<div id="'+x+'errors"></div>','<div>Время РЅР° удар: <input type="text" id="'+x+'waittime" value="'+O.waittime+'" size="5"> сек.</div>','<div>РџРѕ тайму: <select id="'+x+'ontimeout">'+o+"</select></div>",'<table width="100%">',"<tr>","<td>Удар</td>","<td>Блок</td>","</tr>","<tr>","<td>"+i+"</td>","<td>"+n+"</td>","</tr>","</table>"];return r.join("")}function c(){if("off"==h.power)return void clearTimeout(_);var e=$(".m",top.frames.main.document).length;s=$(I,top.frames.main.document).findInput(y.KICK),l=$(I,top.frames.main.document).findInput(y.WIN),d=$(I,top.frames.main.document).findInput(y.DRAW),u=$(I,top.frames.main.document).findInput(y.EXIT),k=$(I,top.frames.main.document).findInput(y.REFRESH);var i=$.parseTime(O.waittime),n=Date.now(),o=n-E;if(t()&&0==e){if($.isEmpty(s))$.isEmpty(l)||"win"!=O.ontimeout?$.isEmpty(d)||"draw"!=O.ontimeout?!$.isEmpty(k)&&o>=i&&(k.click(),E=n):d.click():l.click();else if(o>=i){var p=r(),f=a();$("#A"+p,top.frames.main.document).attr("checked",!0),$("#D"+f,top.frames.main.document).attr("checked",!0),s.click(),E=n}$.isEmpty(u)||u.click()}_=setTimeout(function(){c()},1e3)}function r(){if($.isEmpty(O.kick[0])){var t=!1,e=null;for(var i in O.kick)$.isTrue(O.kick[i])&&(t=!0,e=i);return t?e:r()}return $.random(0,4)}function a(){if(!$.isEmpty(O.block[0]))return $.random(0,4);var t=!1,e=null;for(var i in O.block)$.isTrue(O.block[i])&&(t=!0,e=i);return t?e:a()}var s,l,d,u,k,p=this,f=localStorage,b=(top.frames.main,{myLogin:"",myClass:null,kickScheme:{},kick:{0:"r"},block:{0:"r"},waittime:"1-5",ontimeout:"win"}),h={version:"0.3",power:"off"},m={err_low_perc_kick:"РЎСѓРјРјР° % ударов меньше 100%.",err_big_perc_kick:"РЎСѓРјРјР° % ударов больше 100%.",err_low_perc_block:"РЎСѓРјРјР° % блоков меньше 100%.",err_big_perc_block:"РЎСѓРјРјР° % блоков больше 100%.",err_kick_len:"РС РЅРµ выбрали РєСѓРґР° бить.",err_block_len:"РС РЅРµ выбрали места блоков.",err_no_waittime:"РС РЅРµ указали время РЅР° удар.",set_saved:"Настройки сохранены.",stat_on:"Включен",stat_off:"Выключен",status:"Состояние"},v=["РІ голову","РІ РєРѕСЂРїСѓСЃ","РІ РїРѕСЏСЃ(пах)","РїРѕ ногам"],g=["головы Рё РєРѕСЂРїСѓСЃР°","РєРѕСЂРїСѓСЃР° Рё РїРѕСЏСЃР°","РїРѕСЏСЃР° Рё РЅРѕРі","головы Рё РЅРѕРі"],w={"Победа":"win","Ничья":"draw","Ждать":"wait"},y={KICK:"Вперед",WIN:"РЇ победил",DRAW:"Ничья",EXIT:"Вернуться",REFRESH:"Обновить"},_=-1,E=0,I='input[type="button"], input[type="submit"]',x="kicker-",j=$.isEmpty(f[x+"setting"])?"":JSON.parse(f[x+"setting"]),O=$.extend({},b,j),T=new Dialog({position:"top,left",title:"Автоудар",content:"",onopen:function(){this.set("content",o()),n(),$("#"+x+"status").click(function(){switch(h.power){case"off":h.power="on",$("#"+x+"status").removeClass("red").addClass("green").text("Включен."),c();break;case"on":h.power="off",$("#"+x+"status").removeClass("green").addClass("red").text("Выключен."),clearTimeout(_)}}),$("#"+x+"kick-r").click(function(){$("."+x+"kick").attr("disabled",$(this).is(":checked"))}),$("#"+x+"block-r").click(function(){$("."+x+"block").attr("disabled",$(this).is(":checked"))})},autoopen:!1,buttons:{"Сохранить":function(){e()}}});return this.ShowDialog=function(){T.open||T.show()},p};