| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469 |
- <?php
- /**
- * Run logger script.
- *
- * Exposes an API to log a run.
- *
- * Mandatory PUT parameters are:
- * - result_response: JSON received from game server on run end.
- * - key: User API key.
- *
- * Optional POST parameters are:
- * - result_request: JSON sent to game server on run end.
- * - start_response: JSON received from game server on run start.
- * - start_request: JSON sent to game server on run start.
- *
- * @category API
- */
- /**
- * Calculates the currrent efficiency of a rune.
- *
- * @param mixed[] $rune JSON fragment of the rune.
- * @return int Efficiency (0-100)
- */
- function calculate_efficiency($rune){
- $efficiency = 0.0;
- $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 mixed[] $rune JSON fragment of the rune.
- * @return int Maximum efficiency (0-100)
- */
- function calculate_maximum_efficiency($rune){
- $efficiency = 0.0;
- $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 the JSONs relateds to an Arena run and saves the data.
- *
- * @param resource $db Database connection.
- * @param int $uid Player UID.
- * @param int $id Run ID.
- * @param mixed[] $json List of received JSONs.
- */
- function log_arena_run($db, $id, $uid, $json){
- // Only the result request and response JSONs are needed:
- $response = $json["result_res"];
- // Prepare the "run" insert.
- $statement = $db->prepare("
- INSERT INTO run (
- uid,
- id,
- dtime,
- area_type,
- area,
- stage,
- difficulty,
- win,
- time,
- mana,
- energy,
- crystal,
- guild_points,
- honor_points,
- helper,
- score,
- rank
- ) VALUES (
- :uid,
- :id,
- :dtime,
- :area_type,
- :area,
- :stage,
- :difficulty,
- :win,
- :time,
- :mana,
- :energy,
- :crystal,
- :guild_points,
- :honor_points,
- :helper,
- :score,
- :rank
- );
- ");
- // Bind data and insert
- $statement->bindValue(":uid", $uid, SQLITE3_INTEGER);
- $statement->bindValue(":id", $id, SQLITE3_INTEGER);
- $statement->bindValue(":dtime", $response->{"tvaluelocal"}, SQLITE3_TEXT);
- $statement->bindValue(":area_type", AREA_TYPE_ID::ARENA, SQLITE3_INTEGER);
- $statement->bindValue(":area", AREA_TYPE_ID::ARENA, SQLITE3_INTEGER);
- $statement->bindValue(":stage", null, SQLITE3_INTEGER);
- $statement->bindValue(":difficulty", null, SQLITE3_INTEGER);
- $statement->bindValue(":win", $response->{"win_lose"}, SQLITE3_INTEGER);
- $statement->bindValue(":time", 1, SQLITE3_INTEGER);
- if (array_key_exists("mana", $response->{"reward"})){
- $statement->bindValue(":mana", $response->{"reward"}->{"mana"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
- }
- if (array_key_exists("energy", $response->{"reward"})){
- $statement->bindValue(":energy", $response->{"reward"}->{"energy"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
- }
- if (array_key_exists("crystal", $response->{"reward"})){
- $statement->bindValue(":crystal", $response->{"reward"}->{"crystal"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
- }
- $statement->bindValue(":guild_points", 0, SQLITE3_INTEGER);
- if (array_key_exists("honor_point", $response->{"reward"})){
- $statement->bindValue(":honor_points", $response->{"reward"}->{"honor_point"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":honor_points", 0, SQLITE3_INTEGER);
- }
- $statement->bindValue(":helper", 0, SQLITE3_INTEGER);
- $statement->bindValue(":score", null, SQLITE3_INTEGER);
- $statement->bindValue(":rank", null, SQLITE3_TEXT);
- $statement->execute();
- // Parse party
- $leader = 1;
- foreach ($response->{"unit_list"} as $unit){
- $statement = $db->prepare("
- INSERT INTO run_party
- (run, unit, k_unit, leader, front)
- VALUES
- (:run, :unit, :k_unit, :leader, :front);"
- );
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $unit->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":k_unit", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":leader", $leader, SQLITE3_INTEGER);
- $leader = 0;
- $statement->bindValue(":front", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Return success
- return "201 Created.";
- }
- /**
- * Parses the JSONs relateds to a Dimensional Rift run and saves the data.
- *
- * @param resource $db Database connection.
- * @param int $uid Player UID.
- * @param int $id Run ID.
- * @param mixed[] $json List of received JSONs.
- */
- function log_dark_portal_run($db, $id, $uid, $json){
- // Only the result request and response JSONs are needed:
- $response = $json["result_res"];
- // Prepare the "run" insert.
- $statement = $db->prepare("
- INSERT INTO run (
- uid,
- id,
- dtime,
- area_type,
- area,
- stage,
- difficulty,
- win,
- time,
- mana,
- energy,
- crystal,
- guild_points,
- honor_points,
- helper,
- score,
- rank
- ) VALUES (
- :uid,
- :id,
- :dtime,
- :area_type,
- :area,
- :stage,
- :difficulty,
- :win,
- :time,
- :mana,
- :energy,
- :crystal,
- :guild_points,
- :honor_points,
- :helper,
- :score,
- :rank
- );
- ");
- // Bind data and insert
- $statement->bindValue(":uid", $uid, SQLITE3_INTEGER);
- $statement->bindValue(":id", $id, SQLITE3_INTEGER);
- $statement->bindValue(":dtime", $response->{"tvaluelocal"}, SQLITE3_INTEGER);
- $statement->bindValue(":area_type", AREA_TYPE_ID::DIMENSIONAL_RIFT, SQLITE3_INTEGER);
- // The area is the region the portal is open in.
- $statement->bindValue(":area", $response->{"region_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":stage", null, SQLITE3_INTEGER);
- $statement->bindValue(":difficulty", $response->{"difficulty"}, SQLITE3_INTEGER);
- $statement->bindValue(":win", $response->{"win_lose"}, SQLITE3_INTEGER);
- $statement->bindValue(":time", 1, SQLITE3_INTEGER);
- if (array_key_exists("mana", $response->{"reward"})){
- $statement->bindValue(":mana", $response->{"reward"}->{"mana"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
- }
- if (array_key_exists("energy", $response->{"reward"})){
- $statement->bindValue(":energy", $response->{"reward"}->{"energy"});
- }
- else{
- $statement->bindValue(":energy", 0);
- }
- if (array_key_exists("crystal", $response->{"reward"})){
- $statement->bindValue(":crystal", $response->{"reward"}->{"crystal"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
- }
- $statement->bindValue(":guild_points", 0, SQLITE3_INTEGER);
- $statement->bindValue(":honor_points", 0, SQLITE3_INTEGER);
- $statement->bindValue(":helper", 0, SQLITE3_INTEGER);
- $statement->bindValue(":score", null, SQLITE3_INTEGER);
- $statement->bindValue(":rank", null, SQLITE3_TEXT);
- $statement->execute();
- // Parse party
- $leader = 1;
- foreach ($response->{"unit_list"} as $unit){
- $statement = $db->prepare("
- INSERT INTO run_party
- (run, unit, k_unit, leader, front)
- VALUES
- (:run, :unit, :k_unit, :leader, :front);"
- );
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $unit->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":k_unit", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":leader", $leader, SQLITE3_INTEGER);
- $leader = 0;
- $statement->bindValue(":front", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Return success
- return "201 Created.";
- }
- /**
- * Parses the JSONs relateds to a scenario run and saves the data.
- *
- * @param resource $db Database connection.
- * @param int $uid Player UID.
- * @param int $id Run ID.
- * @param mixed[] $json List of received JSONs.
- */
- function log_scenario_run($db, $id, $uid, $json){
- // Only the result request and response JSONs are needed:
- $response = $json["result_res"];
- if ($json["start_res"] === null){
- return ("400 Start response is required to parse scenario runs.");
- }
- $request = $json["start_res"];
- // Prepare the "run" insert.
- $statement = $db->prepare("
- INSERT INTO run (
- uid,
- id,
- dtime,
- area_type,
- area,
- stage,
- difficulty,
- win,
- time,
- mana,
- energy,
- crystal,
- guild_points,
- honor_points,
- helper,
- score,
- rank
- ) VALUES (
- :uid,
- :id,
- :dtime,
- :area_type,
- :area,
- :stage,
- :difficulty,
- :win,
- :time,
- :mana,
- :energy,
- :crystal,
- :guild_points,
- :honor_points,
- :helper,
- :score,
- :rank
- );
- ");
- // Bind data and insert
- $statement->bindValue(":uid", $uid, SQLITE3_INTEGER);
- $statement->bindValue(":id", $id, SQLITE3_INTEGER);
- $statement->bindValue(":dtime", $response->{"tvaluelocal"}, SQLITE3_TEXT);
- $statement->bindValue(":area_type", AREA_TYPE_ID::SCENARIO, SQLITE3_INTEGER);
- $statement->bindValue(":area", $response->{"scenario_info"}->{"region_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":stage", $response->{"scenario_info"}->{"stage_no"}, SQLITE3_INTEGER);
- $statement->bindValue(":difficulty", $response->{"scenario_info"}->{"difficulty"}, SQLITE3_INTEGER);
- $statement->bindValue(":win", $response->{"win_lose"}, SQLITE3_INTEGER);
- $statement->bindValue(":time", $response->{"clear_time"}->{"current_time"}, SQLITE3_INTEGER);
- if (array_key_exists("mana", $response->{"reward"})){
- $statement->bindValue(":mana", $response->{"reward"}->{"mana"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
- }
- if (array_key_exists("energy", $response->{"reward"})){
- $statement->bindValue(":energy", $response->{"reward"}->{"energy"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
- }
- if (array_key_exists("crystal", $response->{"reward"})){
- $statement->bindValue(":crystal", $response->{"reward"}->{"crystal"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
- }
- $statement->bindValue(":guild_points", 0, SQLITE3_INTEGER);
- $statement->bindValue(":honor_points", 0, SQLITE3_INTEGER);
- if (array_key_exists("helper_unit_list", $request) && sizeof($request->{"helper_unit_list"}) > 0){
- $statement->bindValue(":helper", 1, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":helper", 0, SQLITE3_INTEGER);
- }
- $statement->bindValue(":score", null, SQLITE3_INTEGER);
- $statement->bindValue(":rank", null, SQLITE3_TEXT);
- $statement->execute();
- // Parse various types of reward
- if (array_key_exists("crate", $response->{"reward"})){
- $crate = $response->{"reward"}->{"crate"};
- if (array_key_exists("unit_info", $crate)){
- $unit = $crate->{"unit_info"};
- $statement = $db->prepare("INSERT INTO run_drop_unit (run, unit) VALUES (:run, :unit);");
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->execute();
- }
- if (array_key_exists("craft_stuff", $crate)){
- $item = $crate->{"craft_stuff"};
- $statement = $db->prepare("INSERT INTO run_drop_item (run, item, amount) VALUES (:run, :item, :amount);");
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":item", $item->{"item_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":amount", $item->{"item_quantity"}, SQLITE3_INTEGER);
- $statement->execute();
- }
- if (array_key_exists("random_scroll", $crate)){
- $item = $crate->{"random_scroll"};
- $statement = $db->prepare("INSERT INTO run_drop_item (run, item, amount) VALUES (:run, :item, :amount);");
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":item", $item->{"item_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":amount", $item->{"item_quantity"}, SQLITE3_INTEGER);
- $statement->execute();
- }
- if (array_key_exists("rune", $crate)){
- $rune = $crate->{"rune"};
- $statement = $db->prepare("
- INSERT INTO run_drop_rune (
- run,
- id,
- type,
- slot,
- stars,
- ancient,
- quality,
- value,
- efficiency,
- max_efficiency,
- main_stat,
- main_stat_value,
- innate_stat,
- innate_stat_value,
- substat_1,
- substat_1_value,
- substat_2,
- substat_2_value,
- substat_3,
- substat_3_value,
- substat_4,
- substat_4_value
- ) VALUES (
- :run,
- :id,
- :type,
- :slot,
- :stars,
- :ancient,
- :quality,
- :value,
- :efficiency,
- :max_efficiency,
- :main_stat,
- :main_stat_value,
- :innate_stat,
- :innate_stat_value,
- :substat_1,
- :substat_1_value,
- :substat_2,
- :substat_2_value,
- :substat_3,
- :substat_3_value,
- :substat_4,
- :substat_4_value
- );
- ");
- $statement->bindValue(":run", $id);
- $statement->bindValue(":id", $rune->{"rune_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, SQLITE3_INTEGER);
- $statement->bindValue(":stars", intval($rune->{"class"}) - 10, SQLITE3_INTEGER);
- $statement->bindValue(":quality", intval($rune->{"rank"}) - 10, SQLITE3_INTEGER);
- }
- else{
- // Non Ancient rune
- $statement->bindValue(":ancient", 0, SQLITE3_INTEGER);
- $statement->bindValue(":stars", $rune->{"class"}, SQLITE3_INTEGER);
- $statement->bindValue(":quality", $rune->{"rank"}, SQLITE3_INTEGER);
- }
- $efficiency = calculate_efficiency($rune);
- $max_efficiency = calculate_maximum_efficiency($rune);
- $statement->bindValue(":efficiency", $efficiency, SQLITE3_FLOAT);
- $statement->bindValue(":max_efficiency", $max_efficiency, SQLITE3_FLOAT);
- $statement->bindValue(":main_stat", $rune->{"pri_eff"}[0], SQLITE3_INTEGER);
- $statement->bindValue(":main_stat_value", $rune->{"pri_eff"}[1], SQLITE3_INTEGER);
- if ($rune->{"prefix_eff"}[0] > 0){
- $statement->bindValue(":innate_stat", $rune->{"prefix_eff"}[0], SQLITE3_INTEGER);
- $statement->bindValue(":innate_stat_value", $rune->{"prefix_eff"}[1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":innate_stat", null, SQLITE3_INTEGER);
- $statement->bindValue(":innate_stat_value", 0, SQLITE3_INTEGER);
- }
- if (sizeof($rune->{"sec_eff"}) > 0){
- $statement->bindValue(":substat_1", $rune->{"sec_eff"}[0][0], SQLITE3_INTEGER);
- $statement->bindValue(":substat_1_value", $rune->{"sec_eff"}[0][1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":substat_1", null, SQLITE3_INTEGER);
- $statement->bindValue(":substat_1_value", 0, SQLITE3_INTEGER);
- }
- if (sizeof($rune->{"sec_eff"}) > 1){
- $statement->bindValue(":substat_2", $rune->{"sec_eff"}[1][0], SQLITE3_INTEGER);
- $statement->bindValue(":substat_2_value", $rune->{"sec_eff"}[1][1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":substat_2", null, SQLITE3_INTEGER);
- $statement->bindValue(":substat_2_value", 0, SQLITE3_INTEGER);
- }
- if (sizeof($rune->{"sec_eff"}) > 2){
- $statement->bindValue(":substat_3", $rune->{"sec_eff"}[2][0], SQLITE3_INTEGER);
- $statement->bindValue(":substat_3_value", $rune->{"sec_eff"}[2][1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":substat_3", null, SQLITE3_INTEGER);
- $statement->bindValue(":substat_3_value", 0, SQLITE3_INTEGER);
- }
- if (sizeof($rune->{"sec_eff"}) > 3){
- $statement->bindValue(":substat_4", $rune->{"sec_eff"}[3][0], SQLITE3_INTEGER);
- $statement->bindValue(":substat_4_value", $rune->{"sec_eff"}[3][1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":substat_4", null, SQLITE3_INTEGER);
- $statement->bindValue(":substat_4_value", 0, SQLITE3_INTEGER);
- }
- $statement->execute();
- }
- }
- // Parse party
- $leader = 1;
- foreach ($response->{"unit_list"} as $unit){
- $statement = $db->prepare("
- INSERT INTO run_party
- (run, unit, k_unit, leader, front)
- VALUES
- (:run, :unit, :k_unit, :leader, :front);"
- );
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $unit->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":k_unit", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":leader", $leader, SQLITE3_INTEGER);
- $leader = 0;
- $statement->bindValue(":front", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Return success
- return "201 Created.";
- }
- /**
- * Parses the JSONs relateds to a cairos run and saves the data.
- *
- * @param resource $db Database connection.
- * @param int $uid Player UID.
- * @param int $id Run ID.
- * @param mixed[] $json List of received JSONs.
- */
- function log_rift_raid_run($db, $id, $uid, $json){
- // Only the start and result requests JSONs are needed:
- $result = $json["result_res"];
- if ($json["start_res"] === null){
- return ("400 Start response is required to parse Rift Raid runs.");
- }
- $start = $json["start_res"];
- // Prepare the "run" insert.
- $statement = $db->prepare("
- INSERT INTO run (
- uid,
- id,
- dtime,
- area_type,
- area,
- stage,
- difficulty,
- win,
- time,
- mana,
- energy,
- crystal,
- guild_points,
- honor_points,
- helper,
- score,
- rank
- ) VALUES (
- :uid,
- :id,
- :dtime,
- :area_type,
- :area,
- :stage,
- :difficulty,
- :win,
- :time,
- :mana,
- :energy,
- :crystal,
- :guild_points,
- :honor_points,
- :helper,
- :score,
- :rank
- );
- ");
- // Bind data and insert
- $statement->bindValue(":uid", $uid, SQLITE3_INTEGER);
- $statement->bindValue(":id", $id, SQLITE3_INTEGER);
- $statement->bindValue(":dtime", $result->{"tvaluelocal"}, SQLITE3_TEXT);
- $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
- $statement->bindValue(":area", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
- $statement->bindValue(":stage", $start->{"battle_info"}->{"stage_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":difficulty", null, SQLITE3_INTEGER);
- $statement->bindValue(":win", $result->{"win_lose"}, SQLITE3_INTEGER);
- if ($result->{"win_lose"} == 1){
- $statement->bindValue(":time", $result->{"clear_time"}->{"current_time"}, SQLITE3_TEXT);
- }
- else{
- $statement->bindValue(":time", 1, SQLITE3_INTEGER);
- }
- $statement->bindValue(":mana", 0, SQLITE3_INTEGER); // If reward is mana, i will be updated later
- $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
- $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
- $statement->bindValue(":guild_points", 0, SQLITE3_INTEGER);
- $statement->bindValue(":honor_points", 0, SQLITE3_INTEGER);
- $statement->bindValue(":helper", 0, SQLITE3_INTEGER);
- $statement->bindValue(":score", null, SQLITE3_INTEGER);
- $statement->bindValue(":rank", null, SQLITE3_TEXT);
- $statement->execute();
- // Parse various types of reward
- if ($result->{"win_lose"} == 1 && array_key_exists("battle_reward_list", $result)){
- foreach ($result->{"battle_reward_list"} as $reward_list){
- if ($reward_list->{"wizard_id"} == $uid){
- $reward = $reward_list->{"reward_list"}[0];
- $reward_type = $reward->{"item_master_type"};
- switch ($reward_type){
- case INVENTORY_TYPE_ID::MONSTER: // Unit
- $statement = $db->prepare("INSERT INTO run_drop_unit (run, unit) VALUES (:run, :unit);");
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $reward->{"item_master_id"}, SQLITE3_INTEGER);
- $statement->execute();
- break;
- case INVENTORY_TYPE_ID::SCROLL:
- case INVENTORY_TYPE_ID::ESSENCES:
- $statement = $db->prepare("INSERT INTO run_drop_item (run, item, amount) VALUES (:run, :item, :amount);");
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":item", $reward->{"item_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":amount", $reward->{"item_quantity"}, SQLITE3_INTEGER);
- $statement->execute();
- break;
- case INVENTORY_TYPE_ID::ENCHANTMENT:
- $statement = $db->prepare("
- INSERT INTO run_drop_enchantment (run, id, type, quality, rune, stat, value)
- VALUES (:run, :id, :type, :quality, :rune, :stat, :value);
- ");
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":id", $reward->{"runecraft_item_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":type", $reward->{"runecraft_type"}, SQLITE3_INTEGER);
- $statement->bindValue(":quality", $reward->{"runecraft_rank"}, SQLITE3_INTEGER);
- $statement->bindValue(":rune", $reward->{"runecraft_set_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":stat", $reward->{"runecraft_effect_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":value", 0, SQLITE3_INTEGER);
- $statement->execute();
- break;
- case 102: //: Mana
- $statement = $db->prepare("UPDATE run SET mana = :mana WHERE run = :run;");
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":id", $reward->{"runecraft_item_id"}, SQLITE3_INTEGER);
- $statement->execute();
- break;
- }
- }
- }
- }
- // Parse party
- foreach ($start->{"battle_info"}->{"user_list"} as $user){
- if ($user->{"wizard_id"} == $uid){
- foreach ($user->{"deck_list"} as $unit){
- $statement = $db->prepare("
- INSERT INTO run_party
- (run, unit, k_unit, leader, front)
- VALUES
- (:run, :unit, :k_unit, :leader, :front);"
- );
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $unit->{"unit_info"}->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":k_unit", $unit->{"unit_info"}->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":leader", $unit->{"leader"}, SQLITE3_INTEGER);
- if (intval($unit->{"index"}) <= 4){
- $statement->bindValue(":front", 1, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":front", 0, SQLITE3_INTEGER);
- }
- $statement->execute();
- }
- }
- }
- // Return success
- return "201 Created.";
- }
- /**
- * Parses the JSONs relateds to a cairos run and saves the data.
- *
- * @param resource $db Database connection.
- * @param int $uid Player UID.
- * @param int $id Run ID.
- * @param mixed[] $json List of received JSONs.
- */
- function log_cairos_run($db, $id, $uid, $json){
- // Only the result request and response JSONs are needed:
- $response = $json["result_res"];
- if ($json["result_req"] === null){
- return ("400 Result request is required to parse Cairos runs.");
- }
- $request = $json["result_req"];
- // Prepare the "run" insert.
- $statement = $db->prepare("
- INSERT INTO run (
- uid,
- id,
- dtime,
- area_type,
- area,
- stage,
- difficulty,
- win,
- time,
- mana,
- energy,
- crystal,
- guild_points,
- honor_points,
- helper,
- score,
- rank
- ) VALUES (
- :uid,
- :id,
- :dtime,
- :area_type,
- :area,
- :stage,
- :difficulty,
- :win,
- :time,
- :mana,
- :energy,
- :crystal,
- :guild_points,
- :honor_points,
- :helper,
- :score,
- :rank
- );
- ");
- // Bind data and insert
- $statement->bindValue(":uid", $uid, SQLITE3_INTEGER);
- $statement->bindValue(":id", $id, SQLITE3_INTEGER);
- $statement->bindValue(":dtime", $response->{"tvaluelocal"}, SQLITE3_TEXT);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
- $statement->bindValue(":area", $request->{"dungeon_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":stage", $request->{"stage_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":difficulty", null, SQLITE3_INTEGER);
- $statement->bindValue(":win", $response->{"win_lose"}, SQLITE3_INTEGER);
- $statement->bindValue(":time", $response->{"clear_time"}->{"current_time"}, SQLITE3_INTEGER);
- if (array_key_exists("mana", $response->{"reward"})){
- $statement->bindValue(":mana", $response->{"reward"}->{"mana"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
- }
- if (array_key_exists("energy", $response->{"reward"})){
- $statement->bindValue(":energy", $response->{"reward"}->{"energy"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
- }
- if (array_key_exists("crystal", $response->{"reward"})){
- $statement->bindValue(":crystal", $response->{"reward"}->{"crystal"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
- }
- $statement->bindValue(":guild_points", 0, SQLITE3_INTEGER);
- $statement->bindValue(":honor_points", 0, SQLITE3_INTEGER);
- $statement->bindValue(":helper", 0, SQLITE3_INTEGER); // TODO Where is this information??
- $statement->bindValue(":score", null, SQLITE3_INTEGER);
- $statement->bindValue(":rank", null, SQLITE3_TEXT);
- $statement->execute();
- // Parse various types of reward
- if (array_key_exists("changed_item_list", $response)){
- $reward_type = $response->{"changed_item_list"}[0]->{"type"};
- switch ($reward_type){
- case INVENTORY_TYPE_ID::MONSTER: // Unit
- $statement = $db->prepare("INSERT INTO run_drop_unit (run, unit) VALUES (:run, :unit);");
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $response->{"changed_item_list"}[0]->{"view"}->{"item_master_id"}, SQLITE3_INTEGER);
- $statement->execute();
- break;
- case INVENTORY_TYPE_ID::SCROLL:
- case INVENTORY_TYPE_ID::ESSENCES:
- $statement = $db->prepare("INSERT INTO run_drop_item (run, item, amount) VALUES (:run, :item, :amount);");
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":item", $response->{"changed_item_list"}[0]->{"view"}->{"item_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":amount", $response->{"changed_item_list"}[0]->{"view"}->{"item_quantity"}, SQLITE3_INTEGER);
- $statement->execute();
- break;
- case INVENTORY_TYPE_ID::RUNE:
- $rune = $response->{"changed_item_list"}[0]->{"info"};
- $statement = $db->prepare("
- INSERT INTO run_drop_rune (
- run,
- id,
- type,
- slot,
- stars,
- ancient,
- quality,
- value,
- efficiency,
- max_efficiency,
- main_stat,
- main_stat_value,
- innate_stat,
- innate_stat_value,
- substat_1,
- substat_1_value,
- substat_2,
- substat_2_value,
- substat_3,
- substat_3_value,
- substat_4,
- substat_4_value
- ) VALUES (
- :run,
- :id,
- :type,
- :slot,
- :stars,
- :ancient,
- :quality,
- :value,
- :efficiency,
- :max_efficiency,
- :main_stat,
- :main_stat_value,
- :innate_stat,
- :innate_stat_value,
- :substat_1,
- :substat_1_value,
- :substat_2,
- :substat_2_value,
- :substat_3,
- :substat_3_value,
- :substat_4,
- :substat_4_value
- );
- ");
- $statement->bindValue(":run", $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(":quality", intval($rune->{"rank"}) - 10, SQLITE3_INTEGER);
- }
- else{
- // Non Ancient rune
- $statement->bindValue(":ancient", 0, SQLITE3_INTEGER);
- $statement->bindValue(":stars", $rune->{"class"}, SQLITE3_INTEGER);
- $statement->bindValue(":quality", $rune->{"rank"}, SQLITE3_INTEGER);
- }
- $efficiency = calculate_efficiency($rune);
- $max_efficiency = calculate_maximum_efficiency($rune);
- $statement->bindValue(":efficiency", $efficiency, SQLITE3_FLOAT);
- $statement->bindValue(":max_efficiency", $max_efficiency, SQLITE3_FLOAT);
- $statement->bindValue(":main_stat", $rune->{"pri_eff"}[0], SQLITE3_INTEGER);
- $statement->bindValue(":main_stat_value", $rune->{"pri_eff"}[1], SQLITE3_INTEGER);
- if ($rune->{"prefix_eff"}[0] > 0){
- $statement->bindValue(":innate_stat", $rune->{"prefix_eff"}[0], SQLITE3_INTEGER);
- $statement->bindValue(":innate_stat_value", $rune->{"prefix_eff"}[1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":innate_stat", null, SQLITE3_INTEGER);
- $statement->bindValue(":innate_stat_value", 0, SQLITE3_INTEGER);
- }
- if (sizeof($rune->{"sec_eff"}) > 0){
- $statement->bindValue(":substat_1", $rune->{"sec_eff"}[0][0], SQLITE3_INTEGER);
- $statement->bindValue(":substat_1_value", $rune->{"sec_eff"}[0][1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":substat_1", null, SQLITE3_INTEGER);
- $statement->bindValue(":substat_1_value", 0, SQLITE3_INTEGER);
- }
- 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]);
- }
- else{
- $statement->bindValue(":substat_2", null, SQLITE3_INTEGER);
- $statement->bindValue(":substat_2_value", 0, SQLITE3_INTEGER);
- }
- if (sizeof($rune->{"sec_eff"}) > 2){
- $statement->bindValue(":substat_3", $rune->{"sec_eff"}[2][0], SQLITE3_INTEGER);
- $statement->bindValue(":substat_3_value", $rune->{"sec_eff"}[2][1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":substat_3", null, SQLITE3_INTEGER);
- $statement->bindValue(":substat_3_value", 0, SQLITE3_INTEGER);
- }
- if (sizeof($rune->{"sec_eff"}) > 3){
- $statement->bindValue(":substat_4", $rune->{"sec_eff"}[3][0], SQLITE3_INTEGER);
- $statement->bindValue(":substat_4_value", $rune->{"sec_eff"}[3][1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":substat_4", null, SQLITE3_INTEGER);
- $statement->bindValue(":substat_4_value", 0, SQLITE3_INTEGER);
- }
- $statement->execute();
- break;
- }
- }
- // Parse party
- $leader = 1;
- foreach ($response->{"unit_list"} as $unit){
- $statement = $db->prepare("
- INSERT INTO run_party
- (run, unit, k_unit, leader, front)
- VALUES
- (:run, :unit, :k_unit, :leader, :front);"
- );
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $unit->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":k_unit", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":leader", $leader, SQLITE3_INTEGER);
- $leader = 0;
- $statement->bindValue(":front", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Return success
- return "201 Created.";
- }
- /**
- * Parses the JSONs relateds to a Dimension Hole run and saves the data.
- *
- * @param resource $db Database connection.
- * @param int $uid Player UID.
- * @param int $id Run ID.
- * @param mixed[] $json List of received JSONs.
- */
- function log_dimension_hole_run($db, $id, $uid, $json){
- // Only the result request and response JSONs are needed:
- $response = $json["result_res"];
- if ($json["result_req"] === null){
- return ("400 Result request is required to parse Dimension Hole runs.");
- }
- // Prepare the "run" insert.
- $statement = $db->prepare("
- INSERT INTO run (
- uid,
- id,
- dtime,
- area_type,
- area,
- stage,
- difficulty,
- win,
- time,
- mana,
- energy,
- crystal,
- guild_points,
- honor_points,
- helper,
- score,
- rank
- ) VALUES (
- :uid,
- :id,
- :dtime,
- :area_type,
- :area,
- :stage,
- :difficulty,
- :win,
- :time,
- :mana,
- :energy,
- :crystal,
- :guild_points,
- :honor_points,
- :helper,
- :score,
- :rank
- );
- ");
- // Bind data and insert
- $statement->bindValue(":uid", $uid, SQLITE3_INTEGER);
- $statement->bindValue(":id", $id, SQLITE3_INTEGER);
- $statement->bindValue(":dtime", $response->{"tvaluelocal"}, SQLITE3_TEXT);
- $statement->bindValue(":area_type", AREA_TYPE_ID::DIMENSIONAL_HOLE, SQLITE3_INTEGER);
- $statement->bindValue(":area", $response->{"dungeon_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":stage", $response->{"difficulty"}, SQLITE3_INTEGER); // Difficulty is stage
- $statement->bindValue(":difficulty", null, SQLITE3_INTEGER);
- $statement->bindValue(":win", $response->{"win_lose"}, SQLITE3_INTEGER);
- $statement->bindValue(":time", $response->{"clear_time"}->{"current_time"}, SQLITE3_INTEGER);
- if (array_key_exists("mana", $response->{"reward"})){
- $statement->bindValue(":mana", $response->{"reward"}->{"mana"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
- }
- if (array_key_exists("energy", $response->{"reward"})){
- $statement->bindValue(":energy", $response->{"reward"}->{"energy"}, SQLITE3_INTEGER);
- }
- $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
- if (array_key_exists("crystal", $response->{"reward"})){
- $statement->bindValue(":crystal", $response->{"reward"}->{"crystal"}, SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
- }
- $statement->bindValue(":guild_points", 0, SQLITE3_INTEGER);
- $statement->bindValue(":honor_points", 0, SQLITE3_INTEGER);
- $statement->bindValue(":helper", 0, SQLITE3_INTEGER); // TODO Where is this information??
- $statement->bindValue(":score", null, SQLITE3_INTEGER);
- $statement->bindValue(":rank", null, SQLITE3_TEXT);
- $statement->execute();
- // Parse various types of reward
- if (array_key_exists("changed_item_list", $response)){
- foreach ($response->{"changed_item_list"} as $drop){
- $reward_type = $drop->{"type"};
- switch ($reward_type){
- case INVENTORY_TYPE_ID::CRAFT_STUFF:
- $statement = $db->prepare("INSERT INTO run_drop_item (run, item, amount) VALUES (:run, :item, :amount);");
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":item", $drop->{"view"}->{"item_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":amount", $drop->{"view"}->{"item_quantity"}, SQLITE3_INTEGER);
- $statement->execute();
- break;
- case INVENTORY_TYPE_ID::RUNE:
- // TODO: Investigate with no awaken dungeons
- /*$rune = $response->{"changed_item_list"}[0]->{"info"};
- $statement = $db->prepare("
- INSERT INTO run_drop_rune (
- run,
- id,
- type,
- slot,
- stars,
- ancient,
- quality,
- value,
- efficiency,
- max_efficiency,
- main_stat,
- main_stat_value,
- innate_stat,
- innate_stat_value,
- substat_1,
- substat_1_value,
- substat_2,
- substat_2_value,
- substat_3,
- substat_3_value,
- substat_4,
- substat_4_value
- ) VALUES (
- :run,
- :id,
- :type,
- :slot,
- :stars,
- :ancient,
- :quality,
- :value,
- :efficiency,
- :max_efficiency,
- :main_stat,
- :main_stat_value,
- :innate_stat,
- :innate_stat_value,
- :substat_1,
- :substat_1_value,
- :substat_2,
- :substat_2_value,
- :substat_3,
- :substat_3_value,
- :substat_4,
- :substat_4_value
- );
- ");
- $statement->bindValue(":run", $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(":quality", intval($rune->{"rank"}) - 10, SQLITE3_INTEGER);
- }
- else{
- // Non Ancient rune
- $statement->bindValue(":ancient", 0, SQLITE3_INTEGER);
- $statement->bindValue(":stars", $rune->{"class"}, SQLITE3_INTEGER);
- $statement->bindValue(":quality", $rune->{"rank"}, SQLITE3_INTEGER);
- }
- $efficiency = calculate_efficiency($rune);
- $max_efficiency = calculate_maximum_efficiency($rune);
- $statement->bindValue(":efficiency", $efficiency, SQLITE3_FLOAT);
- $statement->bindValue(":max_efficiency", $max_efficiency, SQLITE3_FLOAT);
- $statement->bindValue(":main_stat", $rune->{"pri_eff"}[0], SQLITE3_INTEGER);
- $statement->bindValue(":main_stat_value", $rune->{"pri_eff"}[1], SQLITE3_INTEGER);
- if ($rune->{"prefix_eff"}[0] > 0){
- $statement->bindValue(":innate_stat", $rune->{"prefix_eff"}[0], SQLITE3_INTEGER);
- $statement->bindValue(":innate_stat_value", $rune->{"prefix_eff"}[1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":innate_stat", null, SQLITE3_INTEGER);
- $statement->bindValue(":innate_stat_value", 0, SQLITE3_INTEGER);
- }
- if (sizeof($rune->{"sec_eff"}) > 0){
- $statement->bindValue(":substat_1", $rune->{"sec_eff"}[0][0], SQLITE3_INTEGER);
- $statement->bindValue(":substat_1_value", $rune->{"sec_eff"}[0][1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":substat_1", null, SQLITE3_INTEGER);
- $statement->bindValue(":substat_1_value", 0, SQLITE3_INTEGER);
- }
- if (sizeof($rune->{"sec_eff"}) > 1){
- $statement->bindValue(":substat_2", $rune->{"sec_eff"}[1][0], SQLITE3_INTEGER);
- $statement->bindValue(":substat_2_value", $rune->{"sec_eff"}[1][1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":substat_2", null, SQLITE3_INTEGER);
- $statement->bindValue(":substat_2_value", 0, SQLITE3_INTEGER);
- }
- if (sizeof($rune->{"sec_eff"}) > 2){
- $statement->bindValue(":substat_3", $rune->{"sec_eff"}[2][0], SQLITE3_INTEGER);
- $statement->bindValue(":substat_3_value", $rune->{"sec_eff"}[2][1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":substat_3", null, SQLITE3_INTEGER);
- $statement->bindValue(":substat_3_value", 0, SQLITE3_INTEGER);
- }
- if (sizeof($rune->{"sec_eff"}) > 3){
- $statement->bindValue(":substat_4", $rune->{"sec_eff"}[3][0], SQLITE3_INTEGER);
- $statement->bindValue(":substat_4_value", $rune->{"sec_eff"}[3][1], SQLITE3_INTEGER);
- }
- else{
- $statement->bindValue(":substat_4", null, SQLITE3_INTEGER);
- $statement->bindValue(":substat_4_value", 0, SQLITE3_INTEGER);
- }
- $statement->execute();*/
- break;
- }
- }
- }
- // Parse party
- $leader = 1;
- foreach ($response->{"unit_list"} as $unit){
- $statement = $db->prepare("
- INSERT INTO run_party
- (run, unit, k_unit, leader, front)
- VALUES
- (:run, :unit, :k_unit, :leader, :front);"
- );
- $statement->bindValue(":run", $id, SQLITE3_INTEGER);
- $statement->bindValue(":unit", $unit->{"unit_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":k_unit", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
- $statement->bindValue(":leader", $leader, SQLITE3_INTEGER);
- $leader = 0;
- $statement->bindValue(":front", 0, SQLITE3_INTEGER);
- $statement->execute();
- }
- // Return success
- return "201 Created.";
- }
- global $db;
- try{
- header("Content-type: application/json; charset=utf-8");
- // Get put data
- parse_str(file_get_contents("php://input"), $_POST);
- // Check API key.
- if (!array_key_exists("key", $_POST)){
- header("HTTP/1.1 400 API key not received.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 API key not received.");
- return 400;
- }
- $key = $_POST["key"];
- // Get all JSON files
- $json = [
- "start_req" => null,
- "start_res" => null,
- "result_req" => null,
- "result_res" => null,
- ];
- // Result response - mandatory
- if (!array_key_exists("result_response", $_POST)){
- header("HTTP/1.1 400 No result response data received.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 No result response data received.");
- return 400;
- }
- $data_json = json_decode($_POST["result_response"]);
- if ($data_json === null){
- header("HTTP/1.1 400 Result response data is no valid JSON.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 Result response data is no valid JSON.");
- return 400;
- }
- $json["result_res"] = $data_json;
- // Result request - optional
- if (array_key_exists("result_request", $_POST)){
- $data_raw = $_POST["result_request"];
- if ($data_raw != null && $data_raw != false){
- $data_json = json_decode($data_raw);
- if ($data_json != null){
- $json["result_req"] = $data_json;
- }
- }
- }
- // Start result - optional
- if (array_key_exists("start_response", $_POST)){
- $data_raw = $_POST["start_response"];
- if ($data_raw != null && $data_raw != false){
- $data_json = json_decode($data_raw);
- if ($data_json != null){
- $json["start_res"] = $data_json;
- }
- }
- }
- // Start result - optional
- if (array_key_exists("start_request", $_POST)){
- $data_raw = $_POST["start_request"];
- if ($data_raw != null && $data_raw != false){
- $data_json = json_decode($data_raw);
- if ($data_json != null){
- $json["start_req"] = $data_json;
- }
- }
- }
- // Extract basic data
- $uid = $json["result_res"]->{"wizard_info"}->{"wizard_id"};
- $command = $json["result_res"]->{"command"};
- // Authenticate
- $statement = $db->prepare("SELECT COUNT(uid) AS c FROM player WHERE uid = :uid AND api_key = :api_key;");
- $statement->bindValue(":uid", $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, "[APIv3] HTTP/1.1 401 Invalid credentials.");
- return 401;
- }
- // Check for implemented command
- $accepted_commands = [
- "BattleDungeonResult_V2", // Dungeon Run
- "BattleDimensionHoleDungeonResult_v2", // Dimension Hole Run
- "BattleRiftOfWorldsRaidResult", // Rift Raid Run
- "BattleScenarioResult", // Scenario Run
- "BattleArenaResult", // Arena Run
- "BattleDarkPortalResult" // Dimensinal Rift Run
- ];
- 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;
- }
- // Check if already in DB
- $dtime = $json["result_res"]->{"tvaluelocal"};
- $statement = $db->prepare("SELECT count(id) AS c FROM run WHERE uid = :uid AND dtime = :dtime;");
- $statement->bindValue(":uid", $uid, SQLITE3_INTEGER);
- $statement->bindValue(":dtime", $dtime, SQLITE3_TEXT);
- if (0 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
- header("HTTP/1.1 409 Run already in database.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 409 Run already in database.");
- return 409;
- }
- // Get new ID
- $q = $db->query("SELECT max(id) + 1 AS id FROM run;");
- $r = $q->fetchArray(SQLITE3_ASSOC);
- if ($r){
- $id = $r["id"];
- }
- else{
- $id = 1;
- }
- // Run parsing function
- $result = "500 Unknown Error";
- switch($command){
- case "BattleDungeonResult_V2":
- $result = log_cairos_run($db, $id, $uid, $json);
- break;
- case "BattleDimensionHoleDungeonResult_v2":
- $result = log_dimension_hole_run($db, $id, $uid, $json);
- break;
- case "BattleRiftOfWorldsRaidResult":
- $result = log_rift_raid_run($db, $id, $uid, $json);
- break;
- case "BattleScenarioResult":
- $result = log_scenario_run($db, $id, $uid, $json);
- break;
- case "BattleArenaResult":
- $result = log_arena_run($db, $id, $uid, $json);
- break;
- case "BattleDarkPortalResult":
- $result = log_dark_portal_run($db, $id, $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_ERR, "[APIv3] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
- return 500;
- }
- ?>
|