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