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

Quick question regarding accounts

Started by leftcatcher, 29 September 2012 - 02:06 PM
leftcatcher #1
Posted 29 September 2012 - 04:06 PM
First off, I'll start by saying I'm basically new to Lua, but it's coming along nicely.

Now, into the question:

What I am wondering if it's possible to have data saved to specific accounts in a login system (I'm not even entirely sure how to create a login system either, let alone one that would go alone with this if necessary, but I'll figure that out sometime). I know it's possible to save the data received to a specific computer ID, but I'm talking about the specific account I used to log in.

For example, if I logged in as Admin, it would display everything from options in a menu to messages received via rednet for that account specifically. However, if I logged in as Guest, it wouldn't show all options(Though the main thing I'm wondering about is the messages) and wouldn't show messages that were specifically sent to the Admin account, but instead only those intended for the Guest account on the same computer.

I've been grinding my head against the wall trying to figure out a way to do this, and tried to find help on here/other places, but to no success. So, I thought I would ask you guys. If it's not possible, just let me know. :)/>/>

Thanks in advance!
Doyle3694 #2
Posted 29 September 2012 - 04:09 PM
go into "tutorials" and then "Basic File IO"…. you'll understand afterwards.
Doyle3694 #3
Posted 29 September 2012 - 04:13 PM
nevermind I think I missunderstood you.
Anonomit #4
Posted 29 September 2012 - 04:20 PM
What you want is for different options to display depending on the account that is logged in? If so, then this could be accomplished with an "if" statement.
if account == "Admin" then
--show options
--do stuff
--do other stuff
elseif account == "Guest" then
--show less options
--do stuff
--do other stuff
elseif acccount == "debug" then
exit()
end
leftcatcher #5
Posted 30 September 2012 - 05:36 AM
Yes, that was one thing I was wondering, thank you.

The other thing I mentioned wondering was this:

Let's say that you have an e-mail service hooked up and all that. Usual e-mail things for computercraft that I've seen can send e-mails to specific computer ID's, so only one computer will get the message, and no other computer can read them because that computer's ID is different than the one the message was sent to. However, I was wondering if it's possible to send e-mails to specific accounts on single computer.

Let's say that it's computer ID is 1, but it has two possible account logins, Admin, and Guest. Someone wanted to send (from computer ID #2) an e-mail that says "Hello!" to computer ID#1, Admin account. Then, if you were logged in as an Admin, you could view the message, "Hello!", BUT. If you were in the Guest account, you would have no new messages because the e-mail that was previously sent was only sent to the Admin account. Is that possible?

A friend had asked me that question and I couldn't figure it out, so I figured here would be the best place to put that. Apologies for not really clarifying this in the OP but I was insanely tired last night while making the thread.

Thanks in advance!
Anonomit #6
Posted 30 September 2012 - 03:45 PM
Sure. On the sending computer, you would create a table containing the id of the sending computer and current account name (to make replying easier) along with the user account you want to send to, and finally, the message itself. Next, use this command:
textutils.serialize( variable )
to turn the table into a string that can be sent across rednet. Send the message, then the receiving computer can use:
textutils.unserialize( variable )
to turn it back into a table that contains the desired account name. Save it to a file, again serializing it to do so, and then display it the next time that account logs in.
leftcatcher #7
Posted 30 September 2012 - 05:35 PM
Hmm. I guess after I finish my current project I better look up how to use rednet. I knew it would require it, of course, I was just wondering for future reference.

As for the table, while I'm familiar is tables, I'm only familiar with tables that would basically represent a menu with all its options, so I would have no clue how a table that would hold the computer ID, username and then add in the message would look like/be made. Are you saying to have it made as the message is sent? As in, when you press the "send" button in the computer it makes a table with those three things and then runs that serialize command and sends it on its way? If so, I still have no clue about how to make such a thing. I'll try and look around here a bit to see if I find anything useful on these things. ._.

Again, thanks!
Anonomit #8
Posted 30 September 2012 - 07:40 PM
Here's how you could send the information:
Spoiler

rednet.open( "right" )
rednet.open( "left" )
rednet.open( "back" )
rednet.open( "top" )
rednet.open( "bottom" )

while true do
    shell.run( "clear" )
    write( "Username: " )
    local currentAccount = read()
    write( "Recipient: " )
    local recipient = read()
    write( "Recipient ID: " )
    local recipientID = tonumber( read() )
    print( "Enter message for "..recipient.." at ID "..recipientID..":" )
    local message = read()
    local table = { recipient, message, currentAccount }
    
    local string = textutils.serialize( table )
    
    rednet.send( recipientID, string )
    
    print( "message sent" )
    sleep(1)
end --while true

And this would receive:
Spoiler

rednet.open( "right" )
rednet.open( "left" )
rednet.open( "back" )
rednet.open( "top" )
rednet.open( "bottom" )

local currentAccount = "account name"

while true do
    shell.run( "clear" )
    print( "Waiting for message" )
    print( "This is terminal "..os.computerID() )
    local senderID, string = rednet.receive()
    
    local table = textutils.unserialize( string )
    
    if currentAccount == table[1] then
        print( "message received from '"..table[3].."' at ID "..senderID..":" )
        print( table[2] )
    end --if currentAccount == table[1]
    
    os.pullEvent( "char" )
end --while true

You'll need to setup a login first, and this program won't save the message, meaning it's lost if the correct account isn't logged in, but hopefully this will get you started.
leftcatcher #9
Posted 01 October 2012 - 01:18 AM
So, if I'm reading this right, the first code grabs what you enter (You enter your username, or the currentAccount could be set to whatever you logged in with I assume, the recipient(other person's account I'm guessing), ID of the other person's computer, then the message), and then it serializes it and it sends it to the ID of the other computer, and then repeats itself asking to send another message basically.

