Skip to content

Commit

Permalink
Moves code for shuffling and dealing to server
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpounds committed Feb 24, 2021
1 parent a1bad7d commit e5ac441
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 23 deletions.
28 changes: 8 additions & 20 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import http from 'http';
import WebSocket from 'ws';
import path from 'path';
import bodyParser from 'body-parser';
import { uid } from 'uid/single';
import serverStore, { resetShuffleAndDeal } from './store.js';

const __dirname = path.resolve();
const app = express();
Expand All @@ -17,34 +17,22 @@ app.get('/', (req, res) => {
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

const players = [1, 2, 3, 4].map(i => ({
id: uid(16),
name: `Player ${i}`,
dealer: false,
}));

const dealer = {
id: uid(16),
name: 'Dealer',
dealer: true,
};
const [dealer, ...players] = serverStore.players;
resetShuffleAndDeal();

const connectedUsers = new Map();

wss.on('connection', ws => {

const noOfConnectedUsers = connectedUsers.size;
if (noOfConnectedUsers > 5) {
if (noOfConnectedUsers > serverStore.players.length) {
ws.terminate();
return false;
}

if (noOfConnectedUsers === 0) {
connectedUsers.set(ws, dealer);
} else {
connectedUsers.set(ws, players[noOfConnectedUsers - 1]);
}

const currentPlayer = noOfConnectedUsers === 0 ? dealer : players[noOfConnectedUsers - 1];
connectedUsers.set(ws, currentPlayer);

ws.isAlive = true;

ws.on('pong', () => {
Expand Down Expand Up @@ -76,7 +64,7 @@ wss.on('connection', ws => {
ws.send('Non-JSON string sent');
}
});
ws.send(JSON.stringify({message: 'Connected to websocket', user: connectedUsers.get(ws)}));
ws.send(JSON.stringify({store: serverStore, currentPlayer}));
});

setInterval(() => {
Expand Down
50 changes: 50 additions & 0 deletions server/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import CONFIG from '../src/data/config.js';
import getResetDeck from '../src/data/deck.js';
import { shuffleDeck } from '../src/data/randomizing.js';
import { uid } from 'uid/single';

const serverStore = {
players: [
{
id: uid(16),
name: 'Dealer',
isDealer: true,
},
{
id: uid(16),
name: 'Player 1',
isDealer: false,
},
{
id: uid(16),
name: 'Player 2',
isDealer: false,
},
{
id: uid(16),
name: 'Player 3',
isDealer: false,
},
{
id: uid(16),
name: 'Player 4',
isDealer: false,
},
],
deck: shuffleDeck(getResetDeck()),
};

export const resetShuffleAndDeal = () => {
const shuffledResetDeck = shuffleDeck(getResetDeck());
const players = serverStore.players.filter(player => !player.isDealer);
const numberOfPlayers = players.length;
const numberOfCards = Math.floor(CONFIG.CARDS_IN_DECK / numberOfPlayers);
const numberOfCardsToDeal = numberOfCards * numberOfPlayers;
[...new Array(numberOfCardsToDeal).keys()].forEach(i => {
const playerIndex = Math.floor(i / numberOfCards);
shuffledResetDeck[i].player = players[playerIndex].id;
});
serverStore.deck = shuffledResetDeck;
};

export default serverStore;
2 changes: 1 addition & 1 deletion src/data/PlayingCard.class.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import SUITS from './suits';
import SUITS from './suits.js';

export const CARD_STATUS = Object.freeze({
IN_DECK: 'IN_DECK',
Expand Down
4 changes: 2 additions & 2 deletions src/data/deck.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PlayingCard from './PlayingCard.class';
import SUITS from './suits';
import PlayingCard from './PlayingCard.class.js';
import SUITS from './suits.js';

const values = [...Array(13).keys()].map(i => i + 1);

Expand Down

0 comments on commit e5ac441

Please sign in to comment.