@jamesrock/rockjs 1.23.0 → 1.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +194 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -297,6 +297,24 @@ export class DisplayObject {
|
|
|
297
297
|
return this;
|
|
298
298
|
|
|
299
299
|
};
|
|
300
|
+
destroy() {
|
|
301
|
+
|
|
302
|
+
this.node.parentNode.removeChild(this.node);
|
|
303
|
+
return this;
|
|
304
|
+
|
|
305
|
+
};
|
|
306
|
+
setStyle(key, value) {
|
|
307
|
+
|
|
308
|
+
this.node.style[key] = value;
|
|
309
|
+
return this;
|
|
310
|
+
|
|
311
|
+
};
|
|
312
|
+
setProp(key, value) {
|
|
313
|
+
|
|
314
|
+
this.node.dataset[key] = value;
|
|
315
|
+
return this;
|
|
316
|
+
|
|
317
|
+
};
|
|
300
318
|
addEventListener(event, handler, passive = true) {
|
|
301
319
|
|
|
302
320
|
this.node.addEventListener(event, handler, {passive});
|
|
@@ -610,3 +628,179 @@ export class Time {
|
|
|
610
628
|
formatHours = (ms) => toDouble(this.getHours(ms));
|
|
611
629
|
format = (ms) => `${this.formatMinutes(ms)}:${this.formatSeconds(ms)}`;
|
|
612
630
|
};
|
|
631
|
+
|
|
632
|
+
export class PlayingCard extends DisplayObject {
|
|
633
|
+
constructor(deck, value, suit) {
|
|
634
|
+
|
|
635
|
+
super();
|
|
636
|
+
|
|
637
|
+
this.deck = deck;
|
|
638
|
+
this.value = value;
|
|
639
|
+
this.suit = suit;
|
|
640
|
+
this.rawValue = (this.deck.values.indexOf(this.value) + 1);
|
|
641
|
+
this.id = `${this.value}${this.suit}`;
|
|
642
|
+
this.color = this.deck.getSuitColor(this.suit);
|
|
643
|
+
this.icon = this.deck.getSuitIcon(this.suit);
|
|
644
|
+
this.node = this.make();
|
|
645
|
+
|
|
646
|
+
};
|
|
647
|
+
make() {
|
|
648
|
+
|
|
649
|
+
const
|
|
650
|
+
node = createNode('div', 'card'),
|
|
651
|
+
svg = createSVGNode('svg'),
|
|
652
|
+
use = createSVGNode('use');
|
|
653
|
+
|
|
654
|
+
use.setAttribute('href', `${this.deck.sprite}#${this.suit}${this.value}`);
|
|
655
|
+
|
|
656
|
+
svg.append(use);
|
|
657
|
+
node.append(svg);
|
|
658
|
+
|
|
659
|
+
return node;
|
|
660
|
+
|
|
661
|
+
};
|
|
662
|
+
};
|
|
663
|
+
|
|
664
|
+
export class DeckOfPlayingCards {
|
|
665
|
+
constructor({
|
|
666
|
+
sprite = '/sprite.svg',
|
|
667
|
+
saved = [],
|
|
668
|
+
cardMaker = (deck, value, suit) => new PlayingCard(deck, value, suit)
|
|
669
|
+
} = {}) {
|
|
670
|
+
|
|
671
|
+
this.sprite = sprite;
|
|
672
|
+
this.cardMaker = cardMaker;
|
|
673
|
+
this.cards = saved.length ? this.makeFromSaved(saved) : this.make();
|
|
674
|
+
this.map = this.makeMap();
|
|
675
|
+
this.shuffledMap = this.makeShuffledMap();
|
|
676
|
+
|
|
677
|
+
};
|
|
678
|
+
make() {
|
|
679
|
+
|
|
680
|
+
return shuffle(shuffle(this.makeDeckValues().map(([value, suit]) => this.cardMaker(this, value, suit))));
|
|
681
|
+
|
|
682
|
+
};
|
|
683
|
+
makeMap() {
|
|
684
|
+
|
|
685
|
+
var out = {};
|
|
686
|
+
this.cards.forEach((card) => {
|
|
687
|
+
out[card.id] = card;
|
|
688
|
+
});
|
|
689
|
+
return out;
|
|
690
|
+
|
|
691
|
+
};
|
|
692
|
+
makeShuffledMap() {
|
|
693
|
+
|
|
694
|
+
return this.cards.map((card) => card.id);
|
|
695
|
+
|
|
696
|
+
};
|
|
697
|
+
makeSaveMap() {
|
|
698
|
+
|
|
699
|
+
return this.cards.map((card) => [card.value, card.suit]);
|
|
700
|
+
|
|
701
|
+
};
|
|
702
|
+
makeFromSaved(saved) {
|
|
703
|
+
|
|
704
|
+
return saved.map(([value, suit]) => this.cardMaker(this, value, suit));
|
|
705
|
+
|
|
706
|
+
};
|
|
707
|
+
appendTo(target) {
|
|
708
|
+
|
|
709
|
+
this.shuffledMap.forEach((id) => {
|
|
710
|
+
this.map[id].appendTo(target);
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
return this;
|
|
714
|
+
|
|
715
|
+
};
|
|
716
|
+
destroy() {
|
|
717
|
+
|
|
718
|
+
this.shuffledMap.forEach((id) => {
|
|
719
|
+
this.map[id].destroy();
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
return this;
|
|
723
|
+
|
|
724
|
+
};
|
|
725
|
+
makeSuitValues() {
|
|
726
|
+
|
|
727
|
+
return makeArray(this.suits.length).map((suit) => this.suits[suit]);
|
|
728
|
+
|
|
729
|
+
};
|
|
730
|
+
makePipValues() {
|
|
731
|
+
|
|
732
|
+
return makeArray(this.values.length).map((value) => this.values[value]);
|
|
733
|
+
|
|
734
|
+
};
|
|
735
|
+
makeDeckValues() {
|
|
736
|
+
|
|
737
|
+
var out = [];
|
|
738
|
+
|
|
739
|
+
this.makeSuitValues().forEach((suit) => {
|
|
740
|
+
this.makePipValues().forEach((value) => {
|
|
741
|
+
out.push([value, suit]);
|
|
742
|
+
});
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
return out;
|
|
746
|
+
|
|
747
|
+
};
|
|
748
|
+
makeCard(value, suit) {
|
|
749
|
+
|
|
750
|
+
return this.cardMaker(this, value, suit);
|
|
751
|
+
|
|
752
|
+
};
|
|
753
|
+
indexOf(id) {
|
|
754
|
+
|
|
755
|
+
return this.shuffledMap.indexOf(id);
|
|
756
|
+
|
|
757
|
+
};
|
|
758
|
+
getCard(index = 0) {
|
|
759
|
+
|
|
760
|
+
return this.cards[index];
|
|
761
|
+
|
|
762
|
+
};
|
|
763
|
+
getSuitIcon(suit) {
|
|
764
|
+
|
|
765
|
+
return this.suitIcons[suit];
|
|
766
|
+
|
|
767
|
+
};
|
|
768
|
+
getSuitColor(suit) {
|
|
769
|
+
|
|
770
|
+
return this.suitColors[suit];
|
|
771
|
+
|
|
772
|
+
};
|
|
773
|
+
suits = [
|
|
774
|
+
'C',
|
|
775
|
+
'D',
|
|
776
|
+
'H',
|
|
777
|
+
'S'
|
|
778
|
+
];
|
|
779
|
+
values = [
|
|
780
|
+
'A',
|
|
781
|
+
'2',
|
|
782
|
+
'3',
|
|
783
|
+
'4',
|
|
784
|
+
'5',
|
|
785
|
+
'6',
|
|
786
|
+
'7',
|
|
787
|
+
'8',
|
|
788
|
+
'9',
|
|
789
|
+
'10',
|
|
790
|
+
'J',
|
|
791
|
+
'Q',
|
|
792
|
+
'K'
|
|
793
|
+
];
|
|
794
|
+
suitIcons = {
|
|
795
|
+
'C': '♣',
|
|
796
|
+
'D': '♦',
|
|
797
|
+
'H': '♥',
|
|
798
|
+
'S': '♠'
|
|
799
|
+
};
|
|
800
|
+
suitColors = {
|
|
801
|
+
'C': 'black',
|
|
802
|
+
'D': 'red',
|
|
803
|
+
'H': 'red',
|
|
804
|
+
'S': 'black'
|
|
805
|
+
};
|
|
806
|
+
};
|