-
Notifications
You must be signed in to change notification settings - Fork 1
/
createNpcLocs.php
68 lines (48 loc) · 1.76 KB
/
createNpcLocs.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
<?php
if (php_sapi_name() != "cli") die("Can only be run from command line!");
print("Creating the npcLocations table...\n");
require("/home/uesp/secrets/esolog.secrets");
require("esoCommon.php");
$db = new mysqli($uespEsoLogReadDBHost, $uespEsoLogReadUser, $uespEsoLogReadPW, $uespEsoLogDatabase);
if ($db->connect_error) exit("Could not connect to mysql database!");
$query = "SELECT id FROM npc;";
$result = $db->query($query);
if (!$result) exit("Failed to load npc data!");
$npcIds = array();
while ($npc = $result->fetch_assoc())
{
$npcIds[] = $npc['id'];
}
$count = count($npcIds);
print("\tFound $count npcs.\n");
$dbWrite = new mysqli($uespEsoLogWriteDBHost, $uespEsoLogWriteUser, $uespEsoLogWritePW, $uespEsoLogDatabase);
if ($dbWrite->connect_error) exit("Could not connect to mysql database!");
$query = "CREATE TABLE IF NOT EXISTS npcLocations (
npcId BIGINT NOT NULL,
zone TINYTEXT NOT NULL,
locCount INTEGER NOT NULL,
PRIMARY KEY (npcId, zone(64))
) ENGINE=MYISAM;";
$result = $dbWrite->query($query);
if (!$result) exit("Failed to create npcLocations table!");
foreach ($npcIds as $i => $npcId)
{
print("\tNPC $npcId...");
$query = "SELECT npcId, zone, count(*) as locCount FROM location WHERE npcId=$npcId GROUP BY zone;";
$result = $db->query($query);
if (!$result)
{
print("Error!\n");
continue;
}
$numRows = $result->num_rows;
print("Loaded $numRows rows.\n");
while ($row = $result->fetch_assoc())
{
$safeZone = $dbWrite->real_escape_string($row['zone']);
$locCount = $row['locCount'];
$query = "INSERT IGNORE INTO npcLocations(npcId, zone, locCount) VALUES($npcId, '$safeZone', $locCount);";
$writeResult = $dbWrite->query($query);
if (!$writeResult) print("\tError: " . $dbWrite->error . "\n");
}
}