| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662 |
- <?php
- /**
- * Profile uploader script.
- *
- * Exposes an API to update an existing profile.
- *
- * It also saves the data to a JSON file in the data directory.
- *
- * Mandatory PUT parameters are:
- * - response: JSON received from game server on HubUserLogin command.
- * - key: User API key.
- *
- * @category API
- */
- global $db;
- /**
- * Calculates the currrent efficiency of a rune.
- *
- * @param JSON $rune JSON fragment of the rune.
- * @return int Efficiency (0-100)
- */
- function calculate_efficiency($rune){
- $efficiency = 0.0;
- $max_efficiency = 0.0;
- $substats = [];
- $level = $rune->{"upgrade_curr"};
- $stars = $rune->{"class"};
- // Ancient stars are + 10
- if ($stars > 10){
- $stars -= 10;
- }
- $list = null;
- // Main stat
- $main_stat = $rune->{"pri_eff"}[0];
- switch ($main_stat){
- case RUNE_STAT_ID::HP:
- $list = RUNE_STAT_VALUES::HP;
- break;
- case RUNE_STAT_ID::HP_P:
- $list = RUNE_STAT_VALUES::HP_P;
- break;
- case RUNE_STAT_ID::ATK:
- $list = RUNE_STAT_VALUES::ATK;
- break;
- case RUNE_STAT_ID::ATK_P:
- $list = RUNE_STAT_VALUES::ATK_P;
- break;
- case RUNE_STAT_ID::DEF:
- $list = RUNE_STAT_VALUES::DEF;
- break;
- case RUNE_STAT_ID::DEF_P:
- $list = RUNE_STAT_VALUES::DEF_P;
- break;
- case RUNE_STAT_ID::SPD:
- $list = RUNE_STAT_VALUES::SPD;
- break;
- case RUNE_STAT_ID::CRR:
- $list = RUNE_STAT_VALUES::CRR;
- break;
- case RUNE_STAT_ID::CRD:
- $list = RUNE_STAT_VALUES::CRD;
- break;
- case RUNE_STAT_ID::ACC:
- $list = RUNE_STAT_VALUES::ACC;
- break;
- case RUNE_STAT_ID::RES:
- $list = RUNE_STAT_VALUES::RES;
- break;
- }
- $efficiency += floatval($list[$stars][$level]) / floatval($list[6][15]);
- // Innate stat
- if ($rune->{"prefix_eff"}[0] > 0){
- $innate_stat = $rune->{"pri_eff"}[0];
- $innate_stat_value = $rune->{"pri_eff"}[1];
- $efficiency += floatval($innate_stat_value) / (5 * floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$innate_stat][6]));
- }
- // Substats
- for ($i = 0; $i <= 3; $i ++){
- if (sizeof($rune->{"sec_eff"}) > $i){
- $substat = $rune->{"sec_eff"}[$i][0];
- $substat_value = $rune->{"sec_eff"}[$i][1];
- $efficiency += floatval($substat_value) / (5 * floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$substat][6]));
- }
- }
- $efficiency = $efficiency / 2.8 * 100;
- if ($efficiency > 100){
- $efficiency = 100;
- }
- return $efficiency;
- }
- /**
- * Calculates the maximum efficiency of a rune.
- *
- * @param JSON $rune JSON fragment of the rune.
- * @return int Maximum efficiency (0-100)
- */
- function calculate_maximum_efficiency($rune){
- $efficiency = 0.0;
- $max_efficiency = 0.0;
- $substats = [];
- $stars = $rune->{"class"};
- // Ancient stars are + 10
- if ($stars > 10){
- $stars -= 10;
- }
- $list = null;
- // Main stat
- $main_stat = $rune->{"pri_eff"}[0];
- switch ($main_stat){
- case RUNE_STAT_ID::HP:
- $list = RUNE_STAT_VALUES::HP;
- break;
- case RUNE_STAT_ID::HP_P:
- $list = RUNE_STAT_VALUES::HP_P;
- break;
- case RUNE_STAT_ID::ATK:
- $list = RUNE_STAT_VALUES::ATK;
- break;
- case RUNE_STAT_ID::ATK_P:
- $list = RUNE_STAT_VALUES::ATK_P;
- break;
- case RUNE_STAT_ID::DEF:
- $list = RUNE_STAT_VALUES::DEF;
- break;
- case RUNE_STAT_ID::DEF_P:
- $list = RUNE_STAT_VALUES::DEF_P;
- break;
- case RUNE_STAT_ID::SPD:
- $list = RUNE_STAT_VALUES::SPD;
- break;
- case RUNE_STAT_ID::CRR:
- $list = RUNE_STAT_VALUES::CRR;
- break;
- case RUNE_STAT_ID::CRD:
- $list = RUNE_STAT_VALUES::CRD;
- break;
- case RUNE_STAT_ID::ACC:
- $list = RUNE_STAT_VALUES::ACC;
- break;
- case RUNE_STAT_ID::RES:
- $list = RUNE_STAT_VALUES::RES;
- break;
- }
- $efficiency += floatval($list[$stars][15]) / floatval($list[6][15]);
- // Innate stat
- if ($rune->{"prefix_eff"}[0] > 0){
- $innate_stat = $rune->{"pri_eff"}[0];
- $innate_stat_value = $rune->{"pri_eff"}[1];
- $efficiency += floatval($innate_stat_value) / (5 * floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$innate_stat][6]));
- }
- // Substats
- for ($i = 0; $i <= 3; $i ++){
- if (sizeof($rune->{"sec_eff"}) > $i){
- $substat = $rune->{"sec_eff"}[$i][0];
- $efficiency += floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$substat][$stars]) / (5 * floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$substat][6]));
- }
- }
- $efficiency = $efficiency / 2.8 * 100;
- if ($efficiency > 100){
- $efficiency = 100;
- }
- return $efficiency;
- }
-
- /**
- * Parses an artifact and saves it to the database.
- *
- * @param string $uid Owner ID.
- * @param JSON artifact JSON fragment of the artifact.
- */
- function parse_artifact($uid, $artifact){
- global $db;
- $statement = $db->prepare("
- INSERT INTO artifact (
- uid,
- id,
- assigned_to,
- type,
- attribute,
- archetype,
- level,
- quality,
- original_quality,
- value,
- efficiency,
- max_efficiency,
- main_stat,
- main_stat_value,
- effect_1,
- effect_1_value,
- effect_1_enchant,
- effect_1_grind,
- effect_2,
- effect_2_value,
- effect_2_enchant,
- effect_2_grind,
- effect_3,
- effect_3_value,
- effect_3_enchant,
- effect_3_grind,
- effect_4,
- effect_4_value,
- effect_4_enchant,
- effect_4_grind,
- locked
- ) VALUES (
- :uid,
- :id,
- :assigned_to,
- :type,
- :attribute,
- :archetype,
- :level,
- :quality,
- :original_quality,
- :value,
- :efficiency,
- :max_efficiency,
- :main_stat,
- :main_stat_value,
- :effect_1,
- :effect_1_value,
- :effect_1_enchant,
- :effect_1_grind,
- :effect_2,
- :effect_2_value,
- :effect_2_enchant,
- :effect_2_grind,
- :effect_3,
- :effect_3_value,
- :effect_3_enchant,
- :effect_3_grind,
- :effect_4,
- :effect_4_value,
- :effect_4_enchant,
- :effect_4_grind,
- :locked
- );
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":id", $artifact->{"rid"});
- if (intval($artifact->{"occupied_id"}) == 0){
- $statement->bindValue(":assigned_to", null);
- }
- else{
- $statement->bindValue(":assigned_to", $artifact->{"occupied_id"});
- }
- if (intval($artifact->{"attribute"}) == 0){
- $statement->bindValue(":attribute", null);
- }
- else{
- $statement->bindValue(":attribute", $artifact->{"attribute"});
- }
- if (intval($artifact->{"type"}) == 0){
- $statement->bindValue(":type", null);
- }
- else{
- $statement->bindValue(":type", $artifact->{"type"});
- }
- if (intval($artifact->{"unit_style"}) == 0){
- $statement->bindValue(":archetype", null);
- }
- else{
- $statement->bindValue(":archetype", $artifact->{"unit_style"});
- }
- $statement->bindValue(":level", $artifact->{"level"});
- $statement->bindValue(":quality", $artifact->{"rank"});
- $statement->bindValue(":original_quality", $artifact->{"natural_rank"});
- $statement->bindValue(":value", 0); // Not in JSON file
- $statement->bindValue(":locked", $artifact->{"locked"});
- $efficiency = 0; // Not standard formula yet
- $max_efficiency = 0; // Not standard formula yet
- $statement->bindValue(":efficiency", $efficiency);
- $statement->bindValue(":max_efficiency", $max_efficiency);
- $statement->bindValue(":main_stat", $artifact->{"pri_effect"}[0]);
- $statement->bindValue(":main_stat_value", $artifact->{"pri_effect"}[1]);
- if (sizeof($artifact->{"sec_effects"}) > 0){
- $statement->bindValue(":effect_1", $artifact->{"sec_effects"}[0][0]);
- $statement->bindValue(":effect_1_value", $artifact->{"sec_effects"}[0][1]);
- $statement->bindValue(":effect_1_enchant", $artifact->{"sec_effects"}[0][2]);
- $statement->bindValue(":effect_1_grind", $artifact->{"sec_effects"}[0][3]);
- }
- else{
- $statement->bindValue(":effect_1", null);
- $statement->bindValue(":effect_1_value", 0);
- $statement->bindValue(":effect_1_enchant", 0);
- $statement->bindValue(":effect_1_grind", 0);
- }
- if (sizeof($artifact->{"sec_effects"}) > 1){
- $statement->bindValue(":effect_2", $artifact->{"sec_effects"}[1][0]);
- $statement->bindValue(":effect_2_value", $artifact->{"sec_effects"}[1][1]);
- $statement->bindValue(":effect_2_enchant", $artifact->{"sec_effects"}[1][2]);
- $statement->bindValue(":effect_2_grind", $artifact->{"sec_effects"}[1][3]);
- }
- else{
- $statement->bindValue(":effect_2", null);
- $statement->bindValue(":effect_2_value", 0);
- $statement->bindValue(":effect_2_enchant", 0);
- $statement->bindValue(":effect_2_grind", 0);
- }
- if (sizeof($artifact->{"sec_effects"}) > 2){
- $statement->bindValue(":effect_3", $artifact->{"sec_effects"}[2][0]);
- $statement->bindValue(":effect_3_value", $artifact->{"sec_effects"}[2][1]);
- $statement->bindValue(":effect_3_enchant", $artifact->{"sec_effects"}[2][2]);
- $statement->bindValue(":effect_3_grind", $artifact->{"sec_effects"}[2][3]);
- }
- else{
- $statement->bindValue(":effect_3", null);
- $statement->bindValue(":effect_3_value", 0);
- $statement->bindValue(":effect_3_enchant", 0);
- $statement->bindValue(":effect_3_grind", 0);
- }
- if (sizeof($artifact->{"sec_effects"}) > 3){
- $statement->bindValue(":effect_4", $artifact->{"sec_effects"}[3][0]);
- $statement->bindValue(":effect_4_value", $artifact->{"sec_effects"}[3][1]);
- $statement->bindValue(":effect_4_enchant", $artifact->{"sec_effects"}[3][2]);
- $statement->bindValue(":effect_4_grind", $artifact->{"sec_effects"}[3][3]);
- }
- else{
- $statement->bindValue(":effect_4", null);
- $statement->bindValue(":effect_4_value", 0);
- $statement->bindValue(":effect_4_enchant", 0);
- $statement->bindValue(":effect_4_grind", 0);
- }
- $statement->execute();
- }
- /**
- * Parses a rune and saves it to the database.
- *
- * @param string $uid Owner ID.
- * @param JSON rune JSON fragment of the rune.
- */
- function parse_rune($uid, $rune, $lock_list){
- global $db;
- $statement = $db->prepare("
- INSERT INTO rune (
- uid,
- id,
- assigned_to,
- type,
- slot,
- stars,
- ancient,
- level,
- quality,
- original_quality,
- value,
- efficiency,
- max_efficiency,
- main_stat,
- main_stat_value,
- innate_stat,
- innate_stat_value,
- substat_1,
- substat_1_value,
- substat_1_enchant,
- substat_1_grind,
- substat_2,
- substat_2_value,
- substat_2_enchant,
- substat_2_grind,
- substat_3,
- substat_3_value,
- substat_3_enchant,
- substat_3_grind,
- substat_4,
- substat_4_value,
- substat_4_enchant,
- substat_4_grind,
- locked
- ) VALUES (
- :uid,
- :id,
- :assigned_to,
- :type,
- :slot,
- :stars,
- :ancient,
- :level,
- :quality,
- :original_quality,
- :value,
- :efficiency,
- :max_efficiency,
- :main_stat,
- :main_stat_value,
- :innate_stat,
- :innate_stat_value,
- :substat_1,
- :substat_1_value,
- :substat_1_enchant,
- :substat_1_grind,
- :substat_2,
- :substat_2_value,
- :substat_2_enchant,
- :substat_2_grind,
- :substat_3,
- :substat_3_value,
- :substat_3_enchant,
- :substat_3_grind,
- :substat_4,
- :substat_4_value,
- :substat_4_enchant,
- :substat_4_grind,
- :locked
- );
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":id", $rune->{"rune_id"});
- if (intval($rune->{"occupied_id"}) == 0){
- $statement->bindValue(":assigned_to", null);
- }
- else{
- $statement->bindValue(":assigned_to", $rune->{"occupied_id"});
- }
- $statement->bindValue(":type", $rune->{"set_id"});
- $statement->bindValue(":slot", $rune->{"slot_no"});
- $statement->bindValue(":value", $rune->{"sell_value"});
- if ($rune->{"class"} > 10){
- // Ancient rune
- $statement->bindValue(":ancient", 1);
- $statement->bindValue(":stars", intval($rune->{"class"}) - 10);
- $statement->bindValue(":original_quality", intval($rune->{"extra"}) - 10);
- }
- else{
- // Non Ancient rune
- $statement->bindValue(":ancient", 0);
- $statement->bindValue(":stars", $rune->{"class"});
- $statement->bindValue(":original_quality", $rune->{"extra"});
- }
- $level = $rune->{"upgrade_curr"};
- $statement->bindValue(":level", $level);
- $quality = QUALITY_ID::NORMAL;
- if ($level >= 12){
- $quality = QUALITY_ID::LEGEND;
- }
- elseif ($level >= 9){
- $quality = QUALITY_ID::HERO;
- }
- elseif ($level >= 6){
- $quality = QUALITY_ID::RARE;
- }
- elseif ($level >= 3){
- $quality = QUALITY_ID::MAGIC;
- }
- if ($rune->{"extra"} > $quality){
- $quality = $rune->{"extra"};
- }
- $statement->bindValue(":quality", $quality);
- $lock = 0;
- if (in_array($rune->{"rune_id"}, $lock_list)){
- $lock = 1;
- }
- $statement->bindValue(":locked", $lock);
- $efficiency = calculate_efficiency($rune);
- $max_efficiency = calculate_maximum_efficiency($rune);
- $statement->bindValue(":efficiency", $efficiency);
- $statement->bindValue(":max_efficiency", $max_efficiency);
- $statement->bindValue(":main_stat", $rune->{"pri_eff"}[0]);
- $statement->bindValue(":main_stat_value", $rune->{"pri_eff"}[1]);
- if ($rune->{"prefix_eff"}[0] > 0){
- $statement->bindValue(":innate_stat", $rune->{"prefix_eff"}[0]);
- $statement->bindValue(":innate_stat_value", $rune->{"prefix_eff"}[1]);
- }
- else{
- $statement->bindValue(":innate_stat", null);
- $statement->bindValue(":innate_stat_value", 0);
- }
- if (sizeof($rune->{"sec_eff"}) > 0){
- $statement->bindValue(":substat_1", $rune->{"sec_eff"}[0][0]);
- $statement->bindValue(":substat_1_value", $rune->{"sec_eff"}[0][1]);
- $statement->bindValue(":substat_1_enchant", $rune->{"sec_eff"}[0][2]);
- $statement->bindValue(":substat_1_grind", $rune->{"sec_eff"}[0][3]);
- }
- else{
- $statement->bindValue(":substat_1", null);
- $statement->bindValue(":substat_1_value", 0);
- $statement->bindValue(":substat_1_enchant", 0);
- $statement->bindValue(":substat_1_grind", 0);
- }
- if (sizeof($rune->{"sec_eff"}) > 1){
- $statement->bindValue(":substat_2", $rune->{"sec_eff"}[1][0]);
- $statement->bindValue(":substat_2_value", $rune->{"sec_eff"}[1][1]);
- $statement->bindValue(":substat_2_enchant", $rune->{"sec_eff"}[1][2]);
- $statement->bindValue(":substat_2_grind", $rune->{"sec_eff"}[1][3]);
- }
- else{
- $statement->bindValue(":substat_2", null);
- $statement->bindValue(":substat_2_value", 0);
- $statement->bindValue(":substat_2_enchant", 0);
- $statement->bindValue(":substat_2_grind", 0);
- }
- if (sizeof($rune->{"sec_eff"}) > 2){
- $statement->bindValue(":substat_3", $rune->{"sec_eff"}[2][0]);
- $statement->bindValue(":substat_3_value", $rune->{"sec_eff"}[2][1]);
- $statement->bindValue(":substat_3_enchant", $rune->{"sec_eff"}[2][2]);
- $statement->bindValue(":substat_3_grind", $rune->{"sec_eff"}[2][3]);
- }
- else{
- $statement->bindValue(":substat_3", null);
- $statement->bindValue(":substat_3_value", 0);
- $statement->bindValue(":substat_3_enchant", 0);
- $statement->bindValue(":substat_3_grind", 0);
- }
- if (sizeof($rune->{"sec_eff"}) > 3){
- $statement->bindValue(":substat_4", $rune->{"sec_eff"}[3][0]);
- $statement->bindValue(":substat_4_value", $rune->{"sec_eff"}[3][1]);
- $statement->bindValue(":substat_4_enchant", $rune->{"sec_eff"}[3][2]);
- $statement->bindValue(":substat_4_grind", $rune->{"sec_eff"}[3][3]);
- }
- else{
- $statement->bindValue(":substat_4", null);
- $statement->bindValue(":substat_4_value", 0);
- $statement->bindValue(":substat_4_enchant", 0);
- $statement->bindValue(":substat_4_grind", 0);
- }
- $statement->execute();
- }
- /**
- * Parses the response to the HubUserLogin command and updates profile.
- *
- * @param SQLiteConn $db Database connection.
- * @param int $uid Player UID.
- * @param JSON $json Response.
- */
- function parse_profile($db, $uid, $json){
- // Save data file
- $uname = $json->{"wizard_info"}->{"wizard_name"};
- $dtime = (new DateTime())->format("Y-m-dTH:i:s");
- $fname = ($_SERVER["DOCUMENT_ROOT"] . "/../data/profile_" . $uid . "_" . $uname . "_" . $dtime . ".json");
- try{
- file_put_contents($fname, json_encode($json));
- }
- catch(Exception $e) {
- header("HTTP/1.1 500 Unable to save profile file: " . $e->getMessage());
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 500 Unable to save profile file: " . $e->getMessage());
- return 500;
- }
- // Clear previous profile data
- $db->query("PRAGMA foreign_keys = OFF;");
- $statement = $db->prepare("DELETE FROM unit_skill WHERE unit IN (SELECT id FROM unit WHERE uid = :uid);");
- $statement->bindValue(":uid", $uid);
- $statement->execute();
- $tables_to_clear = [
- "scenario",
- "defense",
- "gw_defense",
- "unit",
- "rune",
- "artifact",
- "building",
- "decoration",
- "inventory",
- "enchantment"
- ];
- foreach ($tables_to_clear as $table){
- $statement = $db->prepare("DELETE FROM $table WHERE uid = :uid;");
- $statement->bindValue(":uid", $uid);
- $statement->execute();
- }
- $db->query("DELETE FROM summon_special"); # For every player, will be reloaded.
- // Get rune lock list
- $rune_lock_list = $json->{"rune_lock_list"};
- // Update player data
- $statement = $db->prepare("
- UPDATE player
- SET
- name = :name AND
- mana = :mana AND
- crystal = :crystal AND
- country = :country AND
- lang = :lang AND
- level = :level AND
- experience = :experience AND
- energy = :energy AND
- energy_max = :energy_max AND
- energy_per_min = :energy_per_min AND
- arena_energy = :arena_energy AND
- arena_energy_max = :arena_energy_max AND
- rep = :rep AND
- social_point = :social_point AND
- honor_point = :honor_point AND
- guild_point = :guild_point AND
- darkportal_energy = :darkportal_energy AND
- darkportal_energy_max = :darkportal_energy_max AND
- dimension_energy = :dimension_energy AND
- dimension_energy_max = :dimension_energy_max AND
- costume_point = :costume_point AND
- costume_point_max = :costume_point_max AND
- honor_medal = :honor_medal AND
- honor_mark = :honor_mark AND
- event_coin = :event_coin AND
- storage_slots = :storage_slots AND
- island = :island_upgrade
- WHERE uid = :uid;
- ");
- $statement->bindValue(":name", $json->{"wizard_info"}->{"wizard_name"});
- $statement->bindValue(":mana", $json->{"wizard_info"}->{"wizard_mana"});
- $statement->bindValue(":crystal", $json->{"wizard_info"}->{"wizard_crystal"});
- $statement->bindValue(":country", $json->{"wizard_info"}->{"wizard_last_country"});
- $statement->bindValue(":lang", $json->{"wizard_info"}->{"wizard_last_lang"});
- $statement->bindValue(":level", $json->{"wizard_info"}->{"wizard_level"});
- $statement->bindValue(":experience", $json->{"wizard_info"}->{"experience"});
- $statement->bindValue(":energy", $json->{"wizard_info"}->{"wizard_energy"});
- $statement->bindValue(":energy_max", $json->{"wizard_info"}->{"energy_max"});
- $statement->bindValue(":energy_per_min", $json->{"wizard_info"}->{"energy_per_min"});
- $statement->bindValue(":arena_energy", $json->{"wizard_info"}->{"arena_energy"});
- $statement->bindValue(":arena_energy_max", $json->{"wizard_info"}->{"arena_energy_max"});
- $statement->bindValue(":rep", $json->{"wizard_info"}->{"rep_unit_id"});
- $statement->bindValue(":social_point", $json->{"wizard_info"}->{"social_point_current"});
- $statement->bindValue(":honor_point", $json->{"wizard_info"}->{"honor_point"});
- $statement->bindValue(":guild_point", $json->{"wizard_info"}->{"guild_point"});
- $statement->bindValue(":darkportal_energy", $json->{"wizard_info"}->{"darkportal_energy"});
- $statement->bindValue(":darkportal_energy_max", $json->{"wizard_info"}->{"darkportal_energy_max"});
- $statement->bindValue(":dimension_energy", $json->{"dimension_hole_info"}->{"energy"});
- $statement->bindValue(":dimension_energy_max", $json->{"dimension_hole_info"}->{"energy_max"});
- $statement->bindValue(":costume_point", $json->{"wizard_info"}->{"costume_point"});
- $statement->bindValue(":costume_point_max", $json->{"wizard_info"}->{"costume_point_max"});
- $statement->bindValue(":honor_medal", $json->{"wizard_info"}->{"honor_medal"});
- $statement->bindValue(":honor_mark", $json->{"wizard_info"}->{"honor_mark"});
- $statement->bindValue(":event_coin", $json->{"wizard_info"}->{"event_coin"});
- $statement->bindValue(":storage_slots", $json->{"unit_depository_slots"}->{"number"});
- $island_upgrade = 0;
- foreach ($json->{"island_info"} as $island){
- if (intval($island->{"id"}) > 100 && intval($island->{"open"}) == 1){
- $island_upgrade = intval($island->{"id"});
- }
- }
- $statement->bindValue(":island_upgrade", $island_upgrade);
- $statement->bindValue(":uid", $uid);
- $statement->execute();
- // Update scenarion completion
- foreach ($json->{"scenario_list"} as $sce){
- $statement = $db->prepare("
- INSERT INTO scenario (
- uid,
- region,
- difficulty,
- cleared,
- max_cleared
- ) VALUES (
- :uid,
- :region,
- :difficulty,
- :cleared,
- :max_cleared
- );
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":region", $sce->{"region_id"});
- $statement->bindValue(":difficulty", $sce->{"difficulty"});
- $statement->bindValue(":cleared", $sce->{"cleared"});
- $max_cleared = 0;
- foreach ($sce->{"stage_list"} as $stage){
- if (intval($stage->{"cleared"}) == 1){
- $max_cleared = intval($stage->{"stage_no"});
- }
- }
- $statement->bindValue(":max_cleared", $max_cleared);
- $statement->execute();
- }
- // Parse Arena defense
- foreach ($json->{"defense_unit_list"} as $defense){
- $statement = $db->prepare("
- INSERT INTO defense (
- uid,
- unit,
- position
- ) VALUES (
- :uid,
- :unit,
- :position
- );
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":unit", $defense->{"unit_id"});
- $statement->bindValue(":position", $defense->{"pos_id"});
- $statement->execute();
- }
- // Parse GW defense
- if ($json->{"guildwar_defense_unit_list"}[0]){
- $position = 1;
- foreach ($json->{"guildwar_defense_unit_list"}[0] as $defense){
- $statement = $db->prepare("
- INSERT INTO gw_defense (
- uid,
- unit,
- position
- ) VALUES (
- :uid,
- :unit,
- :position
- );
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":unit", $defense->{"unit_id"});
- $statement->bindValue(":position", $position);
- $statement->execute();
- $position ++;
- }
- }
- if ($json->{"guildwar_defense_unit_list"}[1]){
- $position = 4;
- foreach ($json->{"guildwar_defense_unit_list"}[1] as $defense){
- $statement = $db->prepare("
- INSERT INTO gw_defense (
- uid,
- unit,
- position
- ) VALUES (
- :uid,
- :unit,
- :position
- );
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":unit", $defense->{"unit_id"});
- $statement->bindValue(":position", $position);
- $statement->execute();
- $position ++;
- }
- }
- // Parse buildings
- foreach($json->{"building_list"} as $building){
- $statement = $db->prepare("
- INSERT INTO building (
- uid,
- id,
- building,
- gain
- ) VALUES (
- :uid,
- :id,
- :building,
- :gain
- );
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":id", $building->{"building_id"});
- $statement->bindValue(":building", $building->{"building_master_id"});
- $statement->bindValue(":gain", $building->{"gain_per_hour"});
- $statement->execute();
- }
- // Parse decorarion
- foreach($json->{"deco_list"} as $building){
- $statement = $db->prepare("
- INSERT INTO decoration (
- uid,
- id,
- decoration,
- level
- ) VALUES (
- :uid,
- :id,
- :decoration,
- :level
- );
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":id", $building->{"deco_id"});
- $statement->bindValue(":decoration", $building->{"master_id"});
- $statement->bindValue(":level", $building->{"level"});
- $statement->execute();
- }
- // Parse units
- $lock_list = $json->{"unit_lock_list"};
- foreach ($json->{"unit_list"} as $unit){
- $statement = $db->prepare("
- INSERT INTO unit (
- uid,
- id,
- building,
- unit,
- level,
- stars,
- hp,
- attack,
- defense,
- speed,
- crit_rate,
- crit_damage,
- resistance,
- accuracy,
- experience,
- exp_gained,
- exp_gain_rate,
- costume,
- attribute,
- source,
- create_time,
- homunculus,
- homunculus_name,
- lock
- ) VALUES (
- :uid,
- :id,
- :building,
- :unit,
- :level,
- :stars,
- :hp,
- :attack,
- :defense,
- :speed,
- :crit_rate,
- :crit_damage,
- :resistance,
- :accuracy,
- :experience,
- :exp_gained,
- :exp_gain_rate,
- :costume,
- :attribute,
- :source,
- :create_time,
- :homunculus,
- :homunculus_name,
- :lock
- );
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":id", $unit->{"unit_id"});
- $statement->bindValue(":building", $unit->{"building_id"});
- $statement->bindValue(":unit", $unit->{"unit_master_id"});
- $statement->bindValue(":level", $unit->{"unit_level"});
- $statement->bindValue(":stars", $unit->{"class"});
- $statement->bindValue(":hp", intval($unit->{"con"}) * 15); # Always x15
- $statement->bindValue(":attack", $unit->{"atk"});
- $statement->bindValue(":defense", $unit->{"def"});
- $statement->bindValue(":speed", $unit->{"spd"});
- $statement->bindValue(":crit_rate", $unit->{"critical_rate"});
- $statement->bindValue(":crit_damage", $unit->{"critical_damage"});
- $statement->bindValue(":resistance", $unit->{"resist"});
- $statement->bindValue(":accuracy", $unit->{"accuracy"});
- $statement->bindValue(":experience", $unit->{"experience"});
- $statement->bindValue(":exp_gained", $unit->{"exp_gained"});
- $statement->bindValue(":exp_gain_rate", $unit->{"exp_gain_rate"});
- $statement->bindValue(":costume", $unit->{"costume_master_id"});
- $statement->bindValue(":attribute", $unit->{"attribute"});
- $statement->bindValue(":source", $unit->{"source"});
- $statement->bindValue(":create_time", $unit->{"create_time"});
- $statement->bindValue(":homunculus", $unit->{"homunculus"});
- $statement->bindValue(":homunculus_name", $unit->{"homunculus_name"});
- $lock = 0;
- if (in_array($unit->{"unit_id"}, $lock_list)){
- $lock = 1;
- }
- $statement->bindValue(":lock", $lock);
- $statement->execute();
- foreach ($unit->{"skills"} as $skill){
- $statement = $db->prepare("
- INSERT INTO unit_skill (
- unit,
- skill,
- level
- ) VALUES (
- :unit,
- :skill,
- :level
- );
- ");
- $statement->bindValue(":unit", $unit->{"unit_id"});
- $statement->bindValue(":skill", $skill[0]);
- $statement->bindValue(":level", $skill[1]);
- $statement->execute();
- }
- }
- // Parse invetory
- foreach ($json->{"inventory_info"} as $item){
- $statement = $db->prepare("
- INSERT INTO inventory (
- uid,
- id,
- type,
- amount
- ) VALUES (
- :uid,
- :id,
- :type,
- :amount
- );
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":id", $item->{"item_master_id"});
- $statement->bindValue(":type", $item->{"item_master_type"});
- $statement->bindValue(":amount", $item->{"item_quantity"});
- $statement->execute();
- }
- // Fix rune craft item type.
- $db->query("UPDATE inventory SET type = 27 WHERE type = 29 AND id IN (2001, 4001, 4002, 4003, 9001, 9002, 9003, 8001);");
- // Parse Summon Stone summoning list
- // TODO: Set propper dates
- foreach ($json->{"summon_special_info"}->{"this"} as $unit){
- $statement = $db->prepare("
- INSERT INTO summon_special (
- week,
- unit
- ) VALUES (
- :week,
- :unit
- );
- ");
- $statement->bindValue(":week", 0);
- $statement->bindValue(":unit", $unit);
- $statement->execute();
- }
- foreach ($json->{"summon_special_info"}->{"next"} as $unit){
- $statement = $db->prepare("
- INSERT INTO summon_special (
- week,
- unit
- ) VALUES (
- :week,
- :unit
- );
- ");
- $statement->bindValue(":week", 1);
- $statement->bindValue(":unit", $unit);
- $statement->execute();
- }
- foreach ($json->{"summon_special_info"}->{"third"} as $unit){
- $statement = $db->prepare("
- INSERT INTO summon_special (
- week,
- unit
- ) VALUES (
- :week,
- :unit
- );
- ");
- $statement->bindValue(":week", 2);
- $statement->bindValue(":unit", $unit);
- $statement->execute();
- }
- foreach ($json->{"summon_special_info"}->{"fourth"} as $unit){
- $statement = $db->prepare("
- INSERT INTO summon_special (
- week,
- unit
- ) VALUES (
- :week,
- :unit
- );
- ");
- $statement->bindValue(":week", 3);
- $statement->bindValue(":unit", $unit);
- $statement->execute();
- }
- // Parse runes
- foreach ($json->{"runes"} as $rune){
- parse_rune($uid, $rune, $rune_lock_list);
- }
- foreach ($json->{"unit_list"} as $unit){
- foreach ($unit->{"runes"} as $rune){
- parse_rune($uid, $rune, $rune_lock_list);
- }
- }
- // Parse artifacts
- foreach ($json->{"artifacts"} as $artifact){
- parse_artifact($uid, $artifact);
- }
- foreach ($json->{"unit_list"} as $unit){
- foreach ($unit->{"artifacts"} as $artifact){
- parse_artifact($uid, $artifact);
- }
- }
- // Parse enchantments
- foreach ($json->{"rune_craft_item_list"} as $item){
- $statement = $db->prepare("
- INSERT INTO enchantment (
- uid,
- id,
- type,
- quality,
- rune,
- stat,
- value,
- amount
- ) VALUES (
- :uid,
- :id,
- :type,
- :quality,
- :rune,
- :stat,
- :value,
- :amount
- );
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":id", $item->{"craft_item_id"});
- $statement->bindValue(":type", $item->{"craft_type"});
- $statement->bindValue(":value", $item->{"sell_value"});
- $statement->bindValue(":amount", $item->{"amount"});
- $info = str_pad(intval($item->{"craft_type_id"}), 6, "0", STR_PAD_LEFT);
- /*
- info : 5 or 6 digit number: RRSSQQ
- RR: Rune (may not be padded)
- SS: Stat
- QQ: Quality
- */
- $statement->bindValue(":quality", intval(substr($info, 4, 2)));
- $statement->bindValue(":stat", intval(substr($info, 2, 2)));
- $statement->bindValue(":rune", intval(substr($info, 0, 2)));
- $statement->execute();
- }
- // Parse guild
- if ($json->{"guild"}->{"guild_info"} != null){
- $guild = $json->{"guild"}->{"guild_info"};
- $statement = $db->prepare("DELETE FROM guild WHERE id = :id;");
- $statement->bindValue(":id", $guild->{"guild_id"});
- $statement->execute();
- $statement = $db->prepare("DELETE FROM guild_member WHERE guild = :guild;");
- $statement->bindValue(":guild", $guild->{"guild_id"});
- $statement->execute();
- $statement = $db->prepare("
- INSERT INTO guild (
- id,
- name,
- level,
- experience,
- recruiting,
- members,
- leader,
- comment,
- notice
- ) VALUES (
- :id,
- :name,
- :level,
- :experience,
- :recruiting,
- :members,
- :leader,
- :comment,
- :notice
- );
- ");
- $statement->bindValue(":id",$guild->{"guild_id"});
- $statement->bindValue(":name",$guild->{"name"});
- $statement->bindValue(":level",$guild->{"level"});
- $statement->bindValue(":experience",$guild->{"experience"});
- $statement->bindValue(":recruiting",$guild->{"recruit_status"});
- $statement->bindValue(":members",$guild->{"member_now"});
- $statement->bindValue(":leader",$guild->{"master_wizard_id"});
- $statement->bindValue(":comment",$guild->{"comment"});
- $statement->bindValue(":notice",$guild->{"notice"});
- $statement->execute();
- foreach ($json->{"guild"}->{"guild_members"} as $member){
- $statement = $db->prepare("
- INSERT INTO guild_member (
- id,
- guild,
- grade,
- name,
- level,
- rating,
- arena_score,
- joined,
- last_login,
- in_war,
- has_defense
- ) VALUES (
- :id,
- :guild,
- :grade,
- :name,
- :level,
- :rating,
- :arena_score,
- :joined,
- :last_login,
- :in_war,
- :has_defense
- );
- ");
- $statement->bindValue(":id", $member->{"wizard_id"});
- $statement->bindValue(":guild", $member->{"guild_id"});
- $statement->bindValue(":grade", $member->{"grade"});
- $statement->bindValue(":name", $member->{"wizard_name"});
- $statement->bindValue(":level", $member->{"wizard_level"});
- $statement->bindValue(":rating", $member->{"rating_id"});
- $statement->bindValue(":arena_score", $member->{"arena_score"});
- $statement->bindValue(":joined", $member->{"join_timestamp"});
- $statement->bindValue(":last_login", $member->{"last_login_timestamp"});
- $in_war = 0;
- foreach ($json->{"guildwar_member_list"} as $war){
- if ($war->{"wizard_id"} == $member->{"wizard_id"}){
- $in_war = 1;
- break;
- }
- }
- $has_defense = 0;
- foreach ($json->{"guild_member_defense_list"} as $defense){
- if ($defense->{"wizard_id"} == $member->{"wizard_id"}){
- $has_defense = 1;
- break;
- }
- }
- $statement->bindValue(":in_war", $in_war);
- $statement->bindValue(":has_defense", $has_defense);
- $statement->execute();
- }
- }
- // Update invalid teams
- $statement = $db->prepare("
- DELETE FROM team
- WHERE
- id IN (
- SELECT DISTINCT team
- FROM team_unit
- WHERE unit NOT IN (
- SELECT DISTINCT id FROM unit WHERE uid = :uid
- )
- ) AND
- uid = :uid
- ");
- $statement->bindValue(":uid", $uid);
- $statement->execute();
- $db->query("
- DELETE FROM team_unit
- WHERE
- unit NOT IN (
- SELECT DISTINCT id FROM unit
- ) OR
- team NOT IN (
- SELECT DISTINCT id FROM team
- );"
- );
- // Return success
- return "201 Created.";
- }
- /**
- * Parses the response to the GetUnitCollection command and updates the
- * user monster collection status.
- *
- * @param SQLiteConn $db Database connection.
- * @param int $uid Player UID.
- * @param JSON $json Response.
- */
- function parse_collection($db, $uid, $json){
- // Clear previous collection data
- $db->query("PRAGMA foreign_keys = OFF;");
- $statement = $db->prepare("DELETE FROM collection WHERE uid = :uid;");
- $statement->bindValue(":uid", $uid);
- $statement->execute();
- foreach ($json->{"collection"} as $unit){
- $statement = $db->prepare("
- INSERT INTO collection (uid, unit, open)
- VALUES (:uid, :unit, :open);
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":unit", $unit->{"unit_master_id"});
- $statement->bindValue(":open", $unit->{"open"});
- $statement->execute();
- }
- // Return success
- return "201 Created.";
- }
- /**
- * Parses the response to the GetLobbyWizardLog command and updates the
- * user records.
- *
- * @param SQLiteConn $db Database connection.
- * @param int $uid Player UID.
- * @param JSON $json Response.
- */
- function parse_records($db, $uid, $json){
- # Page 1: Cairos non-elemental, rift raid, rift dungeon.
- if (intval($json->{"lobby_wizard_log"}->{"page_no"}) == 1){
- // Update data in player table
- $db->query("PRAGMA foreign_keys = OFF;");
- $statement = $db->prepare("
- UPDATE player SET
- joined = :joined,
- top_rank_arena = :top_rank_arena,
- top_rank_world_arena = :top_rank_world_arena,
- top_rank_special_league = :top_rank_special_league,
- top_rank_gw = :top_rank_gw,
- top_rank_siege = :top_rank_siege,
- top_rank_wboss = :top_rank_wboss,
- top_rank_toan = :top_rank_toan,
- top_rank_toah = :top_rank_toah
- WHERE uid = :uid;
- ");
- $statement->bindValue(":joined", $json->{"lobby_wizard_log"}->{"account_create_timestamp"});
- $statement->bindValue(":top_rank_arena", $json->{"lobby_wizard_log"}->{"pvp_best_rating_id"});
- $statement->bindValue(":top_rank_world_arena", $json->{"lobby_wizard_log"}->{"rtpvp_rank_best_rating_id"});
- $statement->bindValue(":top_rank_special_league", $json->{"lobby_wizard_log"}->{"rtpvp_contest_best_rating_id"});
- $statement->bindValue(":top_rank_gw", $json->{"lobby_wizard_log"}->{"guildwar_best_rating_id"});
- $statement->bindValue(":top_rank_siege", $json->{"lobby_wizard_log"}->{"guildsiege_best_rating_id"});
- $statement->bindValue(":top_rank_wboss", $json->{"lobby_wizard_log"}->{"world_boss_best_rank_id"});
- $statement->bindValue(":top_rank_toan", $json->{"lobby_wizard_log"}->{"trial_tower_normal_best_floor"});
- $statement->bindValue(":top_rank_toah", $json->{"lobby_wizard_log"}->{"trial_tower_hard_best_floor"});
- $statement->bindValue(":uid", $uid);
- $statement->execute();
- // Loop Cairos records
- foreach ($json->{"lobby_wizard_log"}->{"dungeon_best_clear_info_list"} as $record){
- // Delete prevous data
- $statement = $db->prepare("
- DELETE FROM record_party
- WHERE
- uid = :uid AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON);
- $statement->bindValue(":area", $record->{"dungeon_id"});
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM record
- WHERE
- uid = :uid AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON);
- $statement->bindValue(":area", $record->{"dungeon_id"});
- $statement->execute();
- // Insert new data
- $statement = $db->prepare("
- INSERT INTO record
- (uid, area_type, area, stage, time, score, rank)
- VALUES
- (:uid, :area_type, :area, :stage, :time, :score, :rank);
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON);
- $statement->bindValue(":area", $record->{"dungeon_id"});
- $statement->bindValue(":area", $record->{"dungeon_id"});
- $statement->bindValue(":stage", $record->{"stage_id"});
- $statement->bindValue(":time", $record->{"clear_time"});
- $statement->bindValue(":score", 0);
- $statement->bindValue(":rank", null);
- $statement->execute();
- // Loop party
- foreach ($record->{"my_unit_deck_list"} as $party){
- $statement = $db->prepare("
- INSERT INTO record_party
- (uid, area_type, area, unit, k_unit, leader, front)
- VALUES
- (:uid, :area_type, :area, :unit, :k_unit, :leader, :front);
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON);
- $statement->bindValue(":area", $record->{"dungeon_id"});
- $statement->bindValue(":unit", $party->{"unit_id"});
- $statement->bindValue(":k_unit", $party->{"unit_master_id"});
- $statement->bindValue(":leader", $party->{"leader"});
- $statement->bindValue(":front", 0);
- $statement->execute();
- }
- }
- // Rift Raid record
- if (sizeof($json->{"lobby_wizard_log"}->{"raid_best_clear_info_list"}) > 0){
- $record = $json->{"lobby_wizard_log"}->{"raid_best_clear_info_list"}[0];
- // Delete prevous data
- $statement = $db->prepare("
- DELETE FROM record_party
- WHERE
- uid = :uid AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID);
- $statement->bindValue(":area", $record->{"stage_id"});
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM record
- WHERE
- uid = :uid AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID);
- $statement->bindValue(":area", $record->{"stage_id"});
- $statement->execute();
- // Insert new data
- $statement = $db->prepare("
- INSERT INTO record
- (uid, area_type, area, stage, time, score, rank)
- VALUES
- (:uid, :area_type, :area, :stage, :time, :score, :rank);
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID);
- $statement->bindValue(":area", $record->{"stage_id"});
- $statement->bindValue(":stage", $record->{"stage_id"});
- $statement->bindValue(":time", $record->{"clear_time"});
- $statement->bindValue(":score", 0);
- $statement->bindValue(":rank", null);
- $statement->execute();
- // Loop party
- foreach ($record->{"my_unit_deck_list"} as $party){
- $statement = $db->prepare("
- INSERT INTO record_party
- (uid, area_type, area, unit, k_unit, leader, front)
- VALUES
- (:uid, :area_type, :area, :unit, :k_unit, :leader, :front);
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID);
- $statement->bindValue(":area", $record->{"stage_id"});
- $statement->bindValue(":unit", $party->{"unit_id"});
- $statement->bindValue(":k_unit", $party->{"unit_master_id"});
- $statement->bindValue(":leader", $party->{"leader"});
- if ($party->{"slot_index"} <= 4){
- $statement->bindValue(":front", 1);
- }
- else{
- $statement->bindValue(":front", 0);
- }
- $statement->execute();
- }
- }
- // Loop Rift Dungeon records
- foreach ( $json->{"lobby_wizard_log"}->{"rift_dungeon_best_clear_info_list"} as $record){
- // Delete prevous data
- $statement = $db->prepare("
- DELETE FROM record_party
- WHERE
- uid = :uid AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON);
- $statement->bindValue(":area", $record->{"rift_dungeon_id"});
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM record
- WHERE
- uid = :uid AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON);
- $statement->bindValue(":area", $record->{"rift_dungeon_id"});
- $statement->execute();
- // Insert new data
- $statement = $db->prepare("
- INSERT INTO record
- (uid, area_type, area, stage, time, score, rank)
- VALUES
- (:uid, :area_type, :area, :stage, :time, :score, :rank);
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON);
- $statement->bindValue(":area", $record->{"rift_dungeon_id"});
- $statement->bindValue(":stage", 0);
- $statement->bindValue(":time", 0);
- $statement->bindValue(":score", $record->{"clear_damage"});
- switch (intval($record->{"clear_rating"})){
- case 2:
- $statement->bindValue(":rank", "D");
- break;
- case 3:
- $statement->bindValue(":rank", "C");
- break;
- case 4:
- $statement->bindValue(":rank", "B-");
- break;
- case 5:
- $statement->bindValue(":rank", "B");
- break;
- case 6:
- $statement->bindValue(":rank", "B+");
- break;
- case 7:
- $statement->bindValue(":rank", "A-");
- break;
- case 8:
- $statement->bindValue(":rank", "A");
- break;
- case 9:
- $statement->bindValue(":rank", "A+");
- break;
- case 10:
- $statement->bindValue(":rank", "S");
- break;
- case 11:
- $statement->bindValue(":rank", "SS");
- break;
- case 12:
- $statement->bindValue(":rank", "SS");
- break;
- default:
- $statement->bindValue(":rank", "F");
- }
- $statement->execute();
- // Loop party
- foreach ($record->{"my_unit_deck_list"} as $party){
- $statement = $db->prepare("
- INSERT INTO record_party
- (uid, area_type, area, unit, k_unit, leader, front)
- VALUES
- (:uid, :area_type, :area, :unit, :k_unit, :leader, :front);
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON);
- $statement->bindValue(":area", $record->{"rift_dungeon_id"});
- $statement->bindValue(":unit", $party->{"unit_id"});
- $statement->bindValue(":k_unit", $party->{"unit_master_id"});
- $statement->bindValue(":leader", $party->{"leader"});
- if ($party->{"slot_index"} <= 4){
- $statement->bindValue(":front", 1);
- }
- else{
- $statement->bindValue(":front", 0);
- }
- $statement->execute();
- }
- }
- }
- # Page 2: Cairos non-elemental, rift raid, rift dungeon.
- elseif (intval($json->{"lobby_wizard_log"}->{"page_no"} == 2)){
- // Loop Cairos records
- foreach ($json->{"lobby_wizard_log"}->{"dungeon_best_clear_info_list"} as $record){
- // Delete prevous data
- $statement = $db->prepare("
- DELETE FROM record_party
- WHERE
- uid = :uid AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON);
- $statement->bindValue(":area", $record->{"dungeon_id"});
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM record
- WHERE
- uid = :uid AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON);
- $statement->bindValue(":area", $record->{"dungeon_id"});
- $statement->execute();
- // Insert new data
- $statement = $db->prepare("
- INSERT INTO record
- (uid, area_type, area, stage, time, score, rank)
- VALUES
- (:uid, :area_type, :area, :stage, :time, :score, :rank);
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON);
- $statement->bindValue(":area", $record->{"dungeon_id"});
- $statement->bindValue(":area", $record->{"dungeon_id"});
- $statement->bindValue(":stage", $record->{"stage_id"});
- $statement->bindValue(":time", $record->{"clear_time"});
- $statement->bindValue(":score", 0);
- $statement->bindValue(":rank", null);
- $statement->execute();
- // Loop party
- foreach ($record->{"my_unit_deck_list"} as $party){
- $statement = $db->prepare("
- INSERT INTO record_party
- (uid, area_type, area, unit, k_unit, leader, front)
- VALUES
- (:uid, :area_type, :area, :unit, :k_unit, :leader, :front);
- ");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON);
- $statement->bindValue(":area", $record->{"dungeon_id"});
- $statement->bindValue(":unit", $party->{"unit_id"});
- $statement->bindValue(":k_unit", $party->{"unit_master_id"});
- $statement->bindValue(":leader", $party->{"leader"});
- $statement->bindValue(":front", 0);
- $statement->execute();
- }
- }
- }
- elseif (intval($json->{"lobby_wizard_log"}->{"page_no"}) == 3){
- return "204 Page not logged (only pages 1 and 2 are needed).";
- }
- elseif (intval($json->{"lobby_wizard_log"}->{"page_no"}) == 4){
- return "204 Page not logged (only pages 1 and 2 are needed).";
- }
- else{
- return "400 Unknown page.";
- }
- // Return success
- return "201 Created.";
- }
- try{
- header("Content-type: application/json; charset=utf-8");
- // Get put data
- parse_str(file_get_contents("php://input"), $_PUT);
- // Check API key.
- $key = $_PUT["key"];
- if ($key == null || $key == false){
- header("HTTP/1.1 400 API key not received.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 API key not received.");
- return 400;
- }
- // Check data
- $data = $_PUT["response"];
- if ($data == null || $data == false){
- header("HTTP/1.1 400 No data received.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 No data received.");
- return 400;
- }
- // Check data format.
- $json = json_decode($data);
- if ($json === null){
- header("HTTP/1.1 400 Data is no valid JSON.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 Data is no valid JSON.");
- return 400;
- }
- // Get and validate command
- $command = $json->{"command"};
- $accepted_commands = [
- "HubUserLogin", // Login, full profile
- "GetUnitCollection", // Unit collection
- "GetLobbyWizardLog" // Records
- ];
- if (!in_array($command, $accepted_commands)){
- header("HTTP/1.1 405 Command not implemented.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 405 Command not implemented.");
- return 405;
- }
- // Get user info and Authenticate
- switch ($command){
- case "HubUserLogin":
- $uid = $json->{"wizard_id"};
- $statement = $db->prepare("SELECT COUNT(uid) AS c FROM player WHERE uid = :uid AND api_key = :api_key;");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":api_key", $key);
- if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
- header("HTTP/1.1 401 Invalid credentials.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 401 Invalid credentials.");
- return 401;
- }
- break;
- case "GetUnitCollection":
- // No identification in this JSON, get uid from databases
- $statement = $db->prepare("SELECT uid FROM player WHERE api_key = :api_key;");
- $statement->bindValue(":api_key", $key);
- $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
- if ($r){
- $uid = $r["uid"];
- }
- else{
- header("HTTP/1.1 401 Invalid credentials.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 401 Invalid credentials.");
- return 401;
- }
- break;
- case "GetLobbyWizardLog":
- $uid = $json->{"lobby_wizard_log"}->{"wizard_id"};
- $statement = $db->prepare("SELECT COUNT(uid) AS c FROM player WHERE uid = :uid AND api_key = :api_key;");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":api_key", $key);
- if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
- header("HTTP/1.1 401 Invalid credentials.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 401 Invalid credentials.");
- return 401;
- }
- break;
- }
- if ($command == "GetUnitCollection"){ // This command doesnt provide wizard_id
- $statement = $db->prepare("SELECT COUNT(uid) AS c FROM player WHERE uid = :uid AND api_key = :api_key;");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":api_key", $key);
- if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
- header("HTTP/1.1 401 Invalid credentials.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 401 Invalid credentials.");
- return 401;
- }
- }
- // Run parser
- $result = "500 Unknown Error";
- switch($command){
- case "HubUserLogin":
- $result = parse_profile($db, $uid, $json);
- break;
- case "GetUnitCollection":
- $result = parse_collection($db, $uid, $json);
- break;
- case "GetLobbyWizardLog":
- $result = parse_records($db, $uid, $json);
- break;
- }
- // Process result
- $status_code = intval(substr($result, 0, 3));
- $status_message = substr($result, 4);
- if ($status_code == 0 || $status_code < 100 || $status_code > 599){
- header("HTTP/1.1 500 Unknown error.");
- syslog(LOG_ERR, "[APIv3] HTTP/1.1 500 Unknown error.");
- return 500;
- }
- else{
- header("HTTP/1.1 $status_code $status_message");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 $status_code $status_message");
- return $status_code;
- }
- }
- catch(Exception $e) {
- header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
- return 500;
- }
- ?>
|