swarfarm_parser 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. #!/bin/php
  2. <?php
  3. /**
  4. * Swarfarm URL of the monster list (first page).
  5. */
  6. CONST URL_UNIT = 'https://swarfarm.com/api/v2/monsters/';
  7. /**
  8. * Swarfarm URL of the skill list (first page).
  9. */
  10. CONST URL_SKILL = 'https://swarfarm.com/api/v2/skills/';
  11. /**
  12. * Path to the application
  13. */
  14. CONST PATH = __DIR__ . "/../";
  15. /**
  16. * Script verbosity
  17. *
  18. * 0: Critical
  19. * 1: Error
  20. * 2: Warning
  21. * 3: Info
  22. * 4: Debug
  23. * 5: Trace
  24. */
  25. $verbosity = 4;
  26. /**
  27. * Echoes an output message to stdout, depending on the verbosity level.
  28. *
  29. * @param int $level Message priority.
  30. * @param string $message content.
  31. */
  32. function output($level, $message){
  33. global $verbosity;
  34. if ($level <= $verbosity){
  35. if (substr("testers", -1) != PHP_EOL){
  36. $message .= PHP_EOL;
  37. }
  38. echo $message;
  39. }
  40. }
  41. /**
  42. * Gets an entity com2us_id from it's Swarfarm id.
  43. *
  44. * @param int $swarfarm_id Entity's Swarfarm ID.
  45. * @return int Entity's com2us_id, -1 on error.
  46. */
  47. function get_com2us_id($swarfarm_id){
  48. if ($swarfarm_id == null){
  49. return null;
  50. }
  51. $contents = file_get_contents(URL_UNIT . $swarfarm_id . "/");
  52. if($contents === false){
  53. //Print out the contents.
  54. output(1, "Unable to retrieve monster.");
  55. return -1;
  56. }
  57. $contents = json_decode($contents);
  58. return $contents->com2us_id;
  59. }
  60. /**
  61. * Gets an element ID from it's name
  62. *
  63. * @param string $name Element name (case insensitive).
  64. * @return int Element ID, -1 on unknown element.
  65. */
  66. function get_element_id($name){
  67. $id = -1;
  68. switch (strtoupper($name)){
  69. case "MAGIC":
  70. $id = 0;
  71. break;
  72. case "WATER":
  73. $id = 1;
  74. break;
  75. case "FIRE":
  76. $id = 2;
  77. break;
  78. case "WIND":
  79. $id = 3;
  80. break;
  81. case "LIGHT":
  82. $id = 4;
  83. break;
  84. case "DARK":
  85. $id = 5;
  86. break;
  87. case "PURE":
  88. $id = 6;
  89. break;
  90. }
  91. return $id;
  92. }
  93. /**
  94. * Gets an archetype ID from it's name
  95. *
  96. * @param string $name Archetype name (case insensitive).
  97. * @return int Archetype ID, -1 on unknown archetype.
  98. */
  99. function get_archetype_id($name){
  100. $id = 0; // NONE
  101. switch (strtoupper($name)){
  102. case "ATTACK":
  103. $id = 1;
  104. break;
  105. case "DEFENSE":
  106. $id = 2;
  107. break;
  108. case "HP":
  109. $id = 3;
  110. break;
  111. case "SUPPORT":
  112. $id = 4;
  113. break;
  114. case "MATERIAL":
  115. $id = 5;
  116. break;
  117. }
  118. return $id;
  119. }
  120. /**
  121. * Gets an archetype ID from it's name
  122. *
  123. * @param string $name Archetype name (case insensitive).
  124. * @return int Archetype ID, -1 on unknown archetype.
  125. */
  126. function connect_to_database(){
  127. // Connect to the database
  128. $db = new SQLite3(PATH . "application/sw.sqlite");
  129. if (!$db) {
  130. output(0, "Error connecting to th database: " . $db->lastErrorMsg());
  131. exit();
  132. }
  133. // Disable integrity
  134. $db->query("PRAGMA foreign_keys=OFF;");
  135. return $db;
  136. }
  137. // TODO: Parse arguments
  138. // Connect to the database
  139. $db = connect_to_database();
  140. // Loop skills pages
  141. $next = URL_SKILL;
  142. while ($next != null){
  143. output(3, "Reading skills page: $next");
  144. $contents = file_get_contents($next);
  145. if ($contents === false){
  146. output(0, "Unable to read skill page: $next");
  147. return -1;
  148. }
  149. $contents = json_decode($contents);
  150. $next = $contents->next;
  151. $list = $contents->results;
  152. foreach ($list as $skill){
  153. $statement = $db->prepare("SELECT * FROM k_skill WHERE id = :id");
  154. $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
  155. $q = $statement->execute();
  156. $r = $q->fetchArray(SQLITE3_ASSOC);
  157. if (!$r){
  158. $statement = $db->prepare("
  159. INSERT INTO k_skill(
  160. id,
  161. name,
  162. description,
  163. slot,
  164. cooltime,
  165. hits,
  166. passive,
  167. aoe,
  168. max_level,
  169. multiplier_formula
  170. ) VALUES (
  171. :id,
  172. :name,
  173. :description,
  174. :slot,
  175. :cooltime,
  176. :hits,
  177. :passive,
  178. :aoe,
  179. :max_level,
  180. :multiplier_formula
  181. );
  182. ");
  183. $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
  184. $statement->bindValue(":name", $skill->name, SQLITE3_TEXT);
  185. $statement->bindValue(":description", $skill->name, SQLITE3_TEXT);
  186. $statement->bindValue(":slot", $skill->slot, SQLITE3_TEXT);
  187. $statement->bindValue(":cooltime", $skill->cooltime, SQLITE3_INTEGER);
  188. $statement->bindValue(":hits", $skill->hits, SQLITE3_INTEGER);
  189. $statement->bindValue(":passive", $skill->passive, SQLITE3_INTEGER);
  190. $statement->bindValue(":aoe", $skill->aoe, SQLITE3_INTEGER);
  191. $statement->bindValue(":max_level", $skill->max_level, SQLITE3_INTEGER);
  192. $statement->bindValue(":multiplier_formula", $skill->multiplier_formula, SQLITE3_INTEGER);
  193. output(4, "Execute query: ");
  194. output(4, $statement->getSQL(true));
  195. $statement->execute();
  196. $url = 'https://swarfarm.com/static/herders/images/skills/' . $skill->icon_filename;
  197. $out = PATH . "public/img/skill/" . sprintf('%08d', $skill->com2us_id) . ".png";
  198. output(4, "Downloading file '$url' to: '$out'");
  199. if (!file_put_contents($out, file_get_contents($url))) {
  200. output(1, "Error downloading file '$url' to: '$out'");
  201. }
  202. $level = 1;
  203. $statement = $db->prepare("INSERT INTO k_skill_level (skill, level, description) VALUES (:skill, :level, :description);");
  204. $statement->bindValue(":skill", $skill->com2us_id, SQLITE3_INTEGER);
  205. $statement->bindValue(":level", $level, SQLITE3_INTEGER);
  206. foreach ($skill->upgrades as $upgrade){
  207. $level ++;
  208. $statement = $db->prepare("INSERT INTO k_skill_level (skill, level, description) VALUES (:skill, :level, :description);");
  209. $statement->bindValue(":skill", $skill->com2us_id, SQLITE3_INTEGER);
  210. $statement->bindValue(":level", $level, SQLITE3_INTEGER);
  211. $statement->bindValue(":description", str_replace("{0}", $upgrade->amount, $upgrade->effect), SQLITE3_TEXT);
  212. output(4, "Execute query: ");
  213. output(4, $statement->getSQL(true));
  214. $statement->execute();
  215. }
  216. // TODO: Get data for other tables:
  217. // k_skill_effect
  218. }
  219. else{
  220. // Compare all values
  221. if ($r["name"] != $skill->name){
  222. $statement = $db->prepare("UPDATE k_skill SET name = :name WHERE id = :id;");
  223. $statement->bindValue(":name", $skill->name, SQLITE3_TEXT);
  224. $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
  225. output(4, "Execute query: ");
  226. output(4, $statement->getSQL(true));
  227. $statement->execute();
  228. }
  229. if ($r["description"] != $skill->description){
  230. $statement = $db->prepare("UPDATE k_skill SET description = :description WHERE id = :id;");
  231. $statement->bindValue(":description", $skill->description, SQLITE3_TEXT);
  232. $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
  233. output(4, "Execute query: ");
  234. output(4, $statement->getSQL(true));
  235. $statement->execute();
  236. }
  237. if ($r["slot"] != $skill->slot){
  238. $statement = $db->prepare("UPDATE k_skill SET slot = :slot WHERE id = :id;");
  239. $statement->bindValue(":slot", $skill->slot, SQLITE3_TEXT);
  240. $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
  241. output(4, "Execute query: ");
  242. output(4, $statement->getSQL(true));
  243. $statement->execute();
  244. }
  245. if ($r["cooltime"] != $skill->cooltime){
  246. $statement = $db->prepare("UPDATE k_skill SET cooltime = :cooltime WHERE id = :id;");
  247. $statement->bindValue(":cooltime", $skill->cooltime, SQLITE3_TEXT);
  248. $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
  249. output(4, "Execute query: ");
  250. output(4, $statement->getSQL(true));
  251. $statement->execute();
  252. }
  253. if ($r["hits"] != $skill->hits){
  254. $statement = $db->prepare("UPDATE k_skill SET hits = :hits WHERE id = :id;");
  255. $statement->bindValue(":hits", $skill->hits, SQLITE3_TEXT);
  256. $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
  257. output(4, "Execute query: ");
  258. output(4, $statement->getSQL(true));
  259. $statement->execute();
  260. }
  261. $passive = ($skill->passive == 1) ? (1) : (0);
  262. if ($r["passive"] != $passive){
  263. $statement = $db->prepare("UPDATE k_skill SET passive = :passive WHERE id = :id;");
  264. $statement->bindValue(":passive", $passive, SQLITE3_TEXT);
  265. $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
  266. output(4, "Execute query: ");
  267. output(4, $statement->getSQL(true));
  268. $statement->execute();
  269. }
  270. $aoe = ($skill->aoe == 1) ? (1) : (0);
  271. if ($r["aoe"] != $aoe){
  272. $statement = $db->prepare("UPDATE k_skill SET aoe = :aoe WHERE id = :id;");
  273. $statement->bindValue(":aoe", $aoe, SQLITE3_TEXT);
  274. $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
  275. output(4, "Execute query: ");
  276. output(4, $statement->getSQL(true));
  277. $statement->execute();
  278. }
  279. if ($r["max_level"] != $skill->max_level){
  280. $statement = $db->prepare("UPDATE k_skill SET max_level = :max_level WHERE id = :id;");
  281. $statement->bindValue(":max_level", $skill->max_level, SQLITE3_TEXT);
  282. $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
  283. output(4, "Execute query: ");
  284. output(4, $statement->getSQL(true));
  285. $statement->execute();
  286. }
  287. if ($r["multiplier_formula"] != $skill->multiplier_formula){
  288. $statement = $db->prepare("UPDATE k_skill SET multiplier_formula = :multiplier_formula WHERE id = :id;");
  289. $statement->bindValue(":multiplier_formula", $skill->multiplier_formula, SQLITE3_TEXT);
  290. $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
  291. output(4, "Execute query: ");
  292. output(4, $statement->getSQL(true));
  293. $statement->execute();
  294. }
  295. // Image download
  296. $out = PATH . "public/img/skill/" . sprintf('%08d', $skill->com2us_id) . ".png";
  297. if (!file_exists($out)){
  298. $url = 'https://swarfarm.com/static/herders/images/skills/' . $skill->icon_filename;
  299. output(4, "Downloading file '$url' to: '$out'");
  300. if (!file_put_contents($out, file_get_contents($url))) {
  301. output(1, "Error downloading file '$url' to: '$out'");
  302. }
  303. }
  304. }
  305. }
  306. //echo "NEXTPAGE: " . $next;
  307. }
  308. // Loop units pages
  309. $next = URL_UNIT;
  310. while ($next != null){
  311. output(3, "Reading units page: $next");
  312. $contents = file_get_contents($next);
  313. if($contents === false){
  314. output(0, "Unable to retrieve monster list.");
  315. return -1;
  316. }
  317. $contents = json_decode($contents);
  318. $next = $contents->next;
  319. $list = $contents->results;
  320. foreach ($list as $unit){
  321. $statement = $db->prepare("SELECT * FROM k_unit WHERE id = :id");
  322. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  323. $q = $statement->execute();
  324. $r = $q->fetchArray(SQLITE3_ASSOC);
  325. if (!$r){
  326. $statement = $db->prepare("
  327. INSERT INTO k_unit(
  328. id,
  329. family,
  330. name,
  331. element,
  332. archetype,
  333. base_stars,
  334. natural_stars,
  335. obtainable,
  336. can_awaken,
  337. awaken_bonus,
  338. skill_ups_to_max,
  339. leader_skill,
  340. hp,
  341. attack,
  342. defense,
  343. speed,
  344. crit_rate,
  345. crit_damage,
  346. resistance,
  347. accuracy,
  348. raw_hp,
  349. raw_attack,
  350. raw_defense,
  351. max_lvl_hp,
  352. max_lvl_attack,
  353. max_lvl_defense,
  354. awakens_from,
  355. awakens_to,
  356. fusion_food,
  357. homunculus,
  358. craft_cost
  359. ) VALUES (
  360. :id,
  361. :family,
  362. :name,
  363. :element,
  364. :archetype,
  365. :base_stars,
  366. :natural_stars,
  367. :obtainable,
  368. :can_awaken,
  369. :awaken_bonus,
  370. :skill_ups_to_max,
  371. :leader_skill,
  372. :hp,
  373. :attack,
  374. :defense,
  375. :speed,
  376. :crit_rate,
  377. :crit_damage,
  378. :resistance,
  379. :accuracy,
  380. :raw_hp,
  381. :raw_attack,
  382. :raw_defense,
  383. :max_lvl_hp,
  384. :max_lvl_attack,
  385. :max_lvl_defense,
  386. :awakens_from,
  387. :awakens_to,
  388. :fusion_food,
  389. :homunculus,
  390. :craft_cost
  391. );
  392. ");
  393. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_TEXT);
  394. $statement->bindValue(":family", $unit->family_id, SQLITE3_INTEGER);
  395. $statement->bindValue(":name", $unit->name, SQLITE3_TEXT);
  396. $statement->bindValue(":element", get_element_id($unit->element), SQLITE3_INTEGER);
  397. $statement->bindValue(":archetype", get_archetype_id($unit->archetype), SQLITE3_INTEGER);
  398. $statement->bindValue(":base_stars", $unit->base_stars, SQLITE3_INTEGER);
  399. $statement->bindValue(":natural_stars", $unit->natural_stars, SQLITE3_INTEGER);
  400. $statement->bindValue(":obtainable", $unit->obtainable, SQLITE3_INTEGER);
  401. $statement->bindValue(":can_awaken", $unit->can_awaken, SQLITE3_INTEGER);
  402. $statement->bindValue(":awaken_bonus", $unit->awaken_bonus, SQLITE3_TEXT);
  403. $statement->bindValue(":skill_ups_to_max", $unit->skill_ups_to_max, SQLITE3_INTEGER);
  404. $statement->bindValue(":leader_skill", $unit->leader_skill, SQLITE3_INTEGER);
  405. $statement->bindValue(":hp", $unit->base_hp, SQLITE3_INTEGER);
  406. $statement->bindValue(":attack", $unit->base_attack, SQLITE3_INTEGER);
  407. $statement->bindValue(":defense", $unit->base_defense, SQLITE3_INTEGER);
  408. $statement->bindValue(":speed", $unit->speed, SQLITE3_INTEGER);
  409. $statement->bindValue(":crit_rate", $unit->crit_rate, SQLITE3_INTEGER);
  410. $statement->bindValue(":crit_damage", $unit->crit_damage, SQLITE3_INTEGER);
  411. $statement->bindValue(":resistance", $unit->resistance, SQLITE3_INTEGER);
  412. $statement->bindValue(":accuracy", $unit->accuracy, SQLITE3_INTEGER);
  413. $statement->bindValue(":raw_hp", $unit->raw_hp, SQLITE3_INTEGER);
  414. $statement->bindValue(":raw_attack", $unit->raw_attack, SQLITE3_INTEGER);
  415. $statement->bindValue(":raw_defense", $unit->raw_defense, SQLITE3_INTEGER);
  416. $statement->bindValue(":max_lvl_hp", $unit->max_lvl_hp, SQLITE3_INTEGER);
  417. $statement->bindValue(":max_lvl_attack", $unit->max_lvl_attack, SQLITE3_INTEGER);
  418. $statement->bindValue(":max_lvl_defense", $unit->max_lvl_defense, SQLITE3_INTEGER);
  419. $statement->bindValue(":awakens_from", get_com2us_id($unit->awakens_from), SQLITE3_INTEGER);
  420. $statement->bindValue(":awakens_to", get_com2us_id($unit->awakens_to), SQLITE3_TEXT);
  421. $statement->bindValue(":fusion_food", $unit->fusion_food, SQLITE3_INTEGER);
  422. $statement->bindValue(":homunculus", $unit->homunculus, SQLITE3_INTEGER);
  423. $statement->bindValue(":craft_cost", 0, SQLITE3_INTEGER);
  424. output(4, "Execute query: ");
  425. output(4, $statement->getSQL(true));
  426. $statement->execute();
  427. $url = 'https://swarfarm.com/static/herders/images/monsters/' . $unit->image_filename;
  428. $out = PATH . "public/img/unit/" . sprintf('%08d', $unit->com2us_id) . ".png";
  429. output(4, "Downloading file '$url' to: '$out'");
  430. if (!file_put_contents($out, file_get_contents($url))) {
  431. output(1, "Error downloading file '$url' to: '$out'");
  432. }
  433. foreach ($unit->skills as $skill){
  434. $statement = $db->prepare("INSERT INTO k_unit_skill (unit, skill) VALUES (:unit, :skill);");
  435. $statement->bindValue(":unit", $unit->com2us_id, SQLITE3_INTEGER);
  436. $statement->bindValue(":skill", $skill, SQLITE3_INTEGER);
  437. output(4, "Execute query: ");
  438. output(4, $statement->getSQL(true));
  439. $statement->execute();
  440. }
  441. // TODO: Get data for other tables:
  442. // k_unit_essence
  443. // k_unit_source
  444. }
  445. else{
  446. // Compare all values
  447. if ($r["family"] != $unit->family_id){
  448. $statement = $db->prepare("UPDATE k_unit SET family = :family WHERE id = :id;");
  449. $statement->bindValue(":family", $unit->family_id, SQLITE3_INTEGER);
  450. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  451. output(4, "Execute query: ");
  452. output(4, $statement->getSQL(true));
  453. $statement->execute();
  454. }
  455. if ($r["name"] != $unit->name){
  456. $statement = $db->prepare("UPDATE k_unit SET name = :name WHERE id = :id;");
  457. $statement->bindValue(":name", $unit->name, SQLITE3_TEXT);
  458. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  459. output(4, "Execute query: ");
  460. output(4, $statement->getSQL(true));
  461. $statement->execute();
  462. }
  463. $element = get_element_id($unit->element);
  464. if ($r["element"] != $element){
  465. $statement = $db->prepare("UPDATE k_unit SET element = :element WHERE id = :id;");
  466. $statement->bindValue(":element", $element, SQLITE3_TEXT);
  467. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  468. output(4, "Execute query: ");
  469. output(4, $statement->getSQL(true));
  470. $statement->execute();
  471. }
  472. $archetype = get_archetype_id($unit->archetype);
  473. if ($r["archetype"] != $archetype){
  474. $statement = $db->prepare("UPDATE k_unit SET archetype = :archetype WHERE id = :id;");
  475. $statement->bindValue(":archetype", $archetype, SQLITE3_TEXT);
  476. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  477. output(4, "Execute query: ");
  478. output(4, $statement->getSQL(true));
  479. $statement->execute();
  480. }
  481. if ($r["base_stars"] != $unit->base_stars){
  482. $statement = $db->prepare("UPDATE k_unit SET base_stars = :base_stars WHERE id = :id;");
  483. $statement->bindValue(":base_stars", $unit->base_stars, SQLITE3_INTEGER);
  484. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  485. output(4, "Execute query: ");
  486. output(4, $statement->getSQL(true));
  487. $statement->execute();
  488. }
  489. if ($r["natural_stars"] != $unit->natural_stars){
  490. $statement = $db->prepare("UPDATE k_unit SET natural_stars = :natural_stars WHERE id = :id;");
  491. $statement->bindValue(":natural_stars", $unit->natural_stars, SQLITE3_INTEGER);
  492. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  493. output(4, "Execute query: ");
  494. output(4, $statement->getSQL(true));
  495. $statement->execute();
  496. }
  497. if ($r["obtainable"] != $unit->obtainable){
  498. $statement = $db->prepare("UPDATE k_unit SET obtainable = :obtainable WHERE id = :id;");
  499. $statement->bindValue(":obtainable", $unit->obtainable, SQLITE3_INTEGER);
  500. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  501. output(4, "Execute query: ");
  502. output(4, $statement->getSQL(true));
  503. $statement->execute();
  504. }
  505. if ($r["can_awaken"] != $unit->can_awaken){
  506. $statement = $db->prepare("UPDATE k_unit SET can_awaken = :can_awaken WHERE id = :id;");
  507. $statement->bindValue(":can_awaken", $unit->can_awaken, SQLITE3_INTEGER);
  508. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  509. output(4, "Execute query: ");
  510. output(4, $statement->getSQL(true));
  511. $statement->execute();
  512. }
  513. if ($r["awaken_bonus"] != $unit->awaken_bonus && $unit->awaken_bonus != 0){
  514. $statement = $db->prepare("UPDATE k_unit SET awaken_bonus = :awaken_bonus WHERE id = :id;");
  515. $statement->bindValue(":awaken_bonus", $unit->awaken_bonus, SQLITE3_TEXT);
  516. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_TEXT);
  517. output(4, "Execute query: ");
  518. output(4, $statement->getSQL(true));
  519. $statement->execute();
  520. }
  521. if ($r["skill_ups_to_max"] != $unit->skill_ups_to_max){
  522. $statement = $db->prepare("UPDATE k_unit SET skill_ups_to_max = :skill_ups_to_max WHERE id = :id;");
  523. $statement->bindValue(":skill_ups_to_max", $unit->skill_ups_to_max, SQLITE3_INTEGER);
  524. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  525. output(4, "Execute query: ");
  526. output(4, $statement->getSQL(true));
  527. $statement->execute();
  528. }
  529. if ($r["hp"] != $unit->base_hp){
  530. $statement = $db->prepare("UPDATE k_unit SET hp = :hp WHERE id = :id;");
  531. $statement->bindValue(":hp", $unit->base_hp, SQLITE3_INTEGER);
  532. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  533. output(4, "Execute query: ");
  534. output(4, $statement->getSQL(true));
  535. $statement->execute();
  536. }
  537. if ($r["attack"] != $unit->base_attack){
  538. $statement = $db->prepare("UPDATE k_unit SET attack = :attack WHERE id = :id;");
  539. $statement->bindValue(":attack", $unit->base_attack, SQLITE3_INTEGER);
  540. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  541. output(4, "Execute query: ");
  542. output(4, $statement->getSQL(true));
  543. $statement->execute();
  544. }
  545. if ($r["defense"] != $unit->base_defense){
  546. $statement = $db->prepare("UPDATE k_unit SET defense = :defense WHERE id = :id;");
  547. $statement->bindValue(":defense", $unit->base_defense, SQLITE3_INTEGER);
  548. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  549. output(4, "Execute query: ");
  550. output(4, $statement->getSQL(true));
  551. $statement->execute();
  552. }
  553. if ($r["speed"] != $unit->speed){
  554. $statement = $db->prepare("UPDATE k_unit SET speed = :speed WHERE id = :id;");
  555. $statement->bindValue(":speed", $unit->speed, SQLITE3_INTEGER);
  556. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  557. output(4, "Execute query: ");
  558. output(4, $statement->getSQL(true));
  559. $statement->execute();
  560. }
  561. if ($r["crit_rate"] != $unit->crit_rate){
  562. $statement = $db->prepare("UPDATE k_unit SET crit_rate = :crit_rate WHERE id = :id;");
  563. $statement->bindValue(":crit_rate", $unit->crit_rate, SQLITE3_INTEGER);
  564. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  565. output(4, "Execute query: ");
  566. output(4, $statement->getSQL(true));
  567. $statement->execute();
  568. }
  569. if ($r["crit_damage"] != $unit->crit_damage){
  570. $statement = $db->prepare("UPDATE k_unit SET crit_damage = :crit_damage WHERE id = :id;");
  571. $statement->bindValue(":crit_damage", $unit->crit_damage, SQLITE3_INTEGER);
  572. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  573. output(4, "Execute query: ");
  574. output(4, $statement->getSQL(true));
  575. $statement->execute();
  576. }
  577. if ($r["resistance"] != $unit->resistance){
  578. $statement = $db->prepare("UPDATE k_unit SET resistance = :resistance WHERE id = :id;");
  579. $statement->bindValue(":resistance", $unit->resistance, SQLITE3_INTEGER);
  580. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  581. output(4, "Execute query: ");
  582. output(4, $statement->getSQL(true));
  583. $statement->execute();
  584. }
  585. if ($r["accuracy"] != $unit->accuracy){
  586. $statement = $db->prepare("UPDATE k_unit SET accuracy = :accuracy WHERE id = :id;");
  587. $statement->bindValue(":accuracy", $unit->accuracy, SQLITE3_INTEGER);
  588. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  589. output(4, "Execute query: ");
  590. output(4, $statement->getSQL(true));
  591. $statement->execute();
  592. }
  593. if ($r["raw_hp"] != $unit->raw_hp){
  594. $statement = $db->prepare("UPDATE k_unit SET raw_hp = :raw_hp WHERE id = :id;");
  595. $statement->bindValue(":raw_hp", $unit->raw_hp, SQLITE3_INTEGER);
  596. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  597. output(4, "Execute query: ");
  598. output(4, $statement->getSQL(true));
  599. $statement->execute();
  600. }
  601. if ($r["raw_attack"] != $unit->raw_attack){
  602. $statement = $db->prepare("UPDATE k_unit SET raw_attack = :raw_attack WHERE id = :id;");
  603. $statement->bindValue(":raw_attack", $unit->raw_attack, SQLITE3_INTEGER);
  604. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  605. output(4, "Execute query: ");
  606. output(4, $statement->getSQL(true));
  607. $statement->execute();
  608. }
  609. if ($r["raw_defense"] != $unit->raw_defense){
  610. $statement = $db->prepare("UPDATE k_unit SET raw_defense = :raw_defense WHERE id = :id;");
  611. $statement->bindValue(":raw_defense", $unit->raw_defense, SQLITE3_INTEGER);
  612. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  613. output(4, "Execute query: ");
  614. output(4, $statement->getSQL(true));
  615. $statement->execute();
  616. }
  617. if ($r["max_lvl_hp"] != $unit->max_lvl_hp){
  618. $statement = $db->prepare("UPDATE k_unit SET max_lvl_hp = :max_lvl_hp WHERE id = :id;");
  619. $statement->bindValue(":max_lvl_hp", $unit->max_lvl_hp, SQLITE3_INTEGER);
  620. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  621. output(4, "Execute query: ");
  622. output(4, $statement->getSQL(true));
  623. $statement->execute();
  624. }
  625. if ($r["max_lvl_attack"] != $unit->max_lvl_attack){
  626. $statement = $db->prepare("UPDATE k_unit SET max_lvl_attack = :max_lvl_attack WHERE id = :id;");
  627. $statement->bindValue(":max_lvl_attack", $unit->max_lvl_attack, SQLITE3_INTEGER);
  628. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  629. output(4, "Execute query: ");
  630. output(4, $statement->getSQL(true));
  631. $statement->execute();
  632. }
  633. if ($r["max_lvl_defense"] != $unit->max_lvl_defense){
  634. $statement = $db->prepare("UPDATE k_unit SET max_lvl_defense = :max_lvl_defense WHERE id = :id;");
  635. $statement->bindValue(":max_lvl_defense", $unit->max_lvl_defense, SQLITE3_INTEGER);
  636. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  637. output(4, "Execute query: ");
  638. output(4, $statement->getSQL(true));
  639. $statement->execute();
  640. }
  641. $com2us_id = get_com2us_id($unit->awakens_from);
  642. if ($r["awakens_from"] != $com2us_id){
  643. $statement = $db->prepare("UPDATE k_unit SET awakens_from = :awakens_from WHERE id = :id;");
  644. $statement->bindValue(":awakens_from", $com2us_id, SQLITE3_INTEGER);
  645. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  646. output(4, "Execute query: ");
  647. output(4, $statement->getSQL(true));
  648. $statement->execute();
  649. }
  650. $com2us_id = get_com2us_id($unit->awakens_to);
  651. if ($r["awakens_to"] != $com2us_id){
  652. $statement = $db->prepare("UPDATE k_unit SET awakens_to = :awakens_to WHERE id = :id;");
  653. $statement->bindValue(":awakens_to", $com2us_id, SQLITE3_INTEGER);
  654. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  655. output(4, "Execute query: ");
  656. output(4, $statement->getSQL(true));
  657. $statement->execute();
  658. }
  659. if ($r["fusion_food"] != $unit->fusion_food){
  660. $statement = $db->prepare("UPDATE k_unit SET fusion_food = :fusion_food WHERE id = :id;");
  661. $statement->bindValue(":fusion_food", $unit->fusion_food, SQLITE3_INTEGER);
  662. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  663. output(4, "Execute query: ");
  664. output(4, $statement->getSQL(true));
  665. $statement->execute();
  666. }
  667. if ($r["homunculus"] != $unit->homunculus){
  668. $statement = $db->prepare("UPDATE k_unit SET homunculus = :homunculus WHERE id = :id;");
  669. $statement->bindValue(":homunculus", $unit->homunculus, SQLITE3_INTEGER);
  670. $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
  671. output(4, "Execute query: ");
  672. output(4, $statement->getSQL(true));
  673. $statement->execute();
  674. }
  675. // Loop skills. Only for inserting in k_skill and k_unit_skill
  676. $statement = $db->prepare("SELECT count(skill) AS c FROM k_unit_skill WHERE unit = :unit;");
  677. $statement->bindValue(":unit", $unit->com2us_id, SQLITE3_INTEGER);
  678. $q = $statement->execute();
  679. $r = $q->fetchArray(SQLITE3_ASSOC);
  680. if ($r["c"] != sizeof($unit->skills)){
  681. $statement = $db->prepare("DELETE FROM k_unit_skill WHERE unit = :unit;");
  682. $statement->bindValue(":unit", $unit->com2us_id, SQLITE3_INTEGER);
  683. output(4, "Execute query: ");
  684. output(4, $statement->getSQL(true));
  685. $statement->execute();
  686. foreach ($unit->skills as $skill){
  687. $statement = $db->prepare("INSERT INTO k_unit_skill (unit, skill) VALUES (:unit, :skill);");
  688. $statement->bindValue(":unit", $unit->com2us_id, SQLITE3_INTEGER);
  689. $statement->bindValue(":skill", $skill, SQLITE3_INTEGER);
  690. output(4, "Execute query: ");
  691. output(4, $statement->getSQL(true));
  692. $statement->execute();
  693. }
  694. }
  695. // Image download
  696. $out = PATH . "public/img/unit/" . sprintf('%08d', $unit->com2us_id) . ".png";
  697. if (!file_exists($out)){
  698. //echo(" Downloading image " . $unit->image_filename . " to " . sprintf('%08d', $unit->com2us_id) . ".png");
  699. $url = 'https://swarfarm.com/static/herders/images/monsters/' . $unit->image_filename;
  700. output(4, "Downloading file '$url' to: '$out'");
  701. if (!file_put_contents($out, file_get_contents($url))) {
  702. output(1, "Error downloading file '$url' to: '$out'");
  703. }
  704. }
  705. }
  706. }
  707. //echo "NEXTPAGE: " . $next;
  708. }
  709. ?>