-
Notifications
You must be signed in to change notification settings - Fork 1
/
shuffle.js
69 lines (49 loc) · 1.96 KB
/
shuffle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Proof of Concept
const mp = require("mental-poker");
const shuffle = require("lodash.shuffle");
// based on https://github.com/kripod/mental-poker
const config = mp.createConfig(52);
const player1 = mp.createPlayer(config);
const player2 = mp.createPlayer(config);
const player3 = mp.createPlayer(config);
const player4 = mp.createPlayer(config);
const cardCodewords = mp.createDeck(
[player1, player2, player3, player4].map(player => player.cardCodewordFragments),
);
let deck = cardCodewords;
// shuffle
deck = mp.encryptDeck(shuffle(deck), player1.keyPairs[config.cardCount].privateKey);
deck = mp.encryptDeck(shuffle(deck), player2.keyPairs[config.cardCount].privateKey);
deck = mp.encryptDeck(shuffle(deck), player3.keyPairs[config.cardCount].privateKey);
deck = mp.encryptDeck(shuffle(deck), player4.keyPairs[config.cardCount].privateKey);
// locking
deck = mp.encryptDeck(
mp.decryptDeck(deck, player1.keyPairs[config.cardCount].privateKey),
player1.keyPairs.map(keyPair => keyPair.privateKey),
);
deck = mp.encryptDeck(
mp.decryptDeck(deck, player2.keyPairs[config.cardCount].privateKey),
player2.keyPairs.map(keyPair => keyPair.privateKey),
);
deck = mp.encryptDeck(
mp.decryptDeck(deck, player3.keyPairs[config.cardCount].privateKey),
player3.keyPairs.map(keyPair => keyPair.privateKey),
);
deck = mp.encryptDeck(
mp.decryptDeck(deck, player4.keyPairs[config.cardCount].privateKey),
player4.keyPairs.map(keyPair => keyPair.privateKey),
);
// deal
// #1 #2 #3 #4
const dealedCards = [[], [], [], []];
deck.forEach(cardEncrypted => {
const cardDecrypted = mp.decryptCard(
cardEncrypted,
[player1, player2, player3, player4].map(player => player.keyPairs[deck.indexOf(cardEncrypted)].privateKey),
);
const codewordIndex = cardCodewords.findIndex(cardCodeword =>
cardCodeword.equals(cardDecrypted),
);
dealedCards[deck.indexOf(cardEncrypted) % 4].push(codewordIndex);
});
console.log(dealedCards);