Advertisement
iradap

Autoblogo

Nov 10th, 2018
10,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.92 KB | None | 0 0
  1. //written by: pseudonim_artystyczny
  2. //podczas testowania kodu ucierpiało wiele błóg
  3. (function(_pI){
  4. 'use strict';
  5. //helpers
  6. const DEBUG = false;
  7. const HERO_ID = window.getCookie('mchar_id');
  8. const randomInt = (min, max) => Math.floor(Math.random()*(max - min) + min) + 1;
  9. const DebugLog = function(){
  10. if(DEBUG) console.log(...arguments);
  11. };
  12. //settings
  13. const MAX_SLOTS = 3;
  14. let SLOTS = [];
  15. let SLOTS_IDS = [];
  16. let onFightEnd = null;
  17. let onDialogEnd = null;
  18. //functions
  19. function initCSS(){
  20. const str = `
  21. #bless_interface {
  22. width: 64px;
  23. background: #000000e6;
  24. position: absolute !important;
  25. height: 120px;
  26. left: 10px;
  27. top: 10px;
  28. border: 1px solid #789e20;
  29. z-index: 399;
  30. }
  31. .blessing_slot {
  32. width: inherit;
  33. height: 32px;
  34. margin: 4px;
  35. margin-bottom: 6px;
  36. }
  37. .blessing_item {
  38. width: 32px;
  39. height: 32px;
  40. border: 1px solid #e4d614;
  41. display: inline-block;
  42. }
  43. .blessing_checkbox {
  44. width: 18px;
  45. height: 18px;
  46. transform: translateY(-50%);
  47. }
  48. `;
  49. let style = document.createElement('style');
  50. style.type = 'text/css';
  51. style.appendChild(document.createTextNode(str));
  52. document.querySelector('head').appendChild(style);
  53. }
  54. function initHTML(){
  55. let $main = document.createElement('div');
  56. document.querySelector('body').appendChild($main);
  57. $main.id = 'bless_interface';
  58. $($main).draggable({
  59. start: function(){
  60. Tip.disable();
  61. g.lock.add('blessDrag');
  62. },
  63. stop: function(){
  64. Tip.enable();
  65. g.lock.remove('blessDrag');
  66. let position = $(this).position();
  67. window.margoStorage.set('bless_bot/posistion', position.left + '|' + position.top);
  68. }
  69. });
  70. let pos = window.margoStorage.get('bless_bot/posistion', '').split('|');
  71. if(pos[0]) $main.style.left = pos[0] + 'px';
  72. if(pos[1]) $main.style.top = pos[1] + 'px';
  73. return $main;
  74. }
  75. //engine
  76. function findItemsByName(name, data = g.item){
  77. return Object.values(data).filter( a => a.loc === 'g' && a.st == 0 && a.name === name );
  78. }
  79. function readSettings(data){
  80. let names = window.margoStorage.get('bless_bot/' + HERO_ID, []).map( a => a.split('/')[0] );
  81. DebugLog(names);
  82. let deleted = [];
  83. for(let i in data){
  84. if(data[i].loc !== 'g' || data[i].st != '0') continue;
  85. let name = data[i].name;
  86. let idx = names.indexOf(name);
  87. if(idx > -1 && SLOTS[idx].item_name === ''){
  88. SLOTS[idx].setItem(data[i], false, false);
  89. }
  90. if(data[i].del == 1){
  91. deleted.push(data[i].name);
  92. }
  93. }
  94. let SLOTS_NAMES = SLOTS_IDS.map(a => a.split('/')[0]);
  95. let toCheck = deleted.filter(name => SLOTS_NAMES.indexOf(name) > -1).filter((name,index,that) => that.indexOf(name) === index);
  96. toCheck.forEach( name => {
  97. let x = findItemsByName(name).length === 0;
  98. let y = SLOTS.find( slot => slot.item_name === name);
  99. if(y) y.reset();
  100. })
  101. }
  102. function onBlessEnd(oldItem){
  103. for(let slot of SLOTS){
  104. if(slot.item_name === '') continue;
  105. DebugLog(slot.item_name);
  106. let itemsToUse = findItemsByName(slot.item_name);
  107. DebugLog(itemsToUse);
  108. let itemToUse = itemsToUse.filter( item => {
  109. if(item.id === oldItem.id) return false;
  110. let timelimit = /timelimit=(\d+),(\d+);?/.exec(item.stat);
  111. if(timelimit === null) return true;
  112. let reneval_time = parseInt(timelimit[1]) * 60;
  113. let last_use = parseInt(timelimit[2]);
  114. return window.unix_time() > (last_use + reneval_time);
  115. })[0];
  116. DebugLog(itemToUse);
  117. if(itemToUse) return slot.use(itemToUse.id);
  118. }
  119. DebugLog('Brak itemów w slotach lub niedostępne itemki');
  120. }
  121.  
  122. //add listener
  123. window.parseInput = function(a){
  124. let befBless = false;
  125. if(a.item){
  126. readSettings(a.item);
  127. for(let i in a.item){
  128. if(a.item[i].del == '1' && g.item[i] && g.item[i].st === 10){
  129. befBless = Object.assign({}, g.item[i]);
  130. }
  131. }
  132. }
  133. let ret = _pI.apply(this, arguments);
  134. //after
  135. if(befBless !== false) onBlessEnd(befBless);
  136. if(a.f && a.f.close == 1){
  137. if(typeof onFightEnd === 'function'){
  138. onFightEnd();
  139. onFightEnd = null;
  140. }
  141. }
  142. if(a.d && a.d[0] === '4'){
  143. if(typeof onDialogEnd === 'function'){
  144. onDialogEnd();
  145. onDialogEnd = null;
  146. }
  147. }
  148. return ret;
  149. };
  150.  
  151. class Slot{
  152. constructor(id, $interface){
  153. this.id = id;
  154. this.parent = $interface;
  155. this.item_name = '';
  156. this.repeat = false;
  157. }
  158. init(){
  159. //slot
  160. this.$ = document.createElement('div');
  161. this.$.className = 'blessing_slot';
  162. this.$.setAttribute('data-bless_id', this.id);
  163. this.parent.appendChild(this.$);
  164. //item layer
  165. this.$item = document.createElement('div');
  166. this.$item.className = 'blessing_item';
  167. this.$item.setAttribute('ctip', 't_item');
  168. this.$.appendChild(this.$item);
  169. //checkbox
  170. this.$checkbox = document.createElement('input');
  171. this.$checkbox.type = 'checkbox';
  172. this.$checkbox.className = 'blessing_checkbox';
  173. this.$checkbox.setAttribute('tip', 'Powtarzaj');
  174. this.$.appendChild(this.$checkbox);
  175.  
  176. //$Jquery fn
  177. $(this.$).droppable({
  178. drop: (e, b) => this.drop(e, b),
  179. over: (e, b) => this.over(e, b),
  180. out: (e, b) => this.out(e, b)
  181. });
  182. this.$item.addEventListener('click', e => this.reset());
  183. this.$checkbox.addEventListener('change', (event) => {
  184. this.repeat = event.target.checked;
  185. SLOTS_IDS[this.id] = this.item_name + '/' + Number(this.repeat);
  186. this.saveToStorage();
  187. });
  188. }
  189. getItem(b){
  190. if (b.draggable[0].className.indexOf('item') < 0 || b.draggable[0].className.indexOf('ui-draggable') < 0) return false;
  191. let id = b.draggable[0].id.replace('item', '');
  192. return g.item[id];
  193. }
  194. validItem(item, log = false){
  195. let SLOTS_NAMES = SLOTS_IDS.map(a => a.split('/')[0]);
  196. if(SLOTS_NAMES.indexOf(item.name) > -1){
  197. if(log) window.message('Item o tej nazwie już znajduje się w slotach');
  198. return false;
  199. }
  200. return item && item.cl === 25 && item.loc === 'g';
  201. }
  202. over(e, b){
  203. if(!this.validItem(this.getItem(b))) return;
  204. this.$item.style.boxShadow = 'inset 0 0 8px 3px #0089ff';
  205. }
  206. out(e, b){
  207. this.$item.style.boxShadow = '';
  208. }
  209. drop(e, b){
  210. const $item = b.draggable;
  211. let _stop = $item.draggable("option", "stop");
  212. $item.draggable("option", "stop", function() {
  213. g.lock.add("blessWait");
  214. let ret = _stop.apply(this, arguments);
  215. g.lock.remove("blessWait");
  216. $item.draggable("option", "stop", _stop);
  217. return ret;
  218. });
  219.  
  220. this.out(e, b);
  221. const item = this.getItem(b);
  222. if(!this.validItem(item, true)) return;
  223. this.setItem(item);
  224. }
  225. setIcon(item){
  226. this.$item.style.background = 'url(' + window.CFG.ipath + item.icon + ')';
  227. }
  228. setTip(item){
  229. this.$item.setAttribute('tip', this.parseTip(item));
  230. }
  231. parseTip(item){
  232. item = Object.assign({}, item);
  233. item.stat = item.stat.replace(/amount=\d+;?/g,'').replace(/ammo=(\d+);?/g,'');
  234. return window.itemTip(item);
  235. }
  236. setItem(item, save = true, log){
  237. DebugLog('setting item', item, save);
  238. if(!this.validItem(item, log)) return false;
  239. this.item_name = item.name;
  240. this.setIcon(item);
  241. this.setTip(item);
  242. SLOTS_IDS[this.id] = item.name + '/' + Number(this.repeat);
  243. if(save) this.saveToStorage();
  244. }
  245. reset(save = true){
  246. this.$item.style.background = '';
  247. this.$item.removeAttribute('tip');
  248. this.item_name = '';
  249.  
  250. SLOTS_IDS[this.id] = '/' + Number(this.repeat);
  251. if(save) this.saveToStorage();
  252. }
  253. saveToStorage(){
  254. window.margoStorage.set('bless_bot/' + HERO_ID, SLOTS_IDS);
  255. }
  256. setRepeat(val = !this.repeat, setHtml = false){
  257. this.repeat = !!val;
  258. if(setHtml){
  259. this.$checkbox.checked = this.repeat;
  260. }
  261. }
  262. use(id){
  263. if(window.g.fight){
  264. onFightEnd = function(){
  265. this.use(id);
  266. }
  267. return;
  268. }
  269. if(window.g.talk.id){
  270. onDialogEnd = function(){
  271. this.use(id);
  272. }
  273. }
  274. this.equip(id);
  275. }
  276. equip(id){
  277. setTimeout(() => {
  278. window._g("moveitem&id="+id+"&st=1", () => {
  279. if(!this.repeat) this.reset();
  280. else this.checkIsEmpty();
  281. });
  282. }, randomInt(1200, 2000));
  283. }
  284. checkIsEmpty(){
  285. setTimeout(()=>{
  286. let itemsWithSameName = findItemsByName(this.item_name);
  287. DebugLog('checking items', itemsWithSameName);
  288. if(itemsWithSameName.length === 0) this.reset();
  289. }, 500);
  290. }
  291. }
  292.  
  293. //initiate
  294. initCSS();
  295. const $interface = initHTML();
  296. $interface.height = MAX_SLOTS.length * 40 + 'px';
  297.  
  298. let repeatSlots = window.margoStorage.get('bless_bot/' + HERO_ID, []).map( a => Number(a.split('/')[1]) );
  299. for(let i=0; i<MAX_SLOTS; i++){
  300. SLOTS_IDS[i] = '/' + (repeatSlots[i] || 0);
  301. SLOTS[i] = new Slot(i, $interface);
  302. SLOTS[i].init();
  303. if(repeatSlots[i] === 1) SLOTS[i].setRepeat(true, true);
  304. }
  305. window.SLOTS = SLOTS;
  306. })(window.parseInput);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement