FIX: provide entropy for D20

This commit is contained in:
Ivan Vershigora 2020-07-01 18:42:44 +03:00 committed by Overtorment
parent 7396147157
commit e6b1b9c7a3
2 changed files with 23 additions and 8 deletions

View File

@ -60,6 +60,10 @@ export const getEntropy = (number, base) => {
let summ = 0;
while (bits >= 1) {
const block = 2 ** bits;
if (summ + block > base) {
bits -= 1;
continue;
}
if (number < summ + block) {
return { value: number - summ, bits };
}
@ -97,11 +101,15 @@ export const convertToBuffer = ({ entropy, bits }) => {
const Coin = ({ push }) => (
<View style={styles.coinRoot}>
<TouchableOpacity onPress={() => push(getEntropy(0, 2))} style={styles.coinBody}>
<Image style={styles.coinImage} source={require('../../img/coin1.png')} />
<TouchableOpacity onPress={() => push(getEntropy(0, 2))}>
<View style={styles.coinBody}>
<Image style={styles.coinImage} source={require('../../img/coin1.png')} />
</View>
</TouchableOpacity>
<TouchableOpacity onPress={() => push(getEntropy(1, 2))} style={styles.coinBody}>
<Image style={styles.coinImage} source={require('../../img/coin2.png')} />
<TouchableOpacity onPress={() => push(getEntropy(1, 2))}>
<View style={styles.coinBody}>
<Image style={styles.coinImage} source={require('../../img/coin2.png')} />
</View>
</TouchableOpacity>
</View>
);
@ -291,13 +299,10 @@ const styles = StyleSheet.create({
borderRadius: 5,
borderColor: 'grey',
margin: 10,
padding: 10,
},
coinImage: {
flex: 0.9,
aspectRatio: 1,
width: '100%',
height: '100%',
borderRadius: 75,
},
diceScroll: {
backgroundColor: 'white',

View File

@ -86,6 +86,16 @@ describe('getEntropy function', () => {
assert.deepEqual(getEntropy(5, 6), { value: 1, bits: 1 });
});
it('handles 20 sides dice', () => {
assert.deepEqual(getEntropy(0, 20), { value: 0, bits: 4 });
assert.deepEqual(getEntropy(15, 20), { value: 15, bits: 4 });
assert.deepEqual(getEntropy(16, 20), { value: 0, bits: 2 });
assert.deepEqual(getEntropy(17, 20), { value: 1, bits: 2 });
assert.deepEqual(getEntropy(18, 20), { value: 2, bits: 2 });
assert.deepEqual(getEntropy(19, 20), { value: 3, bits: 2 });
});
it('handles odd numbers', () => {
assert.deepEqual(getEntropy(0, 3), { value: 0, bits: 1 });
assert.deepEqual(getEntropy(1, 3), { value: 1, bits: 1 });