Guidlines

DeletedUser

Guest
Where is the best place to find coding info for writing scripts. I have no problems with JS, but need to know if there are specific objects available etc.
 

DeletedUser

Guest
I think most modern browsers have some built-in developer tools. Right click -> inspect element. Or in internet explorer press f12.


Sometimes the code is a pain to read. For this you can use a "beautifier". They'll turn something like this:
Code:
var game_data = {"player":{"name":"cheesasaurus","ally":"0","sitter":"0","sleep_start":"0","sitter_type":"normal","sleep_end":"0","sleep_last":"0","interstitial":"0","email_valid":"1","villages":"1","incomings":"0","supports":0,"knight_location":null,"knight_unit":null,"rank":13438,"points":"36","date_started":"1393160177","is_guest":"0","id":"10536","quest_progress":"0","premium":false,"account_manager":false,"farm_manager":false,"points_formatted":"36","rank_formatted":"13<span class=\"grey\">.<\/span>438","fire_pixel":"0","new_forum_post":"0","new_igm":"1","new_report":"0","new_quest":"1"},"village":{"id":25068,"name":"cheesasauruss village","wood_prod":0.001388888889,"stone_prod":0.001388888889,"iron_prod":0.001388888889,"storage_max":"1000","pop_max":"240","wood_float":435.00624016954,"stone_float":440.00624016954,"iron_float":370.00624016954,"wood":435,"stone":440,"iron":370,"pop":"27","x":"552","y":"637","trader_away":"0","bonus_id":null,"bonus":null,"buildings":{"village":"25068","main":"1","farm":"1","storage":"1","place":"1","barracks":"0","church":"0","church_f":"1","smith":"0","wood":"0","stone":"0","iron":"0","market":"0","stable":"0","wall":"0","garage":"0","hide":"1","snob":"0","statue":"0"},"player_id":"10536","res":[435,0.001388888889,440,0.001388888889,370,0.001388888889,"1000","27","240"],"coord":"552|637"},"nav":{"parent":2},"link_base":"\/game.php?village=25068&amp;screen=","link_base_pure":"\/game.php?village=25068&screen=","csrf":"d289","world":"us14","market":"us","RTL":false,"version":"19798 8.19","majorVersion":"8.19","screen":"mail","mode":null,"device":"desktop"};
into this:
Code:
var game_data = {
    "player": {
        "name": "cheesasaurus",
        "ally": "0",
        "sitter": "0",
        "sleep_start": "0",
        "sitter_type": "normal",
        "sleep_end": "0",
        "sleep_last": "0",
        "interstitial": "0",
        "email_valid": "1",
        "villages": "1",
        "incomings": "0",
        "supports": 0,
        "knight_location": null,
        "knight_unit": null,
        "rank": 13438,
        "points": "36",
        "date_started": "1393160177",
        "is_guest": "0",
        "id": "10536",
        "quest_progress": "0",
        "premium": false,
        "account_manager": false,
        "farm_manager": false,
        "points_formatted": "36",
        "rank_formatted": "13<span class=\"grey\">.<\/span>438",
        "fire_pixel": "0",
        "new_forum_post": "0",
        "new_igm": "1",
        "new_report": "0",
        "new_quest": "1"
    },
    "village": {
        "id": 25068,
        "name": "cheesasauruss village",
        "wood_prod": 0.001388888889,
        "stone_prod": 0.001388888889,
        "iron_prod": 0.001388888889,
        "storage_max": "1000",
        "pop_max": "240",
        "wood_float": 435.00624016954,
        "stone_float": 440.00624016954,
        "iron_float": 370.00624016954,
        "wood": 435,
        "stone": 440,
        "iron": 370,
        "pop": "27",
        "x": "552",
        "y": "637",
        "trader_away": "0",
        "bonus_id": null,
        "bonus": null,
        "buildings": {
            "village": "25068",
            "main": "1",
            "farm": "1",
            "storage": "1",
            "place": "1",
            "barracks": "0",
            "church": "0",
            "church_f": "1",
            "smith": "0",
            "wood": "0",
            "stone": "0",
            "iron": "0",
            "market": "0",
            "stable": "0",
            "wall": "0",
            "garage": "0",
            "hide": "1",
            "snob": "0",
            "statue": "0"
        },
        "player_id": "10536",
        "res": [435, 0.001388888889, 440, 0.001388888889, 370, 0.001388888889, "1000", "27", "240"],
        "coord": "552|637"
    },
    "nav": {
        "parent": 2
    },
    "link_base": "\/game.php?village=25068&amp;screen=",
    "link_base_pure": "\/game.php?village=25068&screen=",
    "csrf": "d289",
    "world": "us14",
    "market": "us",
    "RTL": false,
    "version": "19798 8.19",
    "majorVersion": "8.19",
    "screen": "mail",
    "mode": null,
    "device": "desktop"
};

