evangipson / microstocks

Microstocks is an arcade resource gathering game written in JavaScript. Try and make the most money by selling high and buying low!
http://evangipson.com/microstocks/
1 stars 0 forks source link

Stocks can have negative values and buying them increases your cash #9

Open robertpateii opened 8 years ago

robertpateii commented 8 years ago

It doesn't happen everytime, but on my game i had VOD at -$1 and i bought 100 shares and my cash went up by 100, i gained 100 shares of VOD, and VOD went to $0.

You probably are just missing an abs() somewhere in your code.

robertpateii commented 8 years ago

p.s. I don't think it initialized like that. I took some actions like buying other stocks and a few events occurred like some stocks going up and down. I don't remember which i should have taken a screenshot.

evangipson commented 8 years ago

i noticed this earlier and tried to combat it with a negative check at the source of the problem (the stocks falling negative) in updateStockPrices, line 306:

if(variant < 0 && playerObject["stats"].stocks[i].cost <= variant) 

Where variant is the amount the stocks change. I suppose I should check if stocks[i].cost is less than or equal to ZERO, not variant. Weird logic, not sure why I came up with that.

The fix has to be around there at least.

evangipson commented 8 years ago

This was addressed in commit https://github.com/evangipson/microstocks/commit/6bbd0d8f657d9c1e6f9955e4ba5b9890a3e2a2d1.

I tested it by traveling for around 10 minutes and ensuring I never got a negative stock value at all. Feel free to re-open the issue if you run across this bug again.

evangipson commented 8 years ago

Nevermind - closed this prematurely. Now I get negative numbers less frequently, and when I do they are HUGE. Need to go over updateStockPrices in this file again.

evangipson commented 8 years ago

Alright - tried to fix this again in commit https://github.com/evangipson/microstocks/commit/9eadcf4b4c46ab89430b4f4fd978f44625521bfc , but I'm going to leave this issue open until I don't have negative numbers for quite some time, since my confidence is lowered that they are eradicated just because I'm not seeing them now.

evangipson commented 8 years ago

As Robert suggested, I should put in a conditional breakpoint and SEE when the stock's value.

Also anytime an arithmetic operation is performed, check if the stock's value is ABOVE 0 (not above variant), SET IT TO 0!

evangipson commented 8 years ago

I'm having a very hard time replicating this locally, so I'm going to close this. It can always be reopened if a negative number is encountered.

evangipson commented 8 years ago

I can now see easily that I am getting this bug still when I view the resource's history graph and it falls below $0, as seen here (this was a volatile-type resource which had a flux of -$5): screen shot 2016-07-23 at 4 09 10 pm