The first time I've used tables is in this game for another thing, which I actually need quite a bit of help with, but I guess tables work better for most things, so I may try using tables for this too, as it'll give me more experience with tables, and make the game more reliable. Would you be-able to provide a more detailed explanation on how that would work, like I understand I change ["action"] to what thing I want (Such as login/register and also other things), but would just changing that part of the code most likely work, or would I need to edit the majority of it first? Thanks for this, as it looks like it's 3 different messages in that 1 message, as I always done it the way in the OP as I thought that was the only way possible
So tables are essentially a variable with a lot of variables inside of it. These variables are assigned "keys", which you can use to find them in the table. So "action" in the example is the key, and you will want to leave that name key name alone, so it will be easy to check in all of your if statements. The two other things I added in the table were specific to that action, so they can be changed depending on what action you want the server to perform.
Example: Keep in mind this is more psudeo code instead of actual code, the only thing that will be real in any of it will be the tables.
client code
--# if you wanted to login
rednet.send(serverId, {["action"] = "login", ["username"] = username, ["password"] = password})
--# if you wanted to register
rednet.send(serverId, {["action"] = "register", ["username"] = username, ["password"] = password})
--# updating balance
rednet.send(serverId, {["action"] = "updateMoney", ["user"] = username, ["amount"] = coins})
As you can see the only thing that has to stay the same is the "action" key. I will show why it is important to leave that key name alone below:
server side
if msg["action"] == "updateMoney" then
if msg["user"] ~= nil and msg["amount"] ~= nil then
print(msg["user"].." now has "..msg["amount"].." coins")
coinsWrite = fs.open("/.info/"..msg["user"].."/coins", "w") -- This is line 312
coinsWrite.write(msg["amount"])
coinsWrite.close()
end
elseif msg["action"] == "login" then
--# handle login
--# the username is in the table as msg["username"]
--# password is msg["password"]
--# these keys were made when we made the table in the client
elseif msg["action"] == "register" then
--# handle register
--# basically same thing as login above
end
Tables are one of LUA's most powerful features. Once you learn them they will be incredibly useful.
edit:
Looking over this might help with learning basic table usage:
http://www.lua.org/pil/2.5.htmledit2:
Another thing to keep in mind is that rednet is incredibly insecure. Because of this sending the password over rednet at all would result in anyone being able to find anyones password and username, with very minimum effort.