2014-07-12 18:47:39 -07:00
|
|
|
local bridge = {}
|
|
|
|
|
2015-01-19 17:08:17 -08:00
|
|
|
local socket
|
|
|
|
if INTERNAL then
|
|
|
|
socket = require("socket")
|
|
|
|
end
|
|
|
|
|
2014-07-12 18:47:39 -07:00
|
|
|
local utils = require("util.utils")
|
|
|
|
|
|
|
|
local client = nil
|
2015-03-29 12:23:29 -07:00
|
|
|
local timeStopped = true
|
2014-07-12 18:47:39 -07:00
|
|
|
|
|
|
|
local function send(prefix, body)
|
2015-03-30 15:29:00 -07:00
|
|
|
if client then
|
2014-07-12 18:47:39 -07:00
|
|
|
local message = prefix
|
2015-03-30 15:29:00 -07:00
|
|
|
if body then
|
2014-07-12 18:47:39 -07:00
|
|
|
message = message..","..body
|
|
|
|
end
|
2015-01-19 17:08:17 -08:00
|
|
|
client:send(message.."\n")
|
2014-07-12 18:47:39 -07:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function readln()
|
2015-03-30 15:29:00 -07:00
|
|
|
if client then
|
2015-01-19 17:08:17 -08:00
|
|
|
local s, status, partial = client:receive("*l")
|
2014-07-12 18:47:39 -07:00
|
|
|
if status == "closed" then
|
|
|
|
client = nil
|
|
|
|
return nil
|
|
|
|
end
|
2015-01-19 17:08:17 -08:00
|
|
|
if s and s ~= "" then
|
2014-07-12 18:47:39 -07:00
|
|
|
return s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Wrapper functions
|
|
|
|
|
|
|
|
function bridge.init()
|
2015-01-19 17:08:17 -08:00
|
|
|
if socket then
|
|
|
|
-- io.popen("java -jar Main.jar")
|
|
|
|
client = socket.connect("127.0.0.1", 13378)
|
2015-03-30 15:29:00 -07:00
|
|
|
if client then
|
2015-01-19 17:08:17 -08:00
|
|
|
client:settimeout(0.005)
|
|
|
|
client:setoption("keepalive", true)
|
|
|
|
print("Connected to Java!");
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
print("Error connecting to Java!");
|
|
|
|
end
|
|
|
|
end
|
2014-07-12 18:47:39 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.tweet(message) -- Two of the same tweet in a row will only send one
|
2015-01-19 17:08:17 -08:00
|
|
|
print("tweet::"..message)
|
2014-07-12 18:47:39 -07:00
|
|
|
return send("tweet", message)
|
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.pollForName()
|
|
|
|
bridge.polling = true
|
|
|
|
send("poll_name")
|
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.chat(message, extra)
|
2015-03-30 15:29:00 -07:00
|
|
|
if extra then
|
2014-07-12 18:47:39 -07:00
|
|
|
print(message.." || "..extra)
|
|
|
|
else
|
|
|
|
print(message)
|
|
|
|
end
|
|
|
|
return send("msg", message)
|
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.time(message)
|
2015-03-30 15:29:00 -07:00
|
|
|
if not timeStopped then
|
2014-07-12 18:47:39 -07:00
|
|
|
return send("time", message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.stats(message)
|
|
|
|
return send("stats", message)
|
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.command(command)
|
2015-01-19 17:08:17 -08:00
|
|
|
return send("livesplit_command", command);
|
2014-07-12 18:47:39 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.comparisonTime()
|
2015-01-19 17:08:17 -08:00
|
|
|
return send("livesplit_getcomparisontime");
|
2014-07-12 18:47:39 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.process()
|
|
|
|
local response = readln()
|
2015-03-30 15:29:00 -07:00
|
|
|
if response then
|
2015-01-19 17:08:17 -08:00
|
|
|
-- print(">"..response)
|
2015-03-30 15:29:00 -07:00
|
|
|
if response:find("name:") then
|
2014-07-12 18:47:39 -07:00
|
|
|
return response:gsub("name:", "")
|
|
|
|
else
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.input(key)
|
|
|
|
send("input", key)
|
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.caught(name)
|
2015-03-30 15:29:00 -07:00
|
|
|
if name then
|
2014-07-12 18:47:39 -07:00
|
|
|
send("caught", name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.hp(curr, max)
|
|
|
|
send("hp", curr..","..max)
|
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.liveSplit()
|
|
|
|
send("start")
|
|
|
|
timeStopped = false
|
|
|
|
end
|
|
|
|
|
2015-03-28 21:28:58 -07:00
|
|
|
function bridge.split(finished)
|
2015-03-30 15:29:00 -07:00
|
|
|
if finished then
|
2014-07-12 18:47:39 -07:00
|
|
|
timeStopped = true
|
|
|
|
end
|
|
|
|
send("split")
|
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.encounter()
|
|
|
|
send("encounter")
|
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.reset()
|
|
|
|
send("reset")
|
|
|
|
timeStopped = false
|
|
|
|
end
|
|
|
|
|
|
|
|
function bridge.close()
|
|
|
|
if client then
|
|
|
|
client:close()
|
|
|
|
client = nil
|
|
|
|
end
|
|
|
|
print("Bridge closed")
|
|
|
|
end
|
|
|
|
|
|
|
|
return bridge
|