Optimize the code and fix bugs and errors, write a complete code implementation
```lua
SWEP.Category = "Weapons"
SWEP.Author = "Generic Default, Worshipper, Clavus, and Bob"
SWEP.Contact = ""
SWEP.Purpose = ""
SWEP.Instructions = ""
SWEP.MuzzleAttachment = "1"
SWEP.ShellEjectAttachment = "2"
SWEP.DrawCrosshair = true
SWEP.ViewModelFOV = 65
SWEP.ViewModelFlip = true
SWEP.Base = "bobs_gun_base"
SWEP.Spawnable = true
SWEP.AdminSpawnable = true
local DEFAULT_FOV = 75 -- Constants
local RELOAD_DELAY = 0.25
local BOLT_ACTION_DELAY = 0.1
local FOV_TRANSITION_DURATION = 0.15
local WDIVIDER = 3.125
SWEP.Primary = {
    Round = "smg1", -- Primary weapon attributes
    RPM = 600,
    Cone = 0.15,
    Recoil = 1,
    Damage = 10,
    Spread = 0.01,
    NumShots = 1,
    ClipSize = 30,
    DefaultClip = 30,
    KickUp = 0.2,
    KickDown = 0.1,
    KickHorizontal = 0.1,
    Automatic = true,
    Ammo = "SMG1"
}

SWEP.Secondary = {
    Ammo = "none", -- Secondary weapon attributes
    ScopeZoom = 2,
    UseACOG = false,
    UseMilDot = false,
    UseSVD = false,
    UseParabolic = false,
    UseElcan = false,
    UseGreenDuplex = false,
    Scoped = true,
    BoltAction = false,
    Penetration = true,
    Ricochet = true,
    MaxRicochet = 10,
    Tracer = 0
}

SWEP.data = {
    ironsights = 1
}

SWEP.ScopeScale = 0.5
SWEP.ReticleScale = 0.5
SWEP.IronSightsPos = Vector(2.4537, 1.0923, 0.2696)
SWEP.IronSightsAng = Vector(0.0186, -0.0547, 0)
function SWEP:Initialize()
    self:SetReloading(false)
    util.PrecacheSound(self.Primary.Sound)
    if SERVER then
        self:SetNPCBurstSettings(3, 10, 1)
        self:SetHoldType(self.HoldType)
    end

    if CLIENT then
        self:SetupScope()
        self:SetupViewModel()
    end
end

function SWEP:SetupScope()
    local screenWidth, screenHeight = ScrW(), ScrH()
    self.ScopeTable = self:CalculateDimensions(screenWidth, screenHeight, "scope")
    self.QuadTable = self:CalculateDimensions(screenWidth, screenHeight, "quad")
    self.LensTable = self:CalculateDimensions(screenWidth, screenHeight, "lens")
    self.ReticleTable = self:CalculateDimensions(screenWidth, screenHeight, "reticle")
    self.FilterTable = self:CalculateDimensions(screenWidth, screenHeight, "filter")
end

function SWEP:CalculateDimensions(screenWidth, screenHeight, type)
    local length = screenHeight * self.ScopeScale
    local dimensions = {}
    if type == "scope" then
        dimensions = {
            l = length,
            x1 = 0.5 * (screenWidth + length),
            y1 = 0.5 * (screenHeight - length),
            x2 = 0.5 * (screenWidth + length),
            y2 = 0.5 * (screenHeight + length),
            x3 = 0.5 * (screenWidth - length),
            y3 = 0.5 * (screenHeight + length),
            x4 = 0.5 * (screenWidth - length),
            y4 = 0.5 * (screenHeight - length)
        }
    elseif type == "quad" then
        length = (screenHeight + 1) * self.ScopeScale
        dimensions = {
            x1 = 0,
            y1 = 0,
            w1 = screenWidth,
            h1 = 0.5 * screenHeight - length,
            x2 = 0,
            y2 = 0.5 * screenHeight + length,
            w2 = screenWidth,
            h2 = 0.5 * screenHeight - length,
            x3 = 0,
            y3 = 0,
            w3 = 0.5 * screenWidth - length,
            h3 = screenHeight,
            x4 = 0.5 * screenWidth + length,
            y4 = 0,
            w4 = 0.5 * screenWidth - length,
            h4 = screenHeight
        }
    elseif type == "lens" then
        dimensions = {
            x = self.QuadTable.w3,
            y = self.QuadTable.h1,
            w = 2 * self.ScopeTable.l,
            h = 2 * self.ScopeTable.l
        }
    elseif type == "reticle" or type == "filter" then
        dimensions = {
            wdivider = WDIVIDER,
            hdivider = 1.7579 / (type == "reticle" and self.ReticleScale or 1.35),
            x = (screenWidth / 2) - ((screenHeight / WDIVIDER) / 2),
            y = (screenHeight / 2) - ((screenHeight / WDIVIDER) / 2),
            w = screenHeight / WDIVIDER,
            h = screenHeight / WDIVIDER
        }
    end
    return dimensions
end

function SWEP:SetNPCBurstSettings(minBurst, maxBurst, fireRate)
    self:SetNPCMinBurst(minBurst)
    self:SetNPCMaxBurst(maxBurst)
    self:SetNPCFireRate(fireRate)
end

function SWEP:SetupViewModel()
    self.VElements = table.FullCopy(self.VElements)
    self.WElements = table.FullCopy(self.WElements)
    self.ViewModelBoneMods = table.FullCopy(self.ViewModelBoneMods)
    self:CreateModels(self.VElements)
    self:CreateModels(self.WElements)
    local owner = self:GetOwner()
    if IsValid(owner) and owner:IsPlayer() and owner:Alive() then
        local vm = owner:GetViewModel()
        if IsValid(vm) then
            self:ResetBonePositions(vm)
            vm:SetColor(self.ShowViewModel == nil or self.ShowViewModel and Color(255, 255, 255, 255) or Color(0, 0, 0, 0))
        end
    end

    local oldpath = "vgui/hud/name"
    local newpath = string.gsub(oldpath, "name", self.Gun)
    self.WepSelectIcon = surface.GetTextureID(newpath)
end

function SWEP:BoltBack()
    if not self:IsOwnerValid() or not self:HasAmmo() then return end
    timer.Simple(RELOAD_DELAY, function()
        if SERVER and self:IsOwnerValid() then
            self:SetReloading(true)
            if self:GetClass() == self.Gun then
                self:HandleIronsights(self:GetOwner())
                self:ExecuteBoltAction(self:GetOwner())
            end
        end
    end)
end

function SWEP:HasAmmo()
    return self:Clip1() > 0 or self:GetOwner():GetAmmoCount(self:GetPrimaryAmmoType()) > 0
end

function SWEP:HandleIronsights(owner)
    if self:GetIronsights() then
        owner:SetFOV(0, FOV_TRANSITION_DURATION)
        self:SetIronsights(false)
        owner:DrawViewModel(true)
    end
end

function SWEP:ExecuteBoltAction(owner)
    if not self:IsOwnerValid() then return end
    local boltActionTime = owner:GetViewModel():SequenceDuration()
    timer.Simple(boltActionTime + BOLT_ACTION_DELAY, function()
        if SERVER and self:IsOwnerValid() then
            self:SetReloading(false)
            if owner:KeyDown(IN_ATTACK2) and self:GetClass() == self.Gun then self:AdjustScope(owner) end
        end
    end)
end

function SWEP:AdjustScope(owner)
    if not self:IsOwnerValid() then return end
    local scopeZoom = 75 / self.Secondary.ScopeZoom
    owner:SetFOV(scopeZoom, FOV_TRANSITION_DURATION)
    self.IronSightsPos = self.SightsPos
    self.IronSightsAng = self.SightsAng
    self.DrawCrosshair = false
    self:SetIronsights(true, owner)
    owner:DrawViewModel(false)
end

function SWEP:Reload()
    if self:GetOwner():KeyDown(IN_USE) then return end
    self:DefaultReload(ACT_VM_RELOAD)
    if not self:GetOwner():IsNPC() then self.Idle = CurTime() + self:GetOwner():GetViewModel():SequenceDuration() end
    if self:Clip1() < self.Primary.ClipSize and not self:GetOwner():IsNPC() and IsValid(self) and self:GetOwner():KeyDown(IN_ATTACK2) and self:GetClass() == self.Gun then
        self:GetOwner():SetFOV(0, FOV_TRANSITION_DURATION)
        self:SetIronsights(false)
        self:SetReloading(true)
        if CLIENT then return end
        self:GetOwner():DrawViewModel(true)
    end

    local waitDuration = self:GetOwner():GetViewModel() and self:GetOwner():GetViewModel():SequenceDuration() or 3
    if waitDuration == 3 or waitDuration == nil then return end
    timer.Simple(waitDuration + RELOAD_DELAY, function()
        if not IsValid(self) then return end
        self:SetReloading(false)
        if self:GetOwner():KeyDown(IN_ATTACK2) and self:GetClass() == self.Gun then
            if SERVER then
                self:GetOwner():SetFOV(DEFAULT_FOV / self.Secondary.ScopeZoom, 0.15)
                self.IronSightsPos = self.SightsPos
                self.IronSightsAng = self.SightsAng
                self.DrawCrosshair = false
                self:SetIronsights(true, self:GetOwner())
                self:GetOwner():DrawViewModel(false)
            end
        elseif self:GetOwner():KeyDown(IN_SPEED) and self:GetClass() == self.Gun then
            if self:GetNextPrimaryFire() <= (CurTime() + FOV_TRANSITION_DURATION) then self:SetNextPrimaryFire(CurTime() + FOV_TRANSITION_DURATION) end
            self.IronSightsPos = self.RunSightsPos
            self.IronSightsAng = self.RunSightsAng
            self:SetIronsights(true, self:GetOwner())
            self:GetOwner():SetFOV(0, 0.2)
        end
    end)
end

function SWEP:PostReloadScopeCheck()
    if self then
        self:SetReloading(false)
        if self:GetOwner():KeyDown(IN_ATTACK2) and self:GetClass() == self.Gun then
            if CLIENT then return end
            self:GetOwner():SetFOV(75 / self.Secondary.ScopeZoom, 0.15)
            self.IronSightsPos = self.SightsPos
            self.IronSightsAng = self.SightsAng
            self.DrawCrosshair = false
            self:SetIronsights(true, self:GetOwner())
            self:GetOwner():DrawViewModel(false)
        elseif self:GetOwner():KeyDown(IN_SPEED) and self:GetClass() == self.Gun then
            if self:GetNextPrimaryFire() <= (CurTime() + 0.3) then self:SetNextPrimaryFire(CurTime() + 0.3) end
            self.IronSightsPos = self.RunSightsPos
            self.IronSightsAng = self.RunSightsAng
            self:SetIronsights(true, self:GetOwner())
            self:GetOwner():SetFOV(0, 0.2)
        end
    end
end

function SWEP:IronSight()
    local owner = self:GetOwner()
    if not self:IsOwnerValid() then return end
    if self.SelectiveFire and self.NextFireSelect < CurTime() and not self:GetNWBool("Reloading") and owner:KeyDown(IN_USE) and owner:KeyPressed(IN_RELOAD) then self:SelectFireMode() end
    if owner:KeyDown(IN_USE) and owner:KeyPressed(IN_ATTACK2) then return end
    if owner:KeyPressed(IN_SPEED) and not self:GetNWBool("Reloading") then self:EnterRunMode(owner) end
    if owner:KeyReleased(IN_USE) or owner:KeyReleased(IN_SPEED) then self:ExitRunMode(owner) end
    if owner:KeyPressed(IN_ATTACK2) and not owner:KeyDown(IN_SPEED) and not self:GetNWBool("Reloading") then
        self:EnterScopeMode(owner)
    elseif owner:KeyPressed(IN_ATTACK2) and not self:GetNWBool("Reloading") and owner:KeyDown(IN_SPEED) then
        self:EnterRunMode(owner)
    end

    if (owner:KeyReleased(IN_ATTACK2) or owner:KeyDown(IN_SPEED)) and not owner:KeyDown(IN_USE) and not owner:KeyDown(IN_SPEED) then self:ExitScopeMode(owner) end
    self.SwayScale = owner:KeyDown(IN_ATTACK2) and 0.05 or 1.0
    self.BobScale = owner:KeyDown(IN_ATTACK2) and 0.05 or 1.0
end

function SWEP:EnterRunMode(owner)
    if self:GetNextPrimaryFire() <= (CurTime() + 0.3) then self:SetNextPrimaryFire(CurTime() + 0.3) end
    self.IronSightsPos = self.RunSightsPos
    self.IronSightsAng = self.RunSightsAng
    self:SetIronsights(true, owner)
    owner:SetFOV(0, 0.2)
end

function SWEP:ExitRunMode(owner)
    self:SetIronsights(false, owner)
    self.DrawCrosshair = self.XHair
end

function SWEP:EnterScopeMode(owner)
    owner:SetFOV(75 / self.Secondary.ScopeZoom, 0.15)
    self.IronSightsPos = self.SightsPos
    self.IronSightsAng = self.SightsAng
    self.DrawCrosshair = false
    self:SetIronsights(true, owner)
    if CLIENT then return end
    owner:DrawViewModel(false)
end

function SWEP:ExitScopeMode(owner)
    owner:SetFOV(0, 0.2)
    self:SetIronsights(false, owner)
    self.DrawCrosshair = self.XHair
    if CLIENT then return end
    owner:DrawViewModel(true)
end

function SWEP:DrawHUD()
    local owner = self:GetOwner()
    if not (owner:KeyDown(IN_ATTACK2) and self:GetIronsights() and not owner:KeyDown(IN_SPEED) and not owner:KeyDown(IN_USE)) then return end
    local drawColor = Color(0, 0, 0, 255)
    local lensTextures = {
        ACOG = "scope/gdcw_closedsight",
        MilDot = "scope/gdcw_scopesight",
        SVD = "scope/gdcw_svdsight",
        Parabolic = "scope/gdcw_parabolicsight",
        Elcan = "scope/gdcw_elcansight",
        GreenDuplex = "scope/gdcw_nvgilluminatedduplex",
        Aimpoint = "scope/aimpoint",
        Matador = "scope/rocketscope"
    }

    local reticleTextures = {
        ACOG = "scope/gdcw_acogchevron",
        Elcan = "scope/gdcw_elcanreticle",
        GreenDuplex = "scope/gdcw_closedsight",
        Aimpoint = "scope/gdcw_closedsight"
    }

    local function drawSight(textureID, tableb)
        surface.SetDrawColor(drawColor)
        surface.SetTexture(surface.GetTextureID(textureID))
        surface.DrawTexturedRect(tableb.x, tableb.y, tableb.w, tableb.h)
    end

    if self.Secondary.UseACOG then
        drawSight(lensTextures.ACOG, self.LensTable)
        drawSight(reticleTextures.ACOG, self.ReticleTable)
        drawSight("scope/gdcw_acogcross", self.ReticleTable)
    end

    if self.Secondary.UseMilDot then drawSight(lensTextures.MilDot, self.LensTable) end
    if self.Secondary.UseSVD then drawSight(lensTextures.SVD, self.LensTable) end
    if self.Secondary.UseParabolic then drawSight(lensTextures.Parabolic, self.LensTable) end
    if self.Secondary.UseElcan then
        drawSight(reticleTextures.Elcan, self.ReticleTable)
        drawSight(lensTextures.Elcan, self.LensTable)
    end

    if self.Secondary.UseGreenDuplex then
        drawSight(reticleTextures.GreenDuplex, self.ReticleTable)
        drawSight(lensTextures.ACOG, self.LensTable)
    end

    if self.Secondary.UseAimpoint then
        drawSight(reticleTextures.Aimpoint, self.ReticleTable)
        drawSight(lensTextures.ACOG, self.LensTable)
    end

    if self.Secondary.UseMatador then
        drawSight(lensTextures.Matador, {
            x = self.LensTable.x - 1,
            y = self.LensTable.y,
            w = self.LensTable.w,
            h = self.LensTable.h
        })
    end
end

function SWEP:AdjustMouseSensitivity()
    return self:GetOwner():KeyDown(IN_ATTACK2) and 1 / (self.Secondary.ScopeZoom / 2) or 1
end
```