Last active
December 31, 2015 05:54
-
-
Save ErikDeBruijn/ebef5db43fe224969b40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'socket' | |
server = TCPServer.new 35666 # Server bound to port 35666, just like EE | |
set :port, 8080 | |
hull = 42 | |
shields = 80 | |
hullMax = 80 | |
i = 0 | |
post '/exec.lua' do | |
i += 1 | |
hull = [0,hull - 0.3].max if shields <= 0 | |
shields = [0, shields - 15].max if (i%8==0) | |
shieldsPerc = (shields / 100.0).to_f | |
shields_active = rand(10) > 2 | |
puts "Got a post. Hull=#{hull}, shields=#{shields}, shieldsPerc=#{shieldsPerc}" | |
"{\"shieldsUp\": #{shields_active},\"hull\": #{hull.to_i},\"hullMax\": #{hullMax.to_i}, \"shieldsPercentage\": #{shieldsPerc}}" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' # Built in, no gem required | |
def preParseResponse(response,values_old=nil) | |
values = JSON.parse(response.body) | |
if values['ERROR'] == 'No game' | |
print "Connected. Waiting for a game to start...\r" | |
$stdout.flush # clear the line | |
sleep 2 | |
return 1 | |
end | |
if values['error'] == 'No ship' | |
if(values_old['initial']) | |
print "No ship yet. Waiting for it to be spawned...\r" | |
else | |
$signalLight.send(:off) | |
$strobeLight.send(:off) | |
$smokeSwitch.send(:off) | |
$portD.send(:off) | |
print "Ship not found anymore. Ejected?!\r" | |
end | |
$stdout.flush # clear the line | |
sleep 1 | |
return 2 | |
end | |
if values["hull"] | |
return 3 | |
end | |
end | |
def port_open?(ip, port, seconds=1) | |
Timeout::timeout(seconds) do | |
begin | |
TCPSocket.new(ip, port).close | |
puts "#{ip} #{port} open" | |
true | |
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH,Errno::ENETUNREACH | |
# puts "#{ip} #{port} closed" | |
false | |
end | |
end | |
rescue Timeout::Error | |
false | |
end | |
def is_valid_server?(ip) | |
if(port_open? ip, $settings[:protonServerPort],0.05) | |
if(port_open? ip, $settings[:httpServerPort],0.1) | |
server_ip = ip | |
return server_ip | |
end | |
end | |
end | |
def scan_for_server(start_ip=nil) | |
local_ip = IPSocket.getaddress(Socket.gethostname) | |
start_ip ||= local_ip | |
puts "Scanning subnet of #{start_ip}" | |
subnet = "#{start_ip.split('.')[0]}.#{start_ip.split('.')[1]}.#{start_ip.split('.')[2]}." | |
start_ip = 1 | |
end_ip = 254 | |
until start_ip == end_ip do | |
return "#{subnet}#{start_ip.to_s}" if(is_valid_server? "#{subnet}#{start_ip.to_s}") | |
start_ip += 1 | |
end | |
end | |
def ask_for_ip() | |
puts "Type the server IP or press enter for localhost:" | |
begin | |
Timeout::timeout(5) do | |
apiServer = gets.chomp | |
end | |
rescue Timeout::Error | |
puts "No input recieved. Trying something..." | |
end | |
apiServer = '127.0.0.1' if apiServer.nil? or apiServer.empty? | |
apiServer | |
end | |
def scan_server_loop() | |
server_ip = nil | |
puts "Scanning for server..." | |
while server_ip == nil do | |
server_ip ||= is_valid_server?($settings['last_server']) | |
$settings['scan_ranges'].each { |range| server_ip ||= scan_for_server(range)} | |
server_ip ||= scan_for_server() | |
server_ip ||= scan_for_server(ask_for_ip()) | |
local_ip = Socket::getaddrinfo(Socket.gethostname,"echo",Socket::AF_INET)[0][3] | |
server_ip ||= scan_for_server(local_ip) | |
puts "Server IP: #{server_ip}" | |
end | |
server_ip | |
end | |
def store_settings(settings) | |
File.open('./settings.yml', 'w') {|f| f.write settings.to_yaml } #Store | |
end | |
def load_settings() | |
YAML::load_file('./settings.yml') #Load | |
end | |
def luaPost(url,code) | |
options = { body: code } | |
begin | |
response = HTTParty.post(url,options) | |
rescue | |
print "HTTP post unsuccesful. Retrying...\r" | |
$stdout.flush # clear the line | |
sleep 0.2 | |
retry | |
end | |
return response | |
end | |
def init_duino() | |
puts "Connecting to Arduino board" | |
begin | |
board = Dino::Board.new(Dino::TxRx.new) | |
rescue Dino::BoardNotFound | |
print "." | |
sleep 0.2 | |
retry | |
end | |
puts ". Arduino connected!" | |
$signalLight = Dino::Components::Led.new(pin: $settings[:arduinoPins][:signal], board: board) | |
$strobeLight = Dino::Components::Led.new(pin: $settings[:arduinoPins][:strobe], board: board) | |
$smokeSwitch = Dino::Components::Led.new(pin: $settings[:arduinoPins][:smoke], board: board) | |
$portD = Dino::Components::Led.new(pin: $settings[:arduinoPins][:portD], board: board) | |
return board | |
end | |
def sendToPort(port,state) | |
begin | |
port.send(state) | |
rescue Errno::ENXIO | |
# sleep 1 | |
puts "Waiting for Arduino." | |
$board = init_duino() | |
# retry | |
end | |
end | |
def enableForSeconds(port,seconds) | |
sendToPort(port,:on) | |
sleep seconds | |
sendToPort(port,:off) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For info about dino: http://tutorials.jumpstartlab.com/projects/arduino/introducing_arduino.html | |
# | |
# Installation: | |
# | |
# gem install dino httparty | |
# dino generate-sketch serial | |
# cd du | |
# ino build -m mega2560 | |
# ino upload -m mega2560 | |
# | |
# For testing, to run a fake server: | |
# ruby fake_server.rb | |
# For HTTP script access to the EmptyEpsilon server: | |
require 'httparty' | |
# For arduino connection | |
require 'dino' | |
require 'json' | |
# For scanning for a SeriousProton server: | |
require 'socket' | |
require 'timeout' | |
require_relative 'phy-lib' | |
puts "===== Physical client =====" | |
$settings = Hash.new | |
$settings = load_settings() | |
$board = init_duino() | |
$portD.send(:on) | |
server_ip = scan_server_loop() | |
apiServer = "#{server_ip}:#{$settings[:httpServerPort]}" | |
$settings['last_server'] = server_ip | |
store_settings($settings); | |
values_old = { "shieldsUp" => false,"hull" => 0, "initial" => true } | |
jumpInitiated = false | |
i=0 | |
while true | |
i+=1 | |
sleep (1.0/$settings[:requestsPerSecond]).to_f | |
response = luaPost("http://#{apiServer}#{$settings['apiEndpoint']}",$settings[:pollCode]) | |
if (response.success?) | |
values = JSON.parse(response.body) | |
responseState = preParseResponse(response,values_old) | |
next unless responseState == 3 | |
print "Raw response: #{response.body}\r" | |
$stdout.flush # clear the line | |
hullPercentage = 100.0 * values["hull"].to_i / values["hullMax"].to_i | |
if (values["shieldsUp"].to_s != values_old["shieldsUp"].to_s) | |
puts "Shields changed from " + values_old["shieldsUp"].to_s + " to " + values["shieldsUp"].to_s + "..." | |
sendToPort($signalLight,:on) if values["shieldsUp"] == true | |
sendToPort($signalLight,:off) if values["shieldsUp"] == false | |
end | |
if (values["hull"].to_i < values_old["hull"].to_i) | |
puts "Damage... (hull at #{hullPercentage.to_i}%)" | |
enableForSeconds($strobeLight,0.4) | |
end | |
shields = values["shieldsPercentage"].to_i; | |
# puts "Raw response: #{response.body}" if(i%10==0) | |
# puts " (shields at #{values['shieldsPercentage']}%, hull at #{hullPercentage}%)" | |
if (values["shieldsPercentage"].to_i < values_old["shieldsPercentage"].to_i) | |
puts "Damage... (shields at #{values['shieldsPercentage'].to_i}%, hull at #{hullPercentage.to_i}%)" | |
enableForSeconds($strobeLight,0.2) | |
end | |
if hullPercentage < $settings[:smokeAtDamage].to_i | |
if jumpInitiated == false | |
jumpInitiated = true | |
puts "JUMPING JUMPING" | |
jumpResponse = luaPost("http://#{apiServer}#{$settings['apiEndpoint']}",$settings[:initJumpCode]) | |
puts jumpResponse.body | |
puts "JUMPING JUMPING" | |
end | |
sleepTime = 0.5 | |
sleepTime = 0.8 if hullPercentage < 40 | |
sleepTime = 1.0 if hullPercentage < 30 | |
sleepTime = 1.2 if hullPercentage < 20 | |
sleepTime = 1.5 if hullPercentage < 10 | |
puts "Hull at #{hullPercentage.to_i}%! Repairs needed to fix smoke! (smoke time = #{sleepTime} s)" | |
enableForSeconds($smokeSwitch,sleepTime) | |
end | |
values_old = values | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
last_server: 192.168.0.46 | |
scan_ranges: | |
- 192.168.0.46 | |
apiEndpoint: "/exec.lua" | |
:httpServerPort: 80 | |
:protonServerPort: 35666 | |
:requestsPerSecond: 5 | |
:smokeAtDamage: 70 | |
:arduinoPins: | |
:signal: 7 | |
:strobe: 6 | |
:smoke: 5 | |
:portD: 4 | |
:pollCode: | | |
player = getPlayerShip(-1) | |
if player == nil then return {error = "No ship"} end | |
shieldsVal = player:getShieldsActive() | |
hullVal = player:getHull() | |
hullMaxVal = player:getHullMax() | |
frontShieldVal = player:getFrontShield() | |
frontShieldMaxVal = player:getFrontShieldMax() | |
rearShieldVal = player:getRearShield() | |
rearShieldMaxVal = player:getRearShieldMax() | |
shieldsPercentageVal = 50 * (frontShieldVal / frontShieldMaxVal + rearShieldVal / rearShieldMaxVal) | |
return {hull = hullVal, hullMax = hullMaxVal, shieldsUp = shieldsVal, shieldsPercentage = shieldsPercentageVal} | |
:initJumpCode: |- | |
player = getPlayerShip(-1) | |
if player == nil then return {error = "No ship"} end | |
player:initJump(5.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment