275 lines
11 KiB
PHP
275 lines
11 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Gear-Planer: Werte der Gegenstaende von aion2hub.com holen und auslesen.
|
|
*
|
|
* aion2hub liest die Werte direkt aus dem Spielclient aus (Global-Client). Abgerufen wird nur
|
|
* auf Knopfdruck eines Admins und immer nur eine Seite auf einmal (siehe admin/gear_items.php) -
|
|
* die Schnittstelle /api/ der Seite ist fuer Bots gesperrt, die normalen Seiten und die Icons
|
|
* (/api/icon/) sind erlaubt.
|
|
*/
|
|
|
|
const GEAR_SOURCE_HOST = 'aion2hub.com';
|
|
const GEAR_SOURCE_ITEM_URL = 'https://aion2hub.com/database/items/';
|
|
const GEAR_SOURCE_ICON_URL = 'https://aion2hub.com/api/icon/items/';
|
|
const GEAR_SOURCE_AGENT = 'LuminaAscendensGearPlanner/1.0 (+Legion-Website, einzelne Abrufe auf Knopfdruck)';
|
|
|
|
/** Laedt eine Seite von aion2hub (nur dieser Host, kein Weiterleiten auf andere Seiten). */
|
|
function gear_source_fetch(string $url): string
|
|
{
|
|
$host = strtolower((string) parse_url($url, PHP_URL_HOST));
|
|
if (parse_url($url, PHP_URL_SCHEME) !== 'https' || ($host !== GEAR_SOURCE_HOST && $host !== 'www.' . GEAR_SOURCE_HOST)) {
|
|
throw new RuntimeException('Nur Adressen von ' . GEAR_SOURCE_HOST . ' sind erlaubt.');
|
|
}
|
|
if (!function_exists('curl_init')) {
|
|
throw new RuntimeException('Die PHP-cURL-Erweiterung ist auf diesem Server nicht aktiviert.');
|
|
}
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_FOLLOWLOCATION => false,
|
|
CURLOPT_TIMEOUT => 15,
|
|
CURLOPT_CONNECTTIMEOUT => 5,
|
|
CURLOPT_USERAGENT => GEAR_SOURCE_AGENT,
|
|
CURLOPT_HTTPHEADER => ['Accept: text/html,image/*'],
|
|
]);
|
|
$body = curl_exec($ch);
|
|
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
if ($body === false) {
|
|
throw new RuntimeException('aion2hub nicht erreichbar: ' . $error);
|
|
}
|
|
if ($status === 404) {
|
|
throw new RuntimeException('Diesen Gegenstand gibt es bei aion2hub nicht.');
|
|
}
|
|
if ($status !== 200) {
|
|
throw new RuntimeException('aion2hub antwortet mit Status ' . $status . '.');
|
|
}
|
|
return (string) $body;
|
|
}
|
|
|
|
/** Gegenstands-ID aus einer ID oder einem aion2hub-Link ("…/database/items/110130060"). */
|
|
function gear_source_parse_id(string $input): ?int
|
|
{
|
|
$input = trim($input);
|
|
if (preg_match('/^\d{6,12}$/', $input)) {
|
|
return (int) $input;
|
|
}
|
|
if (preg_match('#aion2hub\.com/database/items/(\d{6,12})#i', $input, $m)) {
|
|
return (int) $m[1];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Kategorien der Datenbank, die der Gear-Planer kennt (Listen ohne Kategorie = alle drei)
|
|
const GEAR_SOURCE_CATEGORIES = ['Weapons', 'Armor', 'Accessories'];
|
|
|
|
/** Alle Gegenstands-IDs, die auf einer Listenseite von aion2hub verlinkt sind. */
|
|
function gear_source_list_ids(string $html): array
|
|
{
|
|
preg_match_all('#href="/database/items/(\d{6,12})"#', $html, $m);
|
|
return array_values(array_unique(array_map('intval', $m[1])));
|
|
}
|
|
|
|
/**
|
|
* Listen-Adresse (aion2hub.com/database?...) in die Listen je Kategorie aufteilen. Ohne
|
|
* Kategorie werden es Waffen, Ruestungen und Schmuck; weitere Filter (z.B. grade) bleiben.
|
|
*
|
|
* @return string[] Adressen ohne Seitenzahl
|
|
* @throws RuntimeException bei fremden Adressen oder Kategorien ohne Ausruestung
|
|
*/
|
|
function gear_source_list_urls(string $url): array
|
|
{
|
|
if (!preg_match('#^https://(www\.)?aion2hub\.com/database/?(\?.*)?$#i', trim($url))) {
|
|
throw new RuntimeException('Bitte eine Gegenstands-ID, einen Gegenstands-Link oder eine Listen-Seite von aion2hub.com/database eintragen.');
|
|
}
|
|
parse_str((string) parse_url(trim($url), PHP_URL_QUERY), $query);
|
|
unset($query['page'], $query['view']);
|
|
$query = array_filter($query, static fn ($value): bool => is_string($value) && $value !== '');
|
|
$categories = isset($query['cat']) ? [(string) $query['cat']] : GEAR_SOURCE_CATEGORIES;
|
|
foreach ($categories as $category) {
|
|
if (!in_array($category, GEAR_SOURCE_CATEGORIES, true)) {
|
|
throw new RuntimeException('Die Kategorie „' . $category . '“ enthält keine Ausrüstung - nur Weapons, Armor und Accessories lassen sich übernehmen.');
|
|
}
|
|
}
|
|
return array_map(
|
|
static fn (string $category): string => 'https://' . GEAR_SOURCE_HOST . '/database?' . http_build_query(['cat' => $category] + $query),
|
|
$categories
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Eine Seite einer Liste: enthaltene IDs, Seitenzahl und Gesamtzahl ("1,844 items · page 1 of 31").
|
|
*
|
|
* @return array{ids: int[], page: int, pages: int, total: int}
|
|
*/
|
|
function gear_source_list_page(string $html): array
|
|
{
|
|
$text = implode(' ', gear_source_tokens($html));
|
|
preg_match('/page\s+(\d+)\s+of\s+(\d+)/i', $text, $pages);
|
|
preg_match('/([\d,]+)\s+items\s+·\s+page/i', $text, $total);
|
|
$ids = gear_source_list_ids($html);
|
|
return [
|
|
'ids' => $ids,
|
|
'page' => (int) ($pages[1] ?? 1),
|
|
'pages' => max(1, (int) ($pages[2] ?? 1)),
|
|
'total' => (int) str_replace(',', '', $total[1] ?? (string) count($ids)),
|
|
];
|
|
}
|
|
|
|
/** Seite in Textbausteine zerlegen (jedes HTML-Element wird zur Trennstelle). */
|
|
function gear_source_tokens(string $html): array
|
|
{
|
|
$html = preg_replace('#<(script|style|noscript)\b.*?</\1>#is', '', $html) ?? '';
|
|
$text = preg_replace('/<[^>]+>/', '|', $html) ?? '';
|
|
$text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
|
$tokens = [];
|
|
foreach (explode('|', $text) as $token) {
|
|
$token = trim(preg_replace('/\s+/u', ' ', $token) ?? '');
|
|
if ($token !== '') {
|
|
$tokens[] = $token;
|
|
}
|
|
}
|
|
return $tokens;
|
|
}
|
|
|
|
/** "1,100" -> [1100, ''], "33%" -> [33, '%'], sonst null */
|
|
function gear_source_number(string $value): ?array
|
|
{
|
|
$value = trim($value);
|
|
if (!preg_match('/^(-?[\d,]+(?:\.\d+)?)\s*(%?)$/', $value, $m)) {
|
|
return null;
|
|
}
|
|
return [(float) str_replace(',', '', $m[1]), $m[2]];
|
|
}
|
|
|
|
/** "Min Attack 106, Max Attack 106" -> [['Min Attack', 106, ''], …] */
|
|
function gear_source_stat_list(string $text): array
|
|
{
|
|
$stats = [];
|
|
foreach (explode(',', $text) as $part) {
|
|
if (preg_match('/^\s*(.+?)\s+(-?[\d.]+)\s*(%?)\s*$/u', $part, $m)) {
|
|
$stats[] = [$m[1], (float) $m[2], $m[3]];
|
|
}
|
|
}
|
|
return $stats;
|
|
}
|
|
|
|
/**
|
|
* Liest eine Gegenstandsseite von aion2hub aus.
|
|
*
|
|
* @return array{source_id: int, name: string, category: string, grade: string, item_level: int,
|
|
* required_level: ?int, slot: string, item_type: string, stats: array, soul_imprint: array,
|
|
* soul_imprint_count: int, manastone_slots: int, godstone_slots: int, enchant_max: int,
|
|
* enchant_stats: array, exceed_max: int, exceed_stats: array, source_version: string}
|
|
*/
|
|
function gear_source_parse_item(string $html): array
|
|
{
|
|
$t = gear_source_tokens($html);
|
|
$n = count($t);
|
|
|
|
// Kopf: "Item Database / <Kategorie> / <Name> / Copy link / <Seltenheit> / Item Lv / 86 / …"
|
|
$start = null;
|
|
for ($i = 0; $i < $n - 4; $i++) {
|
|
if ($t[$i] === 'Item Database' && $t[$i + 1] === '/' && ($t[$i + 4] ?? '') === 'Copy link') {
|
|
$start = $i;
|
|
break;
|
|
}
|
|
}
|
|
if ($start === null) {
|
|
throw new RuntimeException('Die Seite sieht nicht wie eine Gegenstandsseite von aion2hub aus.');
|
|
}
|
|
$item = [
|
|
'category' => $t[$start + 2],
|
|
'name' => $t[$start + 3],
|
|
'grade' => $t[$start + 5],
|
|
'item_level' => 0,
|
|
'required_level' => null,
|
|
'slot' => '',
|
|
'item_type' => '',
|
|
'stats' => [],
|
|
'soul_imprint' => [],
|
|
'soul_imprint_count' => 0,
|
|
'manastone_slots' => 0,
|
|
'godstone_slots' => 0,
|
|
'enchant_max' => 0,
|
|
'enchant_stats' => [],
|
|
'exceed_max' => 0,
|
|
'exceed_stats' => [],
|
|
'source_version' => '',
|
|
'source_id' => 0,
|
|
];
|
|
$i = $start + 6;
|
|
if (($t[$i] ?? '') === 'Item Lv') {
|
|
$item['item_level'] = (int) $t[$i + 1];
|
|
$i += 2;
|
|
}
|
|
if (($t[$i] ?? '') === 'Requires Lv') {
|
|
$item['required_level'] = (int) $t[$i + 1];
|
|
$i += 2;
|
|
}
|
|
$item['slot'] = $t[$i] ?? '';
|
|
$item['item_type'] = $t[$i + 1] ?? '';
|
|
|
|
// Grundwerte: Paare nach "Stats" bis zum naechsten Abschnitt
|
|
$stop = ['Soul Imprint', 'Item Info', 'KR/TW', 'Upgrades', 'How to Obtain'];
|
|
$statsAt = array_search('Stats', array_slice($t, $i, 40, true), true);
|
|
if ($statsAt !== false) {
|
|
for ($j = $statsAt + 1; $j < $n - 1 && !in_array($t[$j], $stop, true); $j += 2) {
|
|
$number = gear_source_number($t[$j + 1]);
|
|
if ($number === null) {
|
|
break;
|
|
}
|
|
$item['stats'][] = [$t[$j], $number[0], $number[1]];
|
|
}
|
|
|
|
// Seelenpraegung: "— 4 of these can be selected", dann "Name / 21 ~ 31" (Fertigkeiten danach zaehlen nicht)
|
|
if (($t[$j] ?? '') === 'Soul Imprint' && preg_match('/(\d+) of these can be selected/', $t[$j + 1] ?? '', $m)) {
|
|
$item['soul_imprint_count'] = (int) $m[1];
|
|
for ($k = $j + 2; $k < $n - 1; $k += 2) {
|
|
if (!preg_match('/^(-?[\d,.]+)(%?) ~ (-?[\d,.]+)(%?)$/', $t[$k + 1], $r)) {
|
|
break;
|
|
}
|
|
$item['soul_imprint'][] = [$t[$k], (float) str_replace(',', '', $r[1]), (float) str_replace(',', '', $r[3]), $r[2]];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sockel und Aufwertung stehen unter "Item Info" / "Upgrades" (nach dem Korea-Vergleich)
|
|
$info = array_search('Item Info', $t, true);
|
|
if ($info !== false) {
|
|
for ($j = $info + 1; $j < $n - 1 && $t[$j] !== 'How to Obtain'; $j++) {
|
|
if ($t[$j] === 'Manastone sockets') {
|
|
$item['manastone_slots'] = (int) $t[$j + 1];
|
|
} elseif ($t[$j] === 'Godstone sockets') {
|
|
$item['godstone_slots'] = (int) $t[$j + 1];
|
|
} elseif (($t[$j] === 'Enchantable to' || str_starts_with($t[$j], 'Exceed')) && ($t[$j + 1] ?? '') === '+') {
|
|
// "Enchantable to / + / 15 / At + / 15 / : / Min Attack 106, Max Attack 106"
|
|
$max = (int) ($t[$j + 2] ?? 0);
|
|
$stats = ($t[$j + 3] ?? '') === 'At +' && ($t[$j + 5] ?? '') === ':' ? gear_source_stat_list($t[$j + 6] ?? '') : [];
|
|
if ($t[$j] === 'Enchantable to') {
|
|
$item['enchant_max'] = $max;
|
|
$item['enchant_stats'] = $stats;
|
|
} else {
|
|
$item['exceed_max'] = $max;
|
|
$item['exceed_stats'] = $stats;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fusszeile: "Item # / 110130060 / · Global data (Launch Scale Test client, 2026-09-19)"
|
|
$foot = array_search('Item #', $t, true);
|
|
if ($foot !== false) {
|
|
$item['source_id'] = (int) ($t[$foot + 1] ?? 0);
|
|
$item['source_version'] = trim(ltrim((string) ($t[$foot + 2] ?? ''), '· '));
|
|
}
|
|
if (!str_contains($item['source_version'], 'Global')) {
|
|
throw new RuntimeException('Keine Werte aus dem Global-Client (nur Korea/Taiwan?) - nicht übernommen.');
|
|
}
|
|
if (!in_array($item['category'], ['Weapons', 'Armor', 'Accessories'], true)) {
|
|
throw new RuntimeException('Nur Waffen, Rüstungen und Schmuck lassen sich übernehmen (das ist: ' . $item['category'] . ').');
|
|
}
|
|
return $item;
|
|
}
|