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

Credit Cards

Started by brett122798, 06 October 2012 - 03:59 AM
brett122798 #1
Posted 06 October 2012 - 05:59 AM
Okay, here's my idea that I'm struggling with. I have a Tekkit server with my friends and I thought to make a credit card system where there is a credit card server(the server holds the actual money so nobody can get a million bucks). The problem is, I need the authorized seller computers to send 3 packets - the buyer, money being transferred, and the seller. My brain is just frying thinking about the idea, please help?
Luanub #2
Posted 06 October 2012 - 06:11 AM
Not sure how much information you are looking for or how experienced you are with Lua. But what you could do is combine the 3 packets into one message sent by rednet. When the server receives the message have it split it into 3 parts. You can use a seperater like a comma or semicolon so the server knows where to split the message at.
brett122798 #3
Posted 06 October 2012 - 06:47 AM
Not sure how much information you are looking for or how experienced you are with Lua. But what you could do is combine the 3 packets into one message sent by rednet. When the server receives the message have it split it into 3 parts. You can use a seperater like a comma or semicolon so the server knows where to split the message at.
Wait, is it possible do this:


rednet.send(1, packet1, packet2, packet3)

I'm fairly good with lua, however, rednet is somewhat new to me.
Lyqyd #4
Posted 06 October 2012 - 06:56 AM
rednet.send(1, info..","..info2..","..info3)

Receive it:

id, msg = rednet.receive()
info, info2, info3 = string.match(msg, "(%w+),(%w+),(%w+)")
robhol #5
Posted 06 October 2012 - 07:07 AM
string.split/explode isn't part of the Lua "library" (even though it probably should be), but there are implementations available if you google.
brett122798 #6
Posted 06 October 2012 - 07:15 AM
rednet.send(1, info..","..info2..","..info3)

Receive it:

id, msg = rednet.receive()
info, info2, info3 = string.match(msg, "(%w+),(%w+),(%w+)")
I do not understand the receiving part. How exactly do I get 3 different things from 1?
Luanub #7
Posted 06 October 2012 - 07:22 AM
string.match is splitting it at the comma's and assigning it to the vars info, info2, and info3

info, info2, info3 = string.match(msg, "(%w+),(%w+),(%w+)")

I would read up on the string API at the Lua Reference Manual http://www.lua.org/manual/5.1/manual.html#5.4
brett122798 #8
Posted 06 October 2012 - 07:40 AM
string.match is splitting it at the comma's and assigning it to the vars info, info2, and info3

info, info2, info3 = string.match(msg, "(%w+),(%w+),(%w+)")

I would read up on the string API at the Lua Reference Manual http://www.lua.org/m...manual.html#5.4

Okay, another question. The usernames, when being sent, will be in their code form and then the server will check for the usernames from the codes(this is to prevent identity theft). But then the money from user1 will have to be transferred to user2, how would I approach in doing so?
Luanub #9
Posted 06 October 2012 - 10:11 AM
What do you mean by being sent in their code? Hash strings vs human readable?

Here's how I would approach it is that is the case:


local tCodes = {"abc123","123abc"} -- create a table of codes
local tUsers = {"luanub","brett122798"} -- create a table of users  | Make sure that the first code matches up to the first user so abc123 == luanub

for x=1, # tUser do
  if info == tCodes[x] then --assuming info is the users code/first part of the message sent
    user1 = tUsers[x]
    break
  end
end

Do the same thing for user2, then play the $ transaction.
Jarle212 #10
Posted 06 October 2012 - 05:10 PM
Here is a bank system I made. If you just change it a bit it will work fine:

Programs:

File name: "Bank_host":
Apis: None.


mess = {id="",user="",pass=""}
message = ""
run = true
rednet.open("top")
encode = textutils.serialize
uncode = textutils.unserialize
inlogged = {user = "", pass= "", c_id = -1}
if not (fs.exists("acc") ) then
fs.makeDir("acc")
end
if not (fs.exists("pass")) then
f = io.open("pass","w")
f:write(" ")
f:close()
end
if not (fs.exists("logg")) then
f = io.open("logg","w")
f:write(" ")
f:close()
end
logg = {""}

