swarfarm_parser 45 KB

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