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

Could Use A Hand With Some Code

Started by elfin8er, 18 April 2013 - 02:12 AM
elfin8er #1
Posted 18 April 2013 - 04:12 AM
So, I'm trying to make a rednet text based adventure game. I have a couple of questions about my code that I was wondering it you guys could help out with? Currently, the login just uses the minecraft servers. I wanted to get right to the point without having to mess around with encryption.

Client
Spoiler


-- Preset Variables
server_id = 6 -- The ID of the server computer
modem_side = "right" -- The side your modem is on

-- Crap that has to be done first
rednet.open(modem_side)

-- Debug (Just in case I need to pre-set some variables)
debug = false
if debug == true then
end

-- Commands

function printstats()
term.clear()
term.setCursorPos(1,1)
print(" ============================================")
print("=   Class: ".. class .."   HP: "..hp.."   MP: "..mp.."   =")
print(" ============================================")
print("")
end

-- Start of Commands

function command_help()
term.clear()
term.setCursorPos(1,1)
printstats()
print("Help Page")
end

function command_inventory()
term.clear()
term.setCursorPos(1,1)
printstats()
print("Inventory Page")
end

-- end of commands

-- start of command handler
function commands()
if command == "/help" then
  command_help()
elseif command == "/inventory" then
  command_inventory()
else userexists("unknowncommand")
end
end
-- end of command handler



function getstats()
rednet.send(server_id, "getstats")
rednet.send(server_id, email)

id, email = rednet.receive()
id, username = rednet.receive()
id, class = rednet.receive()
id, maxhp = rednet.receive()
id, hp = rednet.receive()
id, maxmp = rednet.receive()
id, mp = rednet.receive()
id, age = rednet.receive()

printstats()
end

function userexists(error)
term.clear()
term.setCursorPos(1,1)
getstats()
print("Welcome Back!")
print("Type /help if you need help with anything")
print("")
if error == "unknowncommand" then
print("Unknown Command")
print("")
end
command = read()
commands()
end

-- start register function(s)

function writeplayer()
-- Line 1: Minecraft Email
-- Line 2: Game Username
-- Line 3: Class
-- Line 4: Max HP
-- Line 5: HP
-- Line 6: Max MP
-- Line 7: MP
-- Line 8: Age
print(email)
print(username)
print(class)
print(maxhp)
print(hp)
print(maxmp)
print(mp)
print(age)

rednet.send(server_id, "write_player_info")
rednet.send(server_id, email)
rednet.send(server_id, username)
rednet.send(server_id, class)
rednet.send(server_id, maxhp)
rednet.send(server_id, hp)
rednet.send(server_id, maxmp)
rednet.send(server_id, mp)
rednet.send(server_id, age)
end

function confirmplayer(error)
term.clear()
term.setCursorPos(1,1)
print("Is all of this correct? y/n")
print("")
print("Username: "..username)
print("Class: "..class)
print("Age: "..age)
print("")
if error == "unknowncommand" then
print("Unknown Command")
print("")
end
answer = io.read()
  if answer == "y" then
	writeplayer()
  elseif answer == "n" then
	register()
  else
	confirmplayer("unknowncommand")
  end
end

function chooseage()
term.clear()
term.setCursorPos(1,1)
print("How old is your player?")
age = read()
confirmplayer()
end

function rollstats()
  -- I need to work out a way to add HP to the base HP randomly. The end HP must be a number
  -- rounded to the nearest 50 (150, 200, 250, 300 etc). Any ideas?
  if class == "Warrior" then
	maxhp = 250 -- + math.random(1, 250) rounded to the nearest 50
  end
  if class == "Mage" then
	maxhp = 150
  end

  -- I need to do the same thing with MP as well.
  if class == "Warrior" then
	maxmp = 50
  end
  if class == "Mage" then
	maxmp = 150
  end

  hp = maxhp
  chooseage()
end

function classhelp()
term.clear()
term.setCursorPos(1,1)
print("======================================================== ")
print("=	 Warrior	 =   Base HP: 250   =   Base MP: 50   = ")
print("======================================================== ")
print("=	  Mage	   =   Base HP: 150   =   Base MP: 150  = ")
print("======================================================== ")
print("")
print("Press ENTER to continue")
answer = read()
chooseclass()
end

function chooseclass(error)
term.clear()
term.setCursorPos(1,1)
print("Note: nothing is saved in the database yet, so if you quit now you will loose all of your saved work.")
print("Please choose a class. If you need help with a class, type 'help class'.")
print("")
print("[w]arrior, [m]age")
print("")
  if error == "unknowncommand" then
	print("Unknown Command")
	print("")
  end

answer = io.read()
  if answer == "help class" then
	classhelp()
  elseif answer == "w" then
	class = "Warrior"
	rollstats()
  elseif answer == "m" then
	class = "Mage"
	rollstats()
  else
	chooseclass("unknowncommand")
  end
end

function verifyusername()
term.clear()
term.setCursorPos(1,1)
print("Is ".. username .." correct? Note: You may not change this later y/n")
answer = read()
if answer == "y" then
  term.clear()
  term.setCursorPos(1,1)
  print("Welcome to the game "..username..".")
  chooseclass()
elseif answer == "n" then
  register()
else
  function register()
end
end
end

function register()
term.clear()
term.setCursorPos(1,1)
print("Welcome! It appears this is your first time playing. Please enter the name of which you wish to be called. This will be your username when playing the game, and is how other players will identify you, although you will still login with your minecraft email.")
print("")
username = read()
verifyusername()
end

