"Highscore Contract" Example

Keep a highscore of a certain game with the assumption that the game name is unique

pragma solidity ^0.4.0;

contract GameHighScoreAresh{
    
    struct GameHighScore{
        string username;//the name of the user who got the highscore (unique)
        uint score; //the score acquired
        string dateOfScore; //date of highscore
        string game; //name of the game
        uint8 flag;
    }
    struct gameName
    {
        string name;
        uint highscore;
        string username;
        bool isData;
    }
    mapping (bytes32 => gameName) ListOfGames;
    mapping (bytes32 => GameHighScore) TableOfHighScore;
    function GameHighScoreAresh() public{
    }
    
    function AddUserHighScore(string _username, uint _score, string _game) public 
    
    {
        GameHighScore memory currentHighScore = GameHighScore(_username, _score, "_dateofScore", _game, 1);
        bytes32 hash = keccak256(_username, _score, "_dateofScore", _game, 1);
        TableOfHighScore[hash] = currentHighScore;
        
       //add highscore to GameHighScore
        bytes32 hash2 = keccak256(_game);
        if (_score > ListOfGames[hash2].highscore)
        {
            ListOfGames[hash2] = gameName(_game, _score, _username, true);
        }
    }

    function getHighScoreOfGame(string game) public view returns(string, uint)
    {
        bytes32 hash = keccak256(game);
        return (ListOfGames[hash].username,ListOfGames[hash].highscore);
    }
}

Updated 11/1/2017 Hash function of a struct

Comments

Popular posts from this blog

Is Making the Crypto Space Legally Compliant Paving the Road to Mass (Blockchain) Adoption?

Crypto-Currency "Coin Contract" Example

Solidity Hash of Structs (Testing hash uniqueness)