#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Implementing the game-status method

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Next up, let's implement game status. So, if we're in Postman, we can pull this up, check. I would get the details for this game
0:10 that actually doesn't even exist. Okay, so we have a little bit of work to do. Now, on a couple of these going forward,
0:17 I'm going to take some code and just put it in here. I'm not against typing it out as you guys can probably tell, I'd love to do that for you.
0:23 But there's times when it's just not really worth you watching me write these details, 'cause we really already computed this,
0:30 but we need to change the way which we're computing it because we can't store the state between rounds in a particular game like we did before.
0:38 So we got to go back to the database and compute it. It's kind of clumsy. If we change our data model, we could do
0:44 this much more simply, but remember one of my goals is to actually not change the data model, 'cause I think that's adding too much.
0:52 This is going to be a little bit less pretty, but let's have a look. And when we ask for the game status,
0:58 we want to give several pieces of information. Is the game over, what moves were there? What players were involved in the game?
1:08 And, who won the game if the game is over? So, here, we're grabbing, up here we have the history, these are all the moves played.
1:17 We have, is the game over? These are all things that we've wrote before. We need to quickly, give it an id, find a roll and find a player so
1:26 that we can use the names and stuff in some of these methods down here, like this to_json. So, we've built to sort of look up
1:32 that we can just quickly pass id instead of going back and back and back to the database, we can just store those here.
1:38 This works for a small number of players, for hundreds or thousands, but not millions. Alright, we'd change the way this is written
1:44 if that were the case. Find the player, count how many times they've won, because that actually helps us, I don't need this anymore.
1:52 That actually helps us know whether the game is over, things like that. Okay, so let's go and run this. It shouldn't be super-interesting,
2:02 because there's going to be no game, it might even crash, index out of range, yes. Yeah, I thought so, so there's no history here.
2:11 So let's say we need to do our little 404 thing. We'll say this level, I'm going to just say abort 404, flask.abort(404), and that should throw
2:25 an exception and stop it right away. Let's try that again. Ooh, page not found. So we don't have any games created
2:33 'cause we have to be able to play a game, but I think it's going to work. We'll come back to this and test it again.

Talk Python's Mastodon Michael Kennedy's Mastodon