-- end register function

function checkuserexist()
  rednet.send(server_id, "does_user_exist")
  rednet.send(server_id, email)
  senderID, message = rednet.receive()
	if senderID == server_id and message == "true" then
	  userexists()
	elseif senderID == server_id and message == "false" then
	  register()
	else
	userexist()
	end
end

function login()
term.clear()
term.setCursorPos(1,1)
print("Minecraft Email: ")
email = read()

term.clear()
term.setCursorPos(1,1)
print("Minecraft Password: ")
password = read()

local mclogin = http.get("http://login.minecraft.net/?user=".. email .."&password=".. password .. "&version=13")
password = ("null")
mclogin = mclogin.readAll()
  if mclogin == "Bad login" then
	term.clear()
	term.setCursorPos(1,1)
	print("Failed to login: ".. mclogin)
	print("Press enter to continue")
	answer = read()
	login()
  else
  checkuserexist()
  end
end

term.clear()
term.setCursorPos(1,1)
print("Remember: almost everything is CaSe SeNsItIvE")
login()

Server
Spoiler


rednet.open("right")

debug = true

while true do
senderID, message = rednet.receive()
  command = message

  if command == "does_user_exist" then
	senderID, message = rednet.receive()
	if fs.exists("users/"..message) == true then
	rednet.send(senderID, "true")
	else
	rednet.send(senderID, "false")
	end
  end

  if command == "write_player_info" then
	senderID, message = rednet.receive()
	email = message
	senderID, message = rednet.receive()
	username = message
	senderID, message = rednet.receive()
	class = message
	senderID, message = rednet.receive()
	maxhp = message
	senderID, message = rednet.receive()
	hp = message
	senderID, message = rednet.receive()
	maxmp = message
	senderID, message = rednet.receive()
	mp = message

	if debug == true then
	print("")
	print("received player info: ")
	print(email)
	print(username)
	print(class)
	print(maxhp)
	print(hp)
	print(maxmp)
	print(mp)
	end

	file = fs.open("/users/"..email, "w")
	file.writeLine(email)
	file.writeLine(username)
	file.writeLine(class)
	file.writeLine(maxhp)
	file.writeLine(hp)
	file.writeLine(maxmp)
	file.writeLine(mp)
	file.writeLine(age)
	file.close()
  end

  if command == "getstats" then
	senderID, message = rednet.receive()
	email = message

	file = fs.open("/users/"..email, "r")

	rednet.send(senderID, file.readLine(1))
	rednet.send(senderID, file.readLine(2))
	rednet.send(senderID, file.readLine(3))
	rednet.send(senderID, file.readLine(4))
	rednet.send(senderID, file.readLine(5))
	rednet.send(senderID, file.readLine(6))
	rednet.send(senderID, file.readLine(7))
	rednet.send(senderID, file.readLine(8))
  end

I realize the server would probably be pretty easy to hack, and most of my code can be optimized (I didn't understand rednet fully when I started xD). What I do need help with however is getting random HP rolls (Info in client line: 151). I know of ways I could do it, but just wondering the easiest.

Also, what would you guys suggest for doing inventory? I was sorta thinking that each players inventory could be saved in a table, and then written to a file on the server. In the table, would be the item ID, and the amount of that item. Then I guess there would be a table for each item? Each table would contain info like the items ID, name, HP/mp/atk gain, worth, etc. Not exactly sure how the server would know what information to send to the player at which point though. I could also just save all of the info to a file like I did with the player, and then turn them info variables. That may be easier considering I already know how to do it.

As for the actual map bit, I wanted to sorta do the same thing as the inventory. Each part of the map would have it's own table. (Should I worry about saving them to a file?) The table's would include the location name, the location ID, X and Z coords, as well as places the player could travel, etc. Any thoughts on this? Again, should I just do what I did with the players, and save them to a file instead?

One last thing. For the help commands, I want to have arguments for it. For an example, the player could type "/help inventory" and it would give the help screen for the inventory. Would you suggest doing /help inventory as a separate command then /help, or is there a way to carry arguments over?

I hope I'm not asking too much outa you guys. I just want to be sure I have everything right before moving on. Oh, and btw, I do want to add color someday ;)/>
Hawk777 #2
Posted 18 April 2013 - 04:37 PM
For randomization, I suggest the math.random function. It returns a value from 0 to 1, so multiply and add to give it whatever range you want.

For inventory, a table is the right way to store it. Same for the rooms. Pretty much anything that isn’t obviously something else in Lua is a table.

As for saving to a file, I would probably use textutils.serialize on the tables.
elfin8er #3
Posted 18 April 2013 - 04:57 PM
For randomization, I suggest the math.random function. It returns a value from 0 to 1, so multiply and add to give it whatever range you want.
Yeah, but what if I want it to be a multiple of ten, that is between two numbers?
Hawk777 #4
Posted 18 April 2013 - 06:47 PM
If you want a number between 30 and 70 that is a multiple of ten, then call math.random(3, 7). This gives you a number between 3 and 7 inclusive. I forgot that it did this in my previous post (even had that been the case, there is a way to solve it, but this way is easier). Multiply it by ten.
elfin8er #5
Posted 18 April 2013 - 08:42 PM
If you want a number between 30 and 70 that is a multiple of ten, then call math.random(3, 7). This gives you a number between 3 and 7 inclusive. I forgot that it did this in my previous post (even had that been the case, there is a way to solve it, but this way is easier). Multiply it by ten.
Oh yeah. I'm an idiot! Thanks for your help!