PokeBot/util/bridge.lua

179 lines
3.0 KiB
Lua
Raw Permalink Normal View History

2015-04-06 01:18:46 -07:00
local Bridge = {}
2014-07-12 18:47:39 -07:00
local Utils = require "util.utils"
local json = require "external.json"
2015-04-17 13:40:56 -07:00
local socket
if INTERNAL then
socket = require "socket"
end
2014-07-12 18:47:39 -07:00
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
function Bridge.init(gameName)
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!");
send("init,"..gameName)
return true
else
print("Error connecting to Java!");
end
end
2014-07-12 18:47:39 -07:00
end
function Bridge.tweet(message)
if 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
function Bridge.chatRandom(...)
return Bridge.chat(Utils.random(arg))
end
function Bridge.chat(message, suppressed, extra, newLine)
if not suppressed then
if extra then
p(message.." || "..extra, newLine)
else
p(message, newLine)
end
2014-07-12 18:47:39 -07:00
end
2015-04-21 22:48:15 -07:00
return send("msg", 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:", "")
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-26 16:31:07 -07:00
function Bridge.hp(curr_hp, max_hp, curr_xp, max_xp, level)
send("hpxp", curr_hp..","..max_hp..","..curr_xp..","..max_xp..","..level)
2014-07-12 18:47:39 -07:00
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-17 13:40:56 -07:00
function Bridge.report(report)
if INTERNAL and not STREAMING_MODE then
2015-04-17 13:40:56 -07:00
print(json.encode(report))
end
send("report", json.encode(report))
end
2015-04-29 16:01:33 -07:00
-- GUESSING
2015-05-01 11:10:37 -07:00
function Bridge.guessing(guess, enabled)
2015-05-17 20:41:39 -07:00
send(guess, tostring(enabled))
2015-04-29 11:37:35 -07:00
end
function Bridge.guessResults(guess, result)
2015-05-17 20:41:39 -07:00
send(guess.."results", result)
2015-04-29 16:01:33 -07:00
end
function Bridge.moonResults(encounters, cutter)
Bridge.guessResults("moon", encounters..","..(cutter and "cutter" or "none"))
2015-04-29 16:01:33 -07:00
end
-- RESET
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