Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • Announcements

    • Lizzy Trickster

      Latest Stable OpenComputers Version   11/26/16

      The latest released version of OpenComputers is version 1.7.1 for MC 1.7.10, 1.10.2, 1.11.2 & 1.12.1. See more information here! Beta/Dev builds can be found at the Jenkins Build Server (ci.cil.li)
    • Lizzy Trickster

      !!FORUM DOWNTIME!!   01/16/18

      On 2018-01-27 the forums will be going down at around 1100 GMT0 for up-to 5 hours to allow for hardware configuration changes on the system that hosts these forums as well as various updates to patch recently publicised CPU vulnerabilities. Apologies for the inconvenience that this will cause.  If you would like to keep up-to-date on the progress of the work, join our IRC channel (http://webchat.esper.net/?nick=&channels=oc) or our Discord ( https://discord.gg/0hVukoQ2KYifZFCA ).
  • 0
squall2044

SGCraft and wireless

Question

Hi all I am trying to write a program to control sgcraft gates and
am having some trouble with the server side of things heres what I have
so far:
##########################################################################
local c = require("component")

local e = require("event")

local m = c.modem

local s = c.stargate

local computer = require("computer")

()
m.open(20)

m.broadcast(20, "Online.")

local _, _, f1, port, _, message1 = e.pull("modem_message")

if message1 == "s.dial('s.localAddress()')" then

  m.send(f1, 20, io.stderr:write("Cannot connect to self."))

 else if message1 == "s.dial('???-??????')" and s.energyAvailable() < s.energyToDial('????-??????') then

   m.send(f1, 10, io.stderr:write("Not enough energy to dial that gate."))

  else if message1 == "s.dial('????-??????')" and s.stargateState() == "Idle" then

    m.send(f1, 20, "Dialing.")

   else if message1 == "s.closeIris()" and s.irisState() ~= "offline" then

     m.send(f1, 20, "Closing Iris.")

    else if message1 == "s.openIris()" and s.irisState() ~= "offline" then

      m.send(f1, 20, "Opening Iris.")

     else if message1 == "s.disconnect()" and s.stargateState() ~= "Idle" then

       m.send(f1, 20, "Disconnecting.")

----    else if message1 ==

    else

      m.send(f1, 20, io.stderr:write("Command Not Recognized. PLease wait 30 SECONDS for shutdown"))

     end

    end

   end

  end

 end

end

-----print("Hello")


(message1) ----This is where I am having trouble
computer.shutdown()
#########################################################################

Right now the computer is hooked up to a stargate with an Iris and I cam call the methods
directly from the computer but I want to run them from a tablet so I can 
bury the computer and leave it with no screen and a wake message.

what I want it to do is if I send s.closeIris() the Iris will close but I am just getting a
/home/dial:37: syntax error near 'computer'

Can anyone help me?

Thank you in advance.
 

Edited by squall2044
Added more info

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Well, the error says the syntax is invalid, and it is. If you want to execute the message, then you need to do something like this:

 

load(message1)()

 

This loads the code and compiles it, then the empty parenthesis afterwards executes it.

Share this post


Link to post
Share on other sites
  • 0

I would recommend not executing strings sent down a network.. If you're just in a SP world then fine, have at it. But, if you're on multiplayer then this is a big security problem. Just saying..

A better solution would be to design commands / responses with parameters that can be sent between the client and server. i.e.

-- # On the server

-- # Other code implied up hurr...

local commands = {
	openIris = function()
		if s.irisState() ~= 'offline' then
			m.send( f1, 20, 'Opening Iris.' )
		end
	end,
	dial = function( address )
		if s.irisState() == 'idle' then
			m.send( f1, 20, string.format( 'Dialing %s', address )
		end
	end
	-- ... more commands go here
}

-- # on message...
-- # cmd should look like: { 'dial', 'kjai2sdiwdSOME_ADDRESSksjdlaks' }
local cmd = { string.match( message1, '(%a%d_)' ) }

local command, args = table.remove( cmd, 1 ), { table.unpack( cmd ) }

-- # This calls our command with any args sent with it. This is safer than load('msg') because 'msg' could be malicious.
commands[ command ]( table.unpack( args ) )

-- # DISCLAIMER: I haven't run this code. Could have bugs. But this is generally how I handle commands over the net.

 

Let me know if I need to clarify anything... :)

Edited by Molinko
formatting..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×