function save_log()
file = io.open("logg","w")
print("nSaving log")
for i = 1, #logg do
if not (logg[i] == "") then
print(logg[i])
file:write(logg[i] .. "n")
end
end
print("")
file:close()
end
function load_log()
if fs.exists("logg") then
print("nLoading log")
file = io.open("logg","r")
st = file:read()
while st do
print(st)
logg[#logg+1] = st
st = file:read()
end
file:close()
end
print("End of logn")
end

function j_code(tab,mask_t)
coded_string = "{[0] = "first""
ma_count = 1
for i=1,#tab do
if ma_count <= #mask_t then
coded_string = coded_string ..  "," .. mask_t[ma_count] .. "=" .. """ .. tab[i] .."""
else
coded_string = coded_string  .. ",[" .. i .. "]" .. "=" .. """ .. tab[i] .. """
end
ma_count = ma_count + 1
end
coded_string = coded_string .. "}"
return coded_string
end

uncode = textutils.unserialize
function chage_password(old_pass,new_pass)
if fs.exists("pass") then
r = false
file = io.open("pass","r")
f = file:read()
file:close()
else
f = ""
end
if old_pass == f then
file = io.open("pass","w")
file:write(new_pass)
file:close()
get_password()
r= true
end
return r
end
function get_password()
if fs.exists("pass") then
file = io.open("pass","r")
password = file:read()
file:close()
else
password = ""
end
end
Id = os.getComputerID()

function new_user(name,pass)
r = false
if not (fs.exists("acc/"..name)) then
file2 = io.open("acc/" .. name,"w")
file2:write(name .. "n")
file2:write(pass .. "n")
file2:write("0")
file2:close()
r =  true
end
return r
end

function trans(name,pass,name2,ammount)
r = false
if not (ammount == nil) then
ammount = tonumber(ammount)
if ammount >0 then
if fs.exists("acc/"..name) and fs.exists("acc/"..name2) then
file = io.open("acc/"..name,"r")
n = file:read()
p = file:read()
cred = tonumber(file:read())
file:close()
file = io.open("acc/"..name2,"r")
n2 = file:read()
p2 = file:read()
cred2 = tonumber(file:read())
file:close()
if not (cred==nil) and not (n== nil) and not (p==nil) and not (n2==nil ) and not(cred2 == nil) and not (p2 == nil) then
if cred >= ammount then
cred = cred - ammount
cred2 = cred2 + ammount
file = io.open("acc/"..name,"w")
file:write(n .. "n")
file:write(p .. "n")
file:write(cred)
file:close()
file = io.open("acc/"..name2,"w")
file:write(n2 .. "n")
file:write(p2 .. "n")
file:write(cred2)
file:close()
r = true
end
end
end
end
return r
end
end
function account_login(user,pass,term_id)
for i=1,#inlogged do
if inlogged[i].user == user and inlogged[i].pass == pass and inlogged[i].c_id ~=term_id then
inlogged[i].c_id = term_id
return -3
end
end
if fs.exists("acc/".. user) then
file = io.open("acc/".. user,"r")
local u = io.read()
local p = io.read()
if pass == p then
inlogged[#inlogged+1] = {user=user,pass=pass,c_id=term_id}
return 1
end
return -1
end
return -2
end
function account_logout(index)
inlogged[index] = inlogged[#inlogged]
inlogged[#inlogged] = nil
end

function add_to_acc(name2,ammount2,sec_password2)
r = false
if sec_password2 == password then
if fs.exists("acc/".. name2) then
file = io.open("acc/"..name2,"r")
n = file:read()
p = file:read()
cred = file:read()
file:close()

if not (cred==nil) and not (ammount2 == nil) and not (p==nil) and not (n==nil ) then
cred = tonumber(cred) + tonumber(ammount2)
file = io.open("acc/" .. n,"w")
file:write(n .. "n")
file:write(p .. "n")
file:write(cred)
file:close()
r = true
end
end
end
return r
end
function get_info(name,pass)
r=-1
if fs.exists("acc/"..name) then
file = io.open("acc/"..name,"r")
n = file:read()
p = file:read()
cred = file:read()
file:close()
if pass == p then
r = tonumber(cred)
end
end
return r
end
os.startTimer(0)
function input()
event, p1, p2, p3 = os.pullEventRaw()
if event == "timer" then
os.startTimer(0)
end
if event == "terminate" then
save_log()
end
if event == "rednet_message" then
rednet_input(p1,p2)
end
end

function send_logg()

end
function rednet_input(id, message)
if not (message == nil) then
mess = uncode(message)
if not (mess == nil) then
if not (mess.id == nil) then
print("Ping from " .. id)
if mess.id == "0" then --message
print(mess.message)
logg[#logg+1] = "Message: " .. mess.message
end
if mess.id == "1" then --charge
print("Trying to charge " .. mess.user .. " by " .. mess.a .. " to " .. mess.user2)
if trans(mess.user,mess.pass,mess.user2,mess.a) then
print("Account charged")
logg[#logg+1] = "Charged " .. mess.user .. " by " .. mess.a .. " to " .. mess.user2
rednet.send(id, j_code({"101","true"}, {"id","val"}))
else
print("Faild to charge account")
logg[#logg+1] = "Failed to charge " .. mess.user .. " by " .. mess.a .. " to " .. mess.user2
rednet.send(id, j_code({"101","false"}, {"id","val"}))
end
end
if mess.id == "2" then --get info
print("Getting acc info")
a_info_temp = get_info(mess.user,mess.pass)
if a_info_temp >=0 then
print("Acc exists")
rednet.send(id, j_code({"0",a_info_temp}, {"id","message"}))
else
rednet.send(id, j_code({"0","error"}, {"id","message"}))
end
end
if mess.id == "3" then --make account
print("Trying to make account")
print("3")
if new_user(mess.user,mess.pass) then
print("Account made " .. mess.user)
logg[#logg+1] = ("Account made  " .. mess.user)
rednet.send(id, j_code({"0","Account made"}, {"id","message"}))
else
rednet.send(id, j_code({"0","Account was not made"}, {"id","message"}))
print("Failed to make acc")
end
end
if mess.id == "4" then
print("T Adding " .. mess.a .. " credit to " .. mess.user .. " from " .. id)
if (add_to_acc(mess.user,mess.a,mess.pass)) then
print("Confirmed")
rednet.send(id,j_code({"0","Added " .. mess.a .. " to acc"}, {"id","message"}))
logg[#logg+1] = ("Adding credit to " .. mess.user .. " from " .. id)
else
print("Failed")
logg[#logg+1] = ("Faild to add credit to " .. mess.user .. " from " .. id)
end
end
if mess.id == "5" then
print("T chang password")
if chage_password(mess.o,mess.n) then
print("Password changed")
rednet.send(id, j_code({"0","Password changed"}, {"id","message"}))
logg[#logg+1] = ("Password changed by " .. id)
end
end
if mess.id == "6" then
if mess.pass == password then
run = false
save_log()
end
end
if mess.id == "ping" then
print("Ping from " .. id)
rednet.send(id, j_code({"500",Id}, {"id","ping"}))
end

if mess.id == "7" then
if mess.p == password then
save_log()
os.reboot()
end
end
end
end
end
end
get_password()
print(password)
os.startTimer(0)
load_log()
function update()
while run do
input()
end
end
print("Id: " .. Id)
print("[" ..textutils.formatTime(os.time(),false) .."]" .. " - Server loaded")
logg[#logg+1] = "[" ..textutils.formatTime(os.time(),false) .."]" .. " - Server loaded"
update()


File name: "Bank_client":
Apis: "list_api", "bank_api"

os.loadAPI("list_api")
os.loadAPI("bank_api")
print("Made by Jarle212")
os.sleep(1)
run = true
user = ""
password = ""
--new_list(posx,posy,menu,script,display ammount)
--run_script(script,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)
--use_list(menu_index)
--add_item(str,list,value)
--clear_list(list)
--roll_lists(menu_index,val) 1 = next list 2 = prev list 3 = up 4 = down
--draw_all_lists(menu_index,length of "row")
function nothing()
end
function make_user()
local u,p,p2
print("User name:")
u = io.read()
print("Password:")
p = read("*")
print("Password again:")
p2 = read("*")
if p2 == p then
bank_api.new(u,p)
end
end
function info()
local u, p
print("User name:")
u = io.read()
print("Password")
p = io.read()
bank_api.info(u,p)
end

function pay_account()
local u, p
while (u~=nil) do
print("User name:")
u = io.read()
end
while (p~=nil) do
print("Password:")
p = io.read()
end
while (a~=nil) do
print("Ammount:")
a = io.read()
end
while (u~=nil) do
print("Pay to account:")
s = io.read()
end
if not (tonumber(a) == nil) then
bank_api.info(u,p,s,a)
end
end
function run_script(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)
term.setCursorPos(1,13)
arg1(arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)
end
function shut_server()
term.setCursorPos(2,13)
print("Server password:")
p = read("*")
bank_api.term(p)
end
function change_s_pass()
term.setCursorPos(2,13)
print("Old password")
p = read("*")
print("New password")
p2 = read("*")
bank_api.new_pass(p,p2)
end

function s_reboot()
term.setCursorPos(2,13)
print("Server password:")
p = read("*")
bank_api.reboot(p)
end
function add_to_acc(name,pass,ammount)
term.setCursorPos(2,13)
print("User:")
u = read()
print("Server password")
p = read("*")
print("Ammount")
a = io.read()
bank_api.add(u,p,a)
end

list = list_api
list1 = list.new_list(2,3,1,run_script, 10)
list2 = list.new_list(25,3,1,nothing,10)
list.add_item("Make user",list1,make_user)
list.add_item("Get user info",list1,info)
list.add_item("Pay account",list1,pay_account)
list.add_item("Add credit",list1,add_to_acc)
list.add_item("Shut down server",list1,shut_server)
list.add_item("Change S password",list1,change_s_pass)
list.add_item("Reboot server",list1,s_reboot)
list.add_item("Exit",list1,function() run = false end)
list.selected_list[1] = list1
bank_api.get_bank_id()
count = 0
while (p == nil) and (count < 10) do
rednet.open("top")
print("Searching for bank")
bank_api.get_bank_id()
p, s = rednet.receive(1)
if not (p == nil) and not (s==nil) then
t, bank_api.bank_id = bank_api.net(p,s)
print("Found bank")
break
end
count = count + 1
end
if p == nil then
print("Could not find bank, do you have modem on the top?")
run = false
end
os.startTimer(1)
function input()
event,p1,p2,p3 = os.pullEvent()
if event == "timer" then
os.startTimer(1)
end
if event == "rednet_message" then
redinput(p1,p2)
end
if event == "key" then
key_manager(p1)
end
end

function key_manager(key)
--roll_lists(menu_index,val) 1 = next list 2 = prev list 3 = up 4 = down
if key == 16 then
end
if key == 28 then
list.use_list(1)
end

if key == 203 then
list.roll_lists(1,2)
end
if key == 200 then
list.roll_lists(1,3)
end
if key == 205 then
list.roll_lists(1,1)
end
if key == 208 then
list.roll_lists(1,4)
end
end
function redinput(id,mess)
p,m = bank_api.net(id,mess)
if p == 0 then
list.add_item(m,list2,0)
end
if p == 1 then
if m == "true" then
list.add_item("Account charged",list2,0)
else
list.add_item("Failed to charge account",list2,0)
end
end
end

function update()
while run do
term.clear()
term.setCursorPos(1,1)
list.draw_all_lists(1,20)
input()
end
end
update()

Apis:

File name: "list_api":

items = {}
lists = {}
selected_list = {}
function use_list(menu_index)
count = 0
for it = 1, #items do
if items[it].l == selected_list[menu_index] then
if count == lists[selected_list[menu_index]].selected then
lists[selected_list[menu_index]].script(items[it].val)
break;
end
count = count +1
end
end
end

function run_script(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)
arg1(arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)
end
function new_list(posx,posy,menu,script,items_showed)
lists[#lists+1] = {x = posx, y = posy, men = menu, items = 0, selected = 0, script = script, start_at = 0, count = items_showed}
return #lists
end
function delete_item(list)
lists[list].items = 0
lists[list].selected = 0
for i=1, #items do
if items[i].l == list then
items[i] = {text = nil, l = nil, val = nil}
end
end
end
function add_item(str,list,value)
items[#items+1] = {text = str, l = list, val = value}
lists[list].items =  lists[list].items + 1
end
function roll_lists(menu_index,val)
local count_1
local s_list
s_list = selected_list[menu_index]
if val == 1 then
for l = 1, #lists do
if lists[l].men == menu_index then
if l > selected_list[menu_index] then
selected_list[menu_index] = l
break
end
end
end
end
if val == 2 then
for l = 1, #lists do  -- L=1 #lists = 2   2-1 = 1 2-2 = 0 4-3= 3   4+1-1 = 4  if 4 < 4
if lists[#lists-l+1].men == menu_index then
if #lists-l+1 < selected_list[menu_index] then
selected_list[menu_index] = #lists-l+1
break
end
end
end
end
if val == 3 then
if lists[selected_list[menu_index]].selected >0 then
lists[selected_list[menu_index]].selected =  lists[selected_list[menu_index]].selected -1
if  lists[selected_list[menu_index]].selected < lists[selected_list[menu_index]].start_at+lists[selected_list[menu_index]].count then
if  lists[selected_list[menu_index]].start_at > 0 then
lists[selected_list[menu_index]].start_at = lists[selected_list[menu_index]].start_at -1
end
end
end
end
if val == 4 then
if lists[selected_list[menu_index]].selected < (lists[selected_list[menu_index]].items - 1) then
lists[selected_list[menu_index]].selected = lists[selected_list[menu_index]].selected +1
if lists[selected_list[menu_index]].selected > (lists[selected_list[menu_index]].start_at+lists[selected_list[menu_index]].count) then
lists[selected_list[menu_index]].start_at = lists[selected_list[menu_index]].start_at + 1
end
end
end
end
function draw_all_lists(menu_index,h)
local count_1
local height
local longest_s
local s_x_pos = 0
local s_y_pos = 0
for l = 1, #lists do
term.setCursorPos(lists[l].x,lists[l].y)
if lists[l].men == menu_index then
count_1 = 0
height = 0
longest_s = 0
s_x_pos = 0
s_y_pos = 0
for it = 1, #items do
if items[it].l == l then
if count_1 >= lists[l].start_at and count_1 <=lists[l].start_at+lists[l].count then
if height > h then
s_x_pos = s_x_pos +longest_s+3
s_y_pos = 0
height = 0
longest_s = 0
end
if string.len(items[it].text) > longest_s then
longest_s = string.len(items[it].text)
end
if lists[l].selected == count_1 then
print("[ " .. items[it].text .. " ]")
else
print("  " .. items[it].text .. "  ")
end
s_y_pos = s_y_pos + 1
term.setCursorPos(lists[l].x + s_x_pos,lists[l].y+s_y_pos)
height = height + 1
end
count_1 = count_1 + 1
end
end
end
end
end

File name: "bank_api":

uncode = textutils.unserialize
bank_id = 1
function j_code(tab,mask_t)
coded_string = "{[0] = "first""
ma_count = 1
for i=1,#tab do
if ma_count <= #mask_t then
coded_string = coded_string ..  "," .. mask_t[ma_count] .. "=" .. """ .. tab[i] .."""
else
coded_string = coded_string  .. ",[" .. i .. "]" .. "=" .. """ .. tab[i] .. """
end
ma_count = ma_count + 1
end
coded_string = coded_string .. "}"
return coded_string
end

function new(name, pass)
rednet.send(bank_id,j_code({"3",name,pass},{"id","user","pass"}))
end
function charge(name, pass, name2, ammount)
rednet.send(bank_id,j_code({"1",name,pass,name2,ammount},{"id","user","pass","user2","a"}))
end
function info(name,pass)
rednet.send(bank_id,j_code({"2",name,pass},{"id","user","pass"}))
end
function add(name,pass,ammount)
rednet.send(bank_id,j_code({"4",name,pass,ammount},{"id","user","pass","a"}))
end
function term(pass)
rednet.send(bank_id,j_code({"6",pass},{"id","pass"}))
end
function new_pass(old,new)
rednet.send(bank_id,j_code({"5",old,new},{"id","o","n"}))
end
function reboot(pass)
rednet.send(bank_id,j_code({"7",pass},{"id","p"}))
end
function get_bank_id()
rednet.broadcast(j_code({"ping"},{"id"}))
end
function net(id, message)
if not (message == nil) then
mess = uncode(message)
if not (mess == nil) then
if mess.id == "0" then return 0, mess.message end
if mess.id == "101" then
return 1, mess.val
end
if mess.id == "500" then
bank_id = tonumber(mess.ping)
return 2,tonumber(mess.ping)
end
end
end
end
--get_bank_id()
--p, s = rednet.receive()
--t,bank_id = net(p,s)



NOTE:
*You can only have one bankserver run at the time and you do not need to have its id to connect. The bank_api can scan for one bank server
*To change the password of the bank server use a bank client:
-Go to the change S password
-Type in a [space] key when it sais "Old password" and write a new one.

*You need a client to shut down the server and by using ctrl+t you save a log.
*You can use the bank_api to make a new client if needed for selling stuff.
Jarle212 #11
Posted 06 October 2012 - 05:17 PM
There is somthing wrong with the "Pay account" button trying to fix it now.

Edit:

Here is the fix
In bank_client:
On line 48 start of function pay_account()


function pay_account()
local u,p,a,s = nil
while (u==nil) do
print("User name:")
u = io.read()
end
while (p==nil) do
print("Password:")
p = read("*")
end
while (a==nil) do
print("Ammount:")
a = io.read()
end
while (s==nil) do
print("Pay to account:")
s = io.read()
end
if not (tonumber(a) == nil) then
bank_api.charge(u,p,s,a)
end
end
Luanub #12
Posted 07 October 2012 - 12:23 AM
Here is a bank system I made. If you just change it a bit it will work fine:

Spoilers please, they are the same as the code tags.
hego555 #13
Posted 08 October 2012 - 06:11 AM
Okay, here's my idea that I'm struggling with. I have a Tekkit server with my friends and I thought to make a credit card system where there is a credit card server(the server holds the actual money so nobody can get a million bucks). The problem is, I need the authorized seller computers to send 3 packets - the buyer, money being transferred, and the seller. My brain is just frying thinking about the idea, please help?

I am guessing that you are building the server with rednet… bad idea!

If the money is hosted in game its vulnerable!

I host my money on a PHP server!
brett122798 #14
Posted 08 October 2012 - 06:29 AM
Okay, here's my idea that I'm struggling with. I have a Tekkit server with my friends and I thought to make a credit card system where there is a credit card server(the server holds the actual money so nobody can get a million bucks). The problem is, I need the authorized seller computers to send 3 packets - the buyer, money being transferred, and the seller. My brain is just frying thinking about the idea, please help?

I am guessing that you are building the server with rednet… bad idea!

If the money is hosted in game its vulnerable!

I host my money on a PHP server!
Rednet is fine as long as you have your database well protected from hackers. And my friends don't know anything about lua/CC. They don't even know how to copy files, much less make valid malicious code!