PDA

View Full Version : [Release] Code Box Npc - Lua


stoneharry
04-28-2009, 06:35 PM
post dates people, come on

LanceDH
04-28-2009, 07:08 PM
What if, one person gets a code and spreads it all over the server?
I guess the codes aren't 1 use only

Weliaz
01-22-2011, 03:37 PM
I know this is a necro post, but could somebody please reupload? I need it for inspiration.

X292
01-22-2011, 04:23 PM
No offense but a code box is simple to make in Lua. go on Wowv and theres plenty of tuts to teach you how to do just about everything.

Neglected
01-28-2011, 09:43 AM
One that works effectively, however, is not so simple.. I've made one. I'll post it later.

QQrofl
01-28-2011, 05:01 PM
Codeboxs are F'ing leet.

X292
01-28-2011, 05:34 PM
A simple one takes no time yes but I do agree that for one to work well and be efficient will and does take time

Neglected
01-28-2011, 07:33 PM
--[[
* Simple "Code Box" NPC.
* This NPC will cross-reference a user-given string with a database entry to see if it matches.
* It will then reward the appropriate reward and remove the string from the database.
* Created by Neglected/Sympathy.
* Thanks to Nezth and Hypersniper for reviewing and subsequently fixing bugs in this script.
]]

CodeBoxNPC = {
db_type = 1, -- 1 for World, 2 for Character.
database = "Code_Redemption",
npc_text = 100,
no_rewards_npc_text = 101,
npc_id = 100,
};

function CodeBoxNPC.GossipStarted(pUnit, _, pPlayer)
local result = 0;
result = (CodeBoxNPC.db_type == 1) and WorldDBQuery("SELECT * FROM `"..CodeBoxNPC.database.."`;") or CharDBQuery("SELECT * FROM `"..CodeBoxNPC.database.."`;");
if not result then
pUnit:CreateGossipMenu(CodeBoxNPC.no_rewards_npc_t ext, 0, pPlayer);
pUnit:GossipMenuAddItem(0, "Never mind.", 1, 0);
pUnit:GossipSendMenu(pPlayer);
else
pUnit:GossipCreateMenu(CodeBoxNPC.npc_text, 0, pPlayer);
pUnit:GossipMenuAddItem(0, "Tell "..pUnit:GetName().." the code you have recieved.", 2, 1);
pUnit:GossipMenuAddItem(0, "Never mind.", 1, 0);
pUnit:GossipSendMenu(pPlayer);
end
end

function CodeBoxNPC.GossipSelection(pUnit, _, pPlayer, id, intid, sCode)
if intid == 1 then
pPlayer:GossipComplete()
return
elseif intid == 2 then
-- first cross-reference the code with the database.
local parse = function(str)
str = str:gsub("\x00", "\\x00");
str = str:gsub("\n", "\\n");
str = str:gsub("\r", "\\r");
str = str:gsub("\\", "\\\\");
str = str:gsub("\x1a", "\\x1a");
return str
end -- SCREW YOU EXPLOITERS! (Thanks to Nezth and Hypersniper for pointing out and subsequently fixing this security flaw.
local sCode = parse(sCode)
local query = "SELECT * FROM `"..CodeBoxNPC.database.."` WHERE `code`=%s;";
result = (CodeBoxNPC.db_type == 1) and WorldDBQuery(string.format(query, sCode)) or CharDBQuery(string.format(query, sCode));
if not result then
pPlayer:SendBroadcastMessage("You have entered an invalid code.");
pPlayer:SendAreaTriggerMessage("You have entered an invalid code.");
pPlayer:GossipComplete();
return
else
for n = 1, query:GetRowCount() do
local reward_type = query:GetColumn(0):GetULong();
local reward = query:GetColumn(1):GetULong();
local misc = query:GetColumn(2):GetULong();
if not reward_type or not reward or not misc then
pPlayer:SendBroadcastMessage("Oops! Something has gone wrong. The developers have been notified and will fix this as soon as possible. Sorry for any inconvinience!");
-- You may want to replace this with a file output, idk
error("The row number "..n.." was not filled in correctly in "..CodeBoxNPC.database.."!", 0);
end
if reward_type == 1 then
pPlayer:DealGoldMerit(reward);
elseif reward_type == 2 then
pPlayer:AddItem(reward, misc);
end
query:NextRow();
end
if CodeBoxNPC.db_type == 1 then
WorldDBQuery("DELETE FROM `"..CodeBoxNPC.database.."` WHERE `code`=`"..sCode..";");
else
CharDBQuery("DELETE FROM `"..CodeBoxNPC.database.."` WHERE `code`=`"..sCode..";");
end
pPlayer:GossipComplete();
return
end
end
end

RegisterUnitGossipEvent(CodeBoxNPC.npc_id, 1, CodeBoxNPC.GossipStarted)
RegisterUnitGossipEvent(CodeBoxNPC.npc_id, 2, CodeBoxNPC.GossipSelection)
Hasn't been tested since I don't have a 3.3.5 client but has been reviewed by Hyperbo- I mean, Hypersniper, and Nezth, so all should be in order.