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

Need help with Auto-Semi automatic Trading program

Started by Airgre, 28 February 2014 - 04:29 AM
Airgre #1
Posted 28 February 2014 - 05:29 AM
Hello all, I am new to the computercraft forums but have recently started to learn how to write programs.

I also have no idea where to post this but I figured I could do the least damage posing it here.

so I'm trying to write a program that uses the Trading Post from the mod Extra Utilties by RWtema to do automatic trading or selected trading.

I only started coding 3 days ago so I know this is very poorly done and probably very bad coding but at least the idea is there.

anyway here is the code, I was hoping for ideas or tips, or if someone wanted to take over the whole idea themselves.

(edit)
(because I'm new I have to wait for a mod to allow replies/posts)
@Bomb Bloke that was incredibly helpful thank you very much.

Also Is it Possible to stop a for loop with a key press?
(/edit)


term.clear()
term.setCursorPos(1,1)
trade = peripheral.wrap("right")
print("commands")
print('trades = Villager Trades")
print("q = quit")

while true do
input = read()
ids = trade.getVillagerIds()
idt = trade.getNearestVillagerId()
if input == "ids" then
term.clear()
term.setCursorPos(1,1)
print(textutils.serialize(ids))

elseif input == "q" then
break

elseif input == "trades" then
term.clear()
term.setCursorPos(1,1)

for key,value in pairs(ids) do
tnum = trade.getNumTrades(value)
tprints = print(textutils.serialize(value..":".."ID".."  "..tnum..":".."#Trades"))
end

print("Select Villager Id")
input = read()
var1 = input

print("Select Villager Trade Number 0,1,2,etc.")
input2 = read()
var2 = Iinput2

ts = trade.getItemSold(var1, var2
tb = trade.getItemBought(var1,var2)
tsb = trade.getSecondItemBought(var1,var2)

print(textutils.serialize(ts))
print(textutils.serialize(tb))
print(textutils.serialize(tsb))
print("do you wish to ferform the trade? yes/no")
input3 = read()
  if input3 == "yes" then
	 trade.performTrade(var1,var2)
  end
print("Enter new Command or Press Enter to see Commands")

else
term.clear()
term.setCursorPos(1,1)
print("Commands")
print("trades = see Trades")
print("ids = Villager Ids")
print("q = quit")
end
end
term.clear()
term.setCursorPos(1,1)

at the moment using the "trades" command I can see the villager ids and Number of trades, and even perform a trade.

In this setup I have a trading post direct to the "right" and a storage block connected using wired network cables to house the trading materials, so as long as you have the required materials to do the trade in the storage block, "performTrade" should work.
Edited on 28 February 2014 - 04:20 PM
Bomb Bloke #2
Posted 28 February 2014 - 09:38 AM
This sort of thing won't work:

print("Select Villager Id")
input = read()
var1 = Input

Case sensitivity is in full effect here, so "input" is not the same thing as "Input".

So you'd either do this:

print("Select Villager Id")
input = read()
var1 = input

Or just this:

print("Select Villager Id")
var1 = read()

There are also a number of other issues in the code that the interpreter should make fairly clear to you at run-time.

Anyway, instead of doing this:

ids = trade.getVillagerIds()
idt = trade.getNearestVillagerId()

… directly before you start the "while" loop (so that "ids" and "idt" are set to the same thing all the time), you want to run them directly after the loop starts and the user has typed something (so that they get updated every time the loop repeats):

while true do
  input = read()
  ids = trade.getVillagerIds()
  idt = trade.getNearestVillagerId()
  if input == "ids" then
    .
    .
    .
Airgre #3
Posted 28 February 2014 - 01:40 PM
Regarding the case sensitive code, that was a mistake when I was typing the code out in the post because my code is on a server I didn't have direct access to but now I do.

and this was incredibly helpful thank you very much.
Anymore tips/ideas?

There are also a number of other issues in the code that the interpreter should make fairly clear to you at run-time.

Anyway, instead of doing this:

ids = trade.getVillagerIds()
idt = trade.getNearestVillagerId()

… directly before you start the "while" loop (so that "ids" and "idt" are set to the same thing all the time), you want to run them directly after the loop starts and the user has typed something (so that they get updated every time the loop repeats):

while true do
  input = read()
  ids = trade.getVillagerIds()
  idt = trade.getNearestVillagerId()
  if input == "ids" then
	.
	.
	.