This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Prominer's profile picture

Creating Customersystem

Started by Prominer, 11 May 2014 - 08:51 AM
Prominer #1
Posted 11 May 2014 - 10:51 AM
Hey Guys,

I've been playing with Computercraft recently, because of the Tekkit Lite I play on. I really like how the mod works, and how simple programming in LUA can be. But as every starter, I also have my problems creating my Shopping CustomerSystem.

I tried to create a usersystem with a (look-alike) database in the computer. But for one or another reason, my progress doens't come a lot further than the simple ''basic'' password/username verification.

My plan is as follows:
- Users log in (when they already have a account) or create a account.
- Users get a menu to select how much oil (for example) they want. There is one button: ''[+1 Bucket]".
- They get asked to put a payment into a chest, which will be pumped out (any kind of item detection?)
- The computer registers the payment, and pumps the oil from the backend to the front tank where the customer can take the oil out.
- At last, the customer gets the question if he/she wants more oil, and if not: they can log out by typing: ''no''.
- The whole system resets.

I was thinking about a discount-coupon system. But I'm not sure how I want that coming along nicely.

Does somebody have time/the interest to create this system for me? Thanks!
- Prominer
Lyqyd #2
Posted 11 May 2014 - 11:02 AM
We don't really write programs by request here.

We will help you get your code working, of course. Feel free to ask a many questions as you need along the way.
viluon #3
Posted 11 May 2014 - 11:09 AM
We don't really write programs by request here.

We will help you get your code working, of course. Feel free to ask a many questions as you need along the way.
Exactly. Try asking in Idea Exchange or PM a programmer here.
Prominer #4
Posted 11 May 2014 - 11:11 AM
Oh, okay. Let's start with the first question then! ;)/>

How can I get data properly out a ''data''-base?
I already wrote this code (should work I think):


function dbCheck(name)
if fs.exists(name) == true then
  return true
else
  return false
end
end
function dbLoad(name)
  local temp = fs.open(name,"r")
  local tmp = temp.readAll()
  temp.close()
return textutils.unserialize(tmp)
end
function dbWrite(name,tbl)
local temp = fs.open(name,"w")
temp.write(textutils.serialize(tbl))
temp.close()
end
function dbCreate(name)
local temp = fs.open(name,"w")
temp.close()
end
function dbNewAccount(dbName,name,pass)
accounts = dbLoad(dbName)
if accounts == nil then
accounts = {
  {
   name,
   pass
  }
}
else
local num = #accounts+1
accounts[num] = {
  {
   name,
   pass
  }
}
end
dbWrite(dbName,accounts)
end

But I can only get a list back from it. Does LUA support a search function?
CometWolf #5
Posted 11 May 2014 - 11:18 AM
Since your accounts are numerically indexed, you can just loop the table with a regular for loop looking for whatever you want.

for i=1,#db do
  if db[i].name == "name" then
    return true
  end
end
Prominer #6
Posted 11 May 2014 - 11:30 AM
Oh yes, my bad. Forgot about that. So, if I add a user with dbNewAccount() I can search for it with the code you posted? I can add a else statement if the user does not excist, right?
CometWolf #7
Posted 11 May 2014 - 05:04 PM
Well no, you can't add an else directly. It would have to be after the loop.

local found = false
for i=1,#db do
  if db[i].name == "name" then
    found = true
    break
  end
end
if not found then
  --user not found
end