PtokaX forum

Archive => Archived 4.0 boards => Request for Lua 4 scripts => Topic started by: XPMAN on 04 February, 2004, 19:12:58

Title: A fix for the "give points" script
Post by: XPMAN on 04 February, 2004, 19:12:58
Ok, here is the script:

---start---

points = {}



function Main()

points = dofile("points.dat") or {}

end



function OnExit()

local f = openfile("points.dat", "w+")

assert(f, "points.dat")



write(f, "return {\n")

for nick, value in points do

write(f, "\t"..format("[%q]", nick).." = "..value..",\n")

end; write(f, "}")

closefile(f)

end



function DataArrival(user, data)

if strsub(data, 1, 1) == "<" then

local s, e, cmd, args = strfind(data, "^%b<> %+(%a+)%s*(.*)%|$")

if s then

cmd = strlower(cmd)

if cmd == "give" and user.bOperator then

local s, e, nick, value, reason = strfind(args, "^(%S+)%s*([%d%-]*)%s*(.*)$")

if s then

if value == "" then value = 1

else value = tonumber(value) end



if reason == "" then reason = "for no reason"

else reason = "because "..reason end



points[nick] = (points[nick] or 0) + value

user:SendData(">> "..nick.." now has "..points[nick].." points")

SendToNick(nick, ">> "..user.sName.." has given you "..value.." points "..reason)

OnExit();

else

user:SendData(">> syntax: +give [points] [reason]")

end

elseif cmd == "topusers" then

local index = {}

for nick, value in points do tinsert(index, nick) end

sort(index, function(a, b) return points[a] > points end)



local n = getn(index)

if n > 10 then n = 10 end

user:SendData(">> top "..n.." users..")

for i = 1, n do

local nick = index

local value = points[nick]

user:SendData(">>\t["..value.."]\t "..nick)

end

else return

end; return 1

end

end

end



--end--


This works perfectly:  +give nick points reason  

example: +give john 35 for sharing 500 gigs.


except in the main chat it only states   nick and points , but not the reason.  Can someone fix this to give the reason also? It should work like that but it doesn't.
Title:
Post by: pHaTTy on 04 February, 2004, 19:26:15
i dont understand this >

if reason == "" then reason = "for no reason"

else reason = "because "..reason end

tip ;)
Title:
Post by: tezlo on 04 February, 2004, 20:59:28
where in mainchat.. when you give points?

phatty ?
Title:
Post by: pHaTTy on 04 February, 2004, 21:10:35
QuoteOriginally posted by tezlo
where in mainchat.. when you give points?

phatty ?

i dont understand the question, and ive notused the script, just how i look at it, looks wrong to me
Title:
Post by: tezlo on 04 February, 2004, 21:55:15
well i wrote it.. whats wrong with it?
Title:
Post by: kepp on 04 February, 2004, 22:12:20
i use it like that too...
Is that OK or is it just waste of code since i thought of it as if Arg == nil

it is afterall looking for something to be there, and if there ain't then it's considered nil!!
Title:
Post by: kepp on 04 February, 2004, 22:23:22
But i know what's wrong... it send as user:SendData() which may be considered Main for some...

points = {}

function Main()
   points = dofile("points.dat") or {}
end

function OnExit()
local f = openfile("points.dat", "w+")
assert(f, "points.dat")
write(f, "return {\n")
   for nick, value in points do
      write(f, "\t"..format("[%q]", nick).." = "..value..",\n")
   end;
   write(f, "}")
   closefile(f)
end



function DataArrival(user, data)
if strsub(data, 1, 1) == "<" then
local s, e, cmd, args = strfind(data, "^%b<> %+(%a+)%s*(.*)%|$")
   if s then
      cmd = strlower(cmd)
         if cmd == "give" and user.bOperator then
         local s, e, nick, value, reason = strfind(args, "^(%S+)%s*([%d%-]*)%s*(.*)$")
            if s then
               if value == "" then value = 1
      else value = tonumber(value) end
                  if reason == "" then reason = "for no reason"
      else reason = "because "..reason end
                  points[nick] = (points[nick] or 0) + value
                  SendToAll(">> "..nick.." now has "..points[nick].." points "..reason)
                  SendToNick(nick, ">> "..user.sName.." has given you "..value.." points "..reason)
                  OnExit();
      else
                  user:SendData(">> syntax: +give [points] [reason]")
               end
      elseif cmd == "topusers" then
      local index = {}
         for nick, value in points do tinsert(index, nick) end
            sort(index, function(a, b) return points[a] > points[b] end)
            local n = getn(index)
               if n > 10 then n = 10 end
                  user:SendData(">> top "..n.." users..")
                     for i = 1, n do
                     local nick = index[i]
                     local value = points[nick]
                     user:SendData(">>\t["..value.."]\t "..nick)
               end
            else return
         end; return 1
      end
   end
end
Title:
Post by: tezlo on 04 February, 2004, 22:56:05
user:SendToAll ?

ad regexp..

(.*) will match any or an empty string
so if you dont specify one.. reason is "" (empty string.. not nil)
Title:
Post by: pHaTTy on 04 February, 2004, 22:57:44
QuoteOriginally posted by tezlo
well i wrote it.. whats wrong with it?

sorry about that, i just tabbed it out, and me misread, note please use the code prefix for posting code, i can barely read it when its posted as normal text........
Title:
Post by: kepp on 04 February, 2004, 23:31:19
QuoteOriginally posted by tezlo
user:SendToAll ?

ad regexp..

(.*) will match any or an empty string
so if you dont specify one.. reason is "" (empty string.. not nil)

Yea, lol, went through it fast..
Well i ussualy use (.+) and then often with reasons and other things that shall be optional i use

if arg == nil then
--//Bla Bla
elseif arg = "" then

That is why i want to know if i really need to check it against nil..
Title:
Post by: tezlo on 05 February, 2004, 01:11:43
say you have a regexp..
local s, e, somearg, someotherarg = strfind(something, "(%S+)%s*(.*)")
if s ~= nil then somearg and someotherarg are strings
you only need to check that

btw.. (.+) will never give you an empty string
Title: Thnx Tezlo
Post by: XPMAN on 05 February, 2004, 05:49:58
Thnx Tezlo for fixing. I forgot who made this great script. I did make a notation in it before i started using it in the hub again. Awesome work my friend.
Title:
Post by: kepp on 05 February, 2004, 08:06:29
Thanks alot tezlo... that's why im asking.. waset of code!!