World settings can be grabbed via ajax from xml files.
/interface.php?func=get_config
/interface.php?func=get_building_info
/interface.php?func=get_unit_info

example (world 14):
http://us14.tribalwars.us/interface.php?func=get_config


Regular expressions are nice for getting coordinates out of a village name.
example:
Code:
var my_string = "cheesasauruss village (552|637) K65";
var my_pattern = /[0-9]{1,}\|[0-9]{1,}/gi;
var my_coords = my_string.match(my_pattern).pop();
my_coords will be the string "552|637"


Tribal Wars uses jquery, which can make scraping data off the page much more pleasant.

example: get defending troops out of a battle report

1) check the source HTML:
Code:
<table id="attack_info_def_units" class="vis" style="border-collapse:collapse">
    <tbody>
        <tr class="center">
            <td></td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_spear.png?48b3b" title="Spear fighter" alt="" class="">
            </td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_sword.png?b389d" title="Swordsman" alt="" class="">
            </td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_axe.png?51d94" title="Axeman" alt="" class="">
            </td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_archer.png?db2c3" title="Archer" alt="" class="">
            </td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_spy.png?eb866" title="Scout" alt="" class="">
            </td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_light.png?2d86d" title="Light cavalry" alt="" class="">
            </td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_marcher.png?ad3be" title="Mounted archer" alt="" class="">
            </td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_heavy.png?a83c9" title="Heavy cavalry" alt="" class="">
            </td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_ram.png?2003e" title="Ram" alt="" class="">
            </td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_catapult.png?5659c" title="Catapult" alt="" class="">
            </td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_knight.png?58dd0" title="Paladin" alt="" class="">
            </td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_snob.png?0019c" title="Nobleman" alt="" class="">
            </td>
            <td width="35">
                <img src="http://cdn.tribalwars.net/8.19/19798/graphic/unit/unit_militia.png?ff93f" title="Militia" alt="" class="">
            </td>
        </tr>
        <tr>
            <td width="20%">Quantity:</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
        </tr>
        <tr>
            <td align="left" width="20%">Losses:</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
            <td style="text-align:center" class="unit-item hidden">0</td>
        </tr>
    </tbody>
</table>
2) write the script:
Code:
// for each image in the first row of the defender table, find the unit's name from the source. Then add it to the list of the names.
var unit_names = new Array();
$('#attack_info_def_units tr:eq(0)').find('img').each(function(key, object) {
	unit_names.push(object.src.match(/unit_(.*?)(?=.png)/)[0].replace('unit_',''));
});

// for each descendant of the second row of the defender table, with the class "unit-item", the contents represent the amount of a specific type of troop defending.
// add these numbers to a list
var quantity = new Array();
$('#attack_info_def_units tr:eq(1)').find('.unit-item').each(function(key, object) {
	quantity.push(object.innerHTML);
});

// similar thing for the third row of the defender table
var losses = new Array();
$('#attack_info_def_units tr:eq(2)').find('.unit-item').each(function(key, object) {
	losses.push(object.innerHTML);
});

// we could put the information into one simple list
var units = new Array();
for (key in unit_names) {
	var unit = {
		'name': unit_names[key],
		'quantity': quantity[key],
		'losses': losses[key]
	};
	units[key] = unit;
	
	// or an object
	defender.setUnit(unit.name, unit); // just an example. You would need to define a defender object first, with method setUnit
	
	// object alternative (after all, it's just an associative array)
	defender.units[unit.name] = unit; //another example
};

TLDR
there's no useful documentation. check the source and build your own objects as needed
 
Top