NicklasWallgren / PokemonGoAPI-PHP

Pokemon Go API PHP library
BSD 2-Clause "Simplified" License
130 stars 51 forks source link

Use strings as keys instead of floats #102

Closed mattwells closed 8 years ago

mattwells commented 8 years ago

Floats have been used as keys in the CombatPointsCalculator::$LEVEL_TO_CP_MULTIPLIER array. Floats aren't supported as array keys:

Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.

You can see the difference in the var_dump before and after the change:

array(40) {
  [1]=>float(0.135137432)
  [2]=>float(0.192650919)
  [3]=>float(0.236572661)
  [4]=>float(0.273530381)
  [5]=>float(0.306057377)
  [6]=>float(0.335445036)
  [7]=>float(0.362457751)
  [8]=>float(0.387592406)
  [9]=>float(0.411193551)
  [10]=>float(0.432926419)
  [11]=>float(0.453059958)
  [12]=>float(0.472336083)
  [13]=>float(0.4908558)
  [14]=>float(0.508701765)
  [15]=>float(0.525942511)
  [16]=>float(0.542635767)
  [17]=>float(0.558830576)
  [18]=>float(0.574569153)
  [19]=>float(0.589887917)
  [20]=>float(0.604818814)
  [21]=>float(0.619399365)
  [22]=>float(0.633644533)
  [23]=>float(0.647576426)
  [24]=>float(0.661214806)
  [25]=>float(0.674577537)
  [26]=>float(0.687680648)
  [27]=>float(0.700538673)
  [28]=>float(0.713164996)
  [29]=>float(0.725571552)
  [30]=>float(0.734741009)
  [31]=>float(0.740785574)
  [32]=>float(0.746781211)
  [33]=>float(0.752729087)
  [34]=>float(0.758630378)
  [35]=>float(0.764486065)
  [36]=>float(0.770297266)
  [37]=>float(0.776064962)
  [38]=>float(0.781790055)
  [39]=>float(0.787473578)
  [40]=>float(0.79030001)
}

After:

array(79) {
  [1]=>float(0.094)
  ["1.5"]=>float(0.135137432)
  [2]=>float(0.16639787)
  ["2.5"]=>float(0.192650919)
  [3]=>float(0.21573247)
  ["3.5"]=>float(0.236572661)
  [4]=>float(0.25572005)
  ["4.5"]=>float(0.273530381)
  [5]=>float(0.29024988)
  ["5.5"]=>float(0.306057377)
  [6]=>float(0.3210876)
  ["6.5"]=>float(0.335445036)
  [7]=>float(0.34921268)
  ["7.5"]=>float(0.362457751)
  [8]=>float(0.37523559)
  ["8.5"]=>float(0.387592406)
  [9]=>float(0.39956728)
  ["9.5"]=>float(0.411193551)
  [10]=>float(0.42250001)
  ["10.5"]=>float(0.432926419)
  [11]=>float(0.44310755)
  ["11.5"]=>float(0.453059958)
  [12]=>float(0.46279839)
  ["12.5"]=>float(0.472336083)
  [13]=>float(0.48168495)
  ["13.5"]=>float(0.4908558)
  [14]=>float(0.49985844)
  ["14.5"]=>float(0.508701765)
  [15]=>float(0.51739395)
  ["15.5"]=>float(0.525942511)
  [16]=>float(0.53435433)
  ["16.5"]=>float(0.542635767)
  [17]=>float(0.55079269)
  ["17.5"]=>float(0.558830576)
  [18]=>float(0.56675452)
  ["18.5"]=>float(0.574569153)
  [19]=>float(0.58227891)
  ["19.5"]=>float(0.589887917)
  [20]=>float(0.59740001)
  ["20.5"]=>float(0.604818814)
  [21]=>float(0.61215729)
  ["21.5"]=>float(0.619399365)
  [22]=>float(0.62656713)
  ["22.5"]=>float(0.633644533)
  [23]=>float(0.64065295)
  ["23.5"]=>float(0.647576426)
  [24]=>float(0.65443563)
  ["24.5"]=>float(0.661214806)
  [25]=>float(0.667934)
  ["25.5"]=>float(0.674577537)
  [26]=>float(0.68116492)
  ["26.5"]=>float(0.687680648)
  [27]=>float(0.69414365)
  ["27.5"]=>float(0.700538673)
  [28]=>float(0.70688421)
  ["28.5"]=>float(0.713164996)
  [29]=>float(0.71939909)
  ["29.5"]=>float(0.725571552)
  [30]=>float(0.7317)
  ["30.5"]=>float(0.734741009)
  [31]=>float(0.73776948)
  ["31.5"]=>float(0.740785574)
  [32]=>float(0.74378943)
  ["32.5"]=>float(0.746781211)
  [33]=>float(0.74976104)
  ["33.5"]=>float(0.752729087)
  [34]=>float(0.75568551)
  ["34.5"]=>float(0.758630378)
  [35]=>float(0.76156384)
  ["35.5"]=>float(0.764486065)
  [36]=>float(0.76739717)
  ["36.5"]=>float(0.770297266)
  [37]=>float(0.7731865)
  ["37.5"]=>float(0.776064962)
  [38]=>float(0.77893275)
  ["38.5"]=>float(0.781790055)
  [39]=>float(0.78463697)
  ["39.5"]=>float(0.787473578)
  [40]=>float(0.79030001)
}
NicklasWallgren commented 8 years ago

Thanks, I wasn't aware of that. Can you also update the getMaxCp method to reflect on the changes? :)

$maxCpMultplier = self::$LEVEL_TO_CP_MULTIPLIER[40.0];

mattwells commented 8 years ago

Done

mattwells commented 8 years ago

It doesn’t matter, if it looks like an integer it will be treated as an integer so 40 == '40'. You can see it in the after part of my pull request message, notice that all the integers are integers but the floats are strings. If you feel it is necessary I will do it later

NicklasWallgren commented 8 years ago

Thanks for the contribution :)