-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_spell_vendors.php
73 lines (60 loc) · 2.56 KB
/
get_spell_vendors.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
header('Content-Type: application/json'); // Ensure JSON response
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db_connection.php';
include $_SERVER['DOCUMENT_ROOT'] . '/includes/zone_mapping.php'; // Include the zone mapping
$spellId = $_GET['id'] ?? null;
if (!$spellId) {
echo json_encode(['error' => 'Spell ID is missing.']);
exit;
}
try {
// Step 1: Find the item with the scroll effect matching the spell ID
$stmt = $pdo->prepare("SELECT id, name FROM items WHERE scrolleffect = :spell_id");
$stmt->bindValue(':spell_id', $spellId, PDO::PARAM_INT);
$stmt->execute();
$item = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$item) {
echo json_encode(['error' => 'No item found with this spell effect.']);
exit;
}
$itemId = $item['id'];
// Step 2: Find merchant IDs selling this item
$stmt = $pdo->prepare("SELECT DISTINCT merchantid FROM merchantlist WHERE item = :item_id");
$stmt->bindValue(':item_id', $itemId, PDO::PARAM_INT);
$stmt->execute();
$merchants = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!$merchants) {
echo json_encode(['error' => 'No merchants found selling this item.']);
exit;
}
$merchantIds = array_column($merchants, 'merchantid');
// Step 3: Find NPCs (vendors), their zones, and spawn points
$placeholders = implode(',', array_fill(0, count($merchantIds), '?'));
$stmt = $pdo->prepare("
SELECT
nt.name AS npc_name,
nt.id AS npc_id,
s2.zone AS zone_short_name,
s2.x, s2.y, s2.z
FROM npc_types nt
INNER JOIN spawnentry se ON se.npcid = nt.id
INNER JOIN spawn2 s2 ON s2.spawngroupid = se.spawngroupid
WHERE nt.id IN ($placeholders)
GROUP BY nt.name, nt.id, s2.zone, s2.x, s2.y, s2.z
");
$stmt->execute($merchantIds);
$vendors = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!$vendors) {
echo json_encode(['error' => 'No vendors found for this item.']);
exit;
}
foreach ($vendors as &$vendor) {
$vendor['zone_name'] = $zoneMapping[$vendor['zone_short_name']] ?? $vendor['zone_short_name'];
$vendor['x'] = isset($vendor['x']) && $vendor['x'] != 0 ? round($vendor['x'], 2) : 'N/A';
$vendor['y'] = isset($vendor['y']) && $vendor['y'] != 0 ? round($vendor['y'], 2) : 'N/A';
$vendor['z'] = isset($vendor['z']) && $vendor['z'] != 0 ? round($vendor['z'], 2) : 'N/A';
}
echo json_encode($vendors);
} catch (Exception $e) {
echo json_encode(['error' => 'An error occurred: ' . $e->getMessage()]);
}