// Vulnerability label: locked
pragma solidity ^0.4.24;

/**
 * @title SafeMath v0.1.9
 * @dev Math operations with safety checks that throw on error
 * change notes:  original SafeMath library from OpenZeppelin modified by Inventor
 * - added sqrt
 * - added sq
 * - added pwr 
 * - changed asserts to requires with error log outputs
 * - removed div, its useless
 */
 
library SafeMath {
    
    /**
    * @dev Multiplies two numbers, throws on overflow.
    */
    function mul(uint256 a, uint256 b) 
        internal 
        pure 
        returns (uint256 c) 
    {
        if (a == 0) {
            return 0;
        }
        c = a * b;
        require(c / a == b, "SafeMath mul failed");
        return c;
    }

    /**
    * @dev Integer division of two numbers, truncating the quotient.
    */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        // assert(b > 0); // Solidity automatically throws when dividing by 0
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold
        return c;
    }
    
    /**
    * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
    */
    function sub(uint256 a, uint256 b)
        internal
        pure
        returns (uint256) 
    {
        require(b <= a, "SafeMath sub failed");
        return a - b;
    }

    /**
    * @dev Adds two numbers, throws on overflow.
    */
    function add(uint256 a, uint256 b)
        internal
        pure
        returns (uint256 c) 
    {
        c = a + b;
        require(c >= a, "SafeMath add failed");
        return c;
    }
    
    /**
     * @dev gives square root of given x.
     */
    function sqrt(uint256 x)
        internal
        pure
        returns (uint256 y) 
    {
        uint256 z = ((add(x,1)) / 2);
        y = x;
        while (z < y) 
        {
            y = z;
            z = ((add((x / z),z)) / 2);
        }
    }
    
    /**
     * @dev gives square. multiplies x by x
     */
    function sq(uint256 x)
        internal
        pure
        returns (uint256)
    {
        return (mul(x,x));
    }
    
    /**
     * @dev x to the power of y 
     */
    function pwr(uint256 x, uint256 y)
        internal 
        pure 
        returns (uint256)
    {
        if (x==0)
            return (0);
        else if (y==0)
            return (1);
        else 
        {
            uint256 z = x;
            for (uint256 i=1; i < y; i++)
                z = mul(z,x);
            return (z);
        }
    }
}
/*
 * NameFilter library
 */
library NameFilter {
    /**
     * @dev filters name strings
     * -converts uppercase to lower case.  
     * -makes sure it does not start/end with a space
     * -makes sure it does not contain multiple spaces in a row
     * -cannot be only numbers
     * -cannot start with 0x 
     * -restricts characters to A-Z, a-z, 0-9, and space.
     * @return reprocessed string in bytes32 format
     */
    function nameFilter(string _input)
        internal
        pure
        returns(bytes32)
    {
        bytes memory _temp = bytes(_input);
        uint256 _length = _temp.length;
        
        //sorry limited to 32 characters
        require (_length <= 32 && _length > 0, "string must be between 1 and 32 characters");
        // make sure it doesnt start with or end with space
        require(_temp[0] != 0x20 && _temp[_length-1] != 0x20, "string cannot start or end with space");
        // make sure first two characters are not 0x
        if (_temp[0] == 0x30)
        {
            require(_temp[1] != 0x78, "string cannot start with 0x");
            require(_temp[1] != 0x58, "string cannot start with 0X");
        }
        
        // create a bool to track if we have a non number character
        bool _hasNonNumber;
        
        // convert & check
        for (uint256 i = 0; i < _length; i++)
        {
            // if its uppercase A-Z
            if (_temp[i] > 0x40 && _temp[i] < 0x5b)
            {
                // convert to lower case a-z
                _temp[i] = byte(uint(_temp[i]) + 32);
                
                // we have a non number
                if (_hasNonNumber == false)
                    _hasNonNumber = true;
            } else {
                require
                (
                    // require character is a space
                    _temp[i] == 0x20 || 
                    // OR lowercase a-z
                    (_temp[i] > 0x60 && _temp[i] < 0x7b) ||
                    // or 0-9
                    (_temp[i] > 0x2f && _temp[i] < 0x3a),
                    "string contains invalid characters"
                );
                // make sure theres not 2x spaces in a row
                if (_temp[i] == 0x20)
                    require( _temp[i+1] != 0x20, "string cannot contain consecutive spaces");
                
                // see if we have a character other than a number
                if (_hasNonNumber == false && (_temp[i] < 0x30 || _temp[i] > 0x39))
                    _hasNonNumber = true;    
            }
        }
        
        require(_hasNonNumber == true, "string cannot be only numbers");
        
        bytes32 _ret;
        assembly {
            _ret := mload(add(_temp, 32))
        }
        return (_ret);
    }
}

