Our MMORPG's accounts get characters of their own now. MySQL keeps each character with an id and a name, up to four of them in each world, and a migration turns the players we had into the first characters. The C++ login server answers a login with a session, which stands in for the password from then on, lists the account's characters, makes and deletes them, and gives a token to enter a world as one. In the raylib client, the login comes first, then the worlds, then your characters, each drawn in 3D as it looks, turned with the mouse. This is episode 58 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.
The code
Each step on screen is a tag in the game's repository. The code at the end of the episode is ep058/end.
Transcript
Intro
0:00 Hi there, and welcome to episode fifty-eight. Today, our accounts get characters of their own, up to four in each world, each with a name, and we pick one on a screen that draws it as it looks. Here's where we'll be by the end.
0:14 By the end of the episode, we log in to the account first, then pick a world, and then one of the account's characters in it, drawn beside the list as it looks, which we can turn with the mouse. Each character has a name, a level and things to wear of its own, and we enter the world as the one we picked.
0:33 The series has a website. It links to the code on GitHub, which is open source, and lists the episodes, the latest first, each with a page of its own and the code at each of its steps.
Last time
0:44 First, a quick recap of last time. The game got a cinema tool, which draws each frame through the client's renderer, at any size and for as long as it takes. Each land got a sky, with a sun, and low-poly clouds that drift and cast shadows. A film's clock came apart from the game's, so a shot can slow the game down, and with all of that, the game filmed its own trailer.
Today
1:08 Here's how it goes in the game as it stands. The client opens on the worlds, and we pick one before we log in. Then we log in to that world, and we're in it straight away, as arthur. The account is the player: there's one in each world, called after the account, and no way to have another.
1:26 Most other games keep who you are apart from who you play. The account is yours, with its password, and it holds characters, each with a name other players see, and a life of its own: its level, where it is, and what it carries. So today, MySQL keeps a row for each character, with an id and a name, and the world plays a character by its id. The login server answers a login with a session, which stands in for the password after that, lists the account's characters, and gives a token to enter a world as one. The client logs in first, then shows the worlds, then the characters in the world we pick, each drawn as it looks, which we can turn. Right, let's start with the database, where the characters live.
A row per character
2:06 The tables change in a migration, the tenth, which Flyway runs before anything else uses MySQL. An account can have several characters in a world now, each with an id and a name of its own, which is what other players see. The world servers save into Redis first, so before this runs on a live server, the world servers stop and the saver empties Redis into MySQL. Redis keeps each player by the id they played as, and for some characters, that id changes here. A player's skills and bag point at their row in players by the account and the world, so first, those two foreign keys go, while we change what they point at. Players get an id, at zero for now, and a name, which can be null until we've filled it in. The characters in Ember keep their account's id. That's the id they've played as, and the client draws each player's skin, hair and shirt from it, so they look as they did. The ones in the other worlds can't, as the id is one id across all the worlds now, and their account's id may be its Ember character's. They're numbered from just past the largest account id, with row number, in order of their account and world, so no two of them meet, or meet one of Ember's. Each is named after its account, which is the name it went by. Then the primary key moves from the account and the world to the id, which counts up for each new character from here on, starting past the largest one there is, and the name can't be null any more. Two characters in a world can't share a name, though a name can be taken again in another world. And a key on the account and the world finds an account's characters quickly, which the account's foreign key needs, now the primary key doesn't start with the account. The skills get a player column, filled in from the players they belonged to, by joining on the account and the world. Each skill row takes the id of the character it belonged to, found by its account and world. Once each row has its player, the old primary key goes, which was the account, the world and the skill, and then those two columns. The player's default goes too, and the primary key becomes the player and the skill, and the foreign key points at the character's id, so deleting a character deletes its skills. The bags go that way too, with a player column, filled in by a join, as the skills were, and then they lose their old primary key, and those two columns. Their primary key becomes the player and the place, such as bag zero or the main hand, and a deleted character's bag goes with it.
4:38 Let's run it on some players from before. At the ninth migration, two accounts have three players between them: account one, arthur, in Ember and in Frost, and account two, merlin, in Ember, each a row by its account and world. Flyway runs the tenth migration, characters. Now each has an id and a name. The two in Ember kept their account's id, one and two, and arthur's character in Frost is number three, the first past the largest account id.
5:07 The world plays each player by an id: until now, the account's, from the token it got. From now on, it's the character's. In the players' header file, load takes the character's id, and finds it in the world it's given. A NotFound error says there's no such character in this world. In players, the Redis key for a player is the world and the id, as before, but the id is the character's now. Load used to start from the accounts table, for the name, and left join the account's row in players, if there was one, so an account that hadn't played in this world yet came in at zero. A character has its row from the moment it's made, so load reads it straight from players, by its id and its world. A character from another world gives no row, and so a NotFound error, which names the id and the world. The skills and the bag are read by the character's id too. In save, each player's hash in Redis keeps its world and, under player, its id, for the saver to read back. In flush, the saver reads that field from each hash it takes, and the old reading of a key that held an account's id, from before there were worlds, goes. The saver used to insert each player's row, or update it if it was there. A character gets its row when it's made, which the login server does from the next step on, so a batch is an update. It's a table of the batch's rows, made with values and a row for each player, joined to players by the id. A character that's been deleted since has no row to join, so its save goes nowhere, and its row stays gone. The skills and the bags are still inserted with IGNORE, which skips the rows of a character that's gone, with a warning, instead of failing the batch. A bag that changed has its old rows deleted by the player's id alone, and its new rows have five values each, without the account and the world. A hash without a player, saved by its account before characters, is skipped. The saver flushed those before the characters came, so there are none left to save. Each row of the table is written with ROW, as MySQL wants in a table of values. It starts with the player's id and world, then the rest of the hash as before, from its third field. The bags that changed are deleted by the player's id, and their rows go back in as the player, the place, the item, the count and its state. The skills go in as the player and the skill. The table gets its columns' names, it's joined to players on the id and the world, so a character is updated in its own world alone. Each column of the character's row is set from it. First, where they stand. Then, the experience and the health, as they were in the hash. Then the land they're in, such as the Meadows, and the weapon in their hand, each from its own column in the batch. The bag's revision and the quests keep what MySQL has where the hash has none. And the bags' rows go into MySQL in batches, now five values a row.
8:03 The characters get a class of their own, in a new header file. It brings in Abseil's status or, for an answer that can be an error, and the database it runs its statements on. Characters works on an account's characters, over the database. Each character is in one world, by its id, with a name nobody else in that world has, and what a world saves of it, it saves by that id. An account can have four of them in each world. It's made with the database, which it keeps a reference to. Create makes a new character for an account, in a world, with a name, and returns its id. It has no bag yet, so the world gives it the starting kit the first time it enters, as it did for a new player. Its errors say what went wrong, each in a kind of its own. An invalid argument is a name that won't do, already exists is a name that's taken in that world, and resource exhausted is an account with four there already. Under private, it keeps the database, to run its statements on. In characters, we bring in Abseil's string formatting, for the messages, and the accounts' header, for the rules a name follows. A character's name follows an account name's rules, three to sixteen letters, digits and underscores, so the check on usernames does for both. Then one statement inserts the character, in the middle of its world, as long as the account has fewer than four there. Counting first and inserting after would let two requests at once both see three, and both insert. In one statement, MySQL decides. The unique key on the world and the name turns a taken name into an already exists error, which we give a message a player can read. And when the count stopped it, nothing was inserted, so there's no new id, and that's the account's four. Otherwise, it's the new character's id. In the server's CMake file, characters goes into the server's core library.
9:58 Now, let's change the players' tests to play characters, which need the characters' header. Each test's setup makes an account with the test's name, as before, and keeps its id as the account's, in a member of its own. Then it makes two characters with Characters, over the test's database. The first is in Ember, where most of the tests play, and the second in Frost, both named after the account, which works because a name is unique in its own world. If either can't be made, the test stops there, with the error, and their ids are what the tests load and save by from now on. Tearing down deletes the two characters' hashes, and the hash saved for an id of zero, and takes their keys out of the unsaved sets. Deleting the account deletes its characters, with their skills and bags, down the foreign keys. The fixture keeps the account's id, and its two characters' ids, in Ember and in Frost. In the test of where each player was saved, Ember's character is no player of Frost's, so loading it there is a NotFound error, and the one in Frost loads at zero, as it's new. The test of the flush saves the Frost character, and a save for an id of zero, which has no row, so the update leaves it out, and holds back nobody. MySQL used to refuse that save, and now there's no row for it to update. Either way, the batch goes through for the other players, as it should. Then the test writes a hash by hand, as one saved before characters would be. Its key is the one the first character's hash has now. But inside, it has an account where the player's id would be, as the hashes from before characters had. It says which account it was, and not which character, so the saver leaves it. It's marked unsaved, as it would be, and the saver flushes. The flush has to work, and write one row at least, and then the account's rows are read back by its id, in order of the world. Ember's row stays at zero, while the second row has the values the test gave it: where it stood, its experience and health, its land and its weapon. That's the row the update wrote, joined by the character's id. The other row has its zeros because the hash from before characters was left alone, and the Ember character is as it was made. Then the test deletes the Frost character's hash from Redis, and loads the character again, so its skills come back from MySQL's rows. The bag's tests look its rows up by the player now, and the test that changes a row by hand finds it by the player too. The test of more bags than a statement takes makes forty accounts, each with a character in Ember, named after it. It keeps the accounts, to delete them at the end, and the characters' ids, whose bags it fills and counts. Deleting the accounts deletes their characters too. And the quests' test reads its row by the character's id. The server's tests bring in the characters' header too. They log their players in with a token for an id, which is a character's now. Tearing down deletes the characters' hashes in Redis, and takes them out of the unsaved sets, and then the accounts, in a loop of their own, which their characters go with, down the foreign keys. A new account gets a name made for the test, and comes with a character in the test's world, named after it, whose id it keeps, and returns, for the token, so the tests log in as that character. If either can't be made, the test fails, and goes on with an id of zero. The fixture keeps the characters it made, as well as the accounts, so tearing down can find them both. And one test had stopped testing what it says. The test that bob sees alice's hit builds its own content, with a sword but none of the skills, and since Strike became a skill, a swing needs it, so alice's swing didn't start, and the test failed against MySQL. Its content gets Strike now, as the other tests' does.
13:50 We build the tests at this step, in Docker, and run the players' and the server's tests, against MySQL and Redis, which Compose starts first, with Flyway running the migrations in between. The saver updates the characters' rows, and leaves the hash from before characters, and bob sees alice's hit again. They all pass.
A session, then a character
14:12 Now, the login server. A login used to be one request, with the name and the password, and its reply carried the token to enter the world the client had picked. Now there's more to ask once we're in: our characters, a new one, one to delete, and one to enter as. Sending the password with each of those would mean the client keeping it. So in our proto file, the login request gets a session, field five, which the reply to a login brings, and which the requests after it carry in place of the name and the password. Create character asks for a new one, in the world, with the name, fields seven and eight. Delete character and enter each go with a character's id, field eleven. A character listing is what the login server tells the client about one of the account's characters, enough to list it and to draw it. It has its id, name, world, level and land, what's in its main hand, and what else it wears, by their ids in the items file. And fresh says it hasn't entered its world yet, where it'll get the starting kit. In the login reply, the token that was there is now the token to enter a world as the character we picked. Once we've logged in, the reply brings the session to ask with next, and the account's characters, in all the worlds. The tokens live in Redis, and so will the sessions. In the tokens' header file, a session lasts half an hour after its last request. Issue makes a token to enter a world, once, as the player with the id: a character, from the login server, or a player moving to another instance of a world. Open makes a session for an account, once it's logged in, and returns it, as a string of hex. Account of gives back the account a session is for, and keeps it going for another half hour, or gives a NotFound error once it's run out. In tokens, a session's key starts with session, as a token's starts with token, so the two kinds of keys stay apart in Redis. The thirty-two random bytes in hex, which made each token, move into a function of their own, random hex, as a session is made like a token. Libsodium fills the bytes from the system's secure random source, and writes them out as sixty-four hex digits, which it returns as a string. Issue used to do all that in its own body. Now it takes a player's id, in place of an account's, which is what the world will load, and its token comes from random hex. Redeeming a token reads that id back, which is a character's now. Open makes a session from random hex too, and stores it with SET, under its key, with the account's id as the value, to expire in thirty minutes, given in seconds, as Redis takes them. It returns the session, for the reply. Account of reads a session back. It uses GETEX, with the session's key, and the lifetime in seconds again. GETEX reads the key and sets its expiry again, in one command, so a session in use doesn't run out. A key that's gone comes back from our Redis wrapper as a NotFound error, which is what account of returns for a session that's run out. A session that holds something other than a number is an internal error, as a token's is. Unlike a token, which redeem deletes as it reads it, a session stays, for the next request.
17:21 A delete will need to know whether it deleted anything. In the database's header file, a result gets how many rows an insert, update or delete changed, which MySQL's statement API gives us right after it runs. In the characters' header file, which brings in the protocol's messages, Abseil's status and vectors, the class is what the login server lists, makes and deletes characters with, each in one world, with a name of its own. List gives all of an account's characters, in all the worlds, the oldest first, as the listings the client gets, with what they wear. It reads MySQL, so what a world has saved in Redis and the saver hasn't flushed yet shows a few seconds later. It takes the account's id, and nothing else. Delete deletes one of the account's characters, with its skills and its bag, by its id, and says NotFound when there's no such character, and world of gives the world one of them is in. Both take the account as well as the id, so an account can't touch a character that isn't its own: either one gives a NotFound error then. In characters, we bring in Abseil's number parsing, for the rows' text, and the levels, to turn experience into a level. List comes first in the file. List selects the account's rows, the oldest first, by id, with a weapon of zero where the column's null. A null items revision means the character hasn't saved a bag, so it hasn't entered its world yet, and that's fresh. The listings go in a vector, with room for a row each, and a map from each id to its listing, for the second query. Each row becomes a listing, added at the end of the vector. MySQL's answers come back as text, so the id, the experience and the weapon are parsed into numbers, and a row where they aren't numbers is an internal error, as the columns are numbers in MySQL. The id and the name go straight into the listing, with the world. The level comes from the experience, with the table the world uses, and fresh is the last column, a one or a zero. Once the vector's filled, and won't grow again, the map points at its listings. An account without characters is done there, with an empty list. Otherwise, there's a second query, for what they wear. It finds the items of the account's characters that are in a slot, not in the bag, joined to players for the account, in order of the character and the place. The items in a slot are what the character is wearing, or holding. A failure there is the database's, and goes back as it is. Each row is the character's id, the place and the item, with the numbers parsed from text as before. Each item finds its character's listing through the map, and one whose character isn't listed is skipped. The main hand's is the weapon, while the rest go in wearing. The weapon column, from before bags, gives the weapon of a character that hasn't saved one in the main hand. Delete is one statement, which deletes the row with the id and the account. A failure to run it is the database's, and if no row changed, it wasn't the account's, which is a NotFound error. The foreign keys take its skills and its bag with it. And world of is one query too, which reads the world of the character with that id, if the account is its own, or gives a NotFound error when there's no such row, and otherwise, the world's name, from the row's one column.
20:42 In the login server's header file, handle login answers each request, and takes the characters now as well as the accounts and the tokens. A login, or a new account, gets a session and the account's characters. With that session, a request makes a character, deletes one, or gets a token to enter a world as one, and each answer brings the characters as they are after it. A player's mistake, like a wrong password or a name that's taken, comes back as an error they can read, and an error status means the servers it uses couldn't answer, such as MySQL being down. The login server keeps a reference to the characters, next to the accounts and the tokens. In the login server, we bring in the characters' header, and handle login splits in two. The login that handle login did becomes authenticate, in the unnamed namespace, as nothing outside this file calls it. It takes the request, the accounts, and the reply to fill in when something's wrong. It returns the account the name and the password are for, making it first for a new account. Or it puts the reason it can't in the reply: a name that's no good, a password too short or too long, or a name that's taken. Then it returns zero, which isn't an account. For a login, it finds the account by its name, and a failure other than NotFound is the database's, which goes back as it is. A name that isn't there and a wrong password get one message, so the reply doesn't say which of the two was wrong. Readable sorts a status from the characters into the two kinds. An invalid argument, already exists, resource exhausted or NotFound is the player's to read, so its message goes in the reply. Anything else is the servers', and goes back as it is. That way, a character's mistake reaches the player, and MySQL being down reaches the log. Readable returns ok once the reply has the message. Handle login takes the characters now too, and keeps the account each request is for. A request without a session is a login: authenticate, with the name and the password, and if the database failed, that error goes back. If the name or the password was wrong, the reply already says so, and goes back as it is. Otherwise, the account is ours, and open makes a new session for it, which goes in the reply, for the client to ask with next. A request with a session gets its account from the session, and one that's run out gets an error that asks the player to log in again, without a session in the reply. The client will take that back to the login. A session that's good goes back in the reply, for the next request, and the account it's for is the one we work with. To enter, we first make sure the character is the account's: world of finds its world, by the id and the account, or says it isn't the account's. If it found one, the token is issued for the character's id, so the world loads that character. The world checks it's one of its own, as load reads it with the world's name. A token that couldn't be made is the servers' error, and goes back as it is. A character that isn't the account's is an error the player reads, and the reply still lists their characters. Create character makes one, in the world, with the name, and a refusal, such as a taken name, is an error to read. Delete character deletes one, by its id, and one that isn't the account's is an error to read too. Any other failure is the servers', and goes back as it is, as with entering. The old ending, which set ok and the token for each login, goes. Whatever the request was, even when it was refused, the account's characters come back as they are now, so the client's list is the server's, They go into the reply's characters, and the reply is ok when there's no error. The server is made with the characters, and keeps them, and where it reads each request, handle login gets them. A failure there gets a reply that says the server couldn't do that, as it isn't always a login now, and the player can try again. The log changes too. For the logins, which name the account, it says who logged in, or who made an account, as it did before. A refused login logs the name and the reason. The requests after a login carry the session in place of the name, so they aren't logged. And each ok reply brings the worlds that are up, from Consul, for the client's list, as the worlds' screen comes after the login now. In the login server's main, the characters work on the database the accounts use, and the server gets them.
25:09 The bots log in through the login server, like the client, so each bot needs a character of its own too. In the bot's main, we bring in the algorithms, for a search through the characters. The world they play in is Ember, the world on the game port they connect to, as there's a character to make in it. Ask sends a request to the login server and returns the answer, which a bot needs several times now, so it's a function of its own. It's the call log in made before, over TLS to the login port, and when the login server turns the request down, it's a permission denied error with the reply's message. So to the caller, a refusal looks different from a server that couldn't be reached. Log in, under it, does more now: it makes the bot's account the first time, and its character in Ember, named after it, and returns a token to enter as it. It keeps the bot's name, bot and its number, which the character gets too. The first login goes through ask. It makes its account if the first login is turned down, and asks again, as a new account, which comes back with a session too. Anything else that goes wrong is returned. The requests after that start from one request, which carries the session, and gets what each asks for added to it. Mine finds the bot's character among the reply's characters, by its world, with ranges find if, and gives its id, or zero when there's none in Ember. It looks through the reply's list as it is, so it works for the first reply, and for the one after a create. A bot without one makes it, the first time, from a copy of that request, asking for a character in Ember, named after the bot, and finds it in the reply's list, which has it now. Then it asks to enter as the character, and returns the token, which the rest of the bot takes to the world as before.
26:52 Now for a few tests of the login server, which bring in the characters' header. The fixture makes the characters over the test's database, and ask hands a request to handle login, with the accounts, the tokens and the characters. It expects a reply, not an error status, and returns it. Log in, which the tests had already, now sends its request through ask. Create asks, with a session, for a new character in Ember, with a name. It builds the request the client sends from its characters' screen: the session, create character, the world and the name. Each of those requests goes through ask, so a test reads like what the client does. The fixture keeps the characters next to the accounts and the tokens. The first test used to check that a new account got a token. Now a new account gets a session that account of knows, no token yet, as there's no character to enter as, and no characters. The next test logs in a new account, and makes a character with its session. Its name is the test's own, cut to twelve letters, with underscore one after it, which fits the sixteen. The create has to be ok, or the test stops with the server's message. The reply to a create lists the account's characters, which here is the one we made, with the name we gave it, in Ember, at level one, and fresh, as it hasn't entered Ember yet. Then it builds a request to enter, starting with the session, which it got from logging in. It asks to enter as that character, by its id, the way the client's enter the world button does, and sends it, and gets a token, which redeems to the character's id, as a world would redeem it. Asking to enter as a character the account doesn't have, such as one deleted elsewhere, or by another client, isn't ok. The error says the character isn't ours, and there's no token. The reply still lists the account's character, so the client's list isn't emptied. And with a session for another account, asking to enter as our character gets no token either. Then the refusals, for a new account of their own. Its characters are named after the test, cut to twelve letters, with an underscore and a digit after it. The first is made, and a second character with its name is refused, with the message for it. Three more fill the account's four, each made with the next digit, so a fifth is refused too, with the message that says so, and a two-letter name is too short, with the rule's message. Each of those refusals still brings the list back. Then it deletes the first character, whose id it reads from the list that a refused create brings back. The delete has to be ok, and its reply lists three characters now, and the name refused for a fifth is made, in the room the delete left. And a request with a session Redis doesn't have, as a session that ran out would be, isn't ok, and asks the player to log in again. The test after it checks the password: the right one logs in, and a wrong one is refused, with the error for it. A wrong one's reply comes back without a session, where it used to be without a token.
29:55 We build the tests at this step and run the login server's, against MySQL and Redis. A character is made, entered with a token for its id, and refused when it should be. They pass.
Log in, then pick
30:08 On the client, the login screen becomes three screens, one after the other: the account's login, then the worlds, then the account's characters in the world we picked, to enter as one, make one, or delete one. Until now, it showed the worlds, and then a login for the one picked, and logging in there sent us straight into that world. In its header file, build draws the screen we're on, whichever of the three it is, as the frame is drawn, as it drew the worlds or the login before. Then it returns what to ask the login server, the server that handles accounts and characters, if there's anything to ask. That's a login, or a new account, the characters again, a new or deleted character, or a token to enter as one. Each of those is a request it builds. Show takes what the server said to it: the session, the worlds and the characters, or an error to show, such as a name that's taken. A session that's run out takes it back to the login. World gives the world we picked, which the token from an enter request is for. Character name gives the name of the one we picked, for the log as we enter. Step is which of the three screens we're on, starting at the account. Each of the three gets a build function of its own, the account's in place of the login's. Three small helpers come with them, which we'll see in a moment, in the login screen: with session, here, and count in, each a few lines. They're private, as nothing outside the screen needs them. The screen starts on the account's step, and keeps the session, the worlds and the characters from the last reply, as the server sent them, the world we picked, and the character we picked, by its id. It has a flag for while we're asking whether to delete the one picked, and the new character's name as we type it, up to sixteen letters. In the login screen, build draws whichever screen we're on, each in a function of its own: the account's login, the worlds, or the characters, in a window that stays in the middle of the screen. Show puts the reply's error up, if there's one, or clears the last one. A reply without a session means the login failed, or our session ran out, so we forget the session, and it's back to the login, with what the server said. Otherwise, if we had no session yet, this was the login, so on to the worlds. We keep the session, and the worlds, when the reply is ok, with the selected world kept in the list's range, and the characters, which each reply brings. Then it works out which character to pick, among the characters in this world, which here gives. If one of the characters here has the name we just typed, and the reply is ok, it's the one we made, so we pick it and clear the name. Otherwise we keep the one we'd picked, if it's still there, or else pick the first, or none when there are none here. Character name finds the picked one's name among the characters, or gives an empty name when there's none. With session starts a request that carries the session, for anything after the login, as the password's gone by then. Here gives the characters in the world we picked, the oldest first, as pointers into the list, so nothing's copied, for the screen to list. Count in counts the account's characters in a world, with ranges count if, for the worlds' list. Build account is the login we had, without a world, and without the way back to the worlds. It has the name, the password, log in and create account, as before. Its window is called nanoMMO, and the name has the focus as it appears. The password's box hides what we type, and Enter in it counts as a press of log in. Either one sends the name and the password, and create account sends them too, as a new account. Under the buttons goes the error from the last reply, if there's one, in red. The request carries the name, the password, and whether it's a new account. Then the password is cleared, as the session stands in for it from then on. The old build login goes. It drew a login for the place we'd picked, with its name in the title and a way back to the list, and build account has taken its place. Build worlds is the list of worlds we had, with the arrow keys and Enter, and a refresh as it appears. ImGui's own keyboard navigation stays out of its window, as the arrows are ours. The world the list has picked is called the selected world now, as there's a character to pick as well. That's a rename in a few places: the arrow keys, the list, and the pick. For each world, the list shows its name and how many players are on it, as before. Each world now also says how many of our characters are there, if any. A world with other content says so, as before. Next to refresh and continue goes log out, which forgets the session and the characters, and goes back to the login, where the name we typed is still filled in. Continue, on a world with our content, keeps the world, clears any error and any delete we were asking about, and goes on to its characters, with the first of them picked, or none, when we have none there. A refresh asks with the session alone, which brings the worlds and the characters back as they are, without a login of its own. Build characters, the third screen, starts by finding the characters here, with here, as a list of pointers, and then the one we picked among them, by its id, or none, when it isn't here. Its window is called after the world, and as it appears, it asks for the characters again, so that coming back from the world shows them as MySQL has them, a few seconds behind the world. While we aren't typing a name, and there's a character picked, the arrow keys move the pick. The down arrow picks the next character, as long as there's one after it, and the up arrow the one before, and either calls off a delete we were asking about, as it's another character now. On the left, in a group of their own, under your characters, the characters are listed, each one a selectable, two hundred pixels wide, which a click picks, with its level and its land under it, set in a little. Picking another one calls off a delete we were asking about. With none here yet, it says to make one. Then a line goes under the list. Under the line goes a box for a new character's name, as wide as the list, with a hint in it, and Enter in it, or new character, asks for one with that name. That ends the left group. Next to it, on that line, a second group begins, with the character we picked, when there is one: its name, its level, and the land it's in. Under them go enter the world, which Enter does too while we aren't typing, and delete. Delete asks first, in orange, with the character's name in the question: delete it for good, or keep it, and keep calls the delete off. Worlds, under them, goes back to the list of worlds, clearing the error and any delete we were asking about. Once the window's drawn, the request is built. It's enter, with the character's id, a delete, with its id, which also ends the question. Or it's a new character, in this world that we picked, with the name we typed in the box, or, as the window appears, the characters again. Each goes with the session, and clears the error from before.
37:05 In the client's main, list worlds goes, and a function that asks the login server takes its place. Ask sends the request to the login server, at its host and port, and returns the reply, or shows that the server couldn't be reached, and returns nothing. A reply goes back whether it's ok or not, for the screen to show, as an error is something to show too. Enter took the login request, logged in with it, and connected with the token it got. Now the token comes from a reply already, so enter takes the token, and connects to the world with it. It doesn't take the login server's host any more, as it doesn't log in, and the log says which character we're entering as. The variable that held the login request we'd enter with holds a string now. What we enter with, once the loading screen is up, is that token now, not the login. Entering connects with it, behind the loading screen, as before, with the world, the transport and the token. The branch that listed the worlds goes, as the worlds come with each reply now, and so does the one that kept the login request to enter with. Now whatever the screen asks for goes to the login server. A token for the character we picked takes us into its world. We keep the token and the world's name, and the loading screen covers the window while we connect. Any other reply, the screen shows, such as the characters after a new one, or an error.
In the game
38:29 Let's see it in the game at this step. The login comes first now, and its reply brings a session, the worlds and the account's characters, so Ember can say two of ours are there before we've picked it. In Ember, the window asks again with the session, and lists arthur and gwen, with their levels and lands, from MySQL. A new name goes to the login server, which makes lancelot, and its reply lists lancelot, at level one, in the Meadows, and fresh. A delete asks first, and the reply to it lists two characters again. Entering asks for a token for arthur's id, and the world loads arthur's row, with arthur's gear and place.
A preview to turn
39:09 The character screen we open with C already draws us, idling, beside what we wear: the renderer draws the figure into a texture of its own, with a camera of its own, and ImGui shows the texture. The characters' screen gets one of those. In the login screen's header file, the preview is three hundred pixels wide and three hundred and eighty tall, taller than wide, as a person is. The constructor changes to take more. The screen takes the content's id as before, to keep worlds with other content apart, and the items as well now, to dress a new character in the kit it'll start with. Render draws the character we picked, into the preview, before the frame, as the character screen does. Figure of turns a listing into the figure the renderer draws: its id, which its looks come from, what it holds and wears, or, for a fresh one, the kit. It's static, as it needs nothing of the screen, which lets the tests call it without one. The screen keeps how far the preview's turned, and how far away its camera is, three point six metres to start. And it keeps the preview's texture, the size we gave it. In the login screen, a pixel of a drag turns the character a little over a hundredth of a radian, so a drag across the preview turns it a little over half way round. The camera comes no nearer than two point two metres, and goes no further than five and a half, thirty centimetres with each click of the wheel. Figure of copies the id, which the skin and hair come from, the name and the level into a figure, and the weapon and what it wears, as the listing has them, for a character that's played. A character that hasn't entered its world gets the kit as it does, so the preview shows it in the kit now, as it'll look on the way in. It goes through the starting stacks, skipping any the items file doesn't have, or that nobody wears, such as potions. The first item for each slot goes on, and a second one for a slot that's taken stays in the bag, in the hand or with the rest, the way the world dresses it. And it stands idle, its motion idle from start to end. On the right, above the character's name, the preview takes its place in the window, at the size we gave it. It's an invisible button, which draws nothing of its own, so ImGui tells us when the mouse is on it, or holding it. The texture is drawn on top of it, from the corner the button starts at, at the top left, to the one it ends at, at the bottom right, and flipped, as OpenGL keeps rows bottom up. While the mouse holds it, its movement across turns the character, and while it's over it, the cursor shows that it turns, and the wheel brings the camera closer or further. Under its level, a line says so. Render draws nothing unless we're on the characters' screen, with a character picked. It makes its figure with figure of, and the items, turned as far as we've turned it, and its idle as far along as the client's clock, so it breathes, and looks around, as the character screen's figure does. The camera's field of view is thirty-five degrees, as the character screen's is, in a constant, as we need it twice. The camera looks at its middle, and closer up, higher, so the top of the picture stays two metres up, and the head stays in it. It's as far away as we've zoomed, and the preview renders it. In the client's main, the login screen gets the items from the content, and while we aren't in a world, or entering one, it renders its preview before the frame is drawn.
42:33 Next, let's test how figure of dresses a character, in a new test file for the login screen, which needs no window. The test's items are parsed from text, in the items file's format, into the table the client loads. The kit starts with a sword, a shirt, a second sword and three potions. The first sword is a weapon, with its reach, its swing and its damage, that goes in the main hand. The shirt goes on the body, the potions are used, not worn, and the second sword wants the main hand too, which the first one holds already. A fresh character, with the id seven, is made into a figure with the kit. It's drawn with that id, so with the looks it gives, holding the first sword, in the shirt, and nothing else, and idle, as it stands in the preview. And one that's played, with the id eight, isn't fresh, as it has entered its world. Its listing says it holds the second sword and wears the shirt, so it's drawn in what it wears, whatever the kit has. The test goes into the client's tests, in the CMake file, next to the others.
43:39 We build the client's tests at this step, in Docker, and run the login screen's, which need no servers. A new character is drawn in its kit, and one that's played in its own things. They pass.
Turning it
43:51 Now the characters' screen at this step. The character picked is drawn into the preview's texture before each frame, idling, in what its listing says it wears. A drag on the preview's button turns the figure, as far as the mouse moves across it. Each drag adds to an angle the screen keeps, so a drag back the other way turns it back. The wheel brings the camera closer or further, between two point two metres and five and a half. Closer up, it looks higher, to keep the head in the picture. gwen's listing has a leather cap and tunic, the sword, trousers and shoes, and gwen's skin and hair come from gwen's own id. And lancelot, fresh, is drawn in the kit the world will give lancelot on the way in.
The rankings
44:32 Last, the website. Its rankings joined each player's row to the account's, for the name, and ordered the ties by the username. A character has a name of its own now, and an account can have several, so the rankings list the characters, straight from players, by their experience, then their names. It's the top hundred, with the account each belongs to, its world and its experience. The page shows each character's name, and marks the ones that belong to whoever's logged in to the website, as it marked their account before.
Wrap-up
45:03 So, today, MySQL got a row for each character, with an id and a name, up to four in a world for each account, and a migration turned the players we had into the first characters. The world plays a character by its id, and the saver updates its row. The login server answers a login with a session, which stands in for the password, and with it, lists the account's characters, makes and deletes them, and gives a token to enter a world as one. The client logs in first, then shows the worlds, then our characters in the world we pick, each drawn as it looks, which a drag turns. And the website ranks the characters, under their own names.
Playing it
46:34 Let me know in the comments what you'd like to see next, and if you want to follow along with the series, subscribe to the channel. I hope you find this useful, and thanks for watching.