Author Topic: Table = Idiots guide  (Read 9090 times)

0 Members and 1 Guest are viewing this topic.

Offline NightLitch

  • Emperor
  • **
  • Posts: 1 455
  • Karma: +16/-0
(No subject)
« Reply #25 on: 17 February, 2004, 15:26:33 »
your way is shorter Tezlo I know. BUT! if you type the:

who = nicker

and it is stored Nicker with big N then it will not find it.
//NL

PtokaX forum

(No subject)
« Reply #25 on: 17 February, 2004, 15:26:33 »

Offline tezlo

  • Triple Ace
  • **
  • Posts: 225
  • Karma: +1/-0
(No subject)
« Reply #26 on: 17 February, 2004, 15:27:34 »
same can be done with..
local info = Table[user.sName] or Table[strlower(user.sName)]

but they may well be two different users
« Last Edit: 17 February, 2004, 15:31:21 by tezlo »

Offline NightLitch

  • Emperor
  • **
  • Posts: 1 455
  • Karma: +16/-0
(No subject)
« Reply #27 on: 17 February, 2004, 15:46:11 »
yes, but here he was out for getting the offline data.

so he need to compare the table nicks with the who(nick) he has typed.

and then check with strlower(Nick)==strlower(who)

But this you already know Tezlo, I hope m8 :-)
//NL

Offline nErBoS

  • Emperor
  • **
  • Posts: 1 149
  • Karma: +3/-1
(No subject)
« Reply #28 on: 24 February, 2004, 01:15:47 »
Hi,

I'm new on working with tables give a hint why this is not working..

Code: [Select]
info = {
["IP"] = {}
}

function NewUserConnected(user, data)
info[user.sName].IP = user.sIP

user:SendData(Bot, info[user.sName].IP)

end

It gives me this error in the editor..

Syntax Error: attempt to index a nil value

Best regards, nErBoS
« Last Edit: 24 February, 2004, 01:16:14 by nErBoS »
--## nErBoS Spot ##--

Offline Stravides

  • Triple Ace
  • **
  • Posts: 171
  • Karma: +0/-0
(No subject)
« Reply #29 on: 14 May, 2004, 22:38:05 »
Sorry if I am being thick here, but I reall am losing it on tables...

I have a table

Code: [Select]
table[player] = {
["XP"] = 1,
["LEVEL"] = 1,
["HP"] = 1,
["INITIATIVE"] = 1,
["SPEED"] = 30,
["AC"] = 10,
["GRAPPLE"] = 1,
["MELEE"] = 1,
["RANGED"] = 1,
["FORT"] = 1,
["WILL"] = 1,
["REF"] = 1
}
all in the top variable section of my bot.

In the func main I have
Code: [Select]
SaveToFile(players,players,"players")
LoadFromFile(players)

the functions for these (and your serialize funcs are
Code: [Select]
function Serialize(tTable, sTableName, hFile, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(hFile, "hFile equals nil");

assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");

sTab = sTab or "";

write(hFile, sTab..sTableName.." = {\n" );

for key, value in tTable do
local sKey = (type(key) == "string") and format("[%q]",key) or format("[%d]",key);

if(type(value) == "table") then
Serialize(value, sKey, hFile, sTab.."\t");
else
local sValue = (type(value) == "string") and format("%q",value) or tostring(value);
write(hFile, sTab.."\t"..sKey.." = "..sValue);
end

write(hFile, ",\n");
end

write(hFile, sTab.."}");
end

function SaveToFile(file , table , tablename)
local hFile = openfile(file, "w");
Serialize(table, tablename, hFile);
closefile(hFile);
end

function LoadFromFile (file)
assert(readfrom(file),file.." is not found.Generating new "..file..". All is fine. Don't panic.")
dostring(read("*all"))
readfrom()
end

And in the DataArrival I have

Code: [Select]
elseif (cmd == prefix.."players") then
local tmp = table[curUser.sName]
if tmp then
local XP = tmp["XP"]
local LEVEL = tmp["LEVEL"]
local HP = tmp["HP"]
local INITIATIVE = tmp["INITIATIVE"]
local SPEED = tmp["SPEED"]
local AC = tmp["AC"]
local GRAPPLE = tmp["GRAPPLE"]
local MELEE = tmp["MELEE"]
local RANGED = tmp["RANGED"]
local FORT = tmp["FORT"]
local WILL = tmp["WILL"]
local REF = tmp["REF"]

end
Msg = "Nick: "..tmp..", XP: "..XP..", Level: "..LEVEL..", HP: "..HP..", INIT: "..INITIATIVE.."."
SendToAll(BOTName,Msg)

I get the error message
Syntax Error: attempt to index global `table' (a nil value)

any Ideas ??
Stravides
For RPG Books, Mp3 & Videos
We host trivia  and the ever failing Smeagolbot

Offline pHaTTy

  • Forum Legend
  • ***
  • Posts: 1 916
  • Karma: +5/-1
(No subject)
« Reply #30 on: 14 May, 2004, 23:22:40 »
yep

Code: [Select]
table = {
         player. = {
["XP"] = 1,
["LEVEL"] = 1,
["HP"] = 1,
["INITIATIVE"] = 1,
["SPEED"] = 30,
["AC"] = 10,
["GRAPPLE"] = 1,
["MELEE"] = 1,
["RANGED"] = 1,
["FORT"] = 1,
["WILL"] = 1,
["REF"] = 1
       }
}

hmm never tried but also try

Code: [Select]
table.player. = {
["XP"] = 1,
["LEVEL"] = 1,
["HP"] = 1,
["INITIATIVE"] = 1,
["SPEED"] = 30,
["AC"] = 10,
["GRAPPLE"] = 1,
["MELEE"] = 1,
["RANGED"] = 1,
["FORT"] = 1,
["WILL"] = 1,
["REF"] = 1
}
Resistance is futile!

Offline Stravides

  • Triple Ace
  • **
  • Posts: 171
  • Karma: +0/-0
(No subject)
« Reply #31 on: 15 May, 2004, 00:33:48 »
i take it the players. should be players no dot

??
Stravides
For RPG Books, Mp3 & Videos
We host trivia  and the ever failing Smeagolbot

Offline Stravides

  • Triple Ace
  • **
  • Posts: 171
  • Karma: +0/-0
(No subject)
« Reply #32 on: 15 May, 2004, 01:47:01 »
PART 1.....

Ok I've completely lost the plot here..

I want to hold data on multiple users.  now I have thought about ammending the tables and loads from the function.cfg, but I still do not understand tables...

Would it be possible to do this problem for me as I am slowly running out of time to get this online and am just banging my head against a brick wall

so here goes... Consider the dataset

PLAYER  (this is a users login name)
XP
LEVEL
HP
INITIATIVE
SPEED
AC
GRAPPLE
MELEE
RANGED
FORT
WILL
REF

please can you help me add this table to write to a file
and update, also to add the user via an !addnewplayer details go here kind of action

many thanks as I'm completely lost here again...

Code: [Select]
ver = "4.14"
revdate = "23rd April 2004"
-----------------------------------  Begin Variables used ----------------------------------------

configfile = "dm/data.cfg"
functionfile = "dm/functions.cfg"
kickfunctions = "dm/kickfunctions.cfg"

dofile(configfile);  
dofile(functionfile);  
dofile(kickfunctions);

opchatName = frmHub:GetOpChatName()
HubDesc = frmHub:GetHubDescr()
Time = ""..date("%d").."/"..date("%m").."/"..date("%Y").." * "..date("%H")..":"..date("%M")..""

logindef = "RPGBooks Cymru welcomes a New User"
logoutdef = "RPGBooks Cymru bids farewell to a New User"
botdef =  BOTName
OpChatBOTInfo = "$MyINFO $ALL "..opchatName.." <++V:0.401 Operators ChatRoom $ $Slug"..strchar( 1 ).."$Stravides@RPGBooks-cymru.no-ip.org$"


------------------------------------  Begin Tables used  -----------------------------------------
tabTimers = {n=0}

UserBotTable = { };
LogInTable = { };
LogOutTable = { };

usrKicks = {}
BanTable = {}
KickTable = {}

aNicks = {
["Stravides"] = 1,
["Malevolence"] = 1,
["Blumpy"] = 1,
["GromEyefist"] = 1,
}



-----------------------------------   End Variables used  ----------------------------------------
-- --
-----------------------------------  Begin Constants used ----------------------------------------
ptokaxcommands = {
op = 1, drop = 1, ban = 1, unban = 1, nickban = 1, getbanlist = 1,
getinfo = 1, gag = 1, ungag = 1, banip = 1, ipinfo = 1, iprangeinfo = 1,
userinfo = 1, clrtempban = 1, stat = 1 }

ptokax2commands = {
clrpermban = 1, topic = 1, reloadtxt = 1 }

function Main()
frmHub:RegBot(BOTName)
frmHub:EnableSearchData(1)
frmHub:EnableFullData(1)
SendToAll(BOTNameInfo)
SendToAll(OpChatBOTInfo)
UseTables(log_in,LogInTable)
UseTables(log_out,LogOutTable)
UseTables(botsfile,UserBotTable)
botLen = strlen( BOTName )
RegTimer(ForumTimer, 4*Hour)
RegTimer(LocalTimer, 1*Min)
SetTimer(TmrFreq)
StartTimer()
end

function OpConnected(user)
local BotIn = ""
if UserBotTable[user.sName] then
BotIn = UserBotTable[user.sName]
else
BotIn = BOTName
end
if LogInTable[user.sName] then
SendToAll(BotIn,LogInTable[user.sName])
else    
SendToAll("DungeonMaster", "[DM] "..user.sName.." has Entered the Realm")
end
-- user:SendPM(BOTName,rules)
end

function OpDisconnected(user)
if UserBotTable[user.sName] then
BotIn = UserBotTable[user.sName]
else
BotIn = "DungeonMaster"
end
if LogOutTable[user.sName] then
SendToAll(BotIn,LogOutTable[user.sName])
elseif user.iProfile == 1 then
SendToAll("DungeonMaster", "[DM] "..user.sName.." Has Left the Realm")  
end
end

function NewUserConnected(user)
SendToAll("DungeonMaster", ""..user.sName..", has Entered the Realm" )
end

function UserDisconnected(user)
SendToAll("DungeonMaster", ""..user.sName..", Has Left the Realm" )
end

function DataArrival(user,data)
if (strsub(data, 1, 1) == "$" and strfind(data, "ConnectToMe") and not aNicks[curUser.sName]) then
curUser:SendData("*** You are not allowed to download in this Realm I suggest you remove the filelist from your downloads.")
curUser:SendData("*** For downloading goto rpgbooks-cymru.no-ip.org ")
return 1
end
    if (strsub(data,1,1) == "<") then
local s, e, cmd, args = strfind(data, "^%b<> %!(%a+)%s*(.*)|$")
if user.bOperator then
local s,e,starter,cmd = strfind(data, "%b<>%s+(%S+)(%S+)")
if starter == "!clockoff" then
StopTimer()
frmHub:UnregBot(startchrs ..current ..endchrs)
return 1
elseif starter == "!clockon" then
current = date("%H:%M")
StartTimer()
frmHub:RegBot(startchrs ..current ..endchrs)
return 1
end
end
data=strsub(data,1,strlen(data)-1)
s,e,cmd = strfind(data,"%b<>%s+(%S+)")
cmd = strlower(cmd)

if (cmd == prefix.."rules") then
user:SendPM(BOTName,rules)
return 1
elseif (cmd == prefix.."mmop" ) then
_,_,cmd,message = strfind( data, "%b<>%s+(%S+)%s+(.*)" )
if user.iProfile == 1 or user.iProfile == 0 or user.iProfile == 4 then
SendPmToOps(user.sName, message)
return 1
end
elseif (cmd=="!say") then
if user.iProfile == 1 or user.iProfile == 0 or user.iProfile == 2 then
local s,e,cmd,From,Text = strfind( data, "%b<>%s+(%S+)%s+(%S+)%s*(.*)" )
SendToAll(From,Text)
To="ALL"
LogWhoUsed(To,user,From,Text)
return 1
end
elseif (cmd=="!saypm") then
if user.iProfile == 1 or user.iProfile == 0 or user.iProfile == 2 then
local s,e,cmd,From,To,Text = strfind(data, "%b<>%s+(%S+)%s+(%S+)%s+(%S+)%s+(.*)")
SendPmToNick(To,From,Text)
LogWhoUsed(To,user,From,Text)
return 1
end
elseif (cmd == prefix.."pmip") then
if user.iProfile == 1 or user.iProfile == 0 or user.iProfile == 2 then
local s,e,cmd,username = strfind( data, "%b<>%s+(%S+)%s+(.*)" )
local vUser = GetItemByName(username)
user:SendPM(BOTName,"IP Address of: "..vUser.sName.." is "..vUser.sIP)
return 1
end
elseif (cmd == prefix.."setmaxdice") then
s,e,cmd,qty = strfind( data, "%b<>%s+(%S+)%s+(.*)" )
if user.iProfile == 1 or user.iProfile == 0 or user.iProfile == 2 then
maxdiceroll=qty
return 1
end
elseif (cmd == prefix.."roll") then
local s,e,cmd,qty,type,sign,n = strfind( data, "%b<>%s+(%S+)%s+(%d+)d(%d+)([%+%-]?)(%d*)" )
if  tonumber(qty) > tonumber(maxdiceroll) then
SendToAll(BOTName,user.sName.." Is trying to crash the server rolling "..qty.." dice - Max "..maxdiceroll.." Please")
else
local subtot=0
local scoretable={}
local value = 0
type = tonumber(type)
for i = 1, tonumber(qty) do
randomgen=dice(type)
subtot=subtot + randomgen
SendToAll(BOTName,user.sName.." Roll Number "..i.." was "..randomgen)
end
num = tonumber(n) or 0
if sign == "+" then
subtot = subtot + num
elseif sign == "-" then
subtot = subtot - num
end
SendToAll(BOTName,user.sName.." Rolled "..qty.."d"..type..sign..n.." and got "..subtot)
return 1
end
elseif (cmd == prefix.."d4") then
singledie(user,4)
return 1
elseif (cmd == prefix.."d6") then
singledie(user,6)
return 1
elseif (cmd == prefix.."d8") then
singledie(user,8)
return 1
elseif (cmd == prefix.."d10") then
singledie(user,10)
return 1
elseif (cmd == prefix.."d12") then
singledie(user,12)
return 1
elseif (cmd == prefix.."d20") then
singledie(user,20)
return 1
elseif (cmd == prefix.."d100") then
singledie(user,100)
return 1
elseif (cmd=="!addnewplayer") then

SendToAll(BOTName,"Add New Players goes here :)")

elseif (cmd == "!ban") and user.bOperator then
ComFunc(user,data,"!ban",BanLog,BanTable)
return 1
elseif (cmd == "!kick") and user.bOperator then
ComFunc(user,data,"!kick",KickLog,KickTable)
return 1
elseif (cmd == "!time") and user.bOperator then
SendToAll(BOTName,"Current Server Time is: ".."<"..date("%d").."/"..date("%m").."/"..date("%Y").."  "..date("%H")..":"..date("%M").." GMT >")
return 1
end
elseif strsub(data, 1, 5) == "$To: " then
RightKick2(user, data)
elseif strsub(data,1,6) == "$Kick " then
RightKick1(user, data)
return 1
end
end

Stravides
For RPG Books, Mp3 & Videos
We host trivia  and the ever failing Smeagolbot

Offline Stravides

  • Triple Ace
  • **
  • Posts: 171
  • Karma: +0/-0
(No subject)
« Reply #33 on: 15 May, 2004, 01:48:14 »
Part 2....

function.cfg is
Code: [Select]
function UseTables(filename,table)
local handle = openfile(filename, "r")
if (handle) then
local line = read(handle)
while line do
s,e,username,text = strfind(line, "(.+)|(.+)")
if username ~= nil and text ~= nil  then
table[username]=text
end
line = read(handle)
end
closefile(handle)
end
end

function checkvalue(randomgen,max)
if randomgen==1 then
SendToAll(BOTName," F U M B L E    F U M B L E    F U M B L E    F U M B L E    F U M B L E   ")
elseif randomgen==max then
SendToAll(BOTName," C R I T I C A L     C R I T I C A L     C R I T I C A L     C R I T I C A L    ")
end
end

function singledie(user,die)
if user.iProfile == 1 or user.iProfile == 0 then
local randomgen=dice(tonumber(die))
SendToAll(BOTName,user.sName.." Rolled a d"..die.." and got a "..randomgen)
checkvalue(randomgen,tonumber(die))
return 1
end
end

function dice(type)
randomSeed = random(type)
return randomSeed
end

function CountTableRows(table,rows)
rows=0
for key, value in table do
rows=rows+1
end
return rows
end

function DoRead(curUser)
while 1 do
line = read()
if line == nil then break end
curUser:SendData(BOTName, line)
end
readfrom()
end


function LocalTimer()
frmHub:UnregBot(startchrs ..current ..endchrs)
time = date("%H:%M")
current = time
frmHub:RegBot(startchrs ..current ..endchrs)
end

function RegTimer(f, Interval)
local tmpTrig = Interval / TmrFreq
assert(Interval >= TmrFreq , "RegTimer(): Please Adjust TmrFreq")
local Timer = {n=0}
Timer.func=f
Timer.trig=tmpTrig
Timer.count=1
tinsert(tabTimers, Timer)
end

function OnTimer()
for i=1, getn(tabTimers) do
tabTimers[i].count = tabTimers[i].count + 1
if tabTimers[i].count > tabTimers[i].trig then
tabTimers[i].count=1
tabTimers[i]:func()
end
end
end


function tfind(table, item)
for key, value in table do
if value == item then return key end
end
end

function UseTables(filename,table)
local handle = openfile(filename, "r")
if (handle) then
local line = read(handle)
while line do
s,e,username,text = strfind(line, "(.+)|(.+)")
if username ~= nil and text ~= nil  then
table[username]=text
end
line = read(handle)
end
closefile(handle)
end
end

function SaveTable(filename,table)
writeto(filename)
for key, value in table do
table[key]=value
write(key.."|"..value.."\n")
end
    writeto()
end

function SaveKeyTable(filename,table)
writeto(filename)
keynum=1
for key, value in table do
table[key]=value
write(keynum.."|"..value.."\n")
keynum=keynum+1
end
    writeto()
end

function LoadKeyTable(filename,table)
local handle = openfile(filename, "r")
if (handle) then
local line = read(handle)
while line do
s,e,keynum,username = strfind(line, "(.+)|(.+)")
if username ~= nil then
table[keynum]=username
end
line = read(handle)
end
closefile(handle)
end
end

function LogWhoUsed(To,user,From,Text)
Msg = user.sName.." * "..To.." * "..From.." * "..Text.." * "..date("%d").."/"..date("%m").."/"..date("%Y").." * "..date("%H")..":"..date("%M")..""
local handle = openfile(SayLog, "a")
write(handle,Msg.."\n")
closefile(handle)
end

function AddBlanksToTables(uName,filename,table,textstring)
UseTables(filename,table)
writeto(filename)
for key, value in table do
table[key]=value
write(key.."|"..value.."\n")
end
write(uName.."|"..textstring.."\n")
writeto()
end

data.cfg is

Code: [Select]
------------------------------------------Begin Credits ------------------------------------------
-- --
-- Bot Name : Smeagol --
-- Home : RPGBooks-Cymru.no-ip.org --
--     Credits : Developed By Stravides, --
-- Copyright : There's no problem taking and ammending this config, but --
-- please leave in this Credit Section --
-- --
------------------------------------------ End Credits -------------------------------------------
-- --
--------------------------------------  Variables used -------------------------------------------
BOTName = "DungeonMaster"
HubAdress = "RPGBooks-Cymru-rpg.no-ip.org"
addredirect = "RPGBooks-Cymru.no-ip.org:411"
BOTNameInfo = "$MyINFO $ALL "..BOTName.." <++V:0.306 DungeonMaster $ $Etherial Plane"..strchar( 1 ).."$DungeonMaster@RPGBooks-Cymru.no-ip.org$"
version = BOTName.." "..ver
prefix = "!"
ReverseKick = "Stravides"
mb = 1024 * 1024
gb = 1024 * mb
maxKicks = 3

disconnectUser = nil -- disconnect the user, nil = don't
useTimer = 1
counter = 1

rows = 0
Count = {}

startchrs = "Local: "   -- shows before clock
endchrs = " GMT"    -- shows after clock
current = date("%H:%M:%S")
-------------------------------------  Time Definition -------------------------------------------
Sec = 1000
Min = 60*Sec
Hour = 60*Min
Day = 24*Hour
Week = 7*Day
Month = 4*Week
Year = 365*Day
TmrFreq = 10*Sec
-------------------------------------  Begin Files used ------------------------------------------
BanLog = "dm/banlog.txt"
KickLog = "dm/kicklog.txt"
SayLog = "dm/saylog.txt"
log_in = "dm/logintext.txt"
log_out = "dm/logouttext.txt"
botsfile = "dm/botstext.txt"
fil_block = "dm/blockedusers.txt"
-------------------------------------   End Files used  ------------------------------------------
maxdiceroll = 15

rules = "\r\n"..
"                                   Rules \r\n"..
"\r\n"..
"Failure to comply with these rules will mean permanent exclusion from the Realm.\r\n"..
"\r\n"..
"1.  This realm is for Roleplaying... If you wanna download goto my other hub RPGBooks-Cymru.no-ip.org.  Not Here.\r\n"..
"2.  Keep it clean and nice - I will not tolerate Rants, offensive or distractive players\r\n"..
"3.  Any Offences will result in the loss of XP and Temp Stasis\r\n"..
"4.  3 Stasis' will lead to an automatic and permanent death of your character.\r\n"..
"5.  The DM is Always Right - there will be no rules lawyers tolerated. \r\n"..
"6.  Characters are to be created in E-Tools ver 1.4.1 and e-mailed to [EMAIL]Stravides@RPGBooks-Cymru.no-ip.org[/EMAIL]\r\n"..
"7.  Dice rolls are Public and Only the 1st roll will be accepted.\r\n"..
"8.  You will be asked to roll by the DM, only then should you use the dice rolling engine\r\n"..
"9.  No Munchkins - Read the definition on the web !!!\r\n"..
"\r\n"

helpstart= "\r\n"..
"                                   Starting package \r\n"..
"\r\n"..
"Create a 1st Level char from 3Ed PHB, any other mods must be run by the DM.\r\n"..
"Character has 750 starting gold\r\n"..
"Characters may be of any alignment.\r\n"..
"Characters must not have offensive names, and your Nick must be that of your character\r\n"..
"Ascii names will not be tolerated.\r\n"..
"\r\n"

helpdice= "\r\n"..
"                                   Dice Rolling \r\n"..
"\r\n"..
"Dice rolls can be made by typing !d then the number of sides of the dice you\r\n"..
"wish to throw ie !d4 !d6 !d8 !d10 !d12 !d20 !d100\r\n"..
"If you wish to roll multiple die ie 4d6+3 you type\r\n"..
"!roll 4d6+3  where 4 is the qty 6 is the sides of the dice 3 is added to the score\r\n"..
"\r\n"

ForumAd = "\r\n"..
"=========================== FORUM ============================ \r\n"..
"          Please visit the RPGBooks Cymru Forum @ \r\n"..
"          [URL]http://rpgbooks-cymru.no-ip.org/forum/[/URL] \r\n"..
"    News, Requests, RPG Checklists, & General Discussions \r\n"..
"============================================================== \r\n"

Stravides
For RPG Books, Mp3 & Videos
We host trivia  and the ever failing Smeagolbot

PtokaX forum

(No subject)
« Reply #33 on: 15 May, 2004, 01:48:14 »