TWeaker

Some little tweaks for The-West

Mint 2015.03.04.. Lásd a legutóbbi verzió

// ==UserScript==
// @name TWeaker
// @namespace TomRobert
// @author Vbyec (updated by Tom Robert)
// @description Some little tweaks for The-West
// @include http*://*.the-west.*/game.php*
// @version 0.1.10
//
// @history 0.1.10 Add Polish (TeeNOmore127)
// @history 0.1.9  Fix for the west beta
// @history 0.1.8  Add German (Tom Robert), remove Kick-o-matic autoload
// @history 0.1.7  Add Spanish (pepe100)
// @history	0.1.6  Add Items controls and search
// @history	0.1.5  Add English
// @history	0.1.4  Add Kick-o-matic autoload
// @nocompat Chrome
// @grant none
// ==/UserScript==
function exec(fn) {
  var script = document.createElement('script');
  script.setAttribute('type', 'application/javascript');
  script.textContent = '(' + fn + ')();';
  document.body.appendChild(script); // run the script
  document.body.removeChild(script); // clean up
}
exec(function () {
  Tweaker = {
    version: '0.1.10',
    name: 'Tweaker',
    author: 'Vbyec (updated by Tom Robert)',
    minGame: '2.08',
    maxGame: Game.version.toString(),
    website: 'https://greasyfork.org/scripts/xxxx',
  };
  langs = {
    ru_RU: {
      language: 'Russian (ру́сский)',
      ApiGui: '<b>How to use:</b><br><u>Ctrl+click:</u> Copy <i>*nickname*</i> to active chat.<br><u>Shift+click:</u> Copy "<i>*nickname* change with</i>" to active chat.<br><u>Backspace+click:</u> Open private chat with current player.<br>Automatically add some information (total dmg, last hit) to players description in fortbattle.<br>Write "<i>/items</i>" into chat to check out new items.<br>Write "<i>/items.s name</i>" into chat to find items by name.<br>Write "<i>/items.add</i>" into chat to update local DB.',
      contact: 'контакт',
      TotalDamage: 'Урона за бой',
      LastHit: 'Последний выстрел',
      KillShot: 'Скальп',
      ChangeWith: 'смена с',
      UserMessage: 'Похоже появились новые шмотки.',
      itemsShorthelp: 'Первичная настрока локального хранилища вещей.',
      itemsHelp: 'Первичная настрока локального хранилища вещей, если хранилище уже существует- проверка на появление новых вещей.',
      itemssShorthelp: 'Поиск по названию предмета.',
      itemssHelp: 'Поиск по части названия предмета. Регистронезависимый.',
      itemssUsage: 'часть названия',
      itemsaddShorthelp: 'Добавить новые предметы в локальную базу.',
      itemsaddHelp: 'Добавить все новые предметы в локальную базу, чтобы они не показывали при.',
    },
    en_US: {
      language: 'None (English)',
      ApiGui: '<b>How to use:</b><br><u>Ctrl+click:</u> Copy <i>*nickname*</i> to active chat.<br><u>Shift+click:</u> Copy "<i>*nickname* change with</i>" to active chat.<br><u>Backspace+click:</u> Open private chat with current player.<br>Automatically add some information (total dmg, last hit) to players description in fortbattle.<br>Write "<i>/items</i>" into chat to check out new items.<br>Write "<i>/items.s name</i>" into chat to find items by name.<br>Write "<i>/items.add</i>" into chat to update local DB.',
      contact: 'Contact',
      TotalDamage: 'Total damage',
      LastHit: 'Last hit',
      KillShot: 'Kill shot',
      ChangeWith: 'change with',
      UserMessage: 'New items found.',
      itemsShorthelp: 'Check out new items.',
      itemsHelp: 'Shows the items, which were last added to the game. But you have to set up first a local storage of actual items to detect new items.',
      itemssShorthelp: 'Find items by name.',
      itemssHelp: 'Type in anything to search for items, where that part is included.',
      itemssUsage: 'parte name',
      itemsaddShorthelp: 'Update local DB.',
      itemsaddHelp: 'Add new items to the local storage, which aren\'t registrated yet.',
    },
    es_ES: {
      language: 'Spanish (español)',
      ApiGui: '<b>Uso:</b><br><u>Ctrl+click:</u> Copia <i>*nombre_jugador*</i> en el chat activo.<br><u>Shift+click:</u> Copia el texto "<i>*nombre_jugador* cambia con</i>" en el chat activo.<br><u>Backspace+click:</u> Abre chat privado con el jugador actual.<br>Automáticamente añade la siguiente información (daño total, último tiro) en la descripción de los jugadores en batallas de fuertes, simplemente marcando con el puntero del ratón el jugador en el fuerte.<br>Escribe "<i>/items</i>" en el chat para ver nuevos artículos.<br>Escribe "<i>/items.s nombre</i>" en el chat para encontrar artículos por nombre.<br>Escribe "<i>/items.add</i>" en el char para actualizar la Base de Datos local.',
      contact: 'Contacto',
      TotalDamage: 'Daño Total',
      LastHit: 'Último tiro',
      KillShot: 'Desmayo',
      ChangeWith: 'cambiar con',
      UserMessage: 'Parece que hay ropa nueva.',
      itemsShorthelp: 'Configuración inicial de artículos en la base de datos local.',
      itemsHelp: 'La configuración inicial de la base de datos local, si ya existe la base de datos, comprueba los artículos nuevos.',
      itemssShorthelp: 'Búsqueda por nombre de artículo.',
      itemssHelp: 'Buscar por nombre de artículo, minúsculas.',
      itemssUsage: 'parte del nombre',
      itemsaddShorthelp: 'Agregar nuevos artículos a la base de datos local.',
      itemsaddHelp: 'Agregar nuevos artículos a la base de datos local, los que no se muestran en el registro.',
    },
    de_DE: {
      language: 'German (Deutsch)',
      ApiGui: '<b>Funktionen:</b><br><u>Ctrl+Klick:</u> Kopiert <i>*nickname*</i> in den aktiven Chat.<br><u>Shift+Klick:</u> Kopiert "<i>*nickname* tausche mit</i>" in den aktiven Chat.<br><u>Leertaste+Klick:</u> Öffnet privaten Chat mit dem angeklickten Spieler.<br>Fügt automatisch zusätzliche Informationen (Gesamter Schaden, letzter Treffer) zur Spielerbeschreibung im Fortkampf hinzu.<br>Tippe "<i>/items</i>" in den Chat um neue Items zu finden.<br>Tippe "<i>/items.s Kürzel</i>" in den Chat um Items mit dem Kürzel zu finden.<br>Tippe "<i>/items.add</i>" in den Chat, um den lokalen Speicher zu aktualisieren.',
      contact: 'Kontakt',
      TotalDamage: 'Gesamter Schaden',
      LastHit: 'Letzter Treffer',
      KillShot: 'Todesschuss',
      ChangeWith: 'tausche mit',
      UserMessage: 'Neue Items gefunden.',
      itemsShorthelp: 'Neue Items anschauen.',
      itemsHelp: 'Zeige Items, welche neu zum Spiel hinzugefügt wurden und noch nicht in deinem lokalen Speicher sind.',
      itemssShorthelp: 'Finde Items mit Kürzel.',
      itemssHelp: 'Durch Eingabe von Wortbruchstücken werden alle Items angezeigt, welche dazu gefunden werden.',
      itemssUsage: 'Wortfetzen',
      itemsaddShorthelp: 'Lokalen Speicher aktualisieren.',
      itemsaddHelp: 'Ungespeicherte Items werden dem lokalen Speicher hinzugefügt.',
    },
    pl_PL: {
      language: 'Polish (Polski)',
      ApiGui: '<b>Jak używać:</b><br><u>Ctrl+click:</u> Skopiuj <i>*nickname*</i> do aktywnego czatu.<br><u>Shift+click:</u> Skopiuj "<i>*nickname* rotacja z</i>" do aktywnego czatu.<br><u>Backspace+click:</u> Otwórz czat prywatny z obecnym graczem.<br>Automatycznie dodaje kilka informacji (Całkowite obrażenia, ostatni strzał) do opisu graczy na bitwie.<br>Napisz "<i>/items</i>" na czacie, aby sprawdzić nowe przedmioty.<br>Napisz "<i>/items.s name</i>" na czacie, by znaleźć przedmiot po nazwie.<br>Napisz "<i>/items.add</i>" na czacie,by zaktualizować bazę danych.',
      contact: 'Kontakt',
      TotalDamage: 'Łączne Obrażenia',
      LastHit: 'Ostatni strzał',
      KillShot: 'Zabójstwo!',
      ChangeWith: 'Rotacja z',
      UserMessage: 'Nowe przedmioty znalezione.',
      itemsShorthelp: 'Zobacz nowe przedmioty.',
      itemsHelp: 'Pokaż przedmioty, które ostatnio zostały dodane.',
      itemssShorthelp: 'Szukaj przedmiotów po nazwie.',
      itemssHelp: 'Wpisz cokolwiek, aby znaleźć przedmiot, w których część została uwzględniona.',
      itemssUsage: 'Część nazwy',
      itemsaddShorthelp: 'Zaktualizuj lokalną Baze Danych.',
      itemsaddHelp: 'Dodaj nowe przedmioty do lokalnej pamięci, które nie zostały zarejestrowane.',
    },
  };
  Tweaker.getLang = function () {
    return langs.hasOwnProperty(Game.locale) ? Game.locale : 'en_US';
  };
  var lang = Tweaker.getLang();
  lang = langs[lang];
  //add Auto load Kick-o-matic
  /*if (localStorage.hasOwnProperty('AutoLoad_Kick-o-matic')) {
    if (localStorage['AutoLoad_Kick-o-matic']) {
      var MyScript = TheWestApi.register('Kick-o-matic', 'Kick-o-matic', '2.08', '2.09', 'Macabre2077', 'http://userscripts-mirror.org/scripts/show/96262');
      MyScript.loadScript('http://Vbyec.ru/96262.user.js');
    }
  } else {
    localStorage.setItem('AutoLoad_Kick-o-matic', 1);
  }*/
  var TweakerAPI = TheWestApi.register('TWeaker', Tweaker.name, Tweaker.minGame, Tweaker.maxGame, Tweaker.author, Tweaker.website);
  TweakerAPI.setGui('<br><i>Language detected: </i>' + lang.language + '<br><br>Some little tweaks for The West.<br>Enables commands on forts (simply way to copy nickname of players).<br><br>' + lang.ApiGui + '<br><br><i>' + Tweaker.name + ' v' + Tweaker.version
  + '</i>'); /*<br><br><br><b>' + lang.contact + ':</b><ul style="margin-left:15px;"><li>Send a message to <a target=\'_blanck\' href="http://om.the-west.de/west/de/player/?ref=west_invite_linkrl&player_id=647936&world_id=13&hash=7dda">Tom Robert on German world Arizona</a></li>'
  + '<li>Message me on one of these The West Forum:<br>/ <a target=\'_blanck\' href="http://forum.the-west.de/private.php?do=newpm&u=24502">deutsches Forum</a> / '
  + '<a target=\'_blanck\' href="http://forum.the-west.net/private.php?do=newpm&u=37219">English forum</a> / <a target=\'_blanck\' href="http://forum.the-west.pl/private.php?do=newpm&u=32083">forum polski</a> / '
  + '<a target=\'_blanck\' href="http://forum.the-west.es/private.php?do=newpm&u=13770">foro español</a> /<br>/ <a target=\'_blanck\' href="http://forum.the-west.ru/private.php?do=newpm&u=27430">России форум</a> / '
  + '<a target=\'_blanck\' href="http://forum.the-west.fr/private.php?do=newpm&u=17783">forum français</a> / <a target=\'_blanck\' href="http://forum.the-west.it/private.php?do=newpm&u=14287">forum italiano</a> / '
  + '<a target=\'_blanck\' href="http://forum.beta.the-west.net/private.php?do=newpm&u=4072">beta forum</a> /<br>I will get an e-mail when you sent me the message <img src="../images/chat/emoticons/smile.png"></li></ul>*/
  // local DataBase of items.
  window.Items = {
    room: '',
    add_item: function (id, name) {
      localStorage.setItem('ItemId_' + id, name);
      return true;
    },
    get_name: function (id) {
      return localStorage.getItem('ItemId_' + id);
    },
    search: function (query) {
      localStorage.getItem('Item_count') ? '' : Items.init();
      for (var key in localStorage) {
        if (typeof key === 'string' && key.indexOf('ItemId') === 0) {
          var index = key.split('_') [1];
          if (Items.get_name(index).toLowerCase().indexOf(query.toLowerCase()) >= 0) {
            Items.show_in_chat('item=' + index + ' : ' + '[item=' + index + ']');
          }
        }
      }
    },
    check: function () {
      var count = 0;
      for (var i = 0; i < 999999; i++) {
        var item = ItemManager.get(i);
        if (item !== undefined) {
          count++;
        }
      }
      var prev_count = localStorage.getItem('Item_count') === null ? 0 : localStorage.getItem('Item_count');
      if (prev_count < count) {
        new UserMessage(lang.UserMessage, UserMessage.HINT).show();
        Items.show_new();
      } 
      else {
        Items.show_in_chat('No new items');
      }
    },
    show_new: function () {
      for (var i = 0; i < 999999; i++) {
        var item = ItemManager.get(i);
        if (item !== undefined && Items.get_name(item.item_id) === null) {
          Items.show_in_chat('item=' + item.item_id + ' : ' + '[item=' + item.item_id + ']');
        }
      }
    },
    init: function () {
      if (localStorage.getItem('Item_count') === null) {
        Items.add_all();
      } 
      else {
        Items.check();
      }
    },
    add_all: function () {
      var count = 0;
      for (var i = 0; i < 999999; i++) {
        var item = ItemManager.get(i);
        if (item !== undefined && Items.get_name(item.item_id) === null) {
          Items.add_item(item.item_id, item.name);
          count++;
        }
      }
      if (count > 0) {
        var prev_count = localStorage.getItem('Item_count') === null ? 0 : localStorage.getItem('Item_count');
        localStorage.setItem('Item_count', parseInt(prev_count) + count);
        Items.show_in_chat('Items added:' + count);
      } 
      else {
        Items.show_in_chat('No item to add');
      }
    },
    show_in_chat: function (text) {
      Items.room.addMessage(Game.TextHandler.parse(text) + '<br/>');
    }
  };
  Chat.Operations['^/items$'] = {
    cmd: 'items',
    shorthelp: lang.itemsShorthelp,
    help: lang.itemsHelp,
    usage: '/items',
    func: function (room, msg) {
      Items.room = room;
      Items.init();
    }
  };
  Chat.Operations['^/items.s (.+)$'] = {
    cmd: 'items.s',
    shorthelp: lang.itemssShorthelp,
    help: lang.itemssHelp,
    usage: '/items.s ' + lang.itemssUsage,
    func: function (room, msg, search) {
      Items.room = room;
      Items.search(search[1]);
    }
  };
  Chat.Operations['^/items.add$'] = {
    cmd: 'items.add',
    shorthelp: lang.itemsaddShorthelp,
    help: lang.itemsaddHelp,
    usage: '/items.add',
    func: function (room, msg) {
      Items.room = room;
      Items.add_all();
    }
  };
  FortBattleWindow.showBattleOrigin = FortBattleWindow.showBattle;
  FortBattleWindow.showBattle = function (response) {
    FortBattle.cacheAll(response);
    FortBattleWindow.showBattleOrigin.call(this, response);
  };
  // Add players on fort to localStorage
  //rewrite by while
  //@todo get fort position
  FortBattle.cacheAll = function (resp) {
    Ajax.remoteCallMode('players', 'get_data', {
      x: Character.position.x,
      y: Character.position.y,
      page: 0
    }, function (data) {
      data.players.forEach(function (player) {
        if (!localStorage.hasOwnProperty('PlayerId_' + player.player_id)) {
          localStorage.setItem('PlayerId_' + player.player_id, player.name);
        }
      });
      var count = data.pages;
      for (var i = 1; i < count; i++) {
        Ajax.remoteCallMode('players', 'get_data', {
          x: Character.position.x,
          y: Character.position.y,
          page: i
        }, function (data) {
          data.players.forEach(function (player) {
            if (!localStorage.hasOwnProperty('PlayerId_' + player.player_id)) {
              localStorage.setItem('PlayerId_' + player.player_id, player.name);
            }
          });
        });
      }
    });
  };
  // Show advance information about player
  FortBattle.getCharDataSheetOrigin = FortBattle.getCharDataSheet;
  FortBattle.getCharDataSheet = function (data) {
    return FortBattle.getCharDataSheetOrigin(data) + '<br/><div class=\'total_damage\'>' + lang.TotalDamage + ':<strong>%totalDmg%</strong> </div>' +
    '<div class=\'last_damage\'>' + lang.LastHit + ': <strong>%lastDmg%</strong></div>';
  };
  FortBattle.flashShowCharacterInfoOrigin = FortBattle.flashShowCharacterInfo;
  FortBattle.flashShowCharacterInfo = function (fortId, playerId, healthNow, healthMax, totalDmg, lastDmg, shotat, bonusdata, resp) {
    //Kill shot
    lastDmg = lastDmg < 15000 ? lastDmg : lang.KillShot;
    FortBattle.flashShowCharacterInfoOrigin(fortId, playerId, healthNow, healthMax, totalDmg, lastDmg, shotat, bonusdata);
    FortBattle.flashShowCharacterInfoEl(playerId);
  };
  FortBattle.flashShowCharacterInfoEl = function (playerId) {
    if (parseInt(Chat.MyId.split('_') [1]) === playerId)
    return;
    setTimeout(function () {
      document['onkeyup'] = null;
    }, 2500);
    document['onkeyup'] = function (e) {
      e = e || window.event;
      e.preventDefault();
      var active_chat_input = $('input.message:visible');
      var keyCode = e.keyCode ? e.keyCode : e.charCode;
      var nick = localStorage.getItem('PlayerId_' + playerId);
      switch (keyCode) {
        case 16:
          /* shift */
          if (!nick)
          return;
          active_chat_input.val(active_chat_input.val() + '*' + nick + '* ' + lang.ChangeWith + '  ');
          break
        case 17:
          /* ctrl */
          if (!nick)
          return;
          active_chat_input.val(active_chat_input.val() + '*' + nick + '* ');
          active_chat_input.focus();
          break
        case 8:
          /* backspace */
          var client = Chat.Resource.Manager.getClient('client_' + playerId);
          var room = Chat.Resource.Manager.acquireRoom(client);
          if (room)
          room.openClick();
          break
        default:
          break
      }
      document['onkeyup'] = null;
      /*smth*/
    };
  };
  EventHandler.listen('chat_init', function () {
    for (var room in Chat.Resource.Manager.getRooms()) {
      if (room.indexOf('room_town') === 0) {
        $('<p style=\'left: 50%; position: absolute; top: 44px; margin-left: -305px; color: white; font-weight: bolder; font-size: 18px;\'>' + Chat.Resource.Manager.getRooms() [room].topic + '</p>').appendTo('#ui_topbar');
      }
    }
  });
});