-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
96 lines (76 loc) 路 2.36 KB
/
Copy pathclient.js
File metadata and controls
96 lines (76 loc) 路 2.36 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
if(!process.argv[2]) {
console.error('Usage: Need a username')
process.exit(1)
}
var socket = require('socket.io-client')('http://localhost:3000');
const repl = require('repl')
const chalk = require('chalk');
let user = {};
socket.on('disconnect', function () {
socket.emit('disconnect')
});
socket.on('connect', () => {
console.log(chalk.red('=== start chatting ==='));
user.username = process.argv[2];
socket.emit('username', process.argv[2])
})
socket.on('message', (data) => {
console.log(chalk.magenta(data));
})
socket.on('username', (data) => {
console.log(chalk.blue(`${data.username} joined the game`))
user = data;
// console.log('user is', user);
})
socket.on('updateGame', (gameState) => {
// Displays the username and number of cards the player has
if (user) {
console.log(chalk.blue('Playing as ' + user.username));
console.log(chalk.green((gameState.numCards[user.id] || 0) + ' cards in hand'));
}
// Displays the current player
if (gameState.isStarted) {
console.log(chalk.blue(gameState.currentPlayerUsername + "'s turn"));
} else {
console.log(chalk.blue('Game has not started yet.'));
}
// Displays the number of cards in the game pile
console.log(chalk.white(gameState.cardsInDeck + ' cards in pile'));
// If the game is in a winning state, hide everything and show winning message
if (gameState.win) {
console.log(chalk.red(gameState.win + ' has won the game!'));
}
})
socket.on('playCard', function (data) {
const cardString = data.cardString;
console.log(chalk.white.bgCyan(cardString + ' played'));
});
socket.on('start', function () {
console.log(chalk.blue('game has started'))
});
// socket.on('clearDeck', function () {
// // CODE HERE
// });
// A handler for error messages
socket.on('errorMessage', function (data) {
console.log(chalk.red(data)); //make this red chalk
})
// have different cmd signal different 'clicks'
repl.start({
prompt: '',
eval: (cmd) => {
cmd = cmd.trim()
if (cmd === 'start') { //start the game
socket.emit('start');
}
else if (cmd === 'p') {
socket.emit('playCard');
}
else if (cmd === 's') {
socket.emit('slap');
}
else {
// console.log('default case');
}
}
})