Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
added colors, recipes & skins
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Mar 30, 2017
1 parent 44e4c73 commit 9d0267d
Show file tree
Hide file tree
Showing 6 changed files with 586 additions and 0 deletions.
25 changes: 25 additions & 0 deletions sql/gw2_colors.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CREATE TABLE `gw2_colors` (
`id` INT(10) UNSIGNED NOT NULL,
`item_id` INT(10) UNSIGNED NOT NULL,
`hue` ENUM ('Blue', 'Brown', 'Gray', 'Green', 'Orange', 'Purple', 'Red', 'Yellow') DEFAULT NULL,
`material` ENUM ('Leather', 'Metal', 'Vibrant') DEFAULT NULL,
`rarity` ENUM ('Common', 'Rare', 'Starter', 'Uncommon') DEFAULT NULL,
`name_de` TINYTEXT NOT NULL,
`name_en` TINYTEXT NOT NULL,
`name_es` TINYTEXT NOT NULL,
`name_fr` TINYTEXT NOT NULL,
`name_zh` TEXT NOT NULL,
`data_de` TEXT NOT NULL,
`data_en` TEXT NOT NULL,
`data_es` TEXT NOT NULL,
`data_fr` TEXT NOT NULL,
`data_zh` TEXT NOT NULL,
`updated` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`date_added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_bin;

25 changes: 25 additions & 0 deletions sql/gw2_recipes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CREATE TABLE `gw2_recipes` (
`id` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`output_id` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`output_count` SMALLINT(3) UNSIGNED NOT NULL DEFAULT '0',
`disciplines` SMALLINT(3) UNSIGNED NOT NULL DEFAULT '0',
`rating` SMALLINT(3) UNSIGNED NOT NULL DEFAULT '0',
`type` TINYTEXT NOT NULL,
`from_item` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`ing_id_1` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`ing_count_1` SMALLINT(3) UNSIGNED NOT NULL DEFAULT '0',
`ing_id_2` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`ing_count_2` SMALLINT(3) UNSIGNED NOT NULL DEFAULT '0',
`ing_id_3` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`ing_count_3` SMALLINT(3) UNSIGNED NOT NULL DEFAULT '0',
`ing_id_4` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`ing_count_4` SMALLINT(3) UNSIGNED NOT NULL DEFAULT '0',
`data` TEXT NOT NULL,
`updated` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`date_added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_bin;
26 changes: 26 additions & 0 deletions sql/gw2_skins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
CREATE TABLE `gw2_skins` (
`id` INT(10) UNSIGNED NOT NULL,
`signature` VARCHAR(40) NOT NULL,
`file_id` INT(10) NOT NULL DEFAULT '0',
`type` TINYTEXT NOT NULL,
`subtype` TINYTEXT NOT NULL,
`properties` TINYTEXT NOT NULL,
`name_de` TINYTEXT NOT NULL,
`name_en` TINYTEXT NOT NULL,
`name_es` TINYTEXT NOT NULL,
`name_fr` TINYTEXT NOT NULL,
`name_zh` TINYTEXT NOT NULL,
`data_de` TEXT NOT NULL,
`data_en` TEXT NOT NULL,
`data_es` TEXT NOT NULL,
`data_fr` TEXT NOT NULL,
`data_zh` TEXT NOT NULL,
`updated` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`date_added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_bin;

166 changes: 166 additions & 0 deletions src/Updaters/Items/Colors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php
/**
* Class Colors
*
* @filesource Colors.php
* @created 30.03.2017
* @package chillerlan\GW2DB\Updaters\Items
* @author Smiley <smiley@chillerlan.net>
* @copyright 2017 Smiley
* @license MIT
*/

namespace chillerlan\GW2DB\Updaters\Items;

use chillerlan\GW2DB\Helpers;
use chillerlan\GW2DB\Updaters\{MultiRequestAbstract, UpdaterException};
use chillerlan\TinyCurl\{ResponseInterface, URL};

/**
*/
class Colors extends MultiRequestAbstract{

/**
* @var array
*/
protected $colors;

/**
* @throws \chillerlan\GW2DB\Updaters\UpdaterException
*/
public function init(){
$this->refreshIDs('colors', getenv('TABLE_GW2_COLORS'));

$sql = 'SELECT `id`, `data_de`, `data_en`, `data_es`, `data_fr`, `data_zh`, UNIX_TIMESTAMP(`update_time`) AS `update_time`, UNIX_TIMESTAMP(`date_added`) AS `date_added` FROM `'.getenv('TABLE_GW2_COLORS').'`';

$this->colors = $this->DBDriverInterface->raw($sql, 'id', true, true);

if(!$this->colors || !is_array($this->colors)){
throw new UpdaterException('failed to fetch color data from db');
}

$urls = [];

foreach(array_chunk($this->colors, self::CHUNK_SIZE) as $chunk){
foreach(self::API_LANGUAGES as $lang){
$urls[] = new URL(self::API_BASE.'/colors', ['lang' => $lang, 'ids' => implode(',', array_column($chunk, 'id'))]);
}
}

$this->fetchMulti($urls);
$this->updateStats();
$this->logToCLI(__METHOD__.': end');
}

/**
* @param \chillerlan\TinyCurl\ResponseInterface $response
*
* @return mixed
*/
protected function processResponse(ResponseInterface $response){
$info = $response->info;

parse_str(parse_url($info->url, PHP_URL_QUERY), $params);

$this->lang = $response->headers->{'content-language'} ?: $params['lang'];

if(!$this->checkResponseLanguage($this->lang)){
return false;
}

$sql = 'UPDATE '.getenv('TABLE_GW2_COLORS').' SET `name_'.$this->lang.'`= ?, `data_'.$this->lang.'`= ? WHERE `id` = ?';

$query = $this->DBDriverInterface->multi_callback($sql, $response->json_array, [$this, 'callback']);

if(!$query){
$this->logToCLI('SQL insert failed, retrying URL. ('.$info->url.')');

return new URL($info->url);
}

if(!empty($this->changes)){
$sql = 'INSERT INTO `'.getenv('TABLE_GW2_DIFF').'` (`db_id`, `type`, `lang`, `date`, `data`) VALUES (?,?,?,?,?)';

if($this->DBDriverInterface->multi($sql, $this->changes)){
$this->changes = [];
}
}

$this->logToCLI('['.$this->lang.'] '.md5($info->url).' updated');
}

/**
* @param array $color
*
* @return array
*/
public function callback(array $color){
$old = json_decode(@$this->colors[$color['id']]['data_'.$this->lang], true) ?? [];
$diff = Helpers\array_diff_assoc_recursive($old, $color, true);

if(!empty($old) && !empty($diff)){
$this->changes[] = [
$color['id'],
'color',
$this->lang,
$this->colors[$color['id']]['update_time'] ?? $this->colors[$color['id']]['date_added'] ?? time(),
json_encode($old),
];

$this->logToCLI('['.$this->lang.'] color changed #'.$color['id'].' '.print_r($diff, true));
}

$this->logToCLI('['.$this->lang.'] updated color data #'.$color['id']);

return [
$color['name'],
json_encode($color),
$color['id'],
];
}

/**
* @throws \chillerlan\GW2DB\Updaters\UpdaterException
*/
protected function updateStats(){
$sql = 'SELECT `data_en` FROM `'.getenv('TABLE_GW2_COLORS').'`';

$this->colors = $this->DBDriverInterface->raw($sql, null, true, true);

if(!$this->colors || !is_array($this->colors)){
throw new UpdaterException('failed to fetch color data from db');
}

$sql = 'UPDATE '.getenv('TABLE_GW2_COLORS').' SET `hue`= ?, `material`= ?, `rarity`= ?, `updated`= ? WHERE `id` = ?';

$query = $this->DBDriverInterface->multi_callback($sql, $this->colors, [$this, 'statsCallback']);

if(!$query){
throw new UpdaterException('failed to update stats');
}
}

/**
* @param array $color
*
* @return array
*/
public function statsCallback(array $color):array{
$data = json_decode($color['data_en']);

list($hue, $material, $rarity) = !empty($data->categories) ? $data->categories : [null, null, null];

$this->logToCLI('updated color stats #'.$data->id);

return [
$hue,
$material,
$rarity,
1,
$data->id,
];
}

}


Loading

0 comments on commit 9d0267d

Please sign in to comment.