[SteemNova] Impossible to cancel buildings/research queue bugfix

in #utopian-io8 years ago (edited)

image.png

There were 2 independent issues in new SteemNova game project. Both of them related with job queuing in engine. @fervi did some quick workaround just to let people play but it messed up the visuals of date and timestamps. The source of issues had to be found.

Issue 1: Cancelling running research ends with PHP error

This one was very annoying. Every started technology research could't be stopped. The 'Cancel' button led to server error message. Players spent resources were stuck until the job ends. No matter if it was planned, stored as resource bank or missclick.

Message: Undefined offset: 120
File: /includes/pages/game/ShowResearchPage.class.php
Line: 61
URL: http://steemnova.intinte.org/game.php?page=research
PHP-Version: 7.0.19-1
PHP-API: fpm-fcgi
2Moons Version: 1.8.git
Debug Backtrace:
#0 /includes/pages/game/ShowResearchPage.class.php(61): errorHandler(8, 'Undefined offse...', 'FILEPATH ...', 61, Array)
#1 /includes/pages/game/ShowResearchPage.class.php(342): ShowResearchPage->CancelBuildingFromQueue()
#2 /game.php(57): ShowResearchPage->show()
#3 {main}

Solution

Reverse engineering the code was challenging because of many mis-named arrays. Whole engine is an array-within-array without comments. It took me some time to realize that researches are global account values instead of single planet's. The fix was to replace misstyped array with proper one.

image.png


Fixed

Issue 2: Cancelling queued building ends with PHP error

Second bug was more complicated. While cancel of running building were fine, the queues were broken. 'Cancel' click responded with invalid types error (it was connected with @fervi fix).

Message: DateTime::setTimestamp() expects parameter 1 to be integer, float given
File: /includes/GeneralFunctions.php
Line: 143
URL: http://steemnova.intinte.org/game.php?page=buildings
PHP-Version: 7.0.22-0ubuntu0.16.04.1
PHP-API: apache2handler
2Moons Version: 1.8.git
Debug Backtrace:
#0 [internal function]: errorHandler(2, 'DateTime::setTi...', 'FILEPATH ...', 143, Array)
#1 /includes/GeneralFunctions.php(143): DateTime->setTimestamp(6.3588806566614E+209)
#2 /includes/pages/game/ShowBuildingsPage.class.php(231): _date('U', 6.3588806566614E+209, 'Europe/Berlin')
#3 /includes/pages/game/ShowBuildingsPage.class.php(271): ShowBuildingsPage->getQueueData()
#4 /game.php(57): ShowBuildingsPage->show()
#5 {main}

Solution

It turned out that code had wrong array addressing. And not even once! Both $CurrentQueue[] and $ListIDArray[] pulls were misscalculated by 1. Original code was scaling build time as "estimated cost" instead of "building level" power of estimated build end time. In result it was powering up too big numbers, giving enormous amounts (E+209 ~INF)

image.png
image.png


Fixed

Lastly, @fervi initial quick workaround (unixtime ammendment) had been revoked because it is not needed anymore. The issues with incorrect dates and timestamps are gone.

Links

https://github.com/steemnova/steemnova/pull/3



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

You made a pull request for your own repository. Thats not necessary.

Good to know! Still I think it is much cleaner to see code changes as a pull request.

Hey @wehmoen, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Hey @mys I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.096
BTC 62826.06
ETH 1752.35
USDT 1.00
SBD 0.39