Page 1 of 15

Coding AJAX-compatible GM scripts

PostPosted: Tue Sep 11, 2007 6:55 pm
by lackattack
Here's a tip for anyone trying to fix BOB. There is an function I put in called handleResponse() that runs each time an AJAX response comes in. Perhaps the GM script could re-define that function to call gm_ConquerClubGame() at the end?

PostPosted: Tue Sep 11, 2007 7:25 pm
by wacicha
wow you guys amaze me, I can barly figure how to get this stuff running and you guys talk about all this programing

PostPosted: Tue Sep 11, 2007 7:27 pm
by jiminski
i know Wac .. thank goodness for them!

PostPosted: Tue Sep 11, 2007 9:07 pm
by mfontolan
Lack, I see your new code and I can not figure out how capture the refreshed code.
Maybe I am doing something wrong, but I can not intercept your calls!
For example, I can do the clock works but I can get the changes after it, for example, when the turn begins!
Do you have plans to put stats, card count and map inspect on original CC code?

Code: Select all
// ==UserScript==
// @name          Conquer Club - Ajax BOB
// @namespace     http://www.itj.com.br/
// @description   Ajax BOB
// @include       http://*conquerclub.com*
// ==/UserScript==

var TimeStr = document.getElementById('clock').innerHTML;
var TargetTime = TimeStr.split(/hrs\n|min\n|sec/);
var CurrHour = parseInt(TargetTime[0]);
var CurrMinute = parseInt(TargetTime[1]);
var CurrSecond = parseInt(TargetTime[2]);

ClockInterval = window.setInterval(ClockTimer, 1000);

function ClockTimer()
{
   --CurrSecond;
   if(CurrSecond < 0 ){
     --CurrMinute;
     if( CurrMinute < 0 ){
       --CurrHour;
       if( CurrHour < 0 ){
         CurrHour = 0;
       }
       CurrMinute = 59;
     }
     CurrSecond = 59;
   }
   StrClock = document.getElementById('clock');
   if (CurrHour < 10) {
      StrClock.innerHTML = '0' + CurrHour + 'hrs ';
   } else {
      StrClock.innerHTML = CurrHour + 'hrs ';
   }
   if (CurrMinute < 10) {
      StrClock.innerHTML += '0' +   CurrMinute + 'min ';
   } else {
      StrClock.innerHTML += CurrMinute + 'min ';
   }
   if (CurrSecond < 10) {
      StrClock.innerHTML += '0' +   CurrSecond + 'sec ';
   } else {
      StrClock.innerHTML += CurrSecond + 'sec ';
   }
   
   var CurrDate = new Date();
   if (CurrDate.getHours() < 10) {
      StrClock.innerHTML += ' @ <b>0' + (CurrDate.getHours()) + ':</b>';
   } else {
      StrClock.innerHTML += ' @ <b>' + (CurrDate.getHours()) + ':</b>';
   }
   if (CurrDate.getMinutes() < 10) {
      StrClock.innerHTML += '<b>0' + (CurrDate.getMinutes()) + '</b>';
   } else {
      StrClock.innerHTML += '<b>' + (CurrDate.getMinutes()) + '</b>';
   }
}

var handleResponse = function()
{
   alert('Nothing will happen');
}

PostPosted: Tue Sep 11, 2007 9:14 pm
by DiM
hmm and i though the xml is a bugger. now look at that pretty code :lol:

PostPosted: Tue Sep 11, 2007 9:41 pm
by lackattack
What if you had

Code: Select all
function handleResponse()
{
   alert('Nothing will happen');
}

instead of

Code: Select all
var handleResponse = function()
{
   alert('Nothing will happen');
}

PostPosted: Tue Sep 11, 2007 9:45 pm
by AAFitz
lackattack wrote:Here's a tip for anyone trying to fix BOB. There is an function I put in called handleResponse() that runs each time an AJAX response comes in. Perhaps the GM script could re-define that function to call gm_ConquerClubGame() at the end?


tell you what.......you install this crap for me on my computer, and ill make your moves in your game so you can win one

this would have been so much funnier if it wasnt for that "neverland game we played"

