| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700 |
- <?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.
- *
- * @author Iñigo Valentin <i@inigovalentin.com>
- * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
- * @package SWDB
- * @category API
- */
- /**
- * Parses an artifact and saves it to the database.
- *
- * @param string $player_id Owner's player ID.
- * @param JSON artifact JSON fragment of the artifact.
- */
- function parse_artifact($player_id, $artifact){
- $db = get_context()->get_db();
- $statement = $db->prepare("
- INSERT INTO artifact (
- player,
- id,
- type,
- attribute,
- archetype,
- level,
- quality,
- original_quality,
- value,
- efficiency,
- max_efficiency,
- locked
- ) VALUES (
- :player,
- :id,
- :type,
- :attribute,
- :archetype,
- :level,
- :quality,
- :original_quality,
- :value,
- :efficiency,
- :max_efficiency,
- :locked
- );
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":id", $artifact->{"rid"}, SQLITE3_INTEGER);
- if (intval($artifact->{"attribute"}) == 0){
- $statement->bindValue(":attribute", null, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":attribute", $artifact->{"attribute"}, SQLITE3_INTEGER);
- }
- if (intval($artifact->{"type"}) == 0){
- $statement->bindValue(":type", null, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":type", $artifact->{"type"}, SQLITE3_INTEGER);
- }
- if (intval($artifact->{"unit_style"}) == 0){
- $statement->bindValue(":archetype", null, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":archetype", $artifact->{"unit_style"}, SQLITE3_INTEGER);
- }
- $statement->bindValue(":level", $artifact->{"level"}, SQLITE3_INTEGER);
- $statement->bindValue(":quality", $artifact->{"rank"}, SQLITE3_INTEGER);
- $statement->bindValue(":original_quality", $artifact->{"natural_rank"}, SQLITE3_INTEGER);
- // I don'k know the value formula, but value only depends on quality, so... hardcoding.
- switch ($artifact->{"natural_rank"}){
- case QUALITY_ID::LEGEND:
- $statement->bindValue(":value", 37660, SQLITE3_INTEGER);
- break;
- case QUALITY_ID::HERO:
- $statement->bindValue(":value", 25250, SQLITE3_INTEGER);
- break;
- case QUALITY_ID::RARE:
- $statement->bindValue(":value", 18461, SQLITE3_INTEGER);
- break;
- case QUALITY_ID::MAGIC:
- $statement->bindValue(":value", 9330, SQLITE3_INTEGER);
- break;
- default:
- $statement->bindValue(":value", 3870, SQLITE3_INTEGER);
- }
- $statement->bindValue(":locked", $artifact->{"locked"}, SQLITE3_INTEGER);
- // Efficienccis will be caculated later, when instantiating the artifact.
- $statement->bindValue(":efficiency", 0, SQLITE3_FLOAT);
- $statement->bindValue(":max_efficiency", 0, SQLITE3_FLOAT);
- $statement->execute();
- // Main stat
- $statement = $db->prepare("
- INSERT INTO artifact_stat (
- artifact,
- stat,
- value
- ) VALUES (
- :artifact,
- :stat,
- :value
- );
- ");
- $statement->bindValue(":artifact", $artifact->{"rid"}, SQLITE3_INTEGER);
- $statement->bindValue(":stat", $artifact->{"pri_effect"}[0], SQLITE3_INTEGER);
- $statement->bindValue(":value", $artifact->{"pri_effect"}[1], SQLITE3_INTEGER);
- $statement->execute();
- // Base effect insert query
- $effect_query = "
- INSERT INTO artifact_effect (
- artifact,
- slot,
- effect,
- value,
- enchant,
- grind,
- rolls
- ) VALUES (
- :artifact,
- :slot,
- :effect,
- :value,
- :enchant,
- :grind,
- :rolls
- );
- ";
- // Effect 1
- if (sizeof($artifact->{"sec_effects"}) > 0){
- $statement = $db->prepare($effect_query);
- $statement->bindValue(":artifact", $artifact->{"rid"}, SQLITE3_INTEGER);
- $statement->bindValue(":slot", ARTIFACT_EFFECT_SLOT_ID::EFFECT_1, SQLITE3_INTEGER);
- $statement->bindValue(":effect", $artifact->{"sec_effects"}[0][0], SQLITE3_INTEGER);
- $statement->bindValue(":value", $artifact->{"sec_effects"}[0][1], SQLITE3_INTEGER);
- $statement->bindValue(":grind", $artifact->{"sec_effects"}[0][2], SQLITE3_INTEGER);
- $statement->bindValue(":enchant", $artifact->{"sec_effects"}[0][3], SQLITE3_INTEGER);
- $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Effect 2
- if (sizeof($artifact->{"sec_effects"}) > 1){
- $statement = $db->prepare($effect_query);
- $statement->bindValue(":artifact", $artifact->{"rid"}, SQLITE3_INTEGER);
- $statement->bindValue(":slot", ARTIFACT_EFFECT_SLOT_ID::EFFECT_2, SQLITE3_INTEGER);
- $statement->bindValue(":effect", $artifact->{"sec_effects"}[1][0], SQLITE3_INTEGER);
- $statement->bindValue(":value", $artifact->{"sec_effects"}[1][1], SQLITE3_INTEGER);
- $statement->bindValue(":grind", $artifact->{"sec_effects"}[1][2], SQLITE3_INTEGER);
- $statement->bindValue(":enchant", $artifact->{"sec_effects"}[1][3], SQLITE3_INTEGER);
- $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Effect 3
- if (sizeof($artifact->{"sec_effects"}) > 2){
- $statement = $db->prepare($effect_query);
- $statement->bindValue(":artifact", $artifact->{"rid"}, SQLITE3_INTEGER);
- $statement->bindValue(":slot", ARTIFACT_EFFECT_SLOT_ID::EFFECT_3, SQLITE3_INTEGER);
- $statement->bindValue(":effect", $artifact->{"sec_effects"}[2][0], SQLITE3_INTEGER);
- $statement->bindValue(":value", $artifact->{"sec_effects"}[2][1], SQLITE3_INTEGER);
- $statement->bindValue(":grind", $artifact->{"sec_effects"}[2][2], SQLITE3_INTEGER);
- $statement->bindValue(":enchant", $artifact->{"sec_effects"}[2][3], SQLITE3_INTEGER);
- $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Effect 4
- if (sizeof($artifact->{"sec_effects"}) > 3){
- $statement = $db->prepare($effect_query);
- $statement->bindValue(":artifact", $artifact->{"rid"}, SQLITE3_INTEGER);
- $statement->bindValue(":slot", ARTIFACT_EFFECT_SLOT_ID::EFFECT_4, SQLITE3_INTEGER);
- $statement->bindValue(":effect", $artifact->{"sec_effects"}[3][0], SQLITE3_INTEGER);
- $statement->bindValue(":value", $artifact->{"sec_effects"}[3][1], SQLITE3_INTEGER);
- $statement->bindValue(":grind", $artifact->{"sec_effects"}[3][2], SQLITE3_INTEGER);
- $statement->bindValue(":enchant", $artifact->{"sec_effects"}[3][3], SQLITE3_INTEGER);
- $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Instantiate and calculate efficiency:
- new Artifact($artifact->{"rid"});
- // Assigned?
- if (intval($artifact->{"occupied_id"}) != 0){
- $statement = $db->prepare("
- INSERT INTO unit_artifact (unit, artifact, rta)
- VALUES (:unit, :artifact, :rta);
- ");
- $statement->bindValue(":unit", $artifact->{"occupied_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":artifact", $artifact->{"rid"}, SQLITE3_INTEGER);
- $statement->bindValue(":rta", GAME_MODE_ID::NORMAL, SQLITE3_INTEGER);
- $statement->execute();
- }
- }
- /**
- * Parses a rune and saves it to the database.
- *
- * @param string $player_id Owner ID.
- * @param JSON rune JSON fragment of the rune.
- */
- function parse_rune($player_id, $rune, $lock_list){
- $db = get_context()->get_db();
- $statement = $db->prepare("
- INSERT INTO rune (
- player,
- id,
- type,
- slot,
- stars,
- ancient,
- level,
- quality,
- original_quality,
- value,
- efficiency,
- max_efficiency,
- locked
- ) VALUES (
- :player,
- :id,
- :type,
- :slot,
- :stars,
- :ancient,
- :level,
- :quality,
- :original_quality,
- :value,
- :efficiency,
- :max_efficiency,
- :locked
- );
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":id", $rune->{"rune_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":type", $rune->{"set_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":slot", $rune->{"slot_no"}, SQLITE3_INTEGER);
- $statement->bindValue(":value", $rune->{"sell_value"}, SQLITE3_INTEGER);
- if ($rune->{"class"} > 10){
- // Ancient rune
- $statement->bindValue(":ancient", 1, SQLITE3_INTEGER);
- $statement->bindValue(":stars", intval($rune->{"class"}) - 10, SQLITE3_INTEGER);
- $statement->bindValue(":original_quality", intval($rune->{"extra"}) - 10, SQLITE3_INTEGER);
- }
- else{
- // Non Ancient rune
- $statement->bindValue(":ancient", 0, SQLITE3_INTEGER);
- $statement->bindValue(":stars", $rune->{"class"}, SQLITE3_INTEGER);
- $statement->bindValue(":original_quality", $rune->{"extra"}, SQLITE3_INTEGER);
- }
- $level = $rune->{"upgrade_curr"};
- $statement->bindValue(":level", $level, SQLITE3_INTEGER);
- $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, SQLITE3_INTEGER);
- $lock = 0;
- if (in_array($rune->{"rune_id"}, $lock_list)){
- $lock = 1;
- }
- $statement->bindValue(":locked", $lock, SQLITE3_INTEGER);
- $efficiency = null;
- $max_efficiency = null;
- $statement->bindValue(":efficiency", $efficiency, SQLITE3_FLOAT);
- $statement->bindValue(":max_efficiency", $max_efficiency, SQLITE3_FLOAT);
- $statement->execute();
- $stat_statement = "
- INSERT INTO rune_stat (rune, slot, stat, value, enchant, grind, rolls)
- VALUES (:rune, :slot, :stat, :value, :enchant, :grind, :rolls);
- ";
- // Main stat
- $statement = $db->prepare($stat_statement);
- $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":slot", RUNE_STAT_SLOT_ID::MAIN, SQLITE3_INTEGER);
- $statement->bindValue(":stat", $rune->{"pri_eff"}[0], SQLITE3_INTEGER);
- $statement->bindValue(":value", $rune->{"pri_eff"}[1], SQLITE3_INTEGER);
- $statement->bindValue(":enchant", 0, SQLITE3_INTEGER);
- $statement->bindValue(":grind", 0, SQLITE3_INTEGER);
- $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
- $statement->execute();
- // Innate stat
- if ($rune->{"prefix_eff"}[0] > 0){
- // Main stat
- $statement = $db->prepare($stat_statement);
- $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":slot", RUNE_STAT_SLOT_ID::INNATE, SQLITE3_INTEGER);
- $statement->bindValue(":stat", $rune->{"prefix_eff"}[0], SQLITE3_INTEGER);
- $statement->bindValue(":value", $rune->{"prefix_eff"}[1], SQLITE3_INTEGER);
- $statement->bindValue(":enchant", 0, SQLITE3_INTEGER);
- $statement->bindValue(":grind", 0, SQLITE3_INTEGER);
- $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- if (sizeof($rune->{"sec_eff"}) > 0){
- $statement = $db->prepare($stat_statement);
- $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":slot", RUNE_STAT_SLOT_ID::SUBSTAT_1, SQLITE3_INTEGER);
- $statement->bindValue(":stat", $rune->{"sec_eff"}[0][0], SQLITE3_INTEGER);
- $statement->bindValue(":value", $rune->{"sec_eff"}[0][1], SQLITE3_INTEGER);
- $statement->bindValue(":enchant", $rune->{"sec_eff"}[0][2], SQLITE3_INTEGER);
- $statement->bindValue(":grind", $rune->{"sec_eff"}[0][3], SQLITE3_INTEGER);
- $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- if (sizeof($rune->{"sec_eff"}) > 1){
- $statement = $db->prepare($stat_statement);
- $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":slot", RUNE_STAT_SLOT_ID::SUBSTAT_2, SQLITE3_INTEGER);
- $statement->bindValue(":stat", $rune->{"sec_eff"}[1][0], SQLITE3_INTEGER);
- $statement->bindValue(":value", $rune->{"sec_eff"}[1][1], SQLITE3_INTEGER);
- $statement->bindValue(":enchant", $rune->{"sec_eff"}[1][2], SQLITE3_INTEGER);
- $statement->bindValue(":grind", $rune->{"sec_eff"}[1][3], SQLITE3_INTEGER);
- $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- if (sizeof($rune->{"sec_eff"}) > 2){
- $statement = $db->prepare($stat_statement);
- $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":slot", RUNE_STAT_SLOT_ID::SUBSTAT_3, SQLITE3_INTEGER);
- $statement->bindValue(":stat", $rune->{"sec_eff"}[2][0], SQLITE3_INTEGER);
- $statement->bindValue(":value", $rune->{"sec_eff"}[2][1], SQLITE3_INTEGER);
- $statement->bindValue(":enchant", $rune->{"sec_eff"}[2][2], SQLITE3_INTEGER);
- $statement->bindValue(":grind", $rune->{"sec_eff"}[2][3], SQLITE3_INTEGER);
- $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- if (sizeof($rune->{"sec_eff"}) > 3){
- $statement = $db->prepare($stat_statement);
- $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":slot", RUNE_STAT_SLOT_ID::SUBSTAT_4, SQLITE3_INTEGER);
- $statement->bindValue(":stat", $rune->{"sec_eff"}[3][0], SQLITE3_INTEGER);
- $statement->bindValue(":value", $rune->{"sec_eff"}[3][1], SQLITE3_INTEGER);
- $statement->bindValue(":enchant", $rune->{"sec_eff"}[3][2], SQLITE3_INTEGER);
- $statement->bindValue(":grind", $rune->{"sec_eff"}[3][3], SQLITE3_INTEGER);
- $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Instantiate and calculate efficiency:
- new Rune($rune->{"rune_id"});
- // Assigned?
- if (intval($rune->{"occupied_id"}) != 0){
- $statement = $db->prepare("
- INSERT INTO unit_rune (unit, rune, rta)
- VALUES (:unit, :rune, :rta);
- ");
- $statement->bindValue(":unit", $rune->{"occupied_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":rta", GAME_MODE_ID::NORMAL, SQLITE3_INTEGER);
- $statement->execute();
- }
- }
- /**
- * Parses the response to the HubUserLogin command and updates profile.
- *
- * @param resource $db Database connection.
- * @param int $player_id Player ID.
- * @param mixed[] $json Response.
- */
- function parse_profile($db, $player_id, $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_" . $player_id . "_" . $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, "[APIv1] 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 player = :player);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM unit_rune
- WHERE unit IN (SELECT id FROM unit WHERE player = :player);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM rune_stat
- WHERE rune IN (SELECT id FROM rune WHERE player = :player);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM unit_artifact
- WHERE unit IN (SELECT id FROM unit WHERE player = :player);n ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM artifact_stat
- WHERE artifact IN (SELECT id FROM artifact WHERE player = :player);n ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM artifact_effect
- WHERE artifact IN (SELECT id FROM artifact WHERE player = :player);n ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- $tables_to_clear = [
- "defense",
- "unit",
- "rune",
- "artifact",
- "building",
- "decoration",
- "item",
- "enchantment"
- ];
- foreach ($tables_to_clear as $table){
- $statement = $db->prepare("DELETE FROM $table WHERE player = :player;");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $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,
- mana = :mana,
- crystal = :crystal,
- country = :country,
- lang = :lang,
- level = :level,
- experience = :experience,
- energy = :energy,
- energy_max = :energy_max,
- energy_per_min = :energy_per_min,
- arena_energy = :arena_energy,
- rep = :rep,
- social_point = :social_point,
- honor_point = :honor_point,
- guild_point = :guild_point,
- darkportal_energy = :darkportal_energy,
- dimension_energy = :dimension_energy,
- costume_point = :costume_point,
- honor_medal = :honor_medal,
- honor_mark = :honor_mark,
- event_coin = :event_coin,
- storage_slots = :storage_slots,
- island = :island_upgrade
- WHERE id = :id;
- ");
- $statement->bindValue(":name", $json->{"wizard_info"}->{"wizard_name"}, SQLITE3_TEXT);
- $statement->bindValue(":mana", $json->{"wizard_info"}->{"wizard_mana"}, SQLITE3_INTEGER);
- $statement->bindValue(":crystal", $json->{"wizard_info"}->{"wizard_crystal"}, SQLITE3_INTEGER);
- $statement->bindValue(":country", $json->{"wizard_info"}->{"wizard_last_country"}, SQLITE3_TEXT);
- $statement->bindValue(":lang", $json->{"wizard_info"}->{"wizard_last_lang"}, SQLITE3_TEXT);
- $statement->bindValue(":level", $json->{"wizard_info"}->{"wizard_level"}, SQLITE3_INTEGER);
- $statement->bindValue(":experience", $json->{"wizard_info"}->{"experience"}, SQLITE3_INTEGER);
- $statement->bindValue(":energy", $json->{"wizard_info"}->{"wizard_energy"}, SQLITE3_INTEGER);
- $statement->bindValue(":energy_max", $json->{"wizard_info"}->{"energy_max"}, SQLITE3_INTEGER);
- $statement->bindValue(":energy_per_min", $json->{"wizard_info"}->{"energy_per_min"}, SQLITE3_INTEGER);
- $statement->bindValue(":arena_energy", $json->{"wizard_info"}->{"arena_energy"}, SQLITE3_INTEGER);
- $statement->bindValue(":rep", $json->{"wizard_info"}->{"rep_unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":social_point", $json->{"wizard_info"}->{"social_point_current"}, SQLITE3_INTEGER);
- $statement->bindValue(":honor_point", $json->{"wizard_info"}->{"honor_point"}, SQLITE3_INTEGER);
- $statement->bindValue(":guild_point", $json->{"wizard_info"}->{"guild_point"}, SQLITE3_INTEGER);
- $statement->bindValue(":darkportal_energy", $json->{"wizard_info"}->{"darkportal_energy"}, SQLITE3_INTEGER);
- $statement->bindValue(":dimension_energy", $json->{"dimension_hole_info"}->{"energy"}, SQLITE3_INTEGER);
- $statement->bindValue(":costume_point", $json->{"wizard_info"}->{"costume_point"}, SQLITE3_INTEGER);
- $statement->bindValue(":honor_medal", $json->{"wizard_info"}->{"honor_medal"}, SQLITE3_INTEGER);
- $statement->bindValue(":honor_mark", $json->{"wizard_info"}->{"honor_mark"}, SQLITE3_INTEGER);
- $statement->bindValue(":event_coin", $json->{"wizard_info"}->{"event_coin"}, SQLITE3_INTEGER);
- $statement->bindValue(":storage_slots", $json->{"unit_depository_slots"}->{"number"}, SQLITE3_INTEGER);
- $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, SQLITE3_INTEGER);
- $statement->bindValue(":id", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- // Parse Arena defense
- foreach ($json->{"defense_unit_list"} as $defense){
- $statement = $db->prepare("
- INSERT INTO defense (
- player,
- area_type,
- slot,
- unit,
- position
- ) VALUES (
- :player,
- :area_type,
- :slot,
- :unit,
- :position
- );
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::ARENA, SQLITE3_INTEGER);
- $statement->bindValue(":slot", 0, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $defense->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":position", $defense->{"pos_id"}, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Parse GW defense
- if ($json->{"guildwar_defense_unit_list"}[0]){
- $position = 1;
- $slot = 0;
- foreach ($json->{"guildwar_defense_unit_list"}[0] as $defense){
- $statement = $db->prepare("
- INSERT INTO defense (
- player,
- area_type,
- slot,
- unit,
- position
- ) VALUES (
- :player,
- :area_type,
- :slot,
- :unit,
- :position
- );
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::GUILD_WAR, SQLITE3_INTEGER);
- $statement->bindValue(":slot", $slot, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $defense->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":position", $position, SQLITE3_INTEGER);
- $statement->execute();
- $position ++;
- }
- }
- if ($json->{"guildwar_defense_unit_list"}[1]){
- $position = 1;
- $slot = 1;
- foreach ($json->{"guildwar_defense_unit_list"}[1] as $defense){
- $statement = $db->prepare("
- INSERT INTO defense (
- player,
- area_type,
- slot,
- unit,
- position
- ) VALUES (
- :player,
- :area_type,
- :slot,
- :unit,
- :position
- );
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::GUILD_WAR, SQLITE3_INTEGER);
- $statement->bindValue(":slot", $slot, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $defense->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":position", $position, SQLITE3_INTEGER);
- $statement->execute();
- $position ++;
- }
- }
-
- // Parse siege defenses.
- $i = 0;
- foreach ($json->{"guildsiege_defense_unit_list"} as $defense){
- $statement = $db->prepare("
- INSERT INTO defense (
- player,
- area_type,
- slot,
- unit,
- position
- ) VALUES (
- :player,
- :area_type,
- :slot,
- :unit,
- :position
- );
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::GUILD_SIEGE, SQLITE3_INTEGER);
- $statement->bindValue(":slot", floor($i / 3), SQLITE3_INTEGER);
- $statement->bindValue(":unit", $defense, SQLITE3_INTEGER);
- $statement->bindValue(":position", ($i % 3) + 1, SQLITE3_INTEGER);
- $statement->execute();
- $i ++;
- }
- // Parse buildings
- foreach($json->{"building_list"} as $building){
- $statement = $db->prepare("
- INSERT INTO building (
- player,
- id,
- buildable,
- gain
- ) VALUES (
- :player,
- :id,
- :buildable,
- :gain
- );
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":id", $building->{"building_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":buildable", $building->{"building_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":gain", $building->{"gain_per_hour"}, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Parse decorarion
- foreach($json->{"deco_list"} as $building){
- $statement = $db->prepare("
- INSERT INTO decoration (
- player,
- id,
- decorative,
- level
- ) VALUES (
- :player,
- :id,
- :decorative,
- :level
- );
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":id", $building->{"deco_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":decorative", $building->{"master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":level", $building->{"level"}, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Parse units
- $lock_list = $json->{"unit_lock_list"};
- foreach ($json->{"unit_list"} as $unit){
- $statement = $db->prepare("
- INSERT INTO unit (
- player,
- id,
- building,
- monster,
- level,
- stars,
- hp,
- attack,
- defense,
- speed,
- crit_rate,
- crit_damage,
- resistance,
- accuracy,
- experience,
- exp_gained,
- exp_gain_rate,
- costume,
- source,
- create_time,
- homunculus_name,
- lock
- ) VALUES (
- :player,
- :id,
- :building,
- :monster,
- :level,
- :stars,
- :hp,
- :attack,
- :defense,
- :speed,
- :crit_rate,
- :crit_damage,
- :resistance,
- :accuracy,
- :experience,
- :exp_gained,
- :exp_gain_rate,
- :costume,
- :source,
- :create_time,
- :homunculus_name,
- :lock
- );
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":id", $unit->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":building", $unit->{"building_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":monster", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":level", $unit->{"unit_level"}, SQLITE3_INTEGER);
- $statement->bindValue(":stars", $unit->{"class"}, SQLITE3_INTEGER);
- $statement->bindValue(":hp", intval($unit->{"con"}) * 15, SQLITE3_INTEGER); # Always x15
- $statement->bindValue(":attack", $unit->{"atk"}, SQLITE3_INTEGER);
- $statement->bindValue(":defense", $unit->{"def"}, SQLITE3_INTEGER);
- $statement->bindValue(":speed", $unit->{"spd"}, SQLITE3_INTEGER);
- $statement->bindValue(":crit_rate", $unit->{"critical_rate"}, SQLITE3_INTEGER);
- $statement->bindValue(":crit_damage", $unit->{"critical_damage"}, SQLITE3_INTEGER);
- $statement->bindValue(":resistance", $unit->{"resist"}, SQLITE3_INTEGER);
- $statement->bindValue(":accuracy", $unit->{"accuracy"}, SQLITE3_INTEGER);
- $statement->bindValue(":experience", $unit->{"experience"}, SQLITE3_INTEGER);
- $statement->bindValue(":exp_gained", $unit->{"exp_gained"}, SQLITE3_INTEGER);
- $statement->bindValue(":exp_gain_rate", $unit->{"exp_gain_rate"}, SQLITE3_INTEGER);
- $statement->bindValue(":costume", $unit->{"costume_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":source", $unit->{"source"}, SQLITE3_INTEGER);
- $statement->bindValue(":create_time", $unit->{"create_time"}, SQLITE3_INTEGER);
- $statement->bindValue(":homunculus_name", $unit->{"homunculus_name"}, SQLITE3_TEXT);
- $lock = 0;
- if (in_array($unit->{"unit_id"}, $lock_list)){
- $lock = 1;
- }
- $statement->bindValue(":lock", $lock, SQLITE3_INTEGER);
- $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"}, SQLITE3_INTEGER);
- $statement->bindValue(":skill", $skill[0], SQLITE3_INTEGER);
- $statement->bindValue(":level", $skill[1], SQLITE3_INTEGER);
- $statement->execute();
- }
- }
- // Parse items
- foreach ($json->{"inventory_info"} as $item){
- $statement = $db->prepare("
- INSERT INTO item (
- player,
- id,
- type,
- amount
- ) VALUES (
- :player,
- :id,
- :type,
- :amount
- );
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":id", $item->{"item_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":type", $item->{"item_master_type"}, SQLITE3_INTEGER);
- $statement->bindValue(":amount", $item->{"item_quantity"}, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Fix rune craft item type.
- $db->query("UPDATE item SET type = 27 WHERE type = 29 AND id IN (2001, 4001, 4002, 4003, 9001, 9002, 9003, 8001, 9004);");
-
- // Set Conversion stone to type Craft Stuff and id 9999.
- $db->query("UPDATE item SET type = 27, id = 9999 WHERE type = 75 AND id = 1;");
- // 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, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $unit, SQLITE3_INTEGER);
- $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, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $unit, SQLITE3_INTEGER);
- $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, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $unit, SQLITE3_INTEGER);
- $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, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $unit, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Parse runes
- foreach ($json->{"runes"} as $rune){
- parse_rune($player_id, $rune, $rune_lock_list);
- }
- foreach ($json->{"unit_list"} as $unit){
- foreach ($unit->{"runes"} as $rune){
- parse_rune($player_id, $rune, $rune_lock_list);
- }
- }
- foreach ($json->{"world_arena_rune_equip_list"} as $rta_rune){
- $statement = $db->prepare("
- INSERT INTO unit_rune (unit, rune, rta)
- VALUES (:unit, :rune, :rta);
- ");
- $statement->bindValue(":unit", $rta_rune->{"occupied_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":rune", $rta_rune->{"rune_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":rta", GAME_MODE_ID::RTA, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Parse artifacts
- foreach ($json->{"artifacts"} as $artifact){
- parse_artifact($player_id, $artifact);
- }
- foreach ($json->{"unit_list"} as $unit){
- foreach ($unit->{"artifacts"} as $artifact){
- parse_artifact($player_id, $artifact);
- }
- }
- foreach ($json->{"world_arena_artifact_equip_list"} as $rta_artifact){
- $statement = $db->prepare("
- INSERT INTO unit_artifact (unit, artifact, rta)
- VALUES (:unit, :artifact, :rta);
- ");
- $statement->bindValue(":unit", $rta_artifact->{"occupied_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":artifact", $rta_artifact->{"artifact_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":rta", GAME_MODE_ID::RTA, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Parse enchantments
- foreach ($json->{"rune_craft_item_list"} as $item){
- $statement = $db->prepare("
- INSERT INTO enchantment (
- player,
- id,
- type,
- quality,
- rune,
- stat,
- value,
- amount
- ) VALUES (
- :player,
- :id,
- :type,
- :quality,
- :rune,
- :stat,
- :value,
- :amount
- );
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":id", $item->{"craft_item_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":type", $item->{"craft_type"}, SQLITE3_INTEGER);
- $statement->bindValue(":value", $item->{"sell_value"}, SQLITE3_INTEGER);
- $statement->bindValue(":amount", $item->{"amount"}, SQLITE3_INTEGER);
- $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)), SQLITE3_INTEGER);
- $statement->bindValue(":stat", intval(substr($info, 2, 2)), SQLITE3_INTEGER);
- $statement->bindValue(":rune", intval(substr($info, 0, 2)), SQLITE3_INTEGER);
- $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"}, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("DELETE FROM guild_member WHERE guild = :guild;");
- $statement->bindValue(":guild", $guild->{"guild_id"}, SQLITE3_INTEGER);
- $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"}, SQLITE3_INTEGER);
- $statement->bindValue(":name",$guild->{"name"}, SQLITE3_TEXT);
- $statement->bindValue(":level",$guild->{"level"}, SQLITE3_INTEGER);
- $statement->bindValue(":experience",$guild->{"experience"}, SQLITE3_INTEGER);
- $statement->bindValue(":recruiting",$guild->{"recruit_status"}, SQLITE3_INTEGER);
- $statement->bindValue(":members",$guild->{"member_now"}, SQLITE3_INTEGER);
- $statement->bindValue(":leader",$guild->{"master_wizard_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":comment",$guild->{"comment"}, SQLITE3_TEXT);
- $statement->bindValue(":notice",$guild->{"notice"}, SQLITE3_TEXT);
- $statement->execute();
- foreach ($json->{"guild"}->{"guild_members"} as $member){
- $statement = $db->prepare("
- INSERT INTO guild_member (
- member,
- guild,
- grade,
- name,
- level,
- rating,
- arena_score,
- joined,
- last_login,
- in_war,
- has_defense
- ) VALUES (
- :member,
- :guild,
- :grade,
- :name,
- :level,
- :rating,
- :arena_score,
- :joined,
- :last_login,
- :in_war,
- :has_defense
- );
- ");
- $statement->bindValue(":member", $member->{"wizard_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":guild", $member->{"guild_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":grade", $member->{"grade"}, SQLITE3_INTEGER);
- $statement->bindValue(":name", $member->{"wizard_name"}, SQLITE3_TEXT);
- $statement->bindValue(":level", $member->{"wizard_level"}, SQLITE3_INTEGER);
- $statement->bindValue(":rating", $member->{"rating_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":arena_score", $member->{"arena_score"}, SQLITE3_INTEGER);
- $statement->bindValue(":joined", $member->{"join_timestamp"}, SQLITE3_TEXT);
- $statement->bindValue(":last_login", $member->{"last_login_timestamp"}, SQLITE3_TEXT);
- $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, SQLITE3_INTEGER);
- $statement->bindValue(":has_defense", $has_defense, SQLITE3_INTEGER);
- $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 player = :player
- )
- ) AND
- player = :player
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $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 resource $db Database connection.
- * @param int $player_id Player ID.
- * @param mixed[] $json Response.
- */
- function parse_collection($db, $player_id, $json){
- // Clear previous collection data
- $db->query("PRAGMA foreign_keys = OFF;");
- $statement = $db->prepare("DELETE FROM collection WHERE player = :player;");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- foreach ($json->{"collection"} as $unit){
- $statement = $db->prepare("
- INSERT INTO collection (player, monster, open)
- VALUES (:player, :monster, :open);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":monster", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":open", $unit->{"open"}, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Return success
- return "201 Created.";
- }
- /**
- * Parses the response to the GetLobbyWizardLog command and updates the
- * user records.
- *
- * @param resource $db Database connection.
- * @param int $player_id Player ID.
- * @param mixed[] $json Response.
- */
- function parse_records($db, $player_id, $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;");
- // TODO Check TOA Hell key and update.
- $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 id = :id;
- ");
- $statement->bindValue(":joined", $json->{"lobby_wizard_log"}->{"account_create_timestamp"}, SQLITE3_TEXT);
- $statement->bindValue(":top_rank_arena", $json->{"lobby_wizard_log"}->{"pvp_best_rating_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":top_rank_world_arena", $json->{"lobby_wizard_log"}->{"rtpvp_rank_best_rating_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":top_rank_special_league", $json->{"lobby_wizard_log"}->{"rtpvp_contest_best_rating_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":top_rank_gw", $json->{"lobby_wizard_log"}->{"guildwar_best_rating_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":top_rank_siege", $json->{"lobby_wizard_log"}->{"guildsiege_best_rating_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":top_rank_wboss", $json->{"lobby_wizard_log"}->{"world_boss_best_rank_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":top_rank_toan", $json->{"lobby_wizard_log"}->{"trial_tower_normal_best_floor"}, SQLITE3_INTEGER);
- $statement->bindValue(":top_rank_toah", $json->{"lobby_wizard_log"}->{"trial_tower_hard_best_floor"}, SQLITE3_INTEGER);
- $statement->bindValue(":id", $player_id, SQLITE3_INTEGER);
- $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
- player = :player AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM record
- WHERE
- player = :player AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
- $statement->execute();
- // Insert new data
- $statement = $db->prepare("
- INSERT INTO record
- (player, area_type, area, stage, time, score, rank)
- VALUES
- (:player, :area_type, :area, :stage, :time, :score, :rank);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":stage", $record->{"stage_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":time", $record->{"clear_time"}, SQLITE3_INTEGER);
- $statement->bindValue(":score", 0, SQLITE3_INTEGER);
- $statement->bindValue(":rank", null, SQLITE3_TEXT);
- $statement->execute();
- // Loop party
- foreach ($record->{"my_unit_deck_list"} as $party){
- $statement = $db->prepare("
- INSERT INTO record_party
- (player, area_type, area, unit, monster, leader, front)
- VALUES
- (:player, :area_type, :area, :unit, :monster, :leader, :front);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
- $statement->bindValue(":front", 0, SQLITE3_INTEGER);
- $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
- player = :player AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"stage_id"}, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM record
- WHERE
- player = :player AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"stage_id"}, SQLITE3_INTEGER);
- $statement->execute();
- // Insert new data
- $statement = $db->prepare("
- INSERT INTO record
- (player, area_type, area, stage, time, score, rank)
- VALUES
- (:player, :area_type, :area, :stage, :time, :score, :rank);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"stage_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":stage", $record->{"stage_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":time", $record->{"clear_time"}, SQLITE3_TEXT);
- $statement->bindValue(":score", 0, SQLITE3_INTEGER);
- $statement->bindValue(":rank", null, SQLITE3_TEXT);
- $statement->execute();
- // Loop party
- foreach ($record->{"my_unit_deck_list"} as $party){
- $statement = $db->prepare("
- INSERT INTO record_party
- (player, area_type, area, unit, monster, leader, front)
- VALUES
- (:player, :area_type, :area, :unit, :monster, :leader, :front);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"stage_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
- if ($party->{"slot_index"} <= 4){
- $statement->bindValue(":front", 1, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":front", 0, SQLITE3_INTEGER);
- }
- $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
- player = :player AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"rift_dungeon_id"}, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM record
- WHERE
- player = :player AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"rift_dungeon_id"}, SQLITE3_INTEGER);
- $statement->execute();
- // Insert new data
- $statement = $db->prepare("
- INSERT INTO record
- (player, area_type, area, stage, time, score, rank)
- VALUES
- (:player, :area_type, :area, :stage, :time, :score, :rank);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"rift_dungeon_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":stage", 0, SQLITE3_INTEGER);
- $statement->bindValue(":time", 0, SQLITE3_INTEGER);
- $statement->bindValue(":score", $record->{"clear_damage"}, SQLITE3_INTEGER);
- switch (intval($record->{"clear_rating"})){
- case 2:
- $statement->bindValue(":rank", "D", SQLITE3_TEXT);
- break;
- case 3:
- $statement->bindValue(":rank", "C", SQLITE3_TEXT);
- break;
- case 4:
- $statement->bindValue(":rank", "B-", SQLITE3_TEXT);
- break;
- case 5:
- $statement->bindValue(":rank", "B", SQLITE3_TEXT);
- break;
- case 6:
- $statement->bindValue(":rank", "B+", SQLITE3_TEXT);
- break;
- case 7:
- $statement->bindValue(":rank", "A-", SQLITE3_TEXT);
- break;
- case 8:
- $statement->bindValue(":rank", "A", SQLITE3_TEXT);
- break;
- case 9:
- $statement->bindValue(":rank", "A+", SQLITE3_TEXT);
- break;
- case 10:
- $statement->bindValue(":rank", "S", SQLITE3_TEXT);
- break;
- case 11:
- $statement->bindValue(":rank", "SS", SQLITE3_TEXT);
- break;
- case 12:
- $statement->bindValue(":rank", "SS", SQLITE3_TEXT);
- break;
- default:
- $statement->bindValue(":rank", "F", SQLITE3_TEXT);
- }
- $statement->execute();
- // Loop party
- foreach ($record->{"my_unit_deck_list"} as $party){
- $statement = $db->prepare("
- INSERT INTO record_party
- (player, area_type, area, unit, monster, leader, front)
- VALUES
- (:player, :area_type, :area, :unit, :monster, :leader, :front);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"rift_dungeon_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
- if ($party->{"slot_index"} <= 4){
- $statement->bindValue(":front", 1, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":front", 0, SQLITE3_INTEGER);
- }
- $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
- player = :player AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM record
- WHERE
- player = :player AND
- area_type = :area_type AND
- area = :area;
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
- $statement->execute();
- // Insert new data
- $statement = $db->prepare("
- INSERT INTO record
- (player, area_type, area, stage, time, score, rank)
- VALUES
- (:player, :area_type, :area, :stage, :time, :score, :rank);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":stage", $record->{"stage_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":time", $record->{"clear_time"}, SQLITE3_INTEGER);
- $statement->bindValue(":score", 0, SQLITE3_INTEGER);
- $statement->bindValue(":rank", null, SQLITE3_TEXT);
- $statement->execute();
- // Loop party
- foreach ($record->{"my_unit_deck_list"} as $party){
- $statement = $db->prepare("
- INSERT INTO record_party
- (player, area_type, area, unit, monster, leader, front)
- VALUES
- (:player, :area_type, :area, :unit, :monster, :leader, :front);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
- $statement->bindValue(":front", 0, SQLITE3_INTEGER);
- $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
- $_PUT = [];
- 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, "[APIv1] 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, "[APIv1] 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, "[APIv1] 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, "[APIv1] HTTP/1.1 405 Command not implemented.");
- return 405;
- }
- $db = get_context()->get_db();
- // Get user info and Authenticate
- switch ($command){
- case "HubUserLogin":
- $player_id = $json->{"wizard_id"};
- $statement = $db->prepare("
- SELECT COUNT(player.id) AS c
- FROM
- user,
- player
- WHERE
- user.id = player.user AND
- player.id = :player AND
- user.api_key = :api_key;");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
- if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
- // Player doesnt exists. If there is a user, create player.
- $statement = $db->prepare("
- SELECT id
- FROM user
- WHERE api_key = :api_key;");
- $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
- $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
- if (! $r){
- header("HTTP/1.1 401 Invalid credentials.");
- syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
- return 401;
- }
- $statement = $db->prepare("
- INSERT INTO player (
- id,
- user,
- server,
- country,
- lang,
- name,
- mana,
- crystal,
- level,
- experience,
- energy,
- energy_max,
- energy_per_min,
- arena_energy,
- rep,
- social_point,
- honor_point,
- guild_point,
- darkportal_energy,
- dimension_energy,
- costume_point,
- honor_medal,
- honor_mark,
- event_coin,
- storage_slots,
- island,
- joined,
- top_rank_arena,
- top_rank_world_arena,
- top_rank_special_league,
- top_rank_gw,
- top_rank_siege,
- top_rank_wboss,
- top_rank_toan,
- top_rank_toah,
- top_rank_toal,
- public,
- mode,
- last_access
- ) VALUES (
- :id,
- :user,
- :server,
- :country,
- :lang,
- '', --name,
- 0, --mana,
- 0, --crystal,
- 1, --level,
- 0, --experience,
- 0, --energy,
- 0, --energy_max,
- 0, --energy_per_min,
- 0, --arena_energy,
- null, --rep,
- 0, --social_point,
- 0, --honor_point,
- 0, --guild_point,
- 0, --darkportal_energy,
- 0, --dimension_energy,
- 0, --costume_point,
- 0, --honor_medal,
- 0, --honor_mark,
- 0, --event_coin,
- 0, --storage_slots,
- 0, --island,
- null, --joined,
- 0, --top_rank_arena,
- 0, --top_rank_world_arena,
- 0, --top_rank_special_league,
- 0, --top_rank_gw,
- 0, --top_rank_siege,
- 0, --top_rank_wboss,
- 0, --top_rank_toan,
- 0, --top_rank_toah,
- 0, --top_rank_toal,
- 0, --public,
- 0, --mode,
- null --last_access,
- )");
- $statement->bindValue(":id", $json->{"wizard_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":user", $r["id"], SQLITE3_INTEGER);
- // TODO: Default server EUROPE, detect and fix
- $statement->bindValue(":server", 2, SQLITE3_INTEGER);
- $statement->bindValue(":country", $json->{"wizard_info"}->{"wizard_last_country"}, SQLITE3_TEXT);
- $statement->bindValue(":lang", $json->{"wizard_info"}->{"wizard_last_lang"}, SQLITE3_TEXT);
- $statement->execute();
- }
- break;
- case "GetUnitCollection":
- // TODO: The new user/player distinction wont work here
- // No identification in this JSON, get player id from databases
- header("HTTP/1.1 501 Not implemented.");
- syslog(LOG_INFO, "[APIv1] HTTP/1.1 501 Not implemented.");
- return 501;
-
- $statement = $db->prepare("SELECT id FROM player WHERE api_key = :api_key;");
- $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
- $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
- if ($r){
- $uid = $r["id"];
- }
- else{
- header("HTTP/1.1 401 Invalid credentials.");
- syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
- return 401;
- }
- break;
- case "GetLobbyWizardLog":
- $player_id = $json->{"lobby_wizard_log"}->{"wizard_id"};
- $statement = $db->prepare("
- SELECT COUNT(player.id) AS c
- FROM
- user,
- player
- WHERE
- user.id = player.user AND
- player.id = :player AND
- user.api_key = :api_key;");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
- if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
- header("HTTP/1.1 401 Invalid credentials.");
- syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
- return 401;
- }
- break;
- }
- if ($command == "GetUnitCollection"){ // This command doesnt provide wizard_id
- // TODO: The new user/player distinction wont work here
- $statement = $db->prepare("SELECT COUNT(id) AS c FROM player WHERE id = :id AND api_key = :api_key;");
- $statement->bindValue(":id", $uid, SQLITE3_INTEGER);
- $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
- if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
- header("HTTP/1.1 401 Invalid credentials.");
- syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
- return 401;
- }
- }
- // Run parser
- $result = "500 Unknown Error";
- switch($command){
- case "HubUserLogin":
- $result = parse_profile($db, $player_id, $json);
- break;
- case "GetUnitCollection":
- $result = parse_collection($db, $player_id, $json);
- break;
- case "GetLobbyWizardLog":
- $result = parse_records($db, $player_id, $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, "[APIv1] HTTP/1.1 500 Unknown error.");
- return 500;
- }
- else{
- header("HTTP/1.1 $status_code $status_message");
- syslog(LOG_INFO, "[APIv1] 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, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
- return 500;
- }
|