samedi 24 juin 2017

Powershell: Why is my if statement being directed to the else statement? or, why are the operators being evaluated incorrectly?

I have a pretty basic powershell script, The problem I am having is inside the if statement, in the function "Cast-Wait". Simply put, if you have enough mana to cast a spell, cast it. For the moment I will always have 1500 mana. However, when this statement is evaluated with a SpellCost of 100, it works as intended. However if the SpellCost is 200+ then it goes to the Else. i.e. (1400 >= 200) = false.

[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$script:mana
$script:regen
$count = 0
$continue = $true

function Get-Info {
    $script:mana = Read-Host -Prompt "Total Mana"
    $script:regen = Read-Host -Prompt "Mana Regen"
    #then we can ask about spells and priority and whatnot
}

function Time-Remaining {
    Param ([int]$SpellCost)
    # will actually be (spellcost - remainingMana) / regen
    return $SpellCost / $regen
}

function Auto-It {
    while($continue) {
        if ([console]::KeyAvailable) {
            #echo "Toggle with F12";
            $x = [System.Console]::ReadKey()

            switch ( $x.key) {
                F12 { $continue = $false }
            }
        }
        else {
            Cast-Wait -Spell 1 -SpellCost 100
            Cast-Wait -Spell 1 -SpellCost 200
            Cast-Wait -Spell 4 -SpellCost 300
            Cast-Wait -Spell 4 -SpellCost 400
            Cast-Wait -Spell 1 -SpellCost 500
            Cast-Wait -Spell 1 -SpellCost 600
            Cast-Wait -Spell 1 -SpellCost 700
            Cast-Wait -Spell 1 -SpellCost 800

            $count = $count + 1
            echo "Total count: " $count
      }
    }
}

function Cast-Wait {
    Param ([int]$Spell,[int]$SpellCost,[int]$WaitBefore,[int]$WaitAfter)

    If ($WaitBefore){Sleep -s $WaitBefore}

    If ($mana -ge $SpellCost) {
      [System.Windows.Forms.SendKeys]::SendWait($Spell)
    } Else {
      #Sleep -s (Time-Remaining -SpellCost $SpellCost)
      echo "in this else"
    }

    If ($WaitAfter){Sleep -s $WaitAfter}
}

Get-Info
Auto-It

This is the if statement:

If ($mana -ge $SpellCost) {
  [System.Windows.Forms.SendKeys]::SendWait($Spell)
} Else {
  #Sleep -s (Time-Remaining -SpellCost $SpellCost)
  echo "in this else"
}

and this is me trying out the different values, which I confirmed in each iteration with the ise debugger.

Cast-Wait -Spell 1 -SpellCost 100   #<--- this works as intended
Cast-Wait -Spell 1 -SpellCost 200   #all others do not
Cast-Wait -Spell 4 -SpellCost 300
Cast-Wait -Spell 4 -SpellCost 400
Cast-Wait -Spell 1 -SpellCost 500
Cast-Wait -Spell 1 -SpellCost 600
Cast-Wait -Spell 1 -SpellCost 700
Cast-Wait -Spell 1 -SpellCost 800

Aucun commentaire:

Enregistrer un commentaire