I have a problem because I have several entries in the database, but the script only creates one NPC and that’s it.
What’s the issue? I’ve been trying for several hours already, and what I thought would be the easiest part is taking me the most time...
What’s the issue? I’ve been trying for several hours already, and what I thought would be the easiest part is taking me the most time...
LUA:
function spawnPlayerShops()
local shops = db.storeQuery([[
SELECT ps.player_id, ps.posx, ps.posy, ps.posz,
p.name, p.looktype, p.lookhead, p.lookbody, p.looklegs, p.lookfeet, p.lookaddons
FROM player_shops ps
INNER JOIN players p ON ps.player_id = p.id
]])
if shops then
repeat
local sellerId = result.getNumber(shops, "player_id")
local pos = Position(
result.getNumber(shops, "posx"),
result.getNumber(shops, "posy"),
result.getNumber(shops, "posz")
)
local sellerName = result.getString(shops, "name")
-- outfit z bazy
local outfit = {
lookType = result.getNumber(shops, "looktype") or 128,
lookAddons = result.getNumber(shops, "lookaddons") or 0,
lookHead = result.getNumber(shops, "lookhead") or 0,
lookBody = result.getNumber(shops, "lookbody") or 0,
lookLegs = result.getNumber(shops, "looklegs") or 0,
lookFeet = result.getNumber(shops, "lookfeet") or 0
}
print(string.format("[MARKET DEBUG] Tworze NPC: id=%d, name=%s, pos=(%d,%d,%d)",
sellerId, sellerName, pos.x, pos.y, pos.z))
local npc = Game.createNpc("Shop", pos)
if npc then
setCreatureName(npc, sellerName)
npc:setOutfit(outfit)
print("[MARKET] Utworzono NPC sklepu dla gracza " .. sellerName ..
" na pozycji " .. pos.x .. "," .. pos.y .. "," .. pos.z)
else
print("[MARKET] Błąd tworzenia NPC dla gracza ID " .. sellerId)
end
until not result.next(shops)
result.free(shops)
end
end
function onStartup()
spawnPlayerShops()
end
SQL:
CREATE TABLE `player_shops` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`player_id` INT NOT NULL,
`posx` INT NOT NULL,
`posy` INT NOT NULL,
`posz` INT NOT NULL,
`opened_at` BIGINT NOT NULL,
`expires_at` BIGINT NOT NULL,
CONSTRAINT `fk_player_shops_player` FOREIGN KEY (`player_id`) REFERENCES `players` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;