<Lucky 9> application



    1. dapp name: Lucky 9

    2. dApp description:
      It's a simple poker game. It compares the sum of two cards
      Suppose Banker's cards are [2, 9],
      then the sum is (2 + 9) % 10 == 1.
      Suppose Player's cards are [7, J],
      then the sum is (7 + 0) % 10 == 7.
      Due to 7 > 1, Player wins.
      Card 10, J, Q, K are calculated as 0

    3. online status: yes

    4. dApp contract address:vite_61088b1d4d334271f0ead08a1eec17b08e7ef25141dd427787

    5. dapp contract content:

    pragma soliditypp ^0.4.2;
    contract ViteBet{
        address owner;
    
        struct BetLimit {
            uint256 lowerLimit;
            uint256 upperLimit;
            uint256 tipPer;
        }
    
        tokenId[] tokens = ["tti_5649544520544f4b454e6e40"];
        mapping(tokenId => BetLimit) public tokenMap;
    
        event cardnums(address indexed addr, uint256 betAmount, uint256 customerCard1, uint256 customerCard2, uint256 myCard1, uint256 myCard2);
        event win(address indexed addr, uint256 betAmount, uint256 customerCard1, uint256 customerCard2, uint256 myCard1, uint256 myCard2, uint256 winAmount);
        event lose(address indexed addr, uint256 betAmount, uint256 customerCard1, uint256 customerCard2, uint256 myCard1, uint256 myCard2);
        event draw(address indexed addr, uint256 betAmount, uint256 customerCard1, uint256 customerCard2, uint256 myCard1, uint256 myCard2);
        event suspendBet(address indexed addr, uint256 betAmount, uint256 customerCard1, uint256 customerCard2, uint256 myCard1, uint256 myCard2, uint256 winAmount);
    
        constructor() public {
            owner = msg.sender;
            tokenMap["tti_5649544520544f4b454e6e40"].lowerLimit = 1 vite;
            tokenMap["tti_5649544520544f4b454e6e40"].upperLimit = 1000 vite;
            tokenMap["tti_5649544520544f4b454e6e40"].tipPer = 10;
        }
    
        onMessage () payable {
        }
    
        // Configure the upper and lower limits of the token bet
        // Configure the draw ratio (0 to 20)
        onMessage configBetLimit(uint256 ll, uint256 ul, uint256 tp) {
            require(owner == msg.sender);
            require(ll > 0 && ll <= ul);
            require(tp >= 0 && tp <= 20);
            if (tokenMap[msg.tokenid].lowerLimit == 0){
                tokens.push(msg.tokenid);
            }
            tokenMap[msg.tokenid].lowerLimit = ll;
            tokenMap[msg.tokenid].upperLimit = ul;
            tokenMap[msg.tokenid].tipPer = tp;
        }
    
        onMessage DrawMoney(uint256 amount) {
            require(owner == msg.sender);
            require(amount <= balance(msg.tokenid));
            msg.sender.transfer(msg.tokenid, amount);
        }
    
        // Get the upper and lower limits of the token and the rate
        getter getBetLimit(tokenId token) returns(uint256 ll, uint256 ul, uint256 tipPer) {
            return (tokenMap[token].lowerLimit, tokenMap[token].upperLimit, tokenMap[token].tipPer);
        }
    
        // Get the token list
        getter getTokenList() returns(tokenId[] memory) {
            return tokens;
        }
    
        onMessage BetAndRoll() payable {
            uint256 betAmount = msg.amount;
            address betAddr = msg.sender;
            uint256 ll = tokenMap[msg.tokenid].lowerLimit;
            uint256 ul = tokenMap[msg.tokenid].upperLimit;
            require(ll > 0 && ll <= ul);
            require(betAmount >= ll && betAmount <= ul);
    
            uint256[4] memory cards = getCardsSum();
            uint256 customerCardSum = (cards[0] + cards[1]) % 10;
            uint256 myCardSum = (cards[2] + cards[3]) % 10;
    
            emit cardnums(betAddr, betAmount, cards[0], cards[1], cards[2], cards[3]);
    
            if (customerCardSum > myCardSum) { // customer win
                uint256 winAmount = calcWinAmount(betAmount, msg.tokenid);
                if (winAmount > balance(msg.tokenid)) { // money is not enough
                    betAddr.transfer(msg.tokenid, betAmount); // return back betAmount
                    emit suspendBet(betAddr, betAmount, cards[0], cards[1], cards[2], cards[3], winAmount);
                } else {
                    betAddr.transfer(msg.tokenid, winAmount);
                    emit win(betAddr, betAmount, cards[0], cards[1], cards[2], cards[3], winAmount);
                }
            } else if (customerCardSum == myCardSum) { // draw
                betAddr.transfer(msg.tokenid, betAmount); // return back betAmount
                emit draw(betAddr, betAmount, cards[0], cards[1], cards[2], cards[3]);
            } else { // customer lose
                emit lose(betAddr, betAmount, cards[0], cards[1], cards[2], cards[3]);
            }
        }
    
        function getCardsSum() public returns(uint256[4] memory) {
            uint64 seed = random64();
            uint256[4] memory cards = [uint256(0), uint256(0), uint256(0), uint256(0)];
            for (uint i = 0; i < 4; i++) {
                uint16 card = uint16(seed >> i * 16);
                card = card % 13 + 1;
                if (card >= 10) {
                    card = 0;
                }
                cards[i] = uint256(card);
            }
            return cards;
        }
    
        function calcWinAmount(uint256 betAmount, tokenId token) public view returns(uint256) {
            uint256 bonus = betAmount;
            return betAmount + bonus * (100 - tokenMap[token].tipPer) / 100;
        }
    }
    
    


  • Interesting


Log in to reply