Reputation: 7112
I inherited the job of maintaining a small battle game.
The game works, but when the user clicks the "End" button in the browser, the game is never saved to the database.
This method is called after receiving an "HttpPut" request via the API:
protected override TroopMappingSession UpdateGameModel(TroopMappingSession g, TroopMappingSessionDto dto)
{
var retval = base.UpdateGameModel(g, dto);
if (dto.IsActiveBattle != null && dto.IsActiveBattle == false) {
GameService.SaveToDatabase(g);
}
return retval;
}
"dto" represents the TroopMappingSessionDto, which is a data transfer object.
"g" represents the model of the TroopMappingSession class.
"retval" represents the updated model of the TroopMappingSession class.
As it is like this, dto.IsActiveBattle is always "true" and GameService.End() was never called.
If I change dto.IsActiveBattle == false to retval.IsActiveBattle == false, then it does get called.
My question is, does it make sense to check both the dto.IsActiveBattle and the retval.IsActiveBattle?
I am not sure why the original developer was checking both dto.IsActiveBattle != null and dto.IsActiveBattle == false.
Thanks!
Upvotes: 2
Views: 699
Reputation: 2354
Try this
protected override TroopMappingSession UpdateGameModel(TroopMappingSession g, TroopMappingSessionDto dto)
{
var retval = base.UpdateGameModel(g, dto);
//if (dto.IsActiveBattle != null && dto.IsActiveBattle == false) {
GameService.SaveToDatabase(g);
//}
return retval;
}
Check if the game saves. This proves your retval and GameService are working
Then you can add some logic to ensure it only saves for example if dto.IsActiveBattle is false.
The point being that maybe dto.IsActiveBattle was chaged to true when var retval = base.UpdateGameModel(g, dto); was called.
Upvotes: 2