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