Posted 07 February 2013 - 08:25 AM
so i have this little API i'm working on to store usefull information on a "server computer"
so far i've written this as my API
this is working (at least the save function)
i have this program to start_server and the problem is in here
db.tTYpe:action(table) doesnt seem to work because tType and action are strings… how could i work around this? (i need the : beacuase my api needs the self from local objects inside the api)
so far i've written this as my API
Spoiler
if fs.exists("disk/db") then
print("db api loaded")
else
return false
end
local object_type
local Collection = {}
Collection.__index = Collection
local collections = { turtle = "disk/db/turtle", computer = "disk/db/computer", chest = "disk/db/chest", }
local fenv = getfenv()
if turtle then
object_type = "turtle"
else
object_type = "computer"
end
local function checkInput(self, table)
if self == nil then
print("you must use this command like:")
print("db.collection_name:method_name(input)")
return false
elseif type(table) ~= "table" or table ~= false then
print("db::the input is not a table")
return false
end
return true
end
function Collection.create(path, type)
local collection = {}
setmetatable(collection, Collection)
collection.path = path
collection.type = type
return collection
end
function Collection:save(table)
if not checkInput(self, table) then
return false
end
if fs.exists(self.path.."/"..self.type.."_"..table.id) then
print("there is already a table for this, pelase use update function")
end
local nTable = textutils.serialize(table)
local file = fs.open(self.path.."/"..self.type.."_"..table.id, "w")
file.write(nTable)
file.close()
end
function Collection:update(table)
if not checkInput(self, table) then
return false
end
if not fs.exists(self.path.."/"..self.type.."_"..table.id) then
print("there isnt a table for this, pelase use save function")
return false
end
local reading_file = fs.open(self.path.."/"..self.type.."_"..table.id, "r")
local oTable = textutils.unserialize(read_file.readAll())
reading_file.close()
local nTable = oTable
for key,value in pairs(table) do
if type(table[key]) == "table" then
for k, v in pairs(value) do
if type(nTable[key]) ~= "table" then
nTable[key] = {}
end
nTable[key][k] = v
end
else
nTable[key] = value
end
end
local nTable = textutils.serialize(nTable)
local opened_file = fs.open(self.path.."/"..self.type.."_"..table.id, "w")
opened_file.write(nTable)
opened_file.close()
end
function Collection:delete(id)
if not checkInput(self, false) then
return false
end
if not fs.exists(self.path.."/"..self.type.."_"..table.id) then
print("the file you're trying to delete doesn\'t exists ")
return false
end
fs.delete(self.path.."/"..self.type.."_"..id)
end
for key,value in pairs(collections) do
if type(fenv[key]) ~= "table" then
fenv[key] = {}
end
fenv[key] = Collection.create(value, key)
end
this is working (at least the save function)
i have this program to start_server and the problem is in here
Spoiler
while true do
local id, message, distance = rednet.receive()
message = textutils.unserialize(message)
if type(message) ~= "table" then
print("wrong format")
else
local tType = tostring(message['type'])
local table = message.table
local action = message['action']
if Ttype == "undefined" or table == "undefined" or action == "undefined" then
print("table incomplete")
else
table.id = id
db.tType:action(table)
end
end
end
db.tTYpe:action(table) doesnt seem to work because tType and action are strings… how could i work around this? (i need the : beacuase my api needs the self from local objects inside the api)