PostPosted: Tue Sep 11, 2007 10:48 pm
by lackattack
:lol:

PostPosted: Wed Sep 12, 2007 6:33 am
by mfontolan
Here is where I think we can go for...

Code: Select all
var bob_gameLog = unsafeWindow.gameLog;
var newhandleResponse = unsafeWindow.handleResponse;
unsafeWindow.handleResponse = function() {
  bob_gameLog.innerHTML += 'I am here!<br>';
  return newhandleResponse();
}

PostPosted: Wed Sep 12, 2007 7:13 am
by Ishiro
This might work... I'm looking at trying it now.

PostPosted: Wed Sep 12, 2007 9:21 am
by Ishiro
lackattack wrote:What if you had

Code: Select all
function handleResponse()
{
   alert('Nothing will happen');
}

instead of

Code: Select all
var handleResponse = function()
{
   alert('Nothing will happen');
}


I tried that and it didn't work. Your version of the function executed, and not mine.

PostPosted: Wed Sep 12, 2007 12:09 pm
by Ishiro
mfontolan wrote:Here is where I think we can go for...

Code: Select all
var bob_gameLog = unsafeWindow.gameLog;
var newhandleResponse = unsafeWindow.handleResponse;
unsafeWindow.handleResponse = function() {
  bob_gameLog.innerHTML += 'I am here!<br>';
  return newhandleResponse();
}


This works for inserting code into the AJAX, or replacing functions entirely. Now just the hard work of converting the old scripts to work in the new formats.

PostPosted: Wed Sep 12, 2007 1:37 pm
by mfontolan
Ishiro wrote:This works for inserting code into the AJAX, or replacing functions entirely. Now just the hard work of converting the old scripts to work in the new formats.


We can work together and make a new tool.

This are working and calling CC original function (...I think... :D )
Code: Select all
// ==UserScript==
// @name          Conquer Club - Ajax BOB
// @namespace     http://www.itj.com.br/
// @description   Ajax BOB
// @include       http://*conquerclub.com*
// ==/UserScript==

var bob_gameLog = unsafeWindow.gameLog;
var newhandleResponse = unsafeWindow.handleResponse;
unsafeWindow.handleResponse = function() {
  bob_gameLog.innerHTML += 'I am here!<br>';
  return newhandleResponse();
}


var TimeStr = document.getElementById('clock').innerHTML;
var TargetTime = TimeStr.split(/hrs\n|min\n|sec/);
var CurrHour = parseInt(TargetTime[0]);
var CurrMinute = parseInt(TargetTime[1]);
var CurrSecond = parseInt(TargetTime[2]) - 2;

ClockInterval = window.setInterval(ClockTimer, 1000);


function ClockTimer()
{
   --CurrSecond;
   if(CurrSecond < 0 ){
     --CurrMinute;
     if( CurrMinute < 0 ){
       --CurrHour;
       if( CurrHour < 0 ){
         CurrHour = 0;
       }
       CurrMinute = 59;
     }
     CurrSecond = 59;
   }
   StrClock = document.getElementById('clock');
   StrClock.innerHTML = zslash(CurrHour, 2) + 'hrs ' + zslash(String(CurrMinute), 2) + 'min ' + zslash(CurrSecond, 2) + 'sec ';
   var CurrDate = new Date();
   StrClock.innerHTML += ' @ <b>' + zslash(CurrDate.getHours(), 2) + ':' + zslash(CurrDate.getMinutes(), 2) + '</b>';
}

function zslash(svalue, iwidth)
{
   var szero = String(svalue);
   var ch = szero.substr(0,1);
   while (ch == ' ')
   {
      szero = szero.substr(1, szero.length);
      ch = szero.substr(0,1);
   }
   ch = szero.substr(szero.length - 1, szero.length);
   while (ch == ' ')
   {
      szero = szero.substr(0, szero.length - 1);
   ch = szero.substr(szero.length - 1, szero.length);
   }
   
   var i=0;
   for (i=0; i < (iwidth - szero.length); i++)
   {
      szero = '0' + szero;
   }   

   return szero;
}

PostPosted: Wed Sep 12, 2007 2:07 pm
by Ishiro
I can't seem to get the Options serializations to work like it does in the BOB script, so I'm back to coding in options that will have to be edited by the user.

The following code snippet put in a greasemonkey script will put the confirmation popups back on the various buttons.
Code: Select all
var confirmEnds = true;
var confirmAutoAttack = true;
var confirmDeploy = true;
var confirmChat = true;
var dashboard = document.getElementById('dashboard');

var newsendRequest = unsafeWindow.sendRequest;

unsafeWindow.sendRequest = function(command) {
/* ---    Confirmation Popups --- */
   if (((command == 'End Attacks' || command == 'End Fortification' || (command == 'Fortify' && !dashboard.innerHTML.has("Unlimited"))) && confirmEnds) || (command == 'Auto-Attack' && confirmAutoAttack) || (command == 'Deploy' && confirmDeploy) || (command == 'Post Message' && confirmChat))
   {
      if (confirm("Are you sure?"))
      {
         return newsendRequest(command);
      }
      else {
         return false;
      }
   }
   else {
      return newsendRequest(command);
   }
}

PostPosted: Wed Sep 12, 2007 3:54 pm
by Ishiro
Progress update...

I have everything working on page load, except color blind armies.

However, currently nothing updates as the AJAX updates. The reason is that the script was written to execute once per page, so, no care was given to being able to rebuild already built sections. The result right now is that every time you take an action, items like the player stats, text maps and menus will build 4 times. After taking a regular simple turn (begin, deploy, attack, advance, end attack, end fortification) you'll have 24 copies of each of the BOB items.

Working on it, but may not have a solution until next week. I'll put up what I have later when I have access to a place to upload the script.

PostPosted: Wed Sep 12, 2007 3:55 pm
by Optimus Prime
I just want to say thanks to you guys who are working on getting it refurbished for all of us. That's a lot of extra work you don't have to do.

Thanks again.

PostPosted: Wed Sep 12, 2007 4:06 pm
by Ishiro
Yeah, well, finally an excuse to learn AJAX. :)

PostPosted: Wed Sep 12, 2007 4:12 pm
by stringybeany
Add my thanks. What you are doing is work and has real value. Thanks.

PostPosted: Wed Sep 12, 2007 8:06 pm
by cricket
so u guys have a link to this new script?

PostPosted: Wed Sep 12, 2007 8:26 pm
by Ishiro
Not yet, because parts of it don't work well at all. The map doesn't update when you do things... have to fix that first.

PostPosted: Wed Sep 12, 2007 8:48 pm
by sully800
AAFitz wrote:
lackattack wrote:Here's a tip for anyone trying to fix BOB. There is an function I put in called handleResponse() that runs each time an AJAX response comes in. Perhaps the GM script could re-define that function to call gm_ConquerClubGame() at the end?


tell you what.......you install this crap for me on my computer, and ill make your moves in your game so you can win one

this would have been so much funnier if it wasnt for that "neverland game we played"


lack just beat me in a game :?

He gained 146 points total

PostPosted: Wed Sep 12, 2007 9:27 pm
by mfontolan
Ishiro wrote:Not yet, because parts of it don't work well at all. The map doesn't update when you do things... have to fix that first.

The original BOB was write when pages are totally reloaded and now, only a few part of the page are reloaded.
I do not thing that we can make original BOB work and is not a easy think fix/maintain/upgrade someone code.
Above is what I think that is more important on the new code, that is a prototype model of clock fully working in Ajax way.
The 2 seconds difference (in the setInterval) is intentional to show that after refresh (use page link (Ajax) and not navigator button) the clock became correctly.

Code: Select all
// ==UserScript==
// @name          Conquer Club - Ajax BOB
// @namespace     http://www.itj.com.br/
// @description   Ajax BOB
// @include       http://*conquerclub.com*
// ==/UserScript==


// Starting
var TimeStr = document.getElementById('clock').innerHTML;
var TargetTime = TimeStr.split(/hrs\n|min\n|sec/);
var CurrHour = parseInt(TargetTime[0]);
var CurrMinute = parseInt(TargetTime[1]);
var CurrSecond = parseInt(TargetTime[2]);
ClockInterval = window.setInterval(ClockTimer, 2000);


// Running clock
function ClockTimer()
{
   --CurrSecond;
   if (CurrSecond < 0)
   {
     --CurrMinute;
     if (CurrMinute < 0)
     {
       --CurrHour;
       if (CurrHour < 0)
       {
         CurrHour = 0;
       }
       CurrMinute = 59;
     }
     CurrSecond = 59;
   }
   snextTurn = ' @ ';
   CurrDate = new Date();
   nextTurn = (CurrHour * 60) + CurrMinute + (CurrDate.getHours() * 60) + CurrDate.getMinutes();
   if (nextTurn > 1440)
   {
      nextTurn -= 1440;
      snextTurn = ' tomorrow @ ';
   }
   StrClock = document.getElementById('clock');
   StrClock.innerHTML = zslash(CurrHour, 2) + 'hrs ' + zslash(CurrMinute, 2) + 'min ' + zslash(CurrSecond, 2) + 'sec' + snextTurn + '<b>' + zslash(parseInt(nextTurn / 60), 2) + ':' + zslash(nextTurn - parseInt(parseInt(nextTurn / 60) * 60), 2) + '</b />';
}


// Ajax Code
var bob_gameLog = unsafeWindow.gameLog;
var bob_gameChat = unsafeWindow.gameChat;
var bob_handleResponse = unsafeWindow.handleResponse;
unsafeWindow.handleResponse = function() {
   if (typeof(unsafeWindow.request.responseText) != "undefined")
   {
      var bob_response = unsafeWindow.request.responseText.split("&"); 
      if (bob_response.length > 2)
      {
         if (bob_response[2] != "")
         {
            TargetTime = bob_response[2].split(/hrs|min|sec/);
            CurrHour = parseInt(TargetTime[0]);
            CurrMinute = parseInt(TargetTime[1]);
            CurrSecond = parseInt(TargetTime[2]);
         }
         var CurrDate = new Date();
         bob_gameChat.innerHTML += '<b>' + zslash(CurrDate.getHours(), 2) + ':' + zslash(CurrDate.getMinutes(), 2) + ':' + zslash(CurrDate.getSeconds(), 2) + '</b /> Ajax Test - I am a response!<br />';
         }
   } else {
      var CurrDate = new Date();
      bob_gameChat.innerHTML += '<b>' + zslash(CurrDate.getHours(), 2) + ':' + zslash(CurrDate.getMinutes(), 2) + ':' + zslash(CurrDate.getSeconds(), 2) + '</b /> Ajax Test - I am not!<br />';
   }
   return bob_handleResponse();
}


// Counters, Map inspect...


// Common Functions
function zslash(svalue, iwidth)
{
   var szero = String(svalue);
   var ch = szero.substr(0,1);
   while (ch == ' ')
   {
      szero = szero.substr(1, szero.length);
      ch = szero.substr(0,1);
   }
   ch = szero.substr(szero.length - 1, szero.length);
   while (ch == ' ')
   {
      szero = szero.substr(0, szero.length - 1);
   ch = szero.substr(szero.length - 1, szero.length);
   }
   for (var i=0; i < (iwidth - szero.length); i++)
   {
      szero = '0' + szero;
   }   
   return szero;
}

PostPosted: Wed Sep 12, 2007 9:32 pm
by Incandenza
I too would like to extend my thanks to the brave souls on the front line in the battle against ajax. :D

PostPosted: Thu Sep 13, 2007 6:48 am
by Ishiro
I'm pretty sure I can get the map inspect working properly, however, it may have to lose the color coding and just show attack/defend paths. Working on that today.

PostPosted: Thu Sep 13, 2007 7:25 am
by yeti_c
Most aspects of the map inspect are fairly rigid (i.e. what you can and can't attack)

But obviously the things that change are the colour coding of each territory.

The Table of data code would need to be run on every refresh... of course that's the bit that takes time as it inspects game logs - so may not actually complete in time for a refresh... so could be nasty - unless it's made easier to get this info out of the system now.

C.