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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright, we are down to the big method, this play_round. It turns out this one is fairly complicated, but its important right?
0:08 This is the way that you play a round in the game. Okay I'm going to put some code here. Its not going to be quite complete at first.
0:15 So, here we're going to get this thing called Game Round. I've updated this Game Class, to have this game filed. To have this Game Round,
0:24 and it basically stores all the information about who rolled what, whether that roll is winning, or losing,
0:32 and who won that Round, and things like that. It'll record it in the database, and I'm not really going to go into it because it's
0:39 exactly the same logic as we had before but its just kind of rewritten. Okay, so we're going to use that here. And we also have to do some validation.
0:47 Remember, up here, in our create_user. We have ff not flask.json give the user and someone... Turns out this is way more complicated.
0:57 So let me write one more method that will do this. We'll just put that at the bottom here. Look at all this validation.
1:02 Okay, so, we're going to come in and validate that we have a JSON body. That the game id was passed. That the user was passed.
1:10 That if we try to get the user from the database the user exists That a rule was passed. That if we try to get the rule from the database by name,
1:16 the rule exists. The game is not already over, etc... We could just validate that and then re-ask those questions
1:23 above, but when you're doing web maps and you're doing these data access things, if you already have a hold of
1:28 the roll and the user, just give them back. What we're going to do is we're going to get those three pieces
1:33 as valid data or we'll have an exception and we'll just return a response again, saying what went wrong.
1:40 And then we're going to jsonify the roll, the computer roll, the player, the opponent, outcome, all the stuff we need to
1:46 basically keep track of the client and at least report to the client what happened there. Looks like that reload worked so let's go and see if we
1:55 can Play roll. Lets verify that we have Michael. Michael is there so Michael can play a game. Lets go here and we can create another new game.
2:05 Turns out that it doesn't matter what we actually use there. Let's look at the body. We're going to pass in. Some game id.
2:13 Doesn't have to exist it turns out. Michael, and a Roll. And let's see what we get. Whoa, it worked, look at that.
2:21 We got a 200, response is JSON, very good. So, what did we get down here. Computer rolled water. Is it the Final Round? No.
2:31 The opponent is the computer. It's kind of not the best name, is it? But the opponent is the computer, the player is Michael. We have our IDs.
2:38 Michael rolled rock. Computer rolled water. This is the First Round and the outcome is, The Player loses. Ugh, well, I'm not rolling rock anymore.
2:48 I'm going to roll human. Let's try again. This time, they rolled scissors, I rolled human, and I win. Awesome. 2 for 2.
2:58 Let me just roll human a bunch of times. And one of these times, its going to be the Final Round. Final Round is True,
3:06 so if we go over here and ask for the status of this game, now we can see if this thing actually works. Oh, look, oh no, not quite the same.
3:13 Let's do that. Boom, look at that, here's our status. It's over, the moves are, you know, Michael played rock, Computer played water,
3:21 Michael played human, Computer played scissors, and then Michael just went human on it all the way the end.
3:28 Computer lost by trying to throw the sponge at us. Okay. Here's the players that were participating, and the winner of this whole outcome is Michael.
3:36 How awesome is that? And let's just do one more thing. Let's do our error handling here. Check our error handling.
3:43 What happens if we try to play a Round that's finished? Remember there was an exception? Boom, Invalid Request.
3:49 The game is already over, 400 bad requests. Maybe it should be Invalid Operation? I don't know. There's probably something better than bad requests.
3:55 But 404 is not it so we're going to go with 400 just for the sake of time. That probably felt like a lot, but the entire server is written
4:03 That's it, we're done. We have play_round, top_scores, game_status, all_rolls, now it's just a matter of literally going and writing
4:11 the client that's going to consume it. And that turns out to be quite easy.


Talk Python's Mastodon Michael Kennedy's Mastodon