Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //written by: pseudonim_artystyczny
- //podczas testowania kodu ucierpiało wiele błóg
- (function(_pI){
- 'use strict';
- //helpers
- const DEBUG = false;
- const HERO_ID = window.getCookie('mchar_id');
- const randomInt = (min, max) => Math.floor(Math.random()*(max - min) + min) + 1;
- const DebugLog = function(){
- if(DEBUG) console.log(...arguments);
- };
- //settings
- const MAX_SLOTS = 3;
- let SLOTS = [];
- let SLOTS_IDS = [];
- let onFightEnd = null;
- let onDialogEnd = null;
- //functions
- function initCSS(){
- const str = `
- #bless_interface {
- width: 64px;
- background: #000000e6;
- position: absolute !important;
- height: 120px;
- left: 10px;
- top: 10px;
- border: 1px solid #789e20;
- z-index: 399;
- }
- .blessing_slot {
- width: inherit;
- height: 32px;
- margin: 4px;
- margin-bottom: 6px;
- }
- .blessing_item {
- width: 32px;
- height: 32px;
- border: 1px solid #e4d614;
- display: inline-block;
- }
- .blessing_checkbox {
- width: 18px;
- height: 18px;
- transform: translateY(-50%);
- }
- `;
- let style = document.createElement('style');
- style.type = 'text/css';
- style.appendChild(document.createTextNode(str));
- document.querySelector('head').appendChild(style);
- }
- function initHTML(){
- let $main = document.createElement('div');
- document.querySelector('body').appendChild($main);
- $main.id = 'bless_interface';
- $($main).draggable({
- start: function(){
- Tip.disable();
- g.lock.add('blessDrag');
- },
- stop: function(){
- Tip.enable();
- g.lock.remove('blessDrag');
- let position = $(this).position();
- window.margoStorage.set('bless_bot/posistion', position.left + '|' + position.top);
- }
- });
- let pos = window.margoStorage.get('bless_bot/posistion', '').split('|');
- if(pos[0]) $main.style.left = pos[0] + 'px';
- if(pos[1]) $main.style.top = pos[1] + 'px';
- return $main;
- }
- //engine
- function findItemsByName(name, data = g.item){
- return Object.values(data).filter( a => a.loc === 'g' && a.st == 0 && a.name === name );
- }
- function readSettings(data){
- let names = window.margoStorage.get('bless_bot/' + HERO_ID, []).map( a => a.split('/')[0] );
- DebugLog(names);
- let deleted = [];
- for(let i in data){
- if(data[i].loc !== 'g' || data[i].st != '0') continue;
- let name = data[i].name;
- let idx = names.indexOf(name);
- if(idx > -1 && SLOTS[idx].item_name === ''){
- SLOTS[idx].setItem(data[i], false, false);
- }
- if(data[i].del == 1){
- deleted.push(data[i].name);
- }
- }
- let SLOTS_NAMES = SLOTS_IDS.map(a => a.split('/')[0]);
- let toCheck = deleted.filter(name => SLOTS_NAMES.indexOf(name) > -1).filter((name,index,that) => that.indexOf(name) === index);
- toCheck.forEach( name => {
- let x = findItemsByName(name).length === 0;
- let y = SLOTS.find( slot => slot.item_name === name);
- if(y) y.reset();
- })
- }
- function onBlessEnd(oldItem){
- for(let slot of SLOTS){
- if(slot.item_name === '') continue;
- DebugLog(slot.item_name);
- let itemsToUse = findItemsByName(slot.item_name);
- DebugLog(itemsToUse);
- let itemToUse = itemsToUse.filter( item => {
- if(item.id === oldItem.id) return false;
- let timelimit = /timelimit=(\d+),(\d+);?/.exec(item.stat);
- if(timelimit === null) return true;
- let reneval_time = parseInt(timelimit[1]) * 60;
- let last_use = parseInt(timelimit[2]);
- return window.unix_time() > (last_use + reneval_time);
- })[0];
- DebugLog(itemToUse);
- if(itemToUse) return slot.use(itemToUse.id);
- }
- DebugLog('Brak itemów w slotach lub niedostępne itemki');
- }
- //add listener
- window.parseInput = function(a){
- let befBless = false;
- if(a.item){
- readSettings(a.item);
- for(let i in a.item){
- if(a.item[i].del == '1' && g.item[i] && g.item[i].st === 10){
- befBless = Object.assign({}, g.item[i]);
- }
- }
- }
- let ret = _pI.apply(this, arguments);
- //after
- if(befBless !== false) onBlessEnd(befBless);
- if(a.f && a.f.close == 1){
- if(typeof onFightEnd === 'function'){
- onFightEnd();
- onFightEnd = null;
- }
- }
- if(a.d && a.d[0] === '4'){
- if(typeof onDialogEnd === 'function'){
- onDialogEnd();
- onDialogEnd = null;
- }
- }
- return ret;
- };
- class Slot{
- constructor(id, $interface){
- this.id = id;
- this.parent = $interface;
- this.item_name = '';
- this.repeat = false;
- }
- init(){
- //slot
- this.$ = document.createElement('div');
- this.$.className = 'blessing_slot';
- this.$.setAttribute('data-bless_id', this.id);
- this.parent.appendChild(this.$);
- //item layer
- this.$item = document.createElement('div');
- this.$item.className = 'blessing_item';
- this.$item.setAttribute('ctip', 't_item');
- this.$.appendChild(this.$item);
- //checkbox
- this.$checkbox = document.createElement('input');
- this.$checkbox.type = 'checkbox';
- this.$checkbox.className = 'blessing_checkbox';
- this.$checkbox.setAttribute('tip', 'Powtarzaj');
- this.$.appendChild(this.$checkbox);
- //$Jquery fn
- $(this.$).droppable({
- drop: (e, b) => this.drop(e, b),
- over: (e, b) => this.over(e, b),
- out: (e, b) => this.out(e, b)
- });
- this.$item.addEventListener('click', e => this.reset());
- this.$checkbox.addEventListener('change', (event) => {
- this.repeat = event.target.checked;
- SLOTS_IDS[this.id] = this.item_name + '/' + Number(this.repeat);
- this.saveToStorage();
- });
- }
- getItem(b){
- if (b.draggable[0].className.indexOf('item') < 0 || b.draggable[0].className.indexOf('ui-draggable') < 0) return false;
- let id = b.draggable[0].id.replace('item', '');
- return g.item[id];
- }
- validItem(item, log = false){
- let SLOTS_NAMES = SLOTS_IDS.map(a => a.split('/')[0]);
- if(SLOTS_NAMES.indexOf(item.name) > -1){
- if(log) window.message('Item o tej nazwie już znajduje się w slotach');
- return false;
- }
- return item && item.cl === 25 && item.loc === 'g';
- }
- over(e, b){
- if(!this.validItem(this.getItem(b))) return;
- this.$item.style.boxShadow = 'inset 0 0 8px 3px #0089ff';
- }
- out(e, b){
- this.$item.style.boxShadow = '';
- }
- drop(e, b){
- const $item = b.draggable;
- let _stop = $item.draggable("option", "stop");
- $item.draggable("option", "stop", function() {
- g.lock.add("blessWait");
- let ret = _stop.apply(this, arguments);
- g.lock.remove("blessWait");
- $item.draggable("option", "stop", _stop);
- return ret;
- });
- this.out(e, b);
- const item = this.getItem(b);
- if(!this.validItem(item, true)) return;
- this.setItem(item);
- }
- setIcon(item){
- this.$item.style.background = 'url(' + window.CFG.ipath + item.icon + ')';
- }
- setTip(item){
- this.$item.setAttribute('tip', this.parseTip(item));
- }
- parseTip(item){
- item = Object.assign({}, item);
- item.stat = item.stat.replace(/amount=\d+;?/g,'').replace(/ammo=(\d+);?/g,'');
- return window.itemTip(item);
- }
- setItem(item, save = true, log){
- DebugLog('setting item', item, save);
- if(!this.validItem(item, log)) return false;
- this.item_name = item.name;
- this.setIcon(item);
- this.setTip(item);
- SLOTS_IDS[this.id] = item.name + '/' + Number(this.repeat);
- if(save) this.saveToStorage();
- }
- reset(save = true){
- this.$item.style.background = '';
- this.$item.removeAttribute('tip');
- this.item_name = '';
- SLOTS_IDS[this.id] = '/' + Number(this.repeat);
- if(save) this.saveToStorage();
- }
- saveToStorage(){
- window.margoStorage.set('bless_bot/' + HERO_ID, SLOTS_IDS);
- }
- setRepeat(val = !this.repeat, setHtml = false){
- this.repeat = !!val;
- if(setHtml){
- this.$checkbox.checked = this.repeat;
- }
- }
- use(id){
- if(window.g.fight){
- onFightEnd = function(){
- this.use(id);
- }
- return;
- }
- if(window.g.talk.id){
- onDialogEnd = function(){
- this.use(id);
- }
- }
- this.equip(id);
- }
- equip(id){
- setTimeout(() => {
- window._g("moveitem&id="+id+"&st=1", () => {
- if(!this.repeat) this.reset();
- else this.checkIsEmpty();
- });
- }, randomInt(1200, 2000));
- }
- checkIsEmpty(){
- setTimeout(()=>{
- let itemsWithSameName = findItemsByName(this.item_name);
- DebugLog('checking items', itemsWithSameName);
- if(itemsWithSameName.length === 0) this.reset();
- }, 500);
- }
- }
- //initiate
- initCSS();
- const $interface = initHTML();
- $interface.height = MAX_SLOTS.length * 40 + 'px';
- let repeatSlots = window.margoStorage.get('bless_bot/' + HERO_ID, []).map( a => Number(a.split('/')[1]) );
- for(let i=0; i<MAX_SLOTS; i++){
- SLOTS_IDS[i] = '/' + (repeatSlots[i] || 0);
- SLOTS[i] = new Slot(i, $interface);
- SLOTS[i].init();
- if(repeatSlots[i] === 1) SLOTS[i].setRepeat(true, true);
- }
- window.SLOTS = SLOTS;
- })(window.parseInput);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement