Welcome
Welcome to the Castle Age AutoPlayer forums

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

Artifice's CAAP Coding Classroom

Area to post your tutorials and guides to using CAAP

Moderators: didact69, stallion

Artifice's CAAP Coding Classroom

Postby Artifice » 25 May 2015, 09:26

Welcome!

I've been working on CAAP for a while, and I thought it would be helpful to lay out some basics on what tools help for CAAP coding, how CAAP itself is structured, and some guidelines on CAAP programming. This is intended be a work in progress as well as a discussion, so please ask questions and help other people with their questions, too!

Artifice has been thanked by:
Artifice
Site Admin
Site Admin
 
Posts: 267
Joined: 23 Nov 2013, 07:47
Has thanked: 40 time
Have thanks: 191 time

 

Re: Artifice's CAAP Coding Classroom

Postby Artifice » 25 May 2015, 09:26

First, let's talk about what software and other tools you should have when you start coding for CAAP.

Even before looking at the code, I recommend you download a programming text editor. I personally use Notepad++. It's free, and it's awesome. Whatever program you pick, at a minimum you will want it to colorise your code for easy reading and recognise the .js file type so it will use javascript syntax.

Once you have a good text editor, you can start looking at the code. CAAP uses git as a software versioning system to manage the contributions of many people without causing overwrites of other's code. The main branch of CAAP is on Magowiz's github account, here: https://github.com/magowiz/Castle-Age-Autoplayer. My branch, which contributes to Magowiz's, is here: https://github.com/Artifice/Castle-Age-Autoplayer. You'll want to set up your own github account and fork Magowiz's Castle-Age-Autoplayer to your account, and then make a local clone of it. To make the local clone, you'll need git software. I use Github for Windows, which works fine.

Once you have a local repository, you can start playing and testing with it on your own Chrome instance. In Chrome, go to the chrome://extensions/ settings page, enable the "Developer Mode" box, and you can point to a folder location to compile your own version of CAAP. This will be the Castle-Age-Autoplayer/Chrome/unpacked directory.

Learn to use the Console Log to see the messages and errors from CAAP. You can see this by going to the Javascript Console from the Chrome dropdown under More Tools or by pressing Ctrl + Shift + J. Note that CAAP has 9 log levels. I only use log level 2, since more than that gets spammy. CAAP log level is set in your CAAP config menu under Other > Advanced Items > Log Level.

Debugging
For faster debugging, you should download a JS Lint checker for your text editor. (JS Lint plugin for Notepad++) Javascript itself does not check for syntax errors until code is actually executed. Loading up your new code into Chrome, refreshing the page, and then waiting for your code to execute only to find you forgot an end parenthesis burns time. JS Lint provides much faster feedback on your code syntax. It will also notice unused or undeclared variables and generally guide you toward better coding style.

Even if your syntax is correct, sometimes the code doesn't work. To see what's really happening, learn to use the Chrome debugging tools. They're powerful and let you see the values of variables at runtime so you can see what's actually happening in your code. To see the CAAP files, click on Content Scripts. Then you can open and put in breakpoints in the files to see the action at any point you need. In general, it's better to use the Chrome debugger to look at variable values instead of adding con.logs.

If you have Chrome stopped on a breakpoint in CAAP, you'll have access to all of the CAAP variables and commands in the command line, so now you can use the Console to directly test commands. I often use this to test regex expressions or confirm javascript syntax works as I think it does. Sometimes I'll execute several lines of code there to confirm it works as I expected before saving and reloading the CAAP extension.

Artifice has been thanked by:
Artifice
Site Admin
Site Admin
 
Posts: 267
Joined: 23 Nov 2013, 07:47
Has thanked: 40 time
Have thanks: 191 time

Re: Artifice's CAAP Coding Classroom

Postby Artifice » 25 May 2015, 09:27

Next let's talk about the structure of CAAP itself, and how it does what it does.

CAAP uses a worker based structure for most of its tasks. Examples of workers include battle, monsters, finder, conquest, and quests (soon). Each worker can review the pages at every page load, hold it's own records, have a dashboard, and do actions. Generally, the actions of a worker should be restricted to that worker to avoid unpredictable impacts on other parts of the program from changes.

I've designed the worker code so that adding a new worker should only require three things -- coding up the worker file itself based on the other workers, adding the file to the manifest.json in the Castle-Age-Autoplayer/Chrome/unpacked directory, and adding the menu it if has one to the caap.addControl function in the caap_base.js file (I may fold this last requirement into the worker structure eventually, so it won't be required anymore).

After every page load, all worker checkResults function is called (if they exist). These functions should only be used for information gathering. If actions like clicking buttons or navigating to different pages are done here, that will stop other workers from being able to read the page, and may cause double-clicks without reviewing the intervening page, which can lead to unpredictable actions.

Once all workers have reviewed the page, next CAAP goes through the worker actions in order of priority. The priority list is determined at load time by each of the worker functions that have been added with a priority like this:

worker.addAction({worker : 'battle', priority : 1050, description : 'Battling Players'});

Priority numbers can be positive or negative. Priority 0 is the Idle General. Because the Idle General sets the general, priorities less than zero should never require a specific general. If an action function needs to use a specific general, give it a priority above zero, or else there will be a loop between the action function and the Idle General as they fight over which general should be set.

Artifice has been thanked by:
Artifice
Site Admin
Site Admin
 
Posts: 267
Joined: 23 Nov 2013, 07:47
Has thanked: 40 time
Have thanks: 191 time

Re: Artifice's CAAP Coding Classroom

Postby Artifice » 25 May 2015, 09:29

Let's talk about the structure of web pages, how to work out direct links, and use them in your programs!

When CAAP is navigating through Castle Age, try to use the least amount of clicks necessary. Previously, a lot of effort was spent in CAAP trying to navigate the page (DOM) and find special areas to click. This requires a lot of jQuery to do and uses more CPU to process. Much faster is to figure out what the direct link is, and then send that back to the CA servers as an ajax link. You can learn how to form direct links by reading the page code. To do this, right click on a button, and select "Inspect Element." This will take you to the HTML code behind that button.

The simplest link is a straight ajax link send, such as the View Daily Bonuses button on the top CA page. It's HTML when inspected looks like this:

Code: Select all
<a href="https://apps.facebook.com/castle_age/daily_bonus_list.php" onclick="ajaxLinkSend('globalContainer', 'daily_bonus_list.php'); return false;"><img src="https://castleagegame1-a.akamaihd.net/32703/graphics/daily_btn_view.gif"></a>

To visit this page with a direct link, use "daily_bonus_list.php", with the appropriate domain prefix (either https://apps.facebook.com/castle_age/ or https://web3.castleagegame.com/castle_ws/) you can visit that page directly with a URL.

More complex links will use ajax form sends. Let's look at an Invade Button on the Battle page. The HTML for an invade button looks like this:

Code: Select all
<form onsubmit="ajaxFormSend('globalContainer', 'battle.php', this);return false;" method="post" id="61788691a007bb739b3ae1cb8f61afce">
                        <input type="hidden" name="symbol_id" value="2">
                        <input type="hidden" name="target_id" value="6666">
                        <input type="hidden" name="action" value="battle">
                        <input type="hidden" name="duel" value="false">
                        <input type="hidden" name="bqh" value="30b6a5b77e546ade7598ec8791a4bb54">
                        <div class="imgButton" style="height:31px;">
                            <input alt="Invade" type="image" src="https://castleagegame1-a.akamaihd.net/32703/graphics/battle_btn_invade.gif" name="battle_frm_sbm">
                        </div>
                        <input type="hidden" name="ajax" value="1">
                    </form>

This is an ajaxFormSend type-button. The corresponding direct link would be: 'battle.php?symbol_id=2&target_id= 6666&action=battle&duel=false&bqh= 30b6a5b77e546ade7598ec8791a4bb54&ajax=1'. If you add the correct domain prefix you can hit that target just by visiting a URL. By the way, the "bqh=" string is there for time verification. If you use a value for bqh that is too old, the link won't work. This is how CA prevents players from opening the same page on 20 different tabs, and then using that link for several days until the tabs run out.

These two types, ajax links and forms, account for about 95% of the CA actions. The last kind of link is where CA has it's own function created to do something, such as fast swap a general. Here's the HTML for that:

Code: Select all
<div class="hotSwapGenContainer">
                  <img src="https://castleagegame1-a.akamaihd.net/32703/graphics/hero_zarevok.jpg" class="hotSwapGenImg" onclick="doHotSwapGeneral('24', '0', false);">
                  <div class="hotSwapGenName" onclick="doHotSwapGeneral('24', '0', false);">
                     Zarevok                  </div>
               </div>

In this case CA is calling a javascript function "doHotSwapGeneral" to do the action. You can look at the code of the CA function by inspecting the page source under the sources tab in Chrome. In the end, the function will probably make a post to the server using ajax, and then display that information somewhere on the page. In this case, it's easiest just to call the CA function itself, like this: window.location.href = caap.jss + ":void(doHotSwapGeneral('" + gR.item + "', '" + gR.itype + "', false))";

For ajax links and forms, I created the function caap.navigate3() to navigate to a page using ajax, and then send the link for the appropriate action from that page with an optional general as a third argument. It's not technically necessary to navigate to the page first, but this is helpful to confirm the link is appropriate. For example, before sending a monster direct attack link without first visiting the monster to make sure it's alive, you haven't done over max damage, etc. Navigate3 checks the ajax links and forms on the page navigated to, and if the link isn't there, it will give an error and print the links that are available on that page. This list of links is created by the caap.scrapeLinks() function, which can also be used to list the links available on any page when executed while CAAP is paused via the break function in the Chrome debugger described above.

Artifice has been thanked by:
Artifice
Site Admin
Site Admin
 
Posts: 267
Joined: 23 Nov 2013, 07:47
Has thanked: 40 time
Have thanks: 191 time

Re: Artifice's CAAP Coding Classroom

Postby Artifice » 25 May 2015, 09:30

FB vs. Web3 load process (More here later!)
Artifice
Site Admin
Site Admin
 
Posts: 267
Joined: 23 Nov 2013, 07:47
Has thanked: 40 time
Have thanks: 191 time

Re: Artifice's CAAP Coding Classroom

Postby Artifice » 25 May 2015, 09:30

General coding principles (More here later!)
Artifice
Site Admin
Site Admin
 
Posts: 267
Joined: 23 Nov 2013, 07:47
Has thanked: 40 time
Have thanks: 191 time

Re: Artifice's CAAP Coding Classroom

Postby Artifice » 25 May 2015, 09:30

TBD
Artifice
Site Admin
Site Admin
 
Posts: 267
Joined: 23 Nov 2013, 07:47
Has thanked: 40 time
Have thanks: 191 time

Re: Artifice's CAAP Coding Classroom

Postby Artifice » 26 May 2015, 05:15

Artifice's Assignments!

Here I'll post some things that should be done to the code, but are not currently high on my list. If you're looking for a way to familiarise yourself with the code by making some changes, this is a good place to start! If you have questions while working on an assignment, let me know.

Assignment -- Convert Demi Blessings to a simple ajax link send.
Demi Blessings are still using using the old NavigateTo format, including navigating through the DOM to the right picture. Now that we can find out what the URL is, replace the old code using an caap.ajaxLink() function call to send the right URL to directly get your demi blessing. Normally, I would use caap.navigate3 to check the blessing page first, but blessings are low risk, and you can use caap.ajaxLink() to send the URL directly if it's time.
Bonus Points - move the Demi Blessings code out of caap_base.js and into worker_stats.js, where it belongs.

Assignment -- Clean up Land functions
Move the Land functions out of caap_base.js to worker_stats.js and move the code from caap.checkResults_land() into statsFunc.checkResults().

Assignment -- Liberate pages
Make a new worker called pages, and move the caap.pageList out of caap_base.js and into the pages worker. Also, make easy to use page. variables, including page.current for the current page, page.url for the lastClick, and page.results for the results text from the current page. The page check functions from worker_chores.js should also be moved into this function. Replace the existing state.getItem('page', ''), etc. functions with these new variables.
Artifice
Site Admin
Site Admin
 
Posts: 267
Joined: 23 Nov 2013, 07:47
Has thanked: 40 time
Have thanks: 191 time

Re: Artifice's CAAP Coding Classroom

Postby xaman » 10 Jan 2016, 19:35

Thanks for this thread Artifice! Very interesting for those who are getting into caap programming!
xaman
Knight
Knight
 
Posts: 138
Joined: 20 Jun 2014, 18:05
Has thanked: 26 time
Have thanks: 28 time


Return to Tutorials and Guides

Who is online

Users browsing this forum: No registered users and 1 guest

cron
suspicion-preferred