Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ported from adamlerer#138
This PR fixes the API for the last turn of finished games.
First bug:
Webdip will duplicate the orders of the last phase of games that did not end by solo, creating an entire new set of phases (creating a whole new set of Diplomacy + Retreat + Build where the orders are the exact same as on the previous Diplomacy + Retreat + Build) . There is specifically code to do this, which makes me think it's deliberate and I have some suspicisons why, but anyways the API didn't handle it well.
Since the duplicated orders are actually in the database, we need a way to detect this and filter them out. We can't simply check whether the game was "Won" or not, because some games can be won by resignation/concession rather than by solo, and those games will have the duplication. We can't simply filter out the last turn unconditionally, because solo games don't have this duplication. We could try to count supply centers, but that's awkward. So I just added a heuristic check - if the game is over and the last two turn's orders look like duplicates, we just get rid of one of the copies.
Second bug:
The API unconditionally avoided populating the units array for the last phase from the historical tables, expecting them to be populated by the slightly different database tables that were used during a live game. But if the game is finished, the live game database tables are empty, so we have to populate the last phase units array from the historical tables.
Third detail:
I made the API append an extra dummy phase with the phase name "Finished" for finished games, with the final unit positions. This makes things a bit nicer on the API-caller side. (I suspect the reason why webdip duplicates the final phase orders was a hacky way of doing this for the old UI, except of course it leaves a permanent bit of ugliness in the database in that the final phase orders are now in there twice whereas all other orders are in there only once).
Lastly, I stripped out all of the draw phase idx hackery we had in the UI side. I think we don't need it any more.
Screenshots of second to last phase and last phase after this change: