⚖ī¸Fairness

Card Flip SC Code:

 let mut rand_source = RandomnessSource::<Self::Api>::new();

        // Calculate a random number between 1 - 200 (x2), 1 - 300 (x3) and 1 - 400 (x4)
        let random_result = rand_source.next_u16_in_range(1, game_mode * 100 + 1);
        // The actual card is the rest from division + 1 since selected_card starts from 1
        let result = (random_result % game_mode + 1) as u8;

        // Caller LOST...
        if selected_card != result {
            self.distribute_tokens(collection, &token, &amount, false);

            return ( ManagedBuffer::from(b"unlucky"), result ).into();
        }

        // Caller WON!
        let multiplier = BigUint::from(game_mode as u32);
        let amount_won = &amount * &multiplier;

RPS SC Code:

let mut rand_source = RandomnessSource::<Self::Api>::new();

        // Calculate a random number between 1 - 300
        let random_result = rand_source.next_u16_in_range(1, 301);
        // The actual card is the rest from division + 1 since selected_card starts from 1
        let result = (random_result % 3 + 1) as u8;

        // Caller DRAW...
        if selected_card == result {
            self.distribute_tokens(collection, &token, &amount, true);

            return ( ManagedBuffer::from(b"draw"), result ).into();
        }

        // Caller LOST...
        if (1 == selected_card && 2 == result) // rock loses to paper
            || (2 == selected_card && 3 == result) // paper loses to scissors
            || (3 == selected_card && 1 == result) // scissors loses to rock
        {
            self.distribute_tokens(collection, &token, &amount, false);

            return ( ManagedBuffer::from(b"lose"), result ).into();
        }

        // Caller WON!
        let multiplier = BigUint::from(2u32);
        let amount_won = &amount * &multiplier;

Battle Arena SC Code:

 let mut rand_source = RandomnessSource::<Self::Api>::new();
        // Calculate a random number between 1 - 200
        let random_result = rand_source.next_u8_in_range(1, 201);

        // Caller LOST...
        if 0 == random_result % 2 {
            self.distribute_tokens(&game.creator, collection, &token, &amount_won);

            return ManagedBuffer::from(b"unlucky");
        }

        // Caller WON!
        self.distribute_tokens(&caller, collection, &token, &amount_won);

Last updated