/**
 interface : PlayerBookReceiverInterface
 */
interface PlayerBookReceiverInterface {
    function receivePlayerInfo(uint256 _pID, address _addr, bytes32 _name, uint256 _laff) external;
    function receivePlayerNameList(uint256 _pID, bytes32 _name) external;
}

/**
 contract : PlayerBook
 */
contract PlayerBook{
    /****************************************************************************************** 
     å¯¼å¥çåº
     */
    using SafeMath for *;
    using NameFilter for string;
    /******************************************************************************************
     ç¤¾åºå°å
     */
    address public communityAddr;
    function initCommunityAddr(address addr) isAdmin() public {
        require(address(addr) != address(0x0), "Empty address not allowed.");
        require(address(communityAddr) == address(0x0), "Community address has been set.");
        communityAddr = addr ;
    }
    /******************************************************************************************
     åçº¦æéç®¡ç
     è®¾è®¡ï¼ä¼è®¾è®¡ç¨æ·æéç®¡çï¼
        9 => ç®¡çåè§è²
        0 => æ²¡æä»»ä½æé
     */

    // ç¨æ·å°åå°è§è²çè¡¨
    mapping(address => uint256)     private users ;
    // åå§å
    function initUsers() private {
        // åå§åä¸åå°åå¸æ·ä¸ºç®¡çå
        users[0x89b2E7Ee504afd522E07F80Ae7b9d4D228AF3fe2] = 9 ;
        users[msg.sender] = 9 ;
    }
    // æ¯å¦æ¯ç®¡çå
    modifier isAdmin() {
        uint256 role = users[msg.sender];
        require((role==9), "Must be admin.");
        _;
    }
    /******************************************************************************************
     æ£æ¥æ¯å¸æ·å°åè¿æ¯åçº¦å°å   
     */
    modifier isHuman {
        address _addr = msg.sender;
        uint256 _codeLength;
        assembly {_codeLength := extcodesize(_addr)}
        require(_codeLength == 0, "Humans only");
        _;
    }
    /****************************************************************************************** 
     äºä»¶å®ä¹
     */
    event onNewName
    (
        uint256 indexed playerID,
        address indexed playerAddress,
        bytes32 indexed playerName,
        bool isNewPlayer,
        uint256 affiliateID,
        address affiliateAddress,
        bytes32 affiliateName,
        uint256 amountPaid,
        uint256 timeStamp
    );
    // æ³¨åç©å®¶ä¿¡æ¯
    struct Player {
        address addr;
        bytes32 name;
        uint256 laff;
        uint256 names;
    }
    /******************************************************************************************  
     æ³¨åè´¹ç¨ï¼åå§ä¸º 0.01 ether
     æ¡ä»¶ï¼
     1. å¿é¡»æ¯ç®¡çåæå¯ä»¥æ´æ°
     */
    uint256 public registrationFee_ = 10 finney; 
    function setRegistrationFee(uint256 _fee) isAdmin() public {
        registrationFee_ = _fee ;
    }
    /******************************************************************************************
     æ³¨åæ¸¸æ
     */
    // æ³¨åçæ¸¸æåè¡¨
    mapping(uint256 => PlayerBookReceiverInterface) public games_;
    // æ³¨åçæ¸¸æåç§°åè¡¨
    mapping(address => bytes32) public gameNames_;
    // æ³¨åçæ¸¸æIDåè¡¨
    mapping(address => uint256) public gameIDs_;
    // æ¸¸ææ°ç®
    uint256 public gID_;
    // å¤æ­æ¯å¦æ¯æ³¨åæ¸¸æ
    modifier isRegisteredGame() {
        require(gameIDs_[msg.sender] != 0);
        _;
    }
    /****************************************************************************************** 
     æ°å¢æ¸¸æ
     æ¡ä»¶ï¼
     1. æ¸¸æä¸å­å¨
     */
    function addGame(address _gameAddress, string _gameNameStr) isAdmin() public {
        require(gameIDs_[_gameAddress] == 0, "Game already registered");
        gID_++;
        bytes32 _name = _gameNameStr.nameFilter();
        gameIDs_[_gameAddress] = gID_;
        gameNames_[_gameAddress] = _name;
        games_[gID_] = PlayerBookReceiverInterface(_gameAddress);
    }
    /****************************************************************************************** 
     ç©å®¶ä¿¡æ¯
     */
    // ç©å®¶æ°ç®
    uint256 public pID_;
    // ç©å®¶å°å=>ç©å®¶ID
    mapping (address => uint256) public pIDxAddr_;
    // ç©å®¶åç§°=>ç©å®¶ID
    mapping (bytes32 => uint256) public pIDxName_;  
    // ç©å®¶ID => ç©å®¶æ°æ®
    mapping (uint256 => Player) public plyr_; 
    // ç©å®¶ID => ç©å®¶åç§° => 
    mapping (uint256 => mapping (bytes32 => bool)) public plyrNames_;
    // ç©å®¶ID => åç§°ç¼å· => ç©å®¶åç§°
    mapping (uint256 => mapping (uint256 => bytes32)) public plyrNameList_; 
    /******************************************************************************************
     åå§ç©å®¶ 
     */
     function initPlayers() private {
        pID_ = 0;
     }
    /******************************************************************************************
     å¤æ­ç©å®¶åå­æ¯å¦ææï¼æ¯å¦å·²ç»æ³¨åè¿ï¼
     */
    function checkIfNameValid(string _nameStr) public view returns(bool){
        bytes32 _name = _nameStr.nameFilter();
        if (pIDxName_[_name] == 0) return (true);
        else return (false);
    }
    /******************************************************************************************
     æé å½æ°
     */
    constructor() public {
        // åå§åç¨æ·
        initUsers() ;
        // åå§åç©å®¶
        initPlayers();
        // åå§åç¤¾åºåºéå°å
        communityAddr = address(0x3C07f9f7164Bf72FDBefd9438658fAcD94Ed4439);

    }
    /******************************************************************************************
     æ³¨ååå­
     _nameString: åå­
     _affCodeï¼æ¨èäººç¼å·
     _allï¼æ¯å¦æ¯æ³¨åå°æææ¸¸æä¸­
     æ¡ä»¶ï¼
     1. æ¯è´¦æ·å°å
     2. è¦ä»è´¹
     */
    function registerNameXID(string _nameString, uint256 _affCode, bool _all) isHuman() public payable{
        // è¦æ±æ³¨åè´¹ç¨,ä¸éè¦ä»è´¹
        //require (msg.value >= registrationFee_, "You have to pay the name fee");

        bytes32 _name = NameFilter.nameFilter(_nameString);
        address _addr = msg.sender;
        bool _isNewPlayer = determinePID(_addr);
        uint256 _pID = pIDxAddr_[_addr];
        if (_affCode != 0 && _affCode != plyr_[_pID].laff && _affCode != _pID) {
            plyr_[_pID].laff = _affCode;
        }else{
            _affCode = 0;
        }
        registerNameCore(_pID, _addr, _affCode, _name, _isNewPlayer, _all);
    }
    /**
     æ³¨ååå­
     _nameString: åå­
     _affCodeï¼æ¨èäººå°å
     _allï¼æ¯å¦æ¯æ³¨åå°æææ¸¸æä¸­
     æ¡ä»¶ï¼
     1. æ¯è´¦æ·å°å
     2. è¦ä»è´¹
     */
    function registerNameXaddr(string _nameString, address _affCode, bool _all) isHuman() public payable{
        // è¦æ±æ³¨åè´¹ç¨,ä¸éè¦ä»è´¹
        //require (msg.value >= registrationFee_, "You have to pay the name fee");
        
        bytes32 _name = NameFilter.nameFilter(_nameString);
        address _addr = msg.sender;
        bool _isNewPlayer = determinePID(_addr);
        uint256 _pID = pIDxAddr_[_addr];
        uint256 _affID;
        if (_affCode != address(0) && _affCode != _addr){
            _affID = pIDxAddr_[_affCode];
            if (_affID != plyr_[_pID].laff){
                plyr_[_pID].laff = _affID;
            }
        }
        registerNameCore(_pID, _addr, _affID, _name, _isNewPlayer, _all);
    }
    /**
     æ³¨ååå­
     _nameString: åå­
     _affCodeï¼æ¨èäººåç§°
     _allï¼æ¯å¦æ¯æ³¨åå°æææ¸¸æä¸­
     æ¡ä»¶ï¼
     1. æ¯è´¦æ·å°å
     2. è¦ä»è´¹
     */
    function registerNameXname(string _nameString, bytes32 _affCode, bool _all) isHuman() public payable{
        // è¦æ±æ³¨åè´¹ç¨,ä¸éè¦ä»è´¹
        //require (msg.value >= registrationFee_, "You have to pay the name fee");
        
        bytes32 _name = NameFilter.nameFilter(_nameString);
        address _addr = msg.sender;
        bool _isNewPlayer = determinePID(_addr);
        uint256 _pID = pIDxAddr_[_addr];
        uint256 _affID;
        if (_affCode != "" && _affCode != _name){
            _affID = pIDxName_[_affCode];
            if (_affID != plyr_[_pID].laff){
                plyr_[_pID].laff = _affID;
            }
        }
        registerNameCore(_pID, _addr, _affID, _name, _isNewPlayer, _all);
    }

    /**
     æ³¨å
     _pID:          ç©å®¶ç¼å·
     _addr:         ç©å®¶å°å
     _affID:        ä»å±
     _name:         åç§°
    _isNewPlayer:   æ¯å¦æ¯æ°ç©å®¶
    _all:           æ¯å¦æ³¨åå°æææ¸¸æ
     */
    function registerNameCore(uint256 _pID, address _addr, uint256 _affID, bytes32 _name, bool _isNewPlayer, bool _all) private {
        // å¤æ­æ¯å¦å·²ç»æ³¨åè¿
        if (pIDxName_[_name] != 0)
            require(plyrNames_[_pID][_name] == true, "That names already taken");
        // 
        plyr_[_pID].name = _name;
        pIDxName_[_name] = _pID;
        if (plyrNames_[_pID][_name] == false) {
            plyrNames_[_pID][_name] = true;
            plyr_[_pID].names++;
            plyrNameList_[_pID][plyr_[_pID].names] = _name;
        }
        // å°æ³¨åè´¹ç¨è½¬å°ç¤¾åºåºéåçº¦è´¦æ·ä¸­
        if(address(this).balance>0){
            if(address(communityAddr) != address(0x0)) {
                communityAddr.transfer(address(this).balance);
            }
        }

        if (_all == true)
            for (uint256 i = 1; i <= gID_; i++)
                games_[i].receivePlayerInfo(_pID, _addr, _name, _affID);
        
        emit onNewName(_pID, _addr, _name, _isNewPlayer, _affID, plyr_[_affID].addr, plyr_[_affID].name, msg.value, now);
    }
    /**
     å¦ææ¯æ°ç©å®¶ï¼åè¿åç
     */
    function determinePID(address _addr) private returns (bool) {
        if (pIDxAddr_[_addr] == 0){
            pID_++;
            pIDxAddr_[_addr] = pID_;
            plyr_[pID_].addr = _addr;
            return (true) ;
        }else{
            return (false);
        }
    }
    /**
     */
    function addMeToGame(uint256 _gameID) isHuman() public {
        require(_gameID <= gID_, "Game doesn't exist yet");
        address _addr = msg.sender;
        uint256 _pID = pIDxAddr_[_addr];
        require(_pID != 0, "You dont even have an account");
        uint256 _totalNames = plyr_[_pID].names;
        
        // add players profile and most recent name
        games_[_gameID].receivePlayerInfo(_pID, _addr, plyr_[_pID].name, plyr_[_pID].laff);
        
        // add list of all names
        if (_totalNames > 1)
            for (uint256 ii = 1; ii <= _totalNames; ii++)
                games_[_gameID].receivePlayerNameList(_pID, plyrNameList_[_pID][ii]);
    }

    function addMeToAllGames() isHuman() public {
        address _addr = msg.sender;
        uint256 _pID = pIDxAddr_[_addr];
        require(_pID != 0, "You dont even have an account");
        uint256 _laff = plyr_[_pID].laff;
        uint256 _totalNames = plyr_[_pID].names;
        bytes32 _name = plyr_[_pID].name;
        
        for (uint256 i = 1; i <= gID_; i++){
            games_[i].receivePlayerInfo(_pID, _addr, _name, _laff);
            if (_totalNames > 1)
                for (uint256 ii = 1; ii <= _totalNames; ii++)
                    games_[i].receivePlayerNameList(_pID, plyrNameList_[_pID][ii]);
        }
    }

    function useMyOldName(string _nameString) isHuman() public {
        // filter name, and get pID
        bytes32 _name = _nameString.nameFilter();
        uint256 _pID = pIDxAddr_[msg.sender];
        
        // make sure they own the name 
        require(plyrNames_[_pID][_name] == true, "Thats not a name you own");
        
        // update their current name 
        plyr_[_pID].name = _name;
    }
    /**
     PlayerBookInterface Interface 
     */
    function getPlayerID(address _addr) external returns (uint256){
        determinePID(_addr);
        return (pIDxAddr_[_addr]);
    }

    function getPlayerName(uint256 _pID) external view returns (bytes32){
        return (plyr_[_pID].name);
    }

    function getPlayerLAff(uint256 _pID) external view returns (uint256) {
        return (plyr_[_pID].laff);
    }

    function getPlayerAddr(uint256 _pID) external view returns (address) {
        return (plyr_[_pID].addr);
    }

    function getNameFee() external view returns (uint256){
        return (registrationFee_);
    }
    
    function registerNameXIDFromDapp(address _addr, bytes32 _name, uint256 _affCode, bool _all) 
        isRegisteredGame()
        external payable returns(bool, uint256){
        // è¦æ±æ³¨åè´¹ç¨,ä¸éè¦ä»è´¹
        //require (msg.value >= registrationFee_, "You have to pay the name fee");

        bool _isNewPlayer = determinePID(_addr);
        uint256 _pID = pIDxAddr_[_addr];
        uint256 _affID = _affCode;
        if (_affID != 0 && _affID != plyr_[_pID].laff && _affID != _pID) {
            plyr_[_pID].laff = _affID;
        } else if (_affID == _pID) {
            _affID = 0;
        }      
        registerNameCore(_pID, _addr, _affID, _name, _isNewPlayer, _all);
        return(_isNewPlayer, _affID);
    }
    //
    function registerNameXaddrFromDapp(address _addr, bytes32 _name, address _affCode, bool _all) 
        isRegisteredGame()
        external payable returns(bool, uint256){
        // è¦æ±æ³¨åè´¹ç¨,ä¸éè¦ä»è´¹
        //require (msg.value >= registrationFee_, "You have to pay the name fee");

        bool _isNewPlayer = determinePID(_addr);
        uint256 _pID = pIDxAddr_[_addr];
        uint256 _affID;
        if (_affCode != address(0) && _affCode != _addr){
            _affID = pIDxAddr_[_affCode];
            if (_affID != plyr_[_pID].laff){
                plyr_[_pID].laff = _affID;
            }
        }
        
        registerNameCore(_pID, _addr, _affID, _name, _isNewPlayer, _all);
        
        return(_isNewPlayer, _affID);    
    }
    //
    function registerNameXnameFromDapp(address _addr, bytes32 _name, bytes32 _affCode, bool _all) 
        isRegisteredGame()
        external payable returns(bool, uint256){
        // è¦æ±æ³¨åè´¹ç¨,ä¸éè¦ä»è´¹
        //require (msg.value >= registrationFee_, "You have to pay the name fee");

        bool _isNewPlayer = determinePID(_addr);
        uint256 _pID = pIDxAddr_[_addr];
        uint256 _affID;
        if (_affCode != "" && _affCode != _name){
            _affID = pIDxName_[_affCode];
            if (_affID != plyr_[_pID].laff){
                plyr_[_pID].laff = _affID;
            }
        }
        registerNameCore(_pID, _addr, _affID, _name, _isNewPlayer, _all);
        return(_isNewPlayer, _affID);            
    }
}