While the second one grabs the ID of who sent the message, and all the info sent, unserializes it, and basically says if the account logged in on this computer is = to the recipient that it was sent to then it will print that it received the message from the first computer and it's ID, then display the message?

Also, out of curiosity, does the currentAccount that was sent from the first computer interact with the second computer's "currentAccount = "account name""? Or are those separate for each computer?

Sorry for all the questions, I just like to get a good grasp on this stuff, as I'm taking on some pretty big tasks for only having started Lua coding a few days ago.

Perhaps I could take these two and put them into a client of sorts, where the receive is always running and when a message is received (so long as currentAccount = recipient), it displays a graphic that says "You have a new message!" or whatever, and then you could select an option to see it. Maybe save it so that way it goes into a "read e-mails" option? That part is for another day to figure out, though. While I know there have been many mail clients, I've wanted to take it just one step further and make it account-specific e-mails, rather than "just send it to the ID", which are the only ones I've ever found. Anyways, I also ramble on too much. Thanks for the code, I'll make some use of it and credit you for the help in my program. (I always credit people from whom I've taken snippets of code from/adapted code from).
Anonomit #10
Posted 01 October 2012 - 04:10 AM
First two paragraphs: yes and yes.
Third paragraph: no. The variable name's aren't being sent with the table. That isn't difficult to do, but for simplicity's sake I haven't done it. The second code should have a login screen before it or something so currentAccount isn't "account name". You could also change the currentAccount variable if you'd like, it doesn't matter if it's called different things between computers.

You don't have to credit me, I'm just getting you started. I hope you're successful with your mail system.

Your program actually inspired me to write an instant message program yesterday.
Anonomit #11
Posted 01 October 2012 - 04:10 AM
EDIT: weird multi-post
Anonomit #12
Posted 01 October 2012 - 04:10 AM
EDIT: weird multi-post
Anonomit #13
Posted 01 October 2012 - 04:10 AM
EDIT: weird multi-post
leftcatcher #14
Posted 02 October 2012 - 11:46 AM
Oh, I see. Well, thank you very much for the help on this. :(/>/> The mail system will hopefully be made soon, but I'm finishing other things first. Just made a login system that saves/accesses account data, so hopefully I can mash all these together to get them to work with one another.