Registering High Scores - Part 1
Start course
2h 36m

In this course, we begin building a game in Solidity, and more specifically, we begin to look at defining the mechanics and the components of the game.


In the previous lesson, we actually pretty much finished our ThreeInARow game and now it's time to take care of these high scores, and those are working a little bit different than just in one smart contract because our HighscoreManager is actually just taking any commands from our GameManager. So, the whole point of going via the GameManager, creating a new game, and then giving back this ThreeInARow instance is that we know where our GameManager is located at and then can go via our GameManager back to our HighscoreManager and enter a new high score. Why do we want to do that? Now, if we are adding a new ThreeInARow game like just creating a new instance like that, then we have no real connection within the same transaction that creates this game in order to add something to the high scores. There might be other architectural solutions to that. I opted with this one so that everything is going via one single smart contract. And during starting a new game, we will take the address of the new game, register it here within the GameManager, and then allow this address of our new ThreeInARow instance to add a winner into our highscoremanager. So, at the end we will have something like a modifier that says only ThreeInARow games can call this enterWinner functionality and enterWinner will call highscoremanager and we'll get here and AddWin(address _for) that won the game within our ThreeInARow game. Now this is going to be split in a few parts because it's pretty long. By implementation, we will have a couple of coding challenges within this, and it's going to be pretty exciting because this is really the power of having multiple smart contracts working with each other. That is already very advanced stuff. So, if you got that and if you can follow along here then there is nothing that stops you out there from writing your own smart code. At least reading all of the smart contracts that are out there and also understanding exactly what they are doing. All right, I will go ahead and get started now. We will start with the actual logic between the ThreeInARow game of the GameManager and the highscoremanager and then in the next lectures, we will start actually implementing the highscoremanager because the AddWin functionality here is still empty. There is no data structure here to actually add high scores to have a top 10 list. So, let's get started. The first thing that we want to do is in our GameManager to expand this enterWinner functionality that our address, that is basically, a ThreeInARow game can enter here something. And here we also have the first challenge later on that will be expand the ThreeInARow game itself to enter a winner once the winner is known. So, when somebody won the game, I should call back to the GameManager. I will expand this here now in order to make it possible to not have somebody call enterWinner, but really trust ThreeInARow games themselves so the smart contracts themselves can enter something. For this, I will add a new mapping and this mapping will be called, let's say 'allowedToEnterHighscores maybe, or something. Nope, wrong button. So, we will have mapping and that maps  (address => bool) and this call is allowedToEnterHighscores or high score. AllowedToEnterHighscore is better because it's just one high score. And once we have this on true, then we can call the enterWinner. But in order to make this true, we have to say allowedToEnterHighscore[address ( threeInARow)]. That must be set to true. So, once our threeInARow game is created, we allow the ThreeInARow game itself to enter high scores and now the only thing we need to add is a modifier onlyInGameHighscoreEditing, and that requires (allowedToEnterHighscore[msg.sender]). Must be true or else "You are not allowed to Enter Highscore" here. And then comes the rest of the function and this one goes in here, onlyInGameEditingHighscore. And inside here we are calling the highscoremanager and we say addWin. Where is our GameManager? And .addWin(_Winner);, What did we do? Once more in a slow motion. First of all, when we are via the GameManager creating a new game, start a NewGame, then before we just emitted an event with the person who created the game plus the address of the game. Now, when the game finishes, then it should go back to the GameManager itself and call enterWinner with the address of the winner so that our highscoremanager can register a winner here in addWin, which we still have to implement that. Let me show you this. If you run this. And now you have this interface for enterWinner, but at the end of the day you don't want to have anybody. Like I can just go ahead and say enterWinner. So, we don't we don't want to do that. We just want to have our ThreeInARow games itself inside this setStone logic, which itself calls setWinner. There we want to call back to the GameManager and call this enterWinner. And in order to be able to do that, once the smart contract calls back, then the smart contract itself will be in [msg.sender]. So, the address of the smart contract that calls back the GameManager will have message sender, and this one we have to set to true once the game created. And now here's the coding challenge. Now the coding challenge is once the game finishes, then it should call back to the GameManager and call the enterWinner. If you want to do the coding challenge, then pause the video now, or else just keep watching and I'll program it in a second. All we got to do actually is in our ThreeInARow game, we already know at the beginning, in the constructor where our GameManager is located at. And we set the GameManager instance by calling GameManager with the right address. So, in our GameManager, variable, we already have the gameManager. So, all we got to do is... where is it? SetWinner. We got a call. GameManager. How is it called? EnterWinner with the (_player). All right, that's it for this lecture. And in the next one, we're actually starting to populate our Highscoremanager.


About the Author

Tom is a CTO, senior back-end developer, and systems architect with over twenty years of hands-on development experience in a variety of languages and systems. He has a CS master's degree and has been working with Ethereum and blockchain technologies since 2016.