ForgeProtocol = {}

ForgeProtocol.OPCODE = {
  INCREASE_DUST = 153,
  UPDATE_DATA   = 155
}

function ForgeProtocol.init()
  connect(g_game, {
    onExtendedOpcode = ForgeProtocol.onExtendedOpcode
  })
end

function ForgeProtocol.terminate()
  disconnect(g_game, {
    onExtendedOpcode = ForgeProtocol.onExtendedOpcode
  })
end

function ForgeProtocol.sendIncreaseDust()
  if not g_game.isOnline() then return end

  g_game.sendExtendedOpcode(
    ForgeProtocol.OPCODE.INCREASE_DUST,
    "{}"
  )
end

function ForgeProtocol.onExtendedOpcode(opcode, buffer)
  if opcode ~= ForgeProtocol.OPCODE.UPDATE_DATA then return end

  local data = json.decode(buffer)
  modules.game_forge.onForgeUpdate(data)
end
