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

Big Ideas, Little knowledge. Advice/Help

Started by theDWARN, 23 September 2014 - 04:31 AM
theDWARN #1
Posted 23 September 2014 - 06:31 AM
Ok so basically i have this city. (On a Tekkit Classic Server).
I want it to have its own currency. So i got the idea of making a sort of credit card type thing. (Floppy disks.)
The thing im trying to figure out at the moment is the production of these "cards".
Is there a way to make it, so that when a floppy goes into a disk drive, the computer writes a code to it. Then ejects it?
These codes would have to be sort of user information, (a unique ID number, Username, an amount of fake currency etc.)

Any help would be appreciated. Thanks.
TheOddByte #2
Posted 23 September 2014 - 09:29 PM
Well you'd want to check out fs for writing to a file, and also the disk API for ejecting the disk
ExamplesWriting to a file

-- This opens the file "disk/foo" in write mode
local f = fs.open( "disk/foo", "w" )
-- this writes "bar" and then starts on a new line in the file
f.writeLine( "bar" )
-- this closes the file, it's always important to close the file after you're done reading/writing from/to it
f.close()

Ejecting a disk

disk.eject( "left" ) -- this will eject a disk on the left side of the computer

I left a link below to the wiki, you can check out os.pullEvent there.
Now onto how you could do what you explained

while true do
    local e = { os.pullEvent( "disk" ) } -- wait for a "disk" event
    local path = disk.getMountPath( e[2] ) -- get the path of the disk
    local f = fs.open( path .. "/foo", "w" ) -- open a file called "foo" on the disk
    f.writeLine( "bar" ) -- write "bar" to the disk
    f.close() -- close the file
    disk.eject( e[2] ) -- eject the disk
end

Now since you're new to ComputerCraft and Lua I would suggest that you'd first go and check out the PIL and then the wiki of ComputerCraft, starting with making a bank system as the first program isn't that easy( you're new to CC right? ), or well.. it depends. Yes you can make a bank system, but if you're new to programming the quality and security of the program might not be so good. If you're seriously thinking of creating this then I would strongly suggest that you'd hash + salt the data before storing it into a file, and have like a "server computer" that would store the data.