Created
February 16, 2014 18:46
-
-
Save mkosler/9038798 to your computer and use it in GitHub Desktop.
LuaPlaysPokemon
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
local socket = require("socket") | |
function getNextMove(commands) | |
local max = { | |
command = "", | |
count = 0, | |
} | |
for command, count in pairs(commands) do | |
if count > max.count then | |
max.command = command | |
max.count = count | |
end | |
end | |
return max | |
end | |
function getTotal(commands) | |
local count = 0 | |
for _,v in pairs(commands) do | |
count = count + v | |
end | |
return count | |
end | |
function messageServer(irc, channel, msg) | |
local irc_msg = string.format("PRIVMSG %s :%s\r\n", channel, msg) | |
io.write(irc_msg) | |
irc:send(irc_msg) | |
end | |
-- Socket Information | |
HOSTNAME = "irc.twitch.tv" | |
PORT = 6667 | |
CHANNEL = "#twitchplayspokemon" | |
NICKNAME = "ircplays" | |
REALNAME = "IRC Plays..." | |
OAUTH = io.open("oauth"):read("*a") | |
-- Command information | |
PATTERN = ":(%a+)" | |
COMMAND_TOTALS = { | |
left = 0, | |
right = 0, | |
up = 0, | |
down = 0, | |
a = 0, | |
b = 0, | |
start = 0, | |
} | |
IRC = assert(socket.connect(HOSTNAME, PORT)) | |
print(string.format("Connecting to %s:%d", HOSTNAME, PORT)) | |
IRC:send(string.format("PASS %s\r\n", OAUTH)) | |
IRC:send(string.format("NICK %s\r\n", NICKNAME)) | |
IRC:send(string.format("USER %s %s :%s\r\n", NICKNAME, HOSTNAME, REALNAME)) | |
IRC:send(string.format("JOIN %s\r\n", CHANNEL)) | |
START = os.clock() | |
RATE = 5.0 | |
while true do | |
local data = IRC:receive() | |
if data then | |
if data:find("PING") then | |
local pong = data:gsub("PING", "PONG") | |
print("REPLY: " .. pong) | |
IRC:send(pong) | |
end | |
if data:find("PRIVMSG") then | |
for command in data:gmatch(PATTERN) do | |
if COMMAND_TOTALS[command] ~= nil then | |
COMMAND_TOTALS[command] = COMMAND_TOTALS[command] + 1 | |
end | |
end | |
end | |
end | |
if os.clock() - START >= RATE then | |
local total = getTotal(COMMAND_TOTALS) | |
if total > 0 then | |
local move = getNextMove(COMMAND_TOTALS) | |
local msg = string.format("Kappa %s (%d / %d) (time: %.2f sec) Kappa", move.command, move.count, total, RATE) | |
messageServer(IRC, CHANNEL, msg) | |
else | |
messageServer(IRC, CHANNEL, "Inconclusive") | |
end | |
for k,_ in pairs(COMMAND_TOTALS) do | |
COMMAND_TOTALS[k] = 0 | |
end | |
START = os.clock() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment