def execute(m, args = nil)
unless m.channel
m.reply "Use !battleships in a channel"
return
end
network = bot.config.server
channel = m.channel.name
user = m.user.nick
parsed = BattleshipsGame.parse_command(args.to_s)
game = AsyncDuelHandler.load(BattleshipsGame::PLUGIN, network, channel, RabbotDb)
if parsed[:error]
themed_flood_safe_reply(m, parsed[:error])
return
end
if parsed[:action] == :ai
if game && game["stage"] != "done"
themed_flood_safe_reply(m, "A game is already active here.")
return
end
result = BattleshipsGame.start_ai_game(user: user)
AsyncDuelHandler.save(BattleshipsGame::PLUGIN, network, channel, result[:game], RabbotDb)
themed_flood_safe_reply(m, result[:message])
return
end
unless %i[fire board].include?(parsed[:action])
handled = AsyncDuelHandler.handle(
game_mod: BattleshipsGame,
plugin: BattleshipsGame::PLUGIN,
network: network,
channel: channel,
game: game,
parsed: parsed,
user: user,
channel_users: m.channel.users.keys
)
unless handled[:passthrough]
themed_flood_safe_reply(m, handled[:message] || handled[:error])
AsyncDuelHandler.clear(BattleshipsGame::PLUGIN, network, channel, RabbotDb) if handled[:cleared]
return
end
end
result = case parsed[:action]
when :fire
BattleshipsGame.fire(game: game, user: user, coord: parsed[:coord])
when :board
{ message: BattleshipsGame.format_board(game, user: user) }
when :status
{ message: BattleshipsGame.format_status(game) }
else
{ message: BattleshipsGame.usage_message }
end
if result[:error]
themed_flood_safe_reply(m, result[:error])
return
end
if result[:game]
if result[:done]
AsyncDuelHandler.clear(BattleshipsGame::PLUGIN, network, channel, RabbotDb)
else
AsyncDuelHandler.save(BattleshipsGame::PLUGIN, network, channel, result[:game], RabbotDb)
end
end
themed_flood_safe_reply(m, result[:message])
end