PokeBot/util/bridge.lua

149 lines
2.3 KiB
Lua
Raw Normal View History

2015-04-06 01:18:46 -07:00
local Bridge = {}
2014-07-12 18:47:39 -07: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)
if client then
2014-07-12 18:47:39 -07:00
local message = prefix
if body then
2014-07-12 18:47:39 -07:00
message = message..","..body
end
client:send(message.."\n")
2014-07-12 18:47:39 -07:00
return true
end
end
local function readln()
if client then
local s, status, partial = client:receive("*l")
2014-07-12 18:47:39 -07:00
if status == "closed" then
client = nil
return nil
end
if s and s ~= "" then
2014-07-12 18:47:39 -07:00
return s
end
end
end
-- Wrapper functions
2015-04-06 01:18:46 -07:00
function Bridge.init()
if socket then
-- io.popen("java -jar Main.jar")
client = socket.connect("127.0.0.1", 13378)
if client then
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)
if INTERNAL and STREAMING_MODE then
print("tweet::"..message)
return send("tweet", message)
end
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Bridge.pollForName()
Bridge.polling = true
2014-07-12 18:47:39 -07:00
send("poll_name")
end
2015-04-07 19:06:29 -07:00
function Bridge.chat(message, extra, newLine)
if extra then
2015-04-07 19:06:29 -07:00
p(message.." || "..extra, newLine)
2014-07-12 18:47:39 -07:00
else
2015-04-07 19:06:29 -07:00
p(message, newLine)
2014-07-12 18:47:39 -07:00
end
return send("msg", "/me "..message)
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Bridge.time(message)
if not timeStopped then
2014-07-12 18:47:39 -07:00
return send("time", message)
end
end
2015-04-06 01:18:46 -07:00
function Bridge.stats(message)
2014-07-12 18:47:39 -07:00
return send("stats", message)
end
2015-04-06 01:18:46 -07:00
function Bridge.command(command)
return send("livesplit_command", command);
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Bridge.comparisonTime()
return send("livesplit_getcomparisontime");
2014-07-12 18:47:39 -07:00
end
2015-04-06 01:18:46 -07:00
function Bridge.process()
2014-07-12 18:47:39 -07:00
local response = readln()
if response then
-- print(">"..response)
if response:find("name:") then
2014-07-12 18:47:39 -07:00
return response:gsub("name:", "")
else
end
end
end
2015-04-06 01:18:46 -07:00
function Bridge.input(key)
2014-07-12 18:47:39 -07:00
send("input", key)
end
2015-04-06 01:18:46 -07:00
function Bridge.caught(name)
if name then
2014-07-12 18:47:39 -07:00
send("caught", name)
end
end
2015-04-06 01:18:46 -07:00
function Bridge.hp(curr, max)
2014-07-12 18:47:39 -07:00
send("hp", curr..","..max)
end
2015-04-06 01:18:46 -07:00
function Bridge.liveSplit()
2014-07-12 18:47:39 -07:00
send("start")
timeStopped = false
end
2015-04-06 01:18:46 -07:00
function Bridge.split(finished)
if finished then
2014-07-12 18:47:39 -07:00
timeStopped = true
end
send("split")
end
2015-04-06 01:18:46 -07:00
function Bridge.encounter()
2014-07-12 18:47:39 -07:00
send("encounter")
end
2015-04-06 01:18:46 -07:00
function Bridge.reset()
2014-07-12 18:47:39 -07:00
send("reset")
timeStopped = false
end
2015-04-06 01:18:46 -07:00
function Bridge.close()
2014-07-12 18:47:39 -07:00
if client then
client:close()
client = nil
end
print("Bridge closed")
end
2015-04-06 01:18:46 -07:00
return Bridge