How item dupes happen in an MMORPG, and how the server stops them, in C++. Our red-team client learns to trade, with a partner that keeps the rules, and finds two dupes in last episode's simple trading. swap offers its sword, and once the partner's ready, moves it to another slot: the trade gives the copy it made at the offer and takes nothing, so two swords become three. run says it's ready and leaves at once, before the next tick saves the bags, and comes back to its old bag, sword and all. Now the server holds what's offered where it is, checks each place before it gives anything, has a ready name the revision of the trade it's for, and saves both players of a trade at once, in one Redis transaction with MULTI and EXEC, and a player as they leave. This is episode 75 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 ep075/end.
Transcript
Intro
0:00 Leaves everywhere, and more rain on the way, so autumn's well and truly here! And today, we're going to break last episode's trade.
0:09 Hi, and welcome back to episode seventy-five, where the red-team client from episode seventy-two trades with a partner, and tries to keep the sword it gives away. Most other games call that an item dupe, short for duplicate, and our trade turns out to allow two of them, which the server stops before the episode is over. Before we fix anything, let's watch two swords turn into three.
0:33 Under the game, two copies of the red-team client, mordred and morgan, have just traded mordred's sword. Mordred moved it to another slot once morgan was ready, so morgan comes out with two swords, and mordred still has one. Two swords went in, and three came out.
0:55 The game's code is open source, and the website links to it on GitHub, with a page for each episode, where you can browse its code at each step.
Last time
1:04 Last time, our game got gold, and a way to trade it. Monsters drop a few coins each, which go in the bag's message, so they're sent and saved with the bag. Each world keeps the trades between its players, who ask each other with slash trade, and offer stacks and gold in a window. Once both are ready, the server swaps the offers between copies of both bags, all of it or none. It was the simple version, which took on trust that what was offered stayed where it was, and saved each bag with its own player.
Today
1:35 A dupe leaves an item in the game that nobody earned or looted. I think it's about the worst bug an MMO can have, as each copy makes the real ones worth a bit less, and the players who worked for theirs are the ones who lose out. Our trade gives the copy of each stack that it made at the offer, and takes as many as were offered off the place, ignoring it when there's nothing there by then. And the two bags are saved with their players at the server's next tick, which is up to fifty milliseconds after the trade. So the red-team client is going to trade, with a partner of its own that keeps the rules, and try each of those in turn. Whatever works gets its fix in this episode, as the cancel did in episode seventy-two. All right, let's get started, with the red-team client's trades.
Trading cheats
2:20 First, in the top CMake file, the game moves on to version zero point seventy-five, for this episode's code. Then the red-team client's main, in the cheat folder. Its usage at the top takes a partner after the cheat, and the list of cheats gets three that trade with that partner, who runs take. Take is the honest side of a trade, which asks the partner to trade, offers nothing, and says it's ready once they offer something. Each trading cheat needs a partner that keeps the rules, and take is that partner. Swap offers its sword from the bag, and once the partner's ready, moves it to another slot and says it's ready too, to give the sword away and keep it. And run offers its sword, says it's ready once the partner is, and leaves straight away, to find the sword still in its bag when it comes back. We bring in utility, for std move, as the connection is going to change hands, and Abseil's str cat, to put the counts into words. The enum of cheats takes take, swap and run, after cancel. Trading wraps a trade action in an input, the way Move does for a move in the bag, so it's an input that does something about a trade. Count says how many of an item the player has, in the bag and worn, in words. It takes the item by its id, as the bag's message carries each stack's item as a number, and it starts from a count of zero. It loops over the bag's slots with a structured binding, adding one for each stack of the item, and then over what's worn, the way it did the bag. A sword doesn't stack, so each stack of it is one sword. Then str cat joins the number to sword, or to swords for any other count, so each line reads like a sentence, such as mordred has one sword. Sword or swords is all it needs, as the cheats count nothing else. Slot of, which takes the bag and an item as well, goes through the bag's slots too, and gives back a slot that holds the item, as an optional, which is empty when no slot does. Then Trade, which runs the trading cheats in a loop of their own, apart from main's, as a trade has nothing to measure each second, and it returns what main returns. It takes the TLS context, the name and the password, which run needs to log in again, the cheat, the partner's name, and the connection. The connection is a unique pointer, taken by value, so Trade owns it from here on, and run can close it. They print what they do, and how many swords they have once the trade's over, or for run, once it's back. Hand is the main hand, as a place. Sword is the item the kit put there, which we learn once the bag has come, and zero until then. Then asked is when we last asked the partner, open says whether the trade has opened, stowed whether we've sent the sword to the bag, and offered is the slot we offered it from, if we have. Moved is when swap moved the sword, if it has, and ready says whether we've said we're ready. The loop polls the connection, sleeps five milliseconds, and takes the bag as the server last sent it, with in hand, the main hand's slot as a number, the way the worn map keys it. Until we know the sword, we wait for the bag, and once the main hand has something in it, that's our sword, and we print how many swords we have, which is the first count in the terminal. Then the trade, as the server last sent it. While it isn't with our partner, either it hasn't opened yet, or it was open and it's over now. If it was open, the server has sent a trade with nobody in it, and the bag that comes with the trade's end can come a tick after that. So we keep polling for half a second, to count the bag as it is after the trade. Then we print that the trade's over, with how many swords we have, and return zero. If it hasn't opened, the partner may not be in the world yet, and the server refuses an ask for someone who isn't there, so we ask again each second. The ask names the partner, and as asked starts at the clock's very beginning, the first ask goes out at once, and the next ones a second apart, until the partner asks back. Past that, the trade is open, and the three cheats go their own ways. Take waits for something on the partner's side, and once there is, it sends a ready, remembers it, and prints that the partner offers a sword, so it's ready. Swap and run offer the sword from the bag, as an offer is a list of places in the bag, so first, it has to go there. Until it's stowed, we move it from the hand to the first free slot. Once the bag the server sends back has it in a slot, the else if declares that slot as its condition, which holds while the optional has a value. Then we offer it, keep it as offered, and print where it's offered from. That's slot one, as the kit's potions take slot zero. From there, both of them wait, going round the loop, until the partner's side of the trade says it's ready. Then we build our own ready, for swap and run to send when they choose. This is where swap cheats, once the partner's ready. The first time round, it makes two places, from the slot it offered, to the first free slot of the bag as it is now, and sends a move between them. It notes when, and prints that the partner's ready, so it moves the sword, and where to. Once the bag the server sends back has nothing at the offered slot, the sword has gone, so it says it's ready too, and prints that, which is the moment the trade goes through. And if a second later, the sword's still there, the server didn't move it, so swap says it's ready anyway, with the sword where it was, and prints that. That way, swap gets to the end of its trade against a server that moves the sword, and against one that refuses to. Then run, which says it's ready too, with the whole idea written above it: ready, and gone before the server's next tick saves the bags. So straight after the ready goes out, it resets the connection, which closes it, and the player leaves the game. It prints that the partner's ready, so it's ready too, and leaves at once. A second later, it comes back, through Enter, the function the red-team client logs in with. It made the account the first time round, and this time logs in to it, for a new token. Without a token, it prints that it couldn't come back, with the error, and returns one. Back is a new connection to the world server, with that token, and false for UDP, so the worlds come over TCP, as before. If that fails, it prints the error, with cheat in front of it, and returns one too. Then it polls until the bag has come, which the kit's potions keep from being empty, and prints that it's back, and how many swords it has, which is the line we're after. And if the server closes the connection before any of that, it says so and returns zero, like the other cheats. In main, the usage gets a second line, for the trades, and a partner after the cheat is fine now. The chain of ifs turns take, swap and run into the enum, and trades says whether the cheat is one of those three. A trade needs a partner, and nothing else takes one, so either without the other prints the usage and returns two, the code for a command used wrong. Once it's in, and has printed which cheat it's trying, a trading cheat hands over to Trade, and main returns what Trade returns, with the partner from the fifth argument. Std move and the star take the unique pointer out of the status or, as a value to move from, so Trade gets the connection.
Swapping the sword
9:35 Back in the game at the first step, with arthur a little way south of where new players come in. In the first terminal, we run the red-team client as mordred, with swap, and morgan as its partner, and in the second, as morgan, with take, and mordred as its partner. Each makes its account the first time, comes in where new players start, and says it has one sword, the one the kit gives each new player. Mordred stows its sword in the bag, and offers it from slot one. As soon as morgan sees a sword offered, it says it's ready, which is what mordred has been waiting for, so it moves the sword to slot two, and says it's ready too. Morgan comes out of the trade with two swords, and mordred still has one, so the trade made a sword out of nothing. The trade gave morgan the copy of the sword that it made at the offer, and tried to take the sword off slot one, which was empty by then. That failed, and the simple version ignored the failure, so mordred kept the sword in slot two. It took surprisingly little code, which is a bit worrying!
10:55 Now from a player's side, at the first step, with mordred running swap again, and arthur as its partner. Slash trade and mordred's name ask it to trade, and as mordred has been asking arthur once a second, that opens the trade, with the bag beside it. Mordred stows its sword and offers it, so it shows on mordred's side, as the copy the server made at the offer. A ready from arthur's side is all mordred waits for. It moves the sword to another slot, says it's ready too, and the server swaps the offers. So the sword is in arthur's bag, and nothing on arthur's screen says that mordred kept its sword as well, as it did against morgan a moment ago.
Trading and leaving
11:46 Now the second cheat, against the server at the first step. In the first terminal, gawain runs the run cheat, with percival as its partner, and in the second, percival runs take. Gawain stows its sword and offers it, and once percival is ready for it, gawain says it's ready too, and drops its connection straight after. The trade happens as gawain's ready arrives, and a second later, gawain logs back in. Percival comes out of the trade with two swords, and gawain is back with one, so that's three swords from two again. The server saves the players who've changed once a tick, and gawain was gone before the next one. Nothing saved a player who'd left, so gawain came back to the bag Redis had from before the trade, sword and all, while percival's bag was saved with the sword at the next tick.
Holding what's offered
12:45 So, to stop swap, the world is going to hold each place a player offers from, so that nothing there gets moved, used, worn or thrown away until the trade's over. Then, before the trade gives anything, it checks that each place still has what was offered, for anything else that changes a bag. And while we're at it, each change to an offer moves the trade on to its next revision, which a ready has to name, as an offer can change while a ready is on its way to the server. I went with holding the places, and kept the check as well. Our client already keeps what's offered in place, so this is for a client that doesn't. Its move is refused as it's made, with a line saying why, where the check alone would fail the trade at the end, and the honest partner wouldn't know why.
13:29 We start in our proto file, as the client and the server have to agree on which state of a trade a ready is for. A trade action gets a revision, as field five, outside the oneof, as it goes along with a ready. It's the revision of the trade as the server last sent it, and a ready for an earlier one does nothing, as it's for offers that have changed since. And the Trade message gets its revision, as field four, which goes up with each change to either offer, and which a ready names. In the trades' header file, the paragraph about the simple version goes, and in its place, we write how it works now: nothing a client says is trusted. What's offered is held where it is until the trade's over, and each change to an offer takes the trade to its next revision, which a ready has to name. And the trade gives nothing unless each place still has what was offered. Revision of gives the revision of a player's trade, which starts at zero as it opens, and goes up by one with each change to either offer. Holds says whether a place is one the player offers from, and that matters, as an offered place stays as it is until the end, with what was offered taken from there. The world is going to ask it before each move, use or throw in the bag. After an offer, the trade is at its next revision, so a ready for the offers before it won't count. Ready takes the revision the player saw now, and returns whether both sides are ready, or the error if they aren't trading, or if they're ready for offers that have changed since. So a ready that names an older revision gets that error, and changes nothing. Then Complete, where each place offered has to have at least as many as it did, of the item that was offered, with its rolls, which is the check we're about to write. If one hasn't, a bag hasn't room, or a side hasn't the gold it offered, neither side is ready any more, and the error says why, in words for both players. Gold that would take a bag past the most it carries fails it too, with a line of its own. In private, a side keeps the trade's revision, which both sides keep alike, as a field that starts at zero.
15:33 In trades, we bring in protobuf's message differencer, which compares two messages field by field. In an unnamed namespace goes a helper that says whether what's at a place is the item that was offered, with its rolls, however many of it there are. With nothing at the place, the answer is no. Otherwise, it copies what's there and gives the copy the offered count, and the differencer says whether the two are equal, rolls and all. Setting the count first is what lets a stack that's grown still count as the one offered. Revision of finds the player's side, and gives its revision, or zero without one. Holds is true when the player has a side, the place is in the bag, and their offer has a stack at that slot. A worn place can't be offered, so it isn't held. In Offer, a new offer moves the trade on to its next revision, on both sides, as a ready sent for what was there before is for the revision before. Ready refuses a ready for any revision but the trade's, as a failed precondition, which tells the player the trade has changed, so look at it again. An offer can change while a ready is on its way, and the ready is for the offers the player saw. Taking a ready back needs no revision, as being less ready can't cost anyone anything. And in Complete, the loop where each side gives up what it offered gets its check: each place has to have the item still, and at least as many as were offered, or Take fails. It's at least as many, as a kill can add to a stack that's offered, and what it adds stays the player's. So each place offered has to hold the item that was offered, with its rolls, and Take has to work, which needs at least as many as were offered. If either fails, the trade is refused, for both sides, with a line that says the items offered aren't all there any more.
17:23 In the world's header file, nothing goes to or from a place that's been offered, and move item, destroy item and use item each get a line about it. Ready to trade takes the revision of the trade the player saw, and the trade that trade of gives the client carries its revision as well. Then in the world, which decides any move in the bag, the line a held place gets is a string view constant, which says you can't, while it's offered in your trade. What's offered stays where it is until the trade's over, as the trade takes it from there. Move item refuses a move from a held place, or to one, so nothing can take the offered sword's slot either. Wearing is a move from the bag to what you wear, so this covers it too. Destroy item refuses to throw away what's at a held place, and use item refuses to use it, such as a potion that's offered. Each of the three returns a failed precondition with that line, which the server sends the player as a line from nobody, as it does any refusal of an item. Ready to trade passes the revision on to the trades, and trade of puts the trade's revision into what each client gets. In the server, a ready hands the action's revision on to the world. On the client, the trade window's ready carries the revision of the trade it's drawing, so an honest client is ready for what's on its screen. And the red-team client sends the revision with its readies too, in take, and in swap and run, as a ready without it does nothing now.
18:48 Now, let's pin all of that down with a few quick tests, in the trades' tests. The test gear's blade is item five, and Blade makes one that rolled that much damage. It starts from a stack of the blade, adds a roll to it, and sets the roll's stat to damage and its amount to what it's given, so a test can tell a better blade from a worse one. Then, in the tests we already had, each ready names the revision its player saw now: one after a single offer, and two after two. The first new test is swap's trick, on the trades alone, that an item moved after it's offered isn't given twice. alice has a blade that rolled four damage in her first slot, and one that rolled two in her second, and she offers the first. Then her better blade moves to slot five, straight through her inventory, as it would if the world let it, and both of them say they're ready. Complete is refused, with the line that says the items offered aren't all there any more. Bob's bag stays empty, and the blade in slot five still rolled four, so she kept the better one. Neither side is ready any more, as a trade that fails takes both readies back. Then her worse blade goes into the slot the better one was offered from, and they're both ready again. Revision one is still the trade's, as nobody's offer changed, so both readies count. Complete is refused again, though, as it's a blade with other rolls, so bob doesn't get a worse blade for the one he was shown. Next, that a ready for an older offer does nothing. alice has a blade and a bone, and the trade opens at revision zero. This one's the classic bait and switch, written as a test. Her offer of the blade takes it to one. Then she offers the bone in its place, before bob's ready for the blade arrives, so his ready, for revision one, is refused, as the trade has changed, and he isn't ready. So the bone can't go across on a ready that was meant for the blade. Once he's seen the bone, at revision two, he can be ready for it, and taking his ready back works at any revision, zero too. In the trade through a world, bob's trade is at revision one after alice's offer, which both readies name. Last, that what's offered stays where it is until the trade's over, through a world. It's a world on an empty terrain, with the test gear, where alice joins in the middle and bob two metres along, both with the kit: a sword in hand, and three potions in the first slot. Hand is the main hand, as a place, from the places helper Worn. alice moves her sword to her bag's second slot, which the world allows, as there's no trade yet. Then they ask each other, which opens one, and she offers the sword, each step with assert true, as each has to work. From here on, the sword's slot is held. Held is the line a held place gets, which each refusal has to come back with, word for word. Now she can't wear the sword, which is a move from slot one back to her hand, or move it on to slot five. She can't use it, or throw it away, and she can't move her potions into its slot either. Each of those five comes back with that line. One of the potions she didn't offer moves to slot five, which shows it's moves at the held slot that are refused, and not any move at all. And once bob calls the trade off, the place isn't held any more, so she can wear the sword again. In the server's test of two players trading over the network, the run until that waited for bob's trade to show her potions now waits until alice's own trade shows them too. That way, her ready names the revision her offer made, as she's seen it. And once both are ready, for the offers they've seen, the potions go across. Then the ready's revision comes from alice's trade before she sends it, and from bob's before he sends his, once he sees that she's ready.
22:30 We build at this step, and run the trades' tests without the databases, as none of them needs MySQL or Redis, and the ones through a world run it in the test process. The build takes a little while, as the server's code has changed. A blade moved after it's offered isn't given, what's offered stays where it is, and all ten pass.
22:50 We run swap again, against the server at the second step, where an offered place is held. Mordred runs swap, with morgan as its partner, and morgan runs take, as before. Mordred offers its sword from slot one, and once morgan's ready, it prints that it moves the sword to slot two, as that's what it asks the server to do. The server refuses, as slot one is held for the trade, and tells mordred why, in a line the red-team client doesn't print. A second later, the sword's still in slot one, so mordred says it's ready with it there, and the trade goes through as offered. Morgan has two swords, and mordred has none, so two swords stayed two.
23:40 And from arthur's side again, at the second step, with mordred running swap. Slash trade and its name open the trade, as mordred has been asking, and mordred offers its sword. Once arthur's side is ready, mordred asks the server to move the sword, which it refuses, so a second later, mordred says it's ready with the sword where it was. On arthur's screen, the trade looks just like the last one, with a sword in arthur's bag at the end. Against morgan a moment ago, at this step's code, mordred came out of that trade without a sword, as the server held the slot in place.
Saving a trade as one
24:21 Holding the places does nothing for run, which still comes back with its sword at this step, as it's about when the bags are saved. A trade happens as the second ready arrives, in the handler of its message, between two ticks. The bags were saved at the next tick, with whoever else had changed, and a player who'd gone by then wasn't saved again, as nothing saved a player once they'd left. So now, a trade that's done saves both players there and then, before either of them hears of it, and a player who leaves is saved as they go. And the save goes to Redis as one transaction, so Redis keeps both bags or neither. I care most about that last part, as two bags saved one after the other can be cut short in between, by a crash or a lost connection. Then one bag has the sword while the other still has it too.
25:13 First, our Redis class gets transactions, in its header file. Transaction runs the commands between MULTI and EXEC, in a single round trip. Redis runs them together, one after the other, without anyone else's commands in between, or none of them if it couldn't take one, and it fails if any of them failed. Pipeline, just above it, sends its commands in one round trip too, but Redis runs each of a pipeline's commands on its own, so a connection lost halfway leaves some of them done. In our Redis class's code, Transaction starts like the others, reconnecting if the connection was lost, and returning the error if that fails. Then it appends MULTI, which starts a transaction, each of the commands, which Redis queues, and EXEC, which runs what's queued, so all of it goes out in one write. Then the replies, which come back as OK for MULTI, and QUEUED for each command, or an error for one Redis wouldn't queue, say a command it doesn't know, which makes EXEC run none of them. So we read one reply more than there are commands, for MULTI's. A failed read returns its error, and a reply that's an error goes into error, with Update, which keeps the first one. Then we read EXEC's reply, before returning any error. That way, all the replies are read, so the next command on this connection gets its own reply, and not one meant for this transaction. Once it's run them, EXEC's reply is a list of the commands' replies, one for each command, in the order they were queued. Redis doesn't undo the others when one fails as it runs, say an H set on a key that holds text, so for us, that's an error too. A reply that isn't a list as long as the commands means Redis didn't run the transaction, and any of its elements that's an error fails it, with Redis's own words in the message. I'd much prefer a save that fails loudly here, as a save that failed is tried again at the next tick.
27:04 In the players' header file, Save is one transaction now, so Redis keeps all of it or none, and the two bags a trade changed aren't saved one without the other. In players, Save calls Transaction where it called Pipeline, so the tick's save is a transaction too. In the world's header file, we bring in Abseil's status or, for what ready to trade returns now. Once both are ready, within trade reach, they trade there and then, and ready to trade returns who they traded with, or zero until then, or the error says why not. In the world, ready to trade returns a status or of a number. A refusal from the trades still returns its status, and a ready that doesn't make both of them ready returns zero, as there's no trade yet. At the end, a trade that's done returns the other player's id, where it returned an OK status. In the server's header file, the save that runs once a tick is for everyone who's changed since they were last saved, once a tick. And Save saves those of the players it's given who've changed, into Redis, as one. That's everyone once a tick, the two sides of a trade as it's done, and a player as they leave. In the server, Leave saves the player first, unless the server is handing its players over. Nothing saves them once they're gone, so what's changed since the last tick goes now. The next instance has loaded them as they were saved when the handover began, which this mustn't write over. In Trade, the ready case gets a block of its own, as it declares a variable now, traded, what the world returns, and done is its status. When it names a player, with value or giving zero for an error, they've just traded, so both of them are saved there and then. The reason goes above it: both bags go into Redis together, as they trade, before either player hears of it, so that a crash or a leave can't keep one and lose the other. Save changed now gathers the players in all the copies, and saves them, timed for the metrics as before. Save has the body that save changed had, over the players it's given, finding each one's world with world of. So for each player, it works out their progress and compares it with what was saved last, adding their bag when its revision has moved. Then it all goes to the players' Save, as one transaction, and what was saved is remembered, or the error is printed.
29:21 Next, let's test the save, starting in the server's CMake file, where a test file for Redis joins the server's tests, in the list next to the players'. The new test file brings in the Redis header first, then memory and string, Abseil's status, gtest, and the test services, which find the databases the tests run against. A fixture connects to the test Redis in set up, from test Redis, which reads the Redis host's setting, and set up skips the test, with a message, when there isn't one, as on a machine without Redis. Tear down deletes the two keys a test uses, a hash, and one that holds text, which get random names from test name, so two runs at once don't share a key. The first test checks that a transaction Redis can't take runs none of it. A transaction of an H set of five gold and a command that doesn't exist fails. Getting the gold back afterwards finds nothing, as an H get of a field that isn't there comes back as not found, so the H set didn't run either. A ping gets its pong, which shows that all the replies were read. And that H set on its own goes through, and the gold's there, so we know it was the bad command that failed the first one. The second test is the other way Redis fails a transaction. The text key gets some words, with a plain set, which makes it a string, so an H set on it is a command of the wrong type for that key. Redis can't tell that as it queues the command, and finds out when it runs it. So a transaction of an H set on the hash and one on the text key fails, as Redis runs both, and the second fails on a key that holds text. The hash's gold is still there, as Redis doesn't undo it, which is why Transaction reports the failure, and the next command still gets its pong. And in the server's tests, the test player gets send and leave, which frames the input, and writes it, with a lambda that closes the connection once the write is done. That's the way a client that quits straight after it would leave, before the server's next tick. The lambda keeps the bytes alive until then, through the shared pointer it captures, and captures this too, to call Leave, which closes the socket the test player connected on. Then run's trick, as a test that a trade and a leave leave one sword. It needs the fixture's databases, as the leave has to go through Redis for real. Above the test's content, we note that each player joins with the kit: a sword in hand, and three potions. The content is the test gear, as in the trading test, where the sword is item one. Then it makes a server of its own with that content, on a port of its own, with fifty-millisecond ticks and the fixture's databases, and asserts that it started. It saves through the fixture's Redis, like the fixture's own server, which is what this test is about. alice and bob connect to it, each with a token for their account, so they join the world it runs. Swords is a lambda that counts the swords in a bag and worn, by the sword's item, one, the way Count does in the red-team client. It's a lambda without captures, as it's handed the bag it counts. The count starts at zero, and each stack whose item is one adds one, first in the bag's slots, and then in what's worn, which is where each sword starts, and then it returns the count. We run the event loop until each of them has one, which also means that both of them are in the world. Nothing's been traded yet, so that's two swords between them. Then an input with an item move, built with the mutable accessors, takes alice's sword from her main hand to her bag's second slot. She sends it, and we run until her bag has something in slot one. Next, an input whose trade asks bob, by his name, which alice sends, and then that input asking alice by hers, which bob sends. So they've asked each other, and we run until both of them have a trade from the server, which means it's open. She offers the sword from slot one, in an input whose trade's offer has that slot, and we run until bob's trade shows slot one on her side. Then the ready, an input whose trade says ready, with the revision from bob's trade, which bob sends, so bob is ready for the sword he's been shown. We run until alice's trade shows him ready, then set the ready's revision from her trade, and she sends it with send and leave, so her connection closes as soon as it's written. We run until the market has one player left, which is bob, and bob's bag has two swords, his own and the one she gave him. Then alice comes back, as a new test player with a token for her account, and we run until her bag has a revision past zero, which means it's come. The swords in her bag and in bob's have to add up to two. At the step before, run's dupe made it three. Last, the trades' test through a world, where what ready to trade returns has changed. alice's ready has to return zero, as bob isn't ready yet, and value or gives one for an error, so an error fails the check too. And bob's has to return alice's id, as he's ready last, and they trade. And the check at its end reads the message from the status now, as ready to trade returns a status or.
34:27 We run the Redis tests and the new server test at this step. They need Redis, and alice and bob have real accounts and tokens, so this time Compose starts MySQL and Redis first, with the migrations. Compose builds the tests first, at this step's code. The two ways Redis can fail a transaction are caught, and a trade and a leave leave one sword, so all three pass.
34:52 Now run gets its second go, against the server at the third step, which saves a trade as it's done. Gawain runs the run cheat with percival, as before, and percival runs take. Gawain offers its sword, and once percival is ready, it says it's ready too and leaves at once. This time, the trade saves both players in one transaction, as gawain's ready arrives, before either of them hears of it, and when gawain leaves, there's nothing new left to save. Gawain comes back without its sword, and percival has two, so the trade moved one sword and made none. That's both dupes gone, which feels pretty good!
Writing it down
35:38 Last, the game's docs, starting with the networking page, where the table of what a modified client can try gets swap. It gets nothing from this version on, as a place that's offered is held, and the trade gives nothing unless each place still has what was offered. And run gets nothing either, as a trade saves both bags together as it's done, and a player who leaves is saved as they go. Before this version, each of them made three swords from two. On the gameplay page, the paragraph that said what the simple version trusted is a list of what the server checks now: the held places, the revisions, the check before it gives anything, and the save of both players. And the saving page gets a paragraph on why a trade saves both players at once, in one transaction, and a player who leaves as they go. This choice gets a decision record too, number twenty-eight. A trade is checked against the bags, and saved as one. Its context is the simple version, and the two ways the red-team client found to keep a sword it gave. The decision is the held places, the check before anything's given, the revisions, both players saved as they trade and a leaver as they go, and each save one transaction. The first alternative is checking the bags at the end alone, which stops swap too, but a trade spoilt by a move would fail at the end, for a reason nobody saw at the time. The obvious one, which we didn't try, is taking what's offered out of the bag and into the trade. Nothing could move it then, but each way a trade ends would have to give it back, and a crash in between would lose it, so I prefer holding it where it is. Saving both bags in one pipeline is one round trip, but a lost connection can save one bag and not the other. And saving a trade in MySQL, as guilds are, would have the world's thread wait for MySQL at each trade. What it costs us is a Redis round trip in the handler of each trade's second ready, on the world's thread, much like a guild change costs a MySQL one. Last, the record says what's still trusted, and why. Two stacks of one item with matching rolls and counts can't be told apart, so a copy looks like the one it was made from. Serials on items, a lease on each player, and an audit trail of trades are left for later. It goes in the index too, as the twenty-eighth row.
Wrap-up
37:52 Today, the red-team client learnt to trade, with a partner that keeps the rules. Swap moved its sword once its partner was ready, and run left the moment it traded, and each turned two swords into three. Now, a place that's offered is held until the trade's over, the trade checks each place still has what was offered before it gives anything, and a ready names the revision of the trade it's for. A trade saves both players at once, in one Redis transaction, and a player who leaves is saved as they go. And decision record twenty-eight says why we did it this way.
Playing it
39:17 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.