Originally Posted by Noa_
Questlib:
PHP Code:
FOLDER_DIRECTORY=get_locale_base_path()..'/warehouse'
PATH_TO_FILE=FOLDER_DIRECTORY..'/warehouse_id_'..pc.get_player_id()..".txt"
function CreateFolder()
if os.execute('cd '..FOLDER_DIRECTORY)~=0 then-- if not exist
os.execute('mkdir '..FOLDER_DIRECTORY)-- create folder on directory
end
end
function RemoveFromTable(tab)
-- Create a folder if doesn t exist
remove_=io.open(PATH_TO_FILE,'w')
for index=1,table.getn(tab) do
remove_:write(tab[index][2].." "..tab[index][2]..'\n')
end
remove_:close()
end
function WriteOn(vnum,count)
-- if not exist create a empty file
if io.open(PATH_TO_FILE)==nil then
io.open(PATH_TO_FILE,'w'):close()
end
WriteOnFile=io.open(PATH_TO_FILE,'a+')
WriteOnFile:write(vnum..' '..count..'\n')
WriteOnFile:close()
end
function select2(tab,...)
arg.n = nil
if type(tab) ~= "table" and type(tab) == 'number' then
table.insert(arg,1,tab)
tab = arg
elseif type(tab) ~= "table" and type(tab) == 'string' then
table.insert(arg,1,tab)
table.insert(arg,1,8)
tab = arg
elseif type(tab) == "table" and type(tab[1]) == 'string' then
table.insert(tab,1,8)
end
local max = 5; table.remove(tab,1)
local tablen,outputstr,outputcount,nextc,incit = table.getn(tab),"",0,0,0
table.foreach(tab,
function(i,l)
outputcount = outputcount + 1
if outputcount == 1 then
outputstr=outputstr..'sel = select("'..l..'"'
elseif outputcount == max and tablen > outputcount+incit then
if tablen ~= outputcount+incit+1 then
outputstr=outputstr..',"'..l..'","Nächste Seite") + '..incit..' '
if nextc > 0 then
outputstr = outputstr..'end '
end
outputstr=outputstr..'; if sel == '..(incit+max+1)..' then '
nextc, outputcount, incit= nextc+1,0,incit+max
else
outputstr=outputstr..',"'..l..'"'
end
else
outputstr=outputstr..',"'..l..'"'
end
end
)
outputstr = outputstr..') + '..incit
if nextc > 0 then
outputstr = outputstr..' end'
end
outputstr= outputstr.. '; return sel'
print(outputstr)
local sel = assert(loadstring(outputstr))()
tablen,outputstr,outputcount,nextc,incit = nil,nil,nil,nil,nil -- Speicher freimachen
return sel
end
Quest:
PHP Code:
quest name begin
state start begin
when login with game.get_event_flag('folderFlag')==0 begin
CreateFolder()-- create a folder if not exist
game.set_event_flag('folderFlag',1)-- safety first
end
when mob_vnum.take begin
say_title'My-Warehouse keeper'
say''
say('Save vnum: '..item.get_name()..' count: '..item.get_count()..' ?')
say''
if select('Save',"don't Save")==2 then return end
WriteOn(item.vnum, item.get_count())--only selected items
end
when vnum.use begin
if game.get_event_flag('Warehouse_pw_'..pc.get_player_id())==0 then
say_title'Enter your password'
say''
local pw=tonumber(input())or false
if string.len(tostring(pw))>10 then
say'the password is too long'
return
elseif not pw then
say'only numbers'
return
end
game.set_event_flag('Warehouse_pw_'..pc.get_player_id(),pw)
end
if tonumber(input())~=game.get_event_flag('Warehouse_pw_'..pc.get_player_id()) then
say'wrong password'
return
end
say_title'what do you want to do?'
say''
-- if not exist create empty file
if io.open(PATH_TO_FILE)==nil then
io.open(PATH_TO_FILE,'w'):close()
end
local data=io.open(PATH_TO_FILE,'r')
if data:read(1)==nil then
say'empty warehouse'
data:close()
return
end
-- Table of Contents
local _warehouse,selection={},{}
for line in data:lines() do
vnum=string.format('%s',string.gsub(line,'(%s+%d+)',''))
count=string.format('%s',string.gsub(line,'(%d+%s+)',''))
_warehouse[table.getn(_warehouse)+1]={vnum,count}
table.insert(selection,item_name(vnum)..' ('..count..')')
end
table.insert(selection,'close')
data:close()
local x=select2(selection)-- 5 selections
say''
say(_warehouse[x][1]..' want this item?')
if x==table.getn(selection) then return end
if not pc.enough_inventory(_warehouse[x][1]) then
say_title
return
end
-- give and remove from the table
pc.give_item2(_warehouse[x][1],_warehouse[x][2])
say('your reward: '.._warehouse[x][1]..' x'.._warehouse[x][2])
table.remove(_warehouse,x)
RemoveFromTable(_warehouse)
end
end
end
|