swarfarm_parser 39 KB

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