ika-core.org Ikariam Tools

Next generation Ikariam search and addons script

当前为 2015-02-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ika-core.org Ikariam Tools
  3. // @namespace ika-core
  4. // @description Next generation Ikariam search and addons script
  5. // @license Creative Commons Attribution License
  6. // $icon http://www.ika-core.org/git/icon48.png
  7. // @include http://s*.ikariam.gameforge.com/*
  8. // @include http://m*.ikariam.gameforge.com/*
  9. // @exclude http://support.*.ikariam.gameforge.com/*
  10. // @run-at document-start
  11. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js
  12. // @resource css http://www.ika-core.org/git/ika-core.css
  13. // @resource lic http://www.ika-core.org/git/licencemsg.txt
  14. // @version 1.13
  15. // @grant GM_addStyle
  16. // @grant GM_getValue
  17. // @grant GM_setValue
  18. // @grant GM_getResourceText
  19. // @grant GM_openInTab
  20. // @grant GM_xmlhttpRequest
  21. // ==/UserScript==
  22. this.$ = this.jQuery = jQuery.noConflict(true);
  23. var nlang = (navigator.language) ? navigator.language : navigator.userLanguage;
  24. var ufo = unsafeWindow,
  25. game, log, ikariam, screen, server, player, ally, selectedcity, island, islandx, islandy, realHour, nmb, resman, worlddraw, worldmap, transload = false,
  26. dbg = '',
  27. lang, hours = 36e5,
  28. minutes = 60000,
  29. seconds = 1000,
  30. COMPRESS = true;
  31. var goods = {
  32. 0: '<img src="skin/resources/icon_wood.png"/>',
  33. 1: '<img src="skin/resources/icon_wine.png"/>',
  34. 2: '<img src="skin/resources/icon_marble.png"/>',
  35. 3: '<img src="skin/resources/icon_glass.png"/>',
  36. 4: '<img src="skin/resources/icon_sulfur.png"/>'
  37. };
  38. var buildings = ["townHall", "shipyard", "port", "glassblowing", "branchOffice", "barracks", "academy", "warehouse", "palace", "palaceColony", "tavern", "museum", "workshop", "wall", "dump", "vineyard", "pirateFortress", "safehouse", "embassy", "stonemason", "carpentering", "forester", "winegrower", "alchemist", "optician", "fireworker", "architect", "temple", "blackMarket", "marineChartArchive"];
  39. String.prototype.strip = function() {
  40. return this.replace(/<.*?[^>]>/g, '');
  41. };
  42. String.prototype.digits = function() {
  43. var d = this.replace(/\D/g, '');
  44. if (d == '') return 0;
  45. return d;
  46. };
  47. String.prototype.toJSON = function() {
  48. try {
  49. return JSON.parse(str);
  50. } catch (e) {
  51. return false;
  52. }
  53. };
  54. if (dbg == 'y') {
  55. var deb = ufo.console.log.bind(console);
  56. } else {
  57. deb = function() {};
  58. }
  59. exportFunction(parser, unsafeWindow.console, {
  60. defineAs: "log"
  61. });
  62. document.addEventListener('beforescriptexecute', function(e) {
  63. if (e.target.innerHTML.search(/console\s=/) != -1) {
  64. e.stopPropagation();
  65. e.preventDefault();
  66. window.removeEventListener(e.type, arguments.callee, true);
  67. }
  68. }, true);
  69. document.onreadystatechange = function() {
  70. if (document.readyState == "complete") {
  71. main();
  72. GM_addStyle(GM_getResourceText('css'));
  73. }
  74. };
  75.  
  76. function toJSON(str) {
  77. try {
  78. return JSON.parse(str);
  79. } catch (e) {
  80. return false;
  81. }
  82. }
  83.  
  84.  
  85. function UTF8ArrToStr (aBytes) {
  86.  
  87. var sView = "";
  88.  
  89. for (var nPart, nLen = aBytes.length, nIdx = 0; nIdx < nLen; nIdx++) {
  90. nPart = aBytes[nIdx];
  91. sView += String.fromCharCode(
  92. nPart > 251 && nPart < 254 && nIdx + 5 < nLen ? /* six bytes */
  93. /* (nPart - 252 << 30) may be not so safe in ECMAScript! So...: */
  94. (nPart - 252) * 1073741824 + (aBytes[++nIdx] - 128 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
  95. : nPart > 247 && nPart < 252 && nIdx + 4 < nLen ? /* five bytes */
  96. (nPart - 248 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
  97. : nPart > 239 && nPart < 248 && nIdx + 3 < nLen ? /* four bytes */
  98. (nPart - 240 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
  99. : nPart > 223 && nPart < 240 && nIdx + 2 < nLen ? /* three bytes */
  100. (nPart - 224 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
  101. : nPart > 191 && nPart < 224 && nIdx + 1 < nLen ? /* two bytes */
  102. (nPart - 192 << 6) + aBytes[++nIdx] - 128
  103. : /* nPart < 127 ? */ /* one byte */
  104. nPart
  105. );
  106. }
  107. return sView;
  108.  
  109. }
  110.  
  111. function strToUTF8Arr (sDOMStr) {
  112. var aBytes, nChr, nStrLen = sDOMStr.length, nArrLen = 0;
  113. for (var nMapIdx = 0; nMapIdx < nStrLen; nMapIdx++) {
  114. nChr = sDOMStr.charCodeAt(nMapIdx);
  115. nArrLen += nChr < 0x80 ? 1 : nChr < 0x800 ? 2 : nChr < 0x10000 ? 3 : nChr < 0x200000 ? 4 : nChr < 0x4000000 ? 5 : 6;
  116. }
  117. aBytes = new Uint8Array(nArrLen);
  118. for (var nIdx = 0, nChrIdx = 0; nIdx < nArrLen; nChrIdx++) {
  119. nChr = sDOMStr.charCodeAt(nChrIdx);
  120. if (nChr < 128) {
  121. /* one byte */
  122. aBytes[nIdx++] = nChr;
  123. } else if (nChr < 0x800) {
  124. /* two bytes */
  125. aBytes[nIdx++] = 192 + (nChr >>> 6);
  126. aBytes[nIdx++] = 128 + (nChr & 63);
  127. } else if (nChr < 0x10000) {
  128. /* three bytes */
  129. aBytes[nIdx++] = 224 + (nChr >>> 12);
  130. aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
  131. aBytes[nIdx++] = 128 + (nChr & 63);
  132. } else if (nChr < 0x200000) {
  133. /* four bytes */
  134. aBytes[nIdx++] = 240 + (nChr >>> 18);
  135. aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
  136. aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
  137. aBytes[nIdx++] = 128 + (nChr & 63);
  138. } else if (nChr < 0x4000000) {
  139. /* five bytes */
  140. aBytes[nIdx++] = 248 + (nChr >>> 24);
  141. aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
  142. aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
  143. aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
  144. aBytes[nIdx++] = 128 + (nChr & 63);
  145. } else /* if (nChr <= 0x7fffffff) */ {
  146. /* six bytes */
  147. aBytes[nIdx++] = 252 + (nChr >>> 30);
  148. aBytes[nIdx++] = 128 + (nChr >>> 24 & 63);
  149. aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
  150. aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
  151. aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
  152. aBytes[nIdx++] = 128 + (nChr & 63);
  153. }
  154. }
  155. return aBytes;
  156. }
  157.  
  158. function cachetokenizer(t) {
  159. return player + '_' + t;
  160. }
  161.  
  162. function cachefetch(obj, ttl, compress) {
  163. try {
  164. var cache;
  165. if (typeof compress === 'undefined') {
  166. cache = toJSON(localStorage.getItem(cachetokenizer(obj)));
  167. } else {
  168. cache = toJSON(localStorage.getItem(cachetokenizer(obj)));
  169. }
  170. if (cache) {
  171. if (typeof ttl === 'undefined') {
  172. return cache[1];
  173. }
  174. var milisecs = Math.abs(new Date(cache[0]) - new Date());
  175. if (milisecs > ttl) {
  176. return false;
  177. } else {
  178. return cache[1];
  179. }
  180. } else {
  181. localStorage.removeItem(obj);
  182. return false;
  183. }
  184. } catch (e) {
  185. deb('cache-f:' + obj + ' error:' + e);
  186. localStorage.removeItem(obj);
  187. };
  188. }
  189.  
  190. function cacheset(obj, data, compress) {
  191. try {
  192. var ar = [new Date().toJSON(), data];
  193. if (typeof compress === 'undefined') {
  194. localStorage.setItem(cachetokenizer(obj), JSON.stringify(ar));
  195. } else {
  196. localStorage.setItem(cachetokenizer(obj), JSON.stringify(ar));
  197. }
  198. } catch (e) {
  199. deb('cache-s:' + obj + ' error:' + e);
  200. };
  201. }
  202. function cachedel(obj){
  203. localStorage.removeItem(cachetokenizer(obj));
  204. }
  205. function view(name, title, content, oversize) {
  206. var tobj = {
  207. "boxId": name,
  208. "headerElem": title,
  209. "contentElem": content,
  210. "sidebarEls": {
  211. "0": {},
  212. "length": 1,
  213. "prevObject": {
  214. "0": {},
  215. "context": {},
  216. "length": 1
  217. },
  218. "context": {},
  219. "selector": "div.dynamic"
  220. },
  221. "oversized": true,
  222. "replaceBox": true,
  223. "keepSidebars": true,
  224. "minisized": false
  225. };
  226. ufo.tobj = cloneInto(tobj, ufo);
  227. ikariam.createTemplateView(ufo.tobj);
  228. }
  229.  
  230. function viewhandler(s) {
  231. if (!s.hasOwnProperty("ici")) {
  232. if (jQuery.inArray(s["boxId"], buildings) !== -1) {
  233. s["sidebarEls"]["ici"] = s["boxId"];
  234. }
  235. }
  236. }
  237.  
  238. function bar(g, mg) {
  239. var w = g * 100 / mg;
  240. return '<div class="barBg" title="' + nmb(w) + '%"><div class="ikbar" style="width:' + w + '%"></div></div>';
  241. }
  242.  
  243. function CreateSlot(menu, n, bg, bgl, name, desc, fn) {
  244. menu.append('<li class="expandable slot' + n + '" style="display: inline-block; width: 53px;" onclick=""><div class="image image_friends" style="background:url(' + bg + ') repeat scroll ' + bgl + ' transparent;height:34px;width:32px;"></div><div class="name"><span class="namebox">' + name + '</br><small>' + desc + '</small></span></div></li>')
  245. .find('.slot' + n)
  246. .hover(function() {
  247. $(this).animate({
  248. width: "199px"
  249. }, 300, "swing").parent().parent().css('z-index', '120000');
  250. }, function() {
  251. $(this).animate({
  252. width: "53px"
  253. }, 300, "swing").parent().parent().css('z-index', '65');;
  254. }).click(fn);
  255. }
  256.  
  257. function loadcitybuildings() {
  258. var l = [];
  259. var d = cachefetch('cities');
  260. for (k in d) {
  261. if (d[k].hasOwnProperty("relationship")) {
  262. if (d[k]["relationship"] == "ownCity") {
  263. l.push(cachefetch(k + '_buildings'));
  264. }
  265. }
  266. }
  267. return l;
  268. }
  269.  
  270. function parser(s) {
  271. try {
  272. if (typeof s == "object") {
  273. game = s;
  274. datahandler();
  275. if (s.hasOwnProperty("boxId")) {
  276. viewhandler(s);
  277. }
  278. if (s.hasOwnProperty("ici")) {
  279. deb("fetch:" + s["ici"]);
  280. resman = s["ici"];
  281. }
  282. if ((s.hasOwnProperty("link") && resman) || (s.hasOwnProperty("selectedCity") && resman)) {
  283. var r = $('#sidebar .resources');
  284. if (r.length) {
  285. var wood = r.find('.wood').contents(':not(span)').text().digits();
  286. var wine = r.find('.wine').contents(':not(span)').text().digits();
  287. var marble = r.find('.marble').contents(':not(span)').text().digits();
  288. var glass = r.find('.glass').contents(':not(span)').text().digits();
  289. var sulfur = r.find('.sulfur').contents(':not(span)').text().digits();
  290. var time = r.find('.time').contents(':not(span)').text();
  291. var level = $('#sidebar .actions .showLevel').contents(':not(span)').text().digits();
  292. var bres = {
  293. "0": wood,
  294. "1": wine,
  295. "2": marble,
  296. "3": glass,
  297. "4": sulfur,
  298. "time": time,
  299. "level": level
  300. };
  301. cacheset(selectedcity + '_br_' + resman, bres);
  302. deb("fetch-for-" + resman + "_level" + level.digits() + ":" + JSON.stringify(bres));
  303. }
  304. }
  305. if (s.hasOwnProperty("contentElem")) {
  306. var obj = s["contentElem"];
  307. s["contentElem"] = '';
  308. if (dbg == 'y') {
  309. deb("c:" + JSON.stringify(s));
  310. };
  311. s["contentElem"] = obj;
  312. } else {
  313. if (dbg == 'y') {
  314. deb("o:" + JSON.stringify(s));
  315. };
  316. }
  317. } else {
  318. deb(s);
  319. }
  320. } catch (e) {
  321. deb('parser:' + e);
  322. }
  323. }
  324.  
  325. function datahandler() {
  326. try {
  327. var gm = game;
  328. if (gm.hasOwnProperty('backgroundView')) {
  329. screen = gm['backgroundView'];
  330. deb("view:" + screen);
  331. }
  332. if (gm.hasOwnProperty('gameName')) {
  333. deb("player:" + gm['avatarId']);
  334. player = gm['avatarId'];
  335. deb("ally:" + gm['avatarAllyId']);
  336. ally = gm['avatarAllyId'];
  337. realHour = gm['realHour'];
  338. }
  339. if (gm.hasOwnProperty('serverName')) {
  340. deb("server:" + gm['serverName']);
  341. deb("lang:" + ufo.LocalizationStrings.language);
  342. server = gm['serverName'];
  343. lang = ufo.LocalizationStrings.language;
  344. }
  345. if (gm.hasOwnProperty("bgViewData")) {
  346. deb("island:" + gm['bgViewData']['currentIslandId']);
  347. island = gm['bgViewData']['currentIslandId'];
  348. }
  349. if (gm.hasOwnProperty("islandId")) {
  350. deb("island2:" + gm['islandId']);
  351. island = gm['islandId'];
  352. }
  353. if (gm.hasOwnProperty("islandXCoord")) {
  354. deb("island2x:" + gm['islandXCoord']);
  355. deb("island2y:" + gm['islandYCoord']);
  356. islandx = gm['islandXCoord'];
  357. islandx = gm['islandYCoord'];
  358. }
  359.  
  360. //city list
  361. if (gm.hasOwnProperty("relatedCityData")) {
  362. //deb("citylist:" + JSON.stringify(gm['relatedCityData']));
  363. selectedcity = gm['relatedCityData']['selectedCity'];
  364. deb("selectedcity:" + selectedcity);
  365. cacheset('cities', gm['relatedCityData']);
  366. }
  367. if (gm.hasOwnProperty("cityDropdownMenu")) {
  368. //deb("citylist-dropdown:" + JSON.stringify(gm['cityDropdownMenu']));
  369. selectedcity = gm['cityDropdownMenu']['selectedCity'];
  370. deb("selectedcity:" + selectedcity);
  371. cacheset('cities', gm['cityDropdownMenu']);
  372. }
  373. if (gm.hasOwnProperty("isOwnCity") || gm.hasOwnProperty("relatedCity")) {
  374. var cityres = {
  375. 'currentResources': gm['currentResources'],
  376. 'maxResources': gm['maxResources'],
  377. 'resourceProduction': gm['resourceProduction'],
  378. 'tradegoodProduction': gm['tradegoodProduction'],
  379. 'wineSpendings': gm['wineSpendings'],
  380. 'wineTickInterval': gm['wineTickInterval'],
  381. 'producedTradegood': gm['producedTradegood'],
  382. 'dt': new Date().getTime()
  383. };
  384. cacheset(selectedcity + '_prod', cityres);
  385. if (transload) transporterload(true);
  386. }
  387. if (gm.hasOwnProperty("relatedCity")) {
  388. if (gm["relatedCity"].hasOwnProperty("owncity")) {
  389. if (gm["relatedCity"]["owncity"] == 1) {
  390. var cityres = {
  391. 'currentResources': gm['currentResources'],
  392. 'maxResources': gm['maxResources'],
  393. 'resourceProduction': gm['resourceProduction'],
  394. 'tradegoodProduction': gm['tradegoodProduction'],
  395. 'wineSpendings': gm['wineSpendings'],
  396. 'wineTickInterval': gm['wineTickInterval'],
  397. 'producedTradegood': gm['producedTradegood'],
  398. 'dt': new Date().getTime()
  399. };
  400. //deb(player+'_'+selectedcity+'_prod:'+JSON.stringify(cityres));
  401. cacheset(selectedcity + '_prod', cityres);
  402. if (transload) transporterload(true);
  403. }
  404. }
  405. }
  406. if (gm.hasOwnProperty("backgroundData")) {
  407. cacheset('city_' + gm['backgroundData']['id'] + '_buildings', gm['backgroundData']);
  408. rendercity();
  409. }
  410. if (screen == 'city' && gm.hasOwnProperty("phase") && gm["ownerId"] == player) {
  411. cacheset('city_' + gm['id'] + '_buildings', gm);
  412. rendercity();
  413. }
  414. if (screen == 'island' && gm.hasOwnProperty("cities")) {
  415. cacheset('island_' + gm['id'] + '', gm);
  416. renderisland();
  417. }
  418. } catch (e) {
  419. deb('datahandler:' + e);
  420. }
  421. }
  422.  
  423. function renderisland() {
  424. var m = $('#cities');
  425. var ic = cachefetch('island_' + island);
  426. if (ic) {
  427. ic = ic['cities'];
  428. for (var k = 0; k < ic.length; k++) {
  429. if (ic[k]["type"] == "city") {
  430. var b = m.find('#cityLocation' + k + ' a.island_feature_img');
  431. b.find('.blevel').remove();
  432. b.append('<div class="blevel">' + ic[k]['level'] + '</div>');
  433. var b = m.find('#cityLocation' + k + 'Scroll div.center');
  434. b.find('.infotip').remove();
  435. b.append('<span class="infotip">' + ic[k]['ownerName'] + '</span>');
  436. }
  437. }
  438. }
  439. }
  440.  
  441. function rendercity() {
  442. var lc = cachefetch(selectedcity + '_buildings');
  443. if (lc) {
  444. var cr = cachefetch(selectedcity + '_prod');
  445. if (cr.hasOwnProperty('currentResources')) {
  446. cr=cr['currentResources'];
  447. cr[0]=cr['resource'];
  448. } else {
  449. cr=false;
  450. }
  451. var lcp = lc['position'];
  452. var m = $('#locations');
  453. var bl = loadcitybuildings();
  454. for (var k = 0; k < lcp.length; k++) {
  455. var p = m.find('#position' + k);
  456. p.find('.bname,.blevel,.linfo,.binfo').remove();
  457. var cls = '';
  458. if (lcp[k].hasOwnProperty("name")) {
  459. if (cr) {
  460. var res = cachefetch(selectedcity + '_br_' + lcp[k]['building']);
  461. if (res) {
  462. var dc=true;
  463. for (var j=0;j<5;j++){
  464. if (res[j] - cr[j] > 0) {
  465. dc=false;
  466. break;
  467. }
  468. }
  469. if (dc) {cls = 'bgood';}
  470. } else {
  471. cls = 'bdark';
  472. }
  473. }
  474. p.append('<div class="bname" data="' + lcp[k]['level'] + '">' + lcp[k]['name'] + '</div><div class="blevel ' + cls + '" data="' + lcp[k]['level'] + '">' + lcp[k]['level'] + '</div><div data="' + lcp[k]['building'].replace(' constructionSite', '') + '" class="linfo"></div><div class="binfo" data="' + lcp[k]['building'].replace(' constructionSite', '') + '"></div>');
  475. p.find('.bname').click(function() {
  476. $(this).parent().find('a').click();
  477. });
  478. }
  479. }
  480. m.find('.blevel').click(function() {
  481. $(this).parent().find('.hoverable').click();
  482. }).hover(function() {
  483. var p = $(this).parent().css('z-index', 10000);
  484.  
  485. //binfo - resource info for building upgrade
  486. var g = p.find('.binfo');
  487. var bres = cachefetch(selectedcity + '_br_' + g.attr('data'));
  488. if (bres) {
  489. p.find('.bdark').removeClass('bdark');
  490. var gc=[];
  491. if (cr) {
  492. for (var j=0;j<5;j++){
  493. if (bres[j] - cr[j] > 0) {
  494. gc[j] = true;
  495. }
  496. }
  497. }
  498. var ht = (bres[0] ? '<span '+(gc[0]?'style="color:red"':'')+'>' + goods[0] + ' ' + nmb(bres[0]) + '</span>' : '') +
  499. (bres[1] ? '<span '+(gc[1]?'style="color:red"':'')+'>' + goods[1] + ' ' + nmb(bres[1]) + '</span>' : '') +
  500. (bres[2] ? '<span '+(gc[2]?'style="color:red"':'')+'>' + goods[2] + ' ' + nmb(bres[2]) + '</span>' : '') +
  501. (bres[3] ? '<span '+(gc[3]?'style="color:red"':'')+'>' + goods[3] + ' ' + nmb(bres[3]) + '</span>' : '') +
  502. (bres[4] ? '<span '+(gc[4]?'style="color:red"':'')+'>' + goods[4] + ' ' + nmb(bres[4]) + '</span>' : '') +
  503. '<img src="skin/resources/icon_time.png" width="10px"/>'+bres['time'];
  504. g.html(ufo.BubbleTips.createTooltip(ht, '100px'));
  505. } else{
  506. g.html(ufo.BubbleTips.createTooltip('<span>click on building to fetch needed resources</span>', '100px'));
  507. }
  508. // others cities same buildings
  509. var e = p.find('.linfo');
  510. var bname = e.attr('data');
  511. if (bname == 'palace') bname = 'palaceColony';
  512. if (bname != 'fetched') {
  513. e.attr('data', 'fetched');
  514. var txt = '';
  515. try{
  516. for (var b = 0; b < bl.length; b++) {
  517. var cnm = bl[b]['name'];
  518. if (cnm != lc['name']) {
  519. if (bl[b].hasOwnProperty('position')){
  520. var bp = bl[b]['position'];
  521. for (var p = 0; p < bp.length; p++) {
  522. var lbname = bp[p]['building'];
  523. var hl = '';
  524. if (lbname.indexOf('constructionSite') > 0) {
  525. lbname = lbname.replace(' constructionSite', '');
  526. hl = ' class="red"';
  527. } else {
  528. hl = '';
  529. }
  530. if (lbname == 'palace') lbname = 'palaceColony';
  531. if (lbname == bname) {
  532. txt += '<span ' + hl + '>' + cnm + ' ' + bp[p]['level'] + '</span>';
  533. }
  534. }
  535. }
  536. }
  537. }
  538. } catch(e){deb('linfo hover:'+e);}
  539. if (txt.length==0){txt='<span>Not found in any other city</span>';}
  540. e.html(ufo.BubbleTips.createTooltip('<span style="border-bottom:1px solid #e0b018"><b>'+e.parent().find('.bname').html()+'</b></span>'+txt, '100px'));
  541. }
  542. if (e.html()) e.show();
  543. if (g.html()) g.show();
  544. }, function() {
  545. $(this).parent().css('z-index', '').find('.linfo,.binfo').hide();
  546. });
  547. }
  548. }
  549.  
  550. function searchview() {
  551. view("searchika", "Search for players", '<iframe name="isearch" src="http://www.ika-core.org/" frameborder="0" scrolling="auto" marginwidth="0" marginheight="0" width="100%" height="100%"></iframe>', true);
  552. }
  553.  
  554. function buildingsview() {
  555. try{
  556. var bp, o, i, j, busy, link;
  557. var bl = loadcitybuildings();
  558. var s = '<table class=\"table01 left clearfloat\" style=\"width:100%;margin:0;\"><tbody><tr>\
  559. <th width=\"20%\"><img src=\"skin/icons/livingspace_24x24.png\"\/><\/th>';
  560. for (i = 0; i < buildings.length; i++) {
  561. s += '<th width=\"3%\" title=\"' + buildings[i] + '\"><img src=\"skin/buildings/x40_y40/' + buildings[i] + '.png\" width=\"40\"/><\/th>';
  562. }
  563. s += '<\/tr>';
  564. var alt = '';
  565. //s+=JSON.stringify(bl);
  566. for (i = 0; i < bl.length; i++) {
  567. if (bl[i].hasOwnProperty('position')){
  568. bp = bl[i]['position'];
  569. o = [];
  570. busy = [];
  571. link = [];
  572. for (j = 0; j < bp.length; j++) {
  573. var ind = jQuery.inArray(bp[j]['building'].replace(' constructionSite', ''), buildings);
  574. o[ind] = bp[j]['level'];
  575. busy[ind] = (bp[j]['building'].indexOf('constructionSite') > 0 ? true : false);
  576. link[ind] = '?view=' + bp[j]['building'].replace(' constructionSite', '') + '&cityId=' + bl[i]['id'] + '&position=' + j;
  577. }
  578. s += '<tr class="' + alt + '"><td title=\"' + bl[i]['name'] + '\">' + bl[i]['name'] + '<\/td>';
  579. for (j = 0; j < buildings.length; j++) {
  580. if (o[j]) {
  581. s += '<td title=\"' + buildings[j] + (busy[j] ? ' upgrading' : '') + ' Level: ' + o[j] + '\" class=\"point right' + (busy[j] ? ' red bold blink' : '') + '\" onclick=\"ajaxHandlerCall(\'' + link[j] + '\');return false;\">' + o[j] + '<\/td>';
  582. } else {
  583. s += '<td title=\"' + buildings[j] + ' Does not exist' + '\" class=\"right\">-<\/td>';
  584. }
  585. }
  586. s += '<\/tr>';
  587. if (alt == '') {
  588. alt = 'alt';
  589. } else {
  590. alt = '';
  591. };
  592. }
  593. }
  594. s += '<\/tbody><\/table><iframe src="http://www.ika-core.org/ikariam-new.html" frameborder="0" scrolling="auto" marginwidth="0" marginheight="0" width="100%" height="115px"></iframe>';
  595. view("buildingsika", "Buildings View", s, true);
  596. } catch(e){deb('buildingsview:'+e);}
  597. }
  598.  
  599. function mapislands(data) {
  600. var islands = [];
  601. var l = data.data;
  602. alert(JSON.stringify(l));
  603. var q = data.request;
  604. for (var m = q.x_min; m <= q.x_max; m++) {
  605. for (var o = q.y_min; o <= q.y_max; o++) {
  606. if (!islands[m]) {
  607. islands[m] = new Array();
  608. }
  609. if (l[m] && l[m][o]) {
  610. islands[m][o] = l[m][o];
  611. }
  612. //else { islands[m][o] = "ocean"; }
  613. }
  614. }
  615. alert(JSON.stringify(islands));
  616. return islands;
  617. }
  618.  
  619. function worldviewprepare() {
  620. dumpcachesize('worldislands');
  621. var cache = cachefetch('worldislands', 48 * hours, COMPRESS);
  622. if (cache) {
  623. worldview(cache);
  624. } else {
  625. $.getJSON("/", "action=WorldMap&function=getJSONArea&x_min=0&x_max=101&y_min=0&y_max=101", function(data) {
  626. try{
  627. //var islands=mapislands(data);
  628. var islands=data.data;
  629. cacheset('worldislands', islands, COMPRESS);
  630. worldview(islands);
  631. } catch(e){deb('fetch world:'+e);}
  632. });
  633. }
  634. }
  635.  
  636. function worldview(i) {
  637. var tg = [ufo.LocalizationStrings.wood, ufo.LocalizationStrings.wine, ufo.LocalizationStrings.marble, ufo.LocalizationStrings.crystal, ufo.LocalizationStrings.sulfur];
  638. var s = '<Table><tr><td><table id="worldmap_2d">',
  639. x, y;
  640. for (y = 0; y < 102; y++) {
  641. s += '<tr>';
  642. for (x = 0; x < 102; x++) {
  643. var c=i[x];
  644. if(c){
  645. var ci = c[y];
  646. if (ci) {
  647. s += '<td id="is' + ci[0] + '" class="isle ic_' + x + '_' + y + '" title="' + ci[1] + '[' + x + ':' + y + '] ' + tg[0] + ':' + ci[6] + ' ' + tg[ci[2]] + '"></td>';
  648. } else {
  649. s += '<td class="ocean"></td>';
  650. }
  651. } else {
  652. s += '<td class="ocean"></td>';
  653. }
  654. }
  655. s += '</tr>';
  656. }
  657. s += '</table></td><td>&nbsp;&nbsp;&nbsp;World search will be added here, its almost complete</td></tr></table>';
  658. view("worldika", "World Map", s, false);
  659. var d = cachefetch('cities');
  660. for (k in d) {
  661. if (d[k].hasOwnProperty("relationship")) {
  662. if (d[k]["relationship"] == "ownCity") {}
  663. var t = /(\d{1,2}:\d{1,2})/img.exec(d[k]["coords"])[0].split(':');
  664. var x = t[0];
  665. var y = t[1];
  666. var f = $('#worldmap_2d .ic_' + x + '_' + y);
  667. f.addClass('isleown');
  668. f.attr('title', f.attr('title') + ' ' + d[k]['name']);
  669. }
  670. }
  671. }
  672.  
  673. function transporterload(force) {
  674. t = $('#iTrans');
  675. if (t.is(':hidden') == 'true') return;
  676. if (!transload || force) {
  677. transload = true;
  678. var s = '<table class="table01 left clearfloat" style="width:100%;margin:0;"><tbody><tr>\
  679. <th width="20%"><img src="skin/icons/livingspace_24x24.png"/></th>\
  680. <th width="5%"><img src="skin/resources/icon_population.png"/></th>\
  681. <th width="12%">' + goods[0] + '</th>\
  682. <th width="12%">' + goods[1] + '</th>\
  683. <th width="12%">' + goods[2] + '</th>\
  684. <th width="12%">' + goods[3] + '</th>\
  685. <th width="12%">' + goods[4] + '</th>\
  686. <th width="15%"><img src="skin/interface/btn_max.png"/></th>\
  687. </tr>';
  688. var d = cachefetch('cities');
  689. var alt = '';
  690. var ar = $('#js_ChangeCityActionRequest').val();
  691. var tg = [0, 0, 0, 0, 0];
  692. var pr = [0, 0, 0, 0, 0];
  693. var dt = new Date().getTime();
  694. for (k in d) {
  695. if (d[k].hasOwnProperty("relationship")) {
  696. if (d[k]["relationship"] == "ownCity") {
  697. var cr = cachefetch('city_' + d[k]['id'] + '_prod');
  698. //deb('tr:'+JSON.stringify(cr));
  699. try {
  700. var woodprod = (cr['resourceProduction'] == 0) ? '' : ' <span style="font-size:0.8em">(+' + nmb(cr['resourceProduction'] * realHour) + '/h)</span>';
  701. var goodprod = (cr['tradegoodProduction'] == 0) ? '' : ' <span style="font-size:0.8em">(+' + nmb(cr['tradegoodProduction'] * realHour) + '/h)</span>';
  702. var trgood = cr['producedTradegood'];
  703. var delta = (dt - cr['dt']) / 3600000;
  704. cr['currentResources']['resource'] += delta * cr['resourceProduction'] * realHour;
  705. var winecon;
  706. if (trgood == 1) {
  707. cr['currentResources'][1] += delta * (cr['tradegoodProduction'] * realHour - cr['wineSpendings']);
  708. winecon = (cr['tradegoodProduction'] * realHour - cr['wineSpendings']);
  709. winecon = (winecon == 0) ? '' : ' <span style="font-size:0.8em">(' + ((winecon > 0) ? '+' : '-') + nmb(winecon) + '/h)</span>';
  710. } else {
  711. winecon = (cr['wineSpendings'] == 0) ? '' : ' <span style="font-size:0.8em">(-' + nmb(cr['wineSpendings']) + '/h)</span>';
  712. cr['currentResources'][trgood] += delta * cr['tradegoodProduction'] * realHour;
  713. }
  714. pr[0] += cr['resourceProduction'] * realHour;
  715. pr[trgood] += cr['tradegoodProduction'] * realHour;
  716. pr[1] -= cr['wineSpendings'];
  717. tg[0] += cr['currentResources']['resource'];
  718. tg[1] += cr['currentResources'][1];
  719. tg[2] += cr['currentResources'][2];
  720. tg[3] += cr['currentResources'][3];
  721. tg[4] += cr['currentResources'][4];
  722. s += '<tr class="' + alt + '"><td class="city bold"><a title="click to visit city" href="#" onclick="$(\'#js_cityIdOnChange\').val(\'' + d[k]['id'] + '\').parent().submit();">' + goods[d[k]['tradegood']] + ' ' + d[k]['coords'] + ' ' + d[k]['name'] + '</a></td>' +
  723. '<td class="right">' + nmb(cr['currentResources']['citizens']) + '(' + nmb(cr['currentResources']['population']) + ')</td>' +
  724. '<td class="right">' + nmb(cr['currentResources']['resource']) + woodprod + bar(cr['currentResources']['resource'], cr['maxResources']['resource']) + '</td>' +
  725. '<td class="right" title="' + ((trgood == 1) ? nmb(cr['tradegoodProduction'] * realHour) : '') + '-' + nmb(cr['wineSpendings']) + '">' + nmb(cr['currentResources']['1']) + winecon + bar(cr['currentResources'][1], cr['maxResources'][1]) + '</td>' +
  726. '<td class="right">' + nmb(cr['currentResources'][2]) + ((trgood == 2) ? goodprod : '') + bar(cr['currentResources'][2], cr['maxResources'][2]) + '</td>' +
  727. '<td class="right">' + nmb(cr['currentResources'][3]) + ((trgood == 3) ? goodprod : '') + bar(cr['currentResources'][3], cr['maxResources'][3]) + '</td>' +
  728. '<td class="right">' + nmb(cr['currentResources'][4]) + ((trgood == 4) ? goodprod : '') + bar(cr['currentResources'][4], cr['maxResources'][4]) + '</td>' +
  729. '<td class="center actions">' + (('city_' + d[k]['id'] == selectedcity) ? '' :
  730. '<a href="?view=transport&amp;destinationCityId=' + d[k]['id'] + '&amp;backgroundView=' + screen + '&amp;currentIslandId=' + island + '&amp;templateView=cityDetails&amp;actionRequest=' + ar + '" onclick="ajaxHandlerCall(this.href);return false;" id="itranslinkt' + d[k]['id'] + '"><img width="32" height="22" src="skin/interface/mission_transport.png" alt="Transport" class="vertical_middle"></a>' +
  731. '<a href="?view=deployment&amp;deploymentType=army&amp;destinationCityId=' + d[k]['id'] + '&amp;backgroundView=' + screen + '&amp;currentIslandId=' + island + '&amp;templateView=cityDetails&amp;actionRequest=' + ar + '" onclick="ajaxHandlerCall(this.href);return false;" id="itranslinkt' + d[k]['id'] + '"><img width="32" height="22" src="skin/interface/mission_deployarmy.png" alt="Army" class="vertical_middle"></a>' +
  732. '<a href="?view=deployment&amp;deploymentType=fleet&amp;destinationCityId=' + d[k]['id'] + '&amp;backgroundView=' + screen + '&amp;currentIslandId=' + island + '&amp;templateView=cityDetails&amp;actionRequest=' + ar + '" onclick="ajaxHandlerCall(this.href);return false;" id="itranslinkt' + d[k]['id'] + '"><img width="32" height="22" src="skin/interface/mission_deployfleet.png" alt="Army" class="vertical_middle"></a>') +
  733. '</td></tr>';
  734. } catch (e) {
  735. //deb('tr-catch:'+e);
  736. s += '<tr class="' + alt + '"><td class="city bold"><a href="#" onclick="$(\'#js_cityIdOnChange\').val(\'' + d[k]['id'] + '\').parent().submit();">' + goods[d[k]['tradegood']] + ' ' + d[k]['coords'] + ' ' + d[k]['name'] + '</a></td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>Visit city to update</td></tr>';
  737. }
  738. }
  739. }
  740. if (alt == '') {
  741. alt = 'alt';
  742. } else {
  743. alt = '';
  744. };
  745. }
  746. s += '<tr class="' + alt + ' total"><td class="city bold right">Total</td><td></td><td class="right">' + nmb(tg[0]) + ' <span style="font-size:0.8em">(' + nmb(pr[0]) + '/h)</span></td><td class="right">' + nmb(tg[1]) + ' <span style="font-size:0.8em">(' + nmb(pr[1]) + '/h)</span></td><td class="right">' + nmb(tg[2]) + ' <span style="font-size:0.8em">(' + nmb(pr[2]) + '/h)</span></td><td class="right">' + nmb(tg[3]) + ' <span style="font-size:0.8em">(' + nmb(pr[3]) + '/h)</span></td><td class="right">' + nmb(tg[4]) + ' <span style="font-size:0.8em">(' + nmb(pr[4]) + '/h)</span></td><td></td></tr>';
  747. t.html(s + '</table><iframe src="http://www.ika-core.org/ikariam-new.html" frameborder="0" scrolling="auto" marginwidth="0" marginheight="0" width="100%" height="115px"></iframe>');
  748. }
  749. }
  750.  
  751. function transporter() {
  752. var t = $('body #container').append('<div id="iTrans"></div>').find('#iTrans').mouseleave(function() {
  753. $(this).hide();
  754. });
  755. var tr = $('<span class="white"> > </span><a id="iTransport" class="yellow" title="Transporter" href="#"><img class="vertical_middle" width="20" height="16" alt="Transport" src="skin/interface/mission_transport.png"> Transporter </a>');
  756. var trans = (screen == 'city') ? tr.insertAfter('#js_cityBread') : $('#breadcrumbs').append(tr);
  757. trans.mouseenter(function() {
  758. transporterload();
  759. t.css('left', $('#iTransport').offset().left + 'px').show();
  760. });
  761. }
  762.  
  763. function rendersidebar() {
  764. var lm = $('#leftMenu');
  765. if (lm.length == 0) {
  766. lm = $('#container').append('<div id="leftMenu"><div class="slot_menu city_menu" id="js_viewCityMenu" style="z-index: 65;"><ul class="menu_slots"></ul></div></div>');
  767. }
  768. var menu = lm.find('.menu_slots');
  769. CreateSlot(menu, 8, 'http://gf1.geo.gfsrv.net/cdn00/0aa3bb98504af3b7f3d779bf46b82b.ico', '-1px 1px', 'Search', 'Find players,islands,cities', searchview);
  770. CreateSlot(menu, 9, 'skin/layout/btn_world.jpg', '-28px -1px', 'World', 'Show the mini Wolrd Map', worldviewprepare);
  771. CreateSlot(menu, 10, 'skin/buildings/x40_y40/forester.png', '-1px 1px', 'Buildings', 'Show buidings overview', buildingsview);
  772. }
  773.  
  774. function worldmap_iso_alterview() {
  775. $('body').append('<div id="worldmaphovernfo"></div>');
  776. $("#worldmaphovernfo").click(function() {
  777. $(this).hide();
  778. });
  779. $('#map1 div.islandTile').click(function(e) {
  780. var t = /(\d{1,2}:\d{1,2})/img.exec(this.title)[0].split(':');
  781. var x = t[0];
  782. var y = t[1];
  783. $("#worldmaphovernfo").css({
  784. top: e.clientY,
  785. left: e.clientX
  786. }).show();
  787. var cache = cachefetch('islenfo_' + x + '_' + y, 48 * hours); //48 hours lifetime
  788. if (cache) {
  789. $("#worldmaphovernfo").html(ufo.BubbleTips.createTooltip(cache, '200px'));
  790. } else {
  791. $.getJSON("/", "action=WorldMap&function=getJSONIsland&x=" + x + "&y=" + y, function(data) {
  792. var d = data['data'];
  793. var a = d.length;
  794. var s = '<table><tr class="head"><td><center><img src="skin/characters/y100_citizen_faceright.png" height="24"></center></td><td><center><img src="skin/icons/livingspace_24x24.png" height="24"></center></td></tr>';
  795. for (var p = 0; p < a; p++) {
  796. s += '<tr><td class="first">' + d[p].avatar_name + '</td><td>' + d[p].name + '</td></tr>';
  797. }
  798. s += '</table>';
  799. cacheset('islenfo_' + x + '_' + y, s);
  800. $("#worldmaphovernfo").html(ufo.BubbleTips.createTooltip(s, '300px'));
  801. });
  802. }
  803. });
  804. }
  805.  
  806. function parseresponse(m) {
  807. var success = false;
  808. var data = [];
  809. try {
  810. var o = m.length;
  811. for (var p = 0; p < o; p++) {
  812. var n = m[p][0];
  813. try {
  814. if (n != "custom") {
  815. deb("pr:mapping " + n + " ...");
  816. var e = n;
  817. var d = m[p][1];
  818. if (e == 'updateGlobalData') {
  819. if (d.hasOwnProperty("backgroundData")) {
  820. data = d["backgroundData"];
  821. delete m[p][1]["backgroundData"];
  822. if (d.hasOwnProperty("walkers")) {
  823. delete m[p][1]["walkers"];
  824. }
  825. success = true;
  826. break;
  827. }
  828. }
  829. }
  830. } catch (l) {
  831. deb("pr:unable to parse: " + m[p][0] + "\n with parameters: " + m[p][1]);
  832. deb("pr:" + l);
  833. }
  834. }
  835. deb("pr: calling parser...");
  836. ufo.pobj = cloneInto(m, ufo);
  837. ufo.ajax.Responder.parseResponse(ufo.pobj);
  838. deb("pr:done.");
  839. } catch (l) {
  840. sucess = false;
  841. deb("pr:Unable to parse ajax response: \n" + m + "\n" + l);
  842. }
  843. deb("pr: capture " + JSON.stringify(data));
  844. return success;
  845. }
  846. function dumpcachesize(obj) {
  847. var lobj=cachetokenizer(obj);
  848. if(localStorage[lobj]){
  849. deb('store:' + obj + "=" + ((localStorage[lobj].length * 2) / 1024 / 1024).toFixed(2) + " MB");
  850. }
  851. }
  852.  
  853. function dumplocalstore() {
  854. for (var x in localStorage) deb('store:' + x + "=" + ((localStorage[x].length * 2) / 1024 / 1024).toFixed(2) + " MB");
  855. }
  856.  
  857. function main() {
  858. try {
  859. //localStorage.clear();
  860. ikariam = ufo.ikariam;
  861. nmb = ufo.ikariam.Model.locaNumberFormat;
  862. rendersidebar();
  863. transporter();
  864. switch (screen) {
  865. case 'worldmap_iso':
  866. worldmap_iso_alterview();
  867. break;
  868. }
  869. deb(lang + '-' + server + " - actionrequest:" + ikariam.getModel().actionRequest + ' stub finish.');
  870. //dumplocalstore();
  871. // $.getJSON( "/index.php","view=updateGlobalData&islandId=527&backgroundView=island&currentIslandId=527&actionRequest="+ikariam.getModel().actionRequest+"&ajax=1",parseresponse);
  872. } catch (e) {
  873. deb('main:' + e);
  874. }
  875. }
  876.