PtokaX forum

Lua 5.3/5.2/5.1 Scripts (for PtokaX 0.4.0.0 and newer) => Help with scripts => Topic started by: TZB on 10 October, 2009, 16:09:46

Title: Learning.
Post by: TZB on 10 October, 2009, 16:09:46
What i did here is not mentioned any credits so please dont smite me since am just learning it and as soon i finish it i would definately make sure everyone will get the credits .

I have seen mainchat locker but it was with cleaner on the forum i wanted it to be seperate script for me and i wanted to learn few things like try script as per mutor's style but now i ended up is a place where script gives no error and its not running either and i am still learning.
Please help me if anyone can.

Sorry for not adding the credit mutor :(
this credit was there on the old file that i got.


-- ======================================================== --
--// MainLock by Dj.Ratonu, 22.11.2005
-- ======================================================== --
MC = {
-- Bot Name [ "" =  Hub Bot]
Bot = "-=Locker=-",
-- Bot have a Key? true/false
BotIsOp = true,
-- Bot description
BotDesc = "Main Chat Locker",
-- Bot Email Address
BotMail = "locker@domain.com",
-- Command Menu ["" = hub name]
Menu = "-=Locker=-",
-- Command SubMenu ["" = script name]
SubMenu = "MainChat Lock",
-- Command Prefix [ !,+,#]
prefix = "!",
-- Mainchat Lock Command
mcmd = "main",
-- Main Chat Lock
sMainChatLock = nil,
-- Profile Permissions
tProfile = {
          [-1] = 0,  -- Unregistered User
          [0] = 1,   -- Masters
          [1] = 1,   -- Opertors
          [2] = 0,   -- VIP
          [3] = 0,   -- Registered
},
}

OnStartup = function()

MC.Scp = "Main Chat Lock 1.0"
if MC.Bot == "" then MC.Bot = SetMan.GetString(21) end
if MC.Bot ~= SetMan.GetString(21) then Core.RegBot(MC.Bot,MC.BotDesc,MC.BotMail,MC.BotIsOp) end
if MC.Menu == "" then MC.Menu = SetMan.GetString(0)end
if MC.SubMenu == "" then MC.SunMenu = MC.Scp end
end

function ChatArrival (curUser,data)
        data = string.sub(data, 1, string.len(data)-1)
        if MC.tProfile[User.iProfile] == 1 then
                      local s,e,cmd,arg = string.find(data,"%b<>%s+(%S+)%s+(%S+)")
                      if (cmd == ""..MC.prefix..""..MC.mcmd.."") then
                          if (arg == "on") then
                              if MC.sMainChatLock then
                                     Core.SendToUser(curUser,"<"..MC.Bot.."> Main Chat is locked for user access until further notice contact Opertor.")
                              else
                                     Core.SendToAll("<"..MC.Bot.."> Main Chat Has been Locked until further notice.")
                                     MC.sMainChatLock = 1
                              end
                          end
                             if (arg == "off") then
if MC.sMainChatLock then
Core.SendToAll("<"..MC.Bot.."> Mainchat is now unlocked and users can access again.")
MC.sMainChatLock = nil
else
Core.SendToUser(curUser,"<"..MC.Bot.."> Mainchat is already enabled for user access.")
end
end
return true
end
end
if MC.sMainChatLock then
if MC.tProfile[User.iProfile] == 1 then
return false
else
Core.SendToUser(curUser,"<"..MC.Bot.."> MainChat Was Disable by Admin untill further notice")
return true
end
   end
       end
Title: Re: Learning.
Post by: Madman on 10 October, 2009, 20:00:40
I tried your code. And it gives error when the command is used.
MainChatLocker.lua:44: attempt to index global 'User' (a nil value)

Hope that helps you find the error. If not, let me know, and i'll tell =)

*Edit*

Btw, I can show you some code improvements, when the error is fixed =)
Title: Re: Learning.
Post by: DeAn on 11 October, 2009, 05:27:07
Thank you TZB, I will be waiting for that Main Lock script..:)
Title: Re: Learning.
Post by: TZB on 11 October, 2009, 15:50:43
Thanks Madman i made silly skip when i went throughline 44 and one more line below with using function for curUser,data and madethere as User.iProfile

