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

Some Questions

Started by jumpingjoran, 05 April 2013 - 07:53 AM
jumpingjoran #1
Posted 05 April 2013 - 09:53 AM
Hello! im busy with my login system and i have some questions


the client need to recieve the username and password from the master server
so how can the client ask for the usernames and passwords?
i thought this code?


rednet.send(serverid, "getUser, ..usernameInput..")
id, msg = rednet.receive(5)
if msg == "ok" then
........

so if the server get this he start the command: 'getUser' and then he reads the file where the usernames are and then sends back 'ok' if its a good name
if the name is wrong he would say 'not' and if he gets nothing then he just has no connection…
so is this the good code or need it to be something else?

so another question how to make a log file in the server that is displayed on a 4x3 monitor?
and with a log file i mean that you can see if a user is logging in to a client you will see who has logged in and the client computer id.
something like this:


print("Server ID: 'computerID' |  JJ.os Server |  os.time()
print("---------------------------------------------------")

            log thingiess....

so this is what ur see on the monitor and then the log is under the prints that will allways stay if the log will be too long
and how to get the 'os.time()' always be right on time?



and another question:
if the usernames are stored in the user file and u will reset ur password or delete ur username how to do that? or do i need for every single username a file with the pass and user in it?
Telokis #2
Posted 05 April 2013 - 10:06 AM

rednet.send(serverid, "getUser, ..usernameInput..")

Why do you do this ? Shouldn't you do

rednet.send(serverid, "getUser"..usernameInput)
?

If you want to concat, you should do this way.

I think it's the same in your second code, no ?
Telokis #3
Posted 05 April 2013 - 10:11 AM
and another question: if the usernames are stored in the user file and u will reset ur password or delete ur username how to do that? or do i need for every single username a file with the pass and user in it?

For such a question, you have to choose by yourself how you want to save datas.
Engineer #4
Posted 05 April 2013 - 10:37 AM
You would need to do this for the rednet.send:

rednet.send(serverid, "getUser"..usernameInput)

-- In lua we seperate variables with .. outside the quotations as opposing to php.
--Try this:
local x = 5
print("x is equal to "..x)

To make a log on a monitor we have to:
- wrap to the monitor
- write to the monitor

Lets take a crash course peripherals!

First of all we can easily write to the monitor using:

peripheral.call("side", "write", "yourText")
But this gets annoying after a while, so you can assign a variable to link to the peripheral:

local whateveryoucallthis = peripheral.wrap("side")
Now we can write to the monitor using:

whateveryoucallthis.write("yourText")
Note that you cannot use print on a monitor, unless you use term.redirect, but I dont like that :P/>

So that wraps up the log part.

To get usernames and passwords you only need two files really, but it is line based.
So you can create an user everyline, and this has to be corresponded to the password file. Like so:

--username file:
username1
username2

--password file:
password1
password2

Im going to give you the code for that, and if you want explenation just shout :)/>

The code:

local usernames = {}
local passwords = {}

local handle = fs.open("file/to/path/username", "r")
while true do
	local line = handle.readLine()
	if not line then break end
	table.insert(usernames, line )
end
handle.close()

local handle = fs.open("file/to/path/password", "r")
while true do
	local line = handle.readLine()
	if not line then break end
	table.insert(passwords, line )
end
handle.close()

while true do
	local ID, message = os.pullEvent("rednet_message")
	for i = 1, #usernames do
		if message == usernames[i] then
			local ID, message = rednet.receive() -- Wait for the password
			rednet.send(ID, message == passwords[i] and "Acces granted" or "Acces denied")
			break
		end
	end
end
Not sure if this works, it is untested. Throw me the errors it spits if it does error.

I hope I have helped you quite a bit!
Edited on 05 April 2013 - 08:39 AM
jumpingjoran #5
Posted 05 April 2013 - 11:02 AM
Thnx for helping!!!
Im gonna sleep right now so can't try the code
but tomorrow i will try this out!! I think the server program is getting hard to make :P/> but if i get stucked i will post it!
Im learning every day more :)/>