TW Fred Timer

Fred's Timer View

Fra 20.04.2015. Se den seneste versjonen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        TW Fred Timer
// @name:ru     TW Fred Timer
// @namespace   TW Fred Timer
// @description Fred's Timer View
// @description:ru Счет времени бандита Фреда
// @include         http://*.the-west.*/game.php*
// @include         https://*.the-west.*/game.php*

// @version     0.02 alfa
// @grant       none

// ==/UserScript==


function TWFredScript(fn) {
	var script = document.createElement('script');
	script.setAttribute("type", "application/javascript");
	script.textContent = '(' + fn + ')();';
	document.body.appendChild(script);
	document.body.removeChild(script);
}

TWFredScript(function() {
  
  var VERSION = "0.01 alfa";
  var NAME = "TW Fred Timer";
  var installURL = '';
  
  
  fcContainer = $("<div />", 
                   { 
id: "twfred_container", 
style: "position: absolute; top: 64px; right: 50%; margin-right: 140px; z-index: 10; width: 142px; height: 16px;  background: url('/images/interface/character/character.png') no-repeat scroll left -160px transparent;" 
                   }
                  );
  
  fcContainer.appendTo("#user-interface");
  
  fcWrap = $('<div />',
              {
id: "twfred_wrap",
style:"padding-top:1px; font-size:10px; color:#FFF; text-align:center;",
              }
            );
    
  fcContainer.append(fcWrap);
  
  TWFred = {
    wofid:2, // don't know where find it
    diff: 0,
    timer:2, // seconds
    execCount:0,
    reInitVal:60, // execs to reinit data - on start
    reInitValN:60, // execs to reinit data - on normal work
    reInitValI:7, // execs to reinit data - on zero Timer
    clickObj: null,
    timerId: 0,
  };
  
  fcWrap.click(function(){TWFred.click()});
  
  TWFred.init = function()
  {
    TWFred.execCount = 0;
    TWFred.reInitVal = TWFred.reInitValN;
    TWFred.getClicker();
    
    $.post("/game.php?window=wheeloffortune&mode=init", {wofid:TWFred.wofid},
           function(data)
           {
             //console.log(data);
             
             var  diff = 0;
             var cd = data.mode.cooldowns;
             if (cd.length) // cuz no data at end of Event
             {
               cd = cd[0].cdstamp
               var stime = Game.getServerTime();
               diff = cd-stime;
             }
             TWFred.diff = diff;
             
             //TWFred.showTimer();
             TWFredTimer();
           }
          ); //post
  };
  
  TWFred.showTimer = function()
  {
    var str = TWFred.diff.formatDuration();
    fcWrap.html(str);
  };
  
  TWFred.doTimer = function()
  {
    //console.log('doTimer:' + TWFred.execCount + ',' + TWFred.reInitVal);
    TWFred.timerId = 0;
    if (undefined === TWFred) return;
    
    if (!TWFred.clickObj) TWFred.getClicker(); // init clicker
    
    var diff = TWFred.diff - TWFred.timer;
    if (diff < 0) diff = 0;
    if (!diff) TWFred.reInitVal = TWFred.reInitValI; 
    TWFred.diff = diff;
        
    TWFred.showTimer();
    TWFred.execCount++;
    if (TWFred.execCount > TWFred.reInitVal)
    {
      TWFred.init();
    }
    else
    {
      TWFredTimer();
    }
  };
  
  
  TWFred.getClicker = function()
  {
    if (TWFred.clickObj) return;
    
    var bar = WestUi.NotiBar.getBar();
        
    if (!bar) return;
    bar = bar.list;
        
    TWFred.reInitVal = TWFred.reInitValN;
    
    $.each(bar, function(key,val)
    {
      //console.log(val);
           
      if (val.uid == 'easter2015')
      {
        TWFred.clickObj = val.element;
      }
    });
    
    fcWrap.css("cursor","pointer");
    if (!TWFred.clickObj) TWFred.destroy();
  };
  
  
  TWFred.click = function()
  {
    if (!TWFred.clickObj) return;
    TWFred.init();
    TWFred.clickObj.click();
  };
  
  
  TWFred.destroy = function()
  {
    fcContainer.remove();
    delete TWFred;
  };
  
  function TWFredTimer()
  {
    if (undefined === TWFred) return;
    if (!TWFred.timerId) TWFred.timerId = setTimeout(TWFred.doTimer,TWFred.timer * 1000);
  };
  
  TWFred.init();
  
}
);