i fixed it as below
I want to know the the improvement want to learn it.Please guide me to it.

here is that error fixed.

-- ======================================================== --
--// MainLock by Dj.Ratonu, 22.11.2005
-- ======================================================== --
MC = {
-- Bot Name [ "" =  Hub Bot]
Bot = "-=Locker=-",
-- Bot have a Key? true/false
BotIsOp = true,
-- Bot description
BotDesc = "Main Chat Locker",
-- Bot Email Address
BotMail = "locker@domain.com",
-- Command Menu ["" = hub name]
Menu = "-=Locker=-",
-- Command SubMenu ["" = script name]
SubMenu = "MainChat Lock",
-- Command Prefix [ !,+,#]
prefix = "!",
-- Mainchat Lock Command
mcmd = "main",
-- Main Chat Lock
sMainChatLock = nil,
-- Profile Permissions
tProfile = {
          [-1] = 0,  -- Unregistered User
          [0] = 1,   -- Masters
          [1] = 1,   -- Opertors
          [2] = 0,   -- VIP
          [3] = 0,   -- Registered
},
}

OnStartup = function()

MC.Scp = "Main Chat Lock 1.0"
if MC.Bot == "" then MC.Bot = SetMan.GetString(21) end
if MC.Bot ~= SetMan.GetString(21) then Core.RegBot(MC.Bot,MC.BotDesc,MC.BotMail,MC.BotIsOp) end
if MC.Menu == "" then MC.Menu = SetMan.GetString(0)end
if MC.SubMenu == "" then MC.SunMenu = MC.Scp end
end

function ChatArrival (curUser,data)
        data = string.sub(data, 1, string.len(data)-1)
        if MC.tProfile[curUser.iProfile] == 1 then
                      local s,e,cmd,arg = string.find(data,"%b<>%s+(%S+)%s+(%S+)")
                      if (cmd == ""..MC.prefix..""..MC.mcmd.."") then
                          if (arg == "on") then
                              if MC.sMainChatLock then
                                     Core.SendToUser(curUser,"<"..MC.Bot.."> Main Chat is locked for user access until further notice contact Opertor.")
                              else
                                     Core.SendToAll("<"..MC.Bot.."> Main Chat Has been Locked until further notice.")
                                     MC.sMainChatLock = 1
                              end
                          end
                             if (arg == "off") then
if MC.sMainChatLock then
Core.SendToAll("<"..MC.Bot.."> Mainchat is now unlocked and users can access again.")
MC.sMainChatLock = nil
else
Core.SendToUser(curUser,"<"..MC.Bot.."> Mainchat is already enabled for user access.")
end
end
return true
end
end
if MC.sMainChatLock then
if MC.tProfile[curUser.iProfile] == 1 then
return false
else
Core.SendToUser(curUser,"<"..MC.Bot.."> MainChat Was Disable by Admin untill further notice")
return true
end
   end
       end
Title: Re: Learning.
Post by: Madman on 11 October, 2009, 19:56:13
Guide you say...
Well, first of, the line where you check if it's right command.
there is no need for the ""
if you only check for vars, no string. Then you can use MyVar..MyVarTwo

secondly. data, could be made local

and third Profiles table could be true/false
that way you don't need == 1 in the if statments

Hope you can figuer it out. =)


