A fix for the "give points" script
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

A fix for the "give points" script

Started by XPMAN, 04 February, 2004, 19:12:58

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

XPMAN

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.

pHaTTy

i dont understand this >

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

else reason = "because "..reason end

tip ;)
Resistance is futile!

tezlo

where in mainchat.. when you give points?

phatty ?

pHaTTy

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
Resistance is futile!

tezlo

well i wrote it.. whats wrong with it?

kepp

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!!
Guarding    

kepp

#6
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
Guarding    

tezlo

user:SendToAll ?

ad regexp..

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

pHaTTy

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........
Resistance is futile!

kepp

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..
Guarding    

tezlo

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

XPMAN

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.

kepp

Thanks alot tezlo... that's why im asking.. waset of code!!
Guarding    

SMF spam blocked by CleanTalk