@hammerhead/deckmanager
Advanced tools
+5
-17
| export default class Deck { | ||
| constructor(count = 1, jokers = false, shuffle = true) { | ||
| this.deckId = null; | ||
| constructor(deckId = null, count = 1, jokers = false, shuffle = true) { | ||
| this.deckId = deckId; // use existing deckId if provided | ||
| this.backImage = "https://www.deckofcardsapi.com/static/img/back.png"; | ||
@@ -10,2 +10,4 @@ this._ready = this._init(count, jokers, shuffle); | ||
| async _init(count, jokers, shuffle) { | ||
| if (this.deckId) return; // skip if deckId already exists | ||
| let url = shuffle | ||
@@ -23,3 +25,2 @@ ? new URL("https://www.deckofcardsapi.com/api/deck/new/shuffle/") | ||
| // Ensure deck is initialized | ||
| async _ensureReady() { | ||
@@ -30,6 +31,4 @@ if (!this._ready) return; | ||
| // Draw cards from main deck | ||
| async draw(count = 1) { | ||
| await this._ensureReady(); | ||
| const url = new URL( | ||
@@ -45,6 +44,4 @@ `https://www.deckofcardsapi.com/api/deck/${this.deckId}/draw/` | ||
| // Shuffle main deck | ||
| async shuffle(remaining = true) { | ||
| await this._ensureReady(); | ||
| const url = new URL( | ||
@@ -59,3 +56,3 @@ `https://www.deckofcardsapi.com/api/deck/${this.deckId}/shuffle/` | ||
| // Add cards to a named pile | ||
| // Pile functions remain the same | ||
| async addToPile(pileName, cards) { | ||
@@ -66,3 +63,2 @@ await this._ensureReady(); | ||
| : cards; | ||
| const url = new URL( | ||
@@ -72,3 +68,2 @@ `https://www.deckofcardsapi.com/api/deck/${this.deckId}/pile/${pileName}/add/` | ||
| url.searchParams.append("cards", cardsParam); | ||
| const res = await fetch(url); | ||
@@ -78,6 +73,4 @@ return await res.json(); | ||
| // Draw cards from a named pile | ||
| async drawFromPile(pileName, count = 1) { | ||
| await this._ensureReady(); | ||
| const url = new URL( | ||
@@ -87,3 +80,2 @@ `https://www.deckofcardsapi.com/api/deck/${this.deckId}/pile/${pileName}/draw/` | ||
| url.searchParams.append("count", count); | ||
| const res = await fetch(url); | ||
@@ -97,6 +89,4 @@ const data = await res.json(); | ||
| // Shuffle a named pile | ||
| async shufflePile(pileName) { | ||
| await this._ensureReady(); | ||
| const url = new URL( | ||
@@ -109,6 +99,4 @@ `https://www.deckofcardsapi.com/api/deck/${this.deckId}/pile/${pileName}/shuffle/` | ||
| // List all cards in a pile | ||
| async listPile(pileName) { | ||
| await this._ensureReady(); | ||
| const url = new URL( | ||
@@ -115,0 +103,0 @@ `https://www.deckofcardsapi.com/api/deck/${this.deckId}/pile/${pileName}/list/` |
+1
-1
| { | ||
| "name": "@hammerhead/deckmanager", | ||
| "version": "1.0.0", | ||
| "version": "1.1.0", | ||
| "description": "A ready-to-use card deck manager with pile support using Deck of Cards API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
5529
-1.36%85
-5.56%