Add don't listen to mutors ideas yet... he is trying to blow your mind, lol
Title: Re: Learning.
Post by: TZB on 13 October, 2009, 19:32:37
Yes indeed i have figured i tout and mutor helped me with the script also with the local commands. yesi understood about the profile permissions i will make that change as well. i will add the right clicks and also the new credits for all the scriptors who helped me out and post it again please let me know how i can improve further more.
Title: Re: Learning.
Post by: TZB on 13 October, 2009, 20:53:42
Madman and mutor thanks a lot for the help and indeed i did learn many new things while making this script.
Here are the changes according to madman for profile permissions and the local data was already done by mutor and he helped me understand it yes it was like mind getting blown up at site but when i tried to read it and go through it step by step it was getting easier please let me know how can i sharpen my knowledge even more.
here is the code if i forgot anything to mention in credits please let me know mutor because i am not very much into this its my first go towards it.
-- ======================================================== --
--// MainLock by Dj.Ratonu, 22.11.2005
-- ======================================================== --
-- Converted to LUA 5.1x [API2] on 13/10/2009 -- ||-T-z-B-||
-- ---===[Mainlock 1.0]===---
-- Fixed Bugs and improvements to the new version By Mutor and Madman
-- Changes from older version :
-- Added profile permission
-- Added Lazy Right clicks
-- ======================================================== --
MC = {
-- Bot Name [ "" =  Hub Bot]
Bot = "-=Locker=-",
-- Bot have a Key? true/false
BotIsOp = true,
-- Bot description
BotDesc = "Main Chat Locker",
-- Bot Email Address
BotMail = "locker@domain.com",
-- Command Menu ["" = hub name]
Menu = "-=Locker=-",
-- Command SubMenu ["" = script name]
SubMenu = "MainChat Lock",
-- Command Prefix [ !,+,#]
prefix = "!",
-- Mainchat Lock Command
mcmd = "main",
-- Main Chat Lock
sMainChatLock = true,
-- Profile Permissions [For Command/Right-Click/Exclusion]
tProfile = {
[-1] = false,  -- Unregistered User
[0] = true,   -- Masters
[1] = true,   -- Opertors
[2] = false,   -- VIP
[3] = false,   -- Registered
},
}

OnStartup = function()
MC.Scp = "Main Chat Lock 1.0"
if MC.Bot == "" then MC.Bot = SetMan.GetString(21) end
if MC.Bot ~= SetMan.GetString(21) then Core.RegBot(MC.Bot,MC.BotDesc,MC.BotMail,MC.BotIsOp) end
if MC.Menu == "" then MC.Menu = SetMan.GetString(0) end
if MC.SubMenu == "" then MC.SunMenu = MC.Scp end
end

function ChatArrival (curUser,data)
if MC.tProfile[curUser.iProfile] then
local _,_,cmd,arg = string.find(data,"^%b<> ["..MC.prefix.."]("..MC.mcmd..") (o[nf]+)|")
if cmd and arg then
if (arg == "on") then
if MC.sMainChatLock then
Core.SendToUser(curUser,"<"..MC.Bot.."> Main Chat is locked for user access until further notice contact Opertor.")
else
Core.SendToAll("<"..MC.Bot.."> Main Chat Has been Locked until further notice.")
MC.sMainChatLock = true
end
end
if (arg == "off") then
if MC.sMainChatLock then
Core.SendToAll("<"..MC.Bot.."> Mainchat is now unlocked and users can access again.")
MC.sMainChatLock = false
else
Core.SendToUser(curUser,"<"..MC.Bot.."> Mainchat is already enabled for user access.")
end
end
return true
end
end
if MC.sMainChatLock then
if MC.tProfile[curUser.iProfile] then
Core.SendToUser(curUser,"<"..MC.Bot.."> MainChat Was Disabled by Admin until further notice.")
return true
end
end
end

UserConnected = function(curUser)
         if MC.tProfile[curUser.iProfile] then
         local Cmd = "$UserCommand 1 3 "..MC.Menu.."\\"..MC.SubMenu.."$<%[mynick]> "..MC.prefix..""..MC.mcmd.." %[line:Type On/Off]&#124;|"
         Core.SendToUser(curUser,Cmd)
    end
end
OpConnected,RegConnected = UserConnected,UserConnected
Title: Re: Learning.
Post by: Ravage on 13 October, 2009, 21:36:21
Hi TZB,

That`s a nice achievment there. Just for learning purpose and interest
for myself, could you please explain why there are brackets '( )'
at these positions?

if (arg == "on") then

and

if (arg == "off") then


Wouldn`t it work also without setting it there?

greets,
Ravage
Title: Re: Learning.
Post by: TZB on 13 October, 2009, 21:54:46
It Shows no error as in w/o " ()" may be but its something that i just followed while making this script and since i am still much newer to scripting what i can say is that may be mutor would explain it better or madman.
Title: Re: Learning.
Post by: Madman on 14 October, 2009, 14:45:48
it has to do with the if you use ....(gf stole the keybord again)....  right. if you use more then one var check.
if Var and Var2 or Var3 then  will check for Var and Var2 or Var and Var3.. I think, I just wake up.. but it should be right.
but if (Var and Var2) or Var3 then will check for Var and Var2 or Var3
so using () with only one statement is just extra insurence that it compare the cmd with the right string.
Title: Re: Learning.
Post by: TZB on 14 October, 2009, 14:50:42
Thanks madman for all the helpi hope the new code posted had the changes you guided me for.
Title: Re: Learning.
Post by: TZB on 15 October, 2009, 14:46:12
Mutor i wanted to learn it :( now how i am gona learn it when you have already done it :( but thanks though.
Title: Re: Learning.
Post by: DeAn on 15 October, 2009, 16:57:06
Thank you very much Mutor. The script is working great.. I had been waiting for this all along.. It would be better if this final edited script would be moved to Finished Scripts section..  :)


Thanks,
DeAn
Title: Re: Learning.
Post by: TZB on 15 October, 2009, 17:09:01
:| its not yet final
Title: Re: Learning.
Post by: DeAn on 15 October, 2009, 18:55:24
This isn't final ? What more do you want to add in this ?



DeAn
Title: Re: Learning.
Post by: Madman on 15 October, 2009, 20:41:16
Okey, let's use the one you posted as base.
This is very basic loging.


first of you need to add the Serilize functions.
then you need to add a file value in the MC table. i would use "scripts/Logs.txt".
after that you need to check if the file exist at OnStartup. io.open is the key for that.
Then if file you need to use LoadFromFile, if not create an empty table. i would use tLog.
Now it's time to log the cmd.
os.date() returns a simple date string, that we can use
inserting a value in to a table is easy
it's either table.insert(table,"value") that will create

table = {
[1] = "value",
}

or we could use table["key"] = "value" and then we would get

table = {
["key"] = "value",
}

the actual loging is best place just above the  return true
then it's just to use to save the file.
it might be tricky if you don't know. it's used path to file as string, unless we use the MC value we added.
then it's the actual table. and the last is the table name as string, with is just "tLog" in my code
SaveToFile("file string",table,"table")

Hope you can create something out of this. =)





-- Serialize functions

function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
sTmp = ""
sTmp = sTmp..sTab..sTableName.." = {\n"
for key, value in pairs(tTable) do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end
sTmp = sTmp..",\n"
end
sTmp = sTmp..sTab.."}"
return sTmp
end

function SaveToFile(file , table , tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:flush()
handle:close()
end

function LoadFromFile(filename)
local f = io.open(filename)
if f then
local r = f:read("*a")
f:flush()
f:close()
local func,err = loadstring(r)
if func then x,err = pcall(func) end
end
end
Title: Re: Learning.
Post by: TZB on 16 October, 2009, 06:39:36
Wow this is becoming so easy Madman thanks a lot i will have to try making a test script with the guide you have given its a great help in understanding the table strings. Thanks a lot madman and Mutor.
Title: Re: Learning.
Post by: DeAn on 16 October, 2009, 08:56:35
QuoteDo it your own way, that's how. There are 100 ways to do anything in Lua.

Quite interesting..


DeAn