-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
170 lines (151 loc) · 3.64 KB
/
client.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const net = require("net");
const readline = require("readline");
const ora = require("ora");
const printResult = require("./printResult");
/* Constants */
const NAME_CHANGE = "NAME_CHANGE";
const START_GAME = "START_GAME";
const GAME_MESSAGE = "GAME_MESSAGE";
const SHOW_GAME_BOARD = "SHOW_GAME_BOARD";
const GAME_MOVE = "GAME_MOVE";
const readMove = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const blank = "\n".repeat(process.stdout.rows);
const gameFlow = {
name: null,
pos: null,
steps: {
askName: false
}
};
let SERVER_ADDRESS = "127.0.0.1";
let SERVER_PORT = 1337;
var client = new net.Socket();
askForServerAddress();
function askForServerAddress() {
readMove.question(`\nEnter server address ( 127.0.0.1:1337 ) : `, address => {
if (address) {
const serverAddress = address.split(":");
SERVER_ADDRESS = serverAddress[0];
SERVER_PORT = serverAddress[1];
} else {
// askForServerAddress();
}
connecToTheServer();
});
}
function connecToTheServer() {
client.connect(SERVER_PORT, SERVER_ADDRESS, function() {
console.log("Connected");
});
}
client.on("data", function(data) {
const parsedData = JSON.parse(data.toString());
const { type, payload } = parsedData;
if (type === START_GAME) {
gameFlow.name = `Player ${payload.player}`;
gameFlow.pos = payload.player;
chooseTheMove();
}
if (type === GAME_MESSAGE) {
if (payload.spinner === true) {
showMessageWithSpinner(payload.message);
} else {
console.log(`\n${payload.message}`);
}
}
if (type === SHOW_GAME_BOARD) {
showGameBoard(payload.board);
}
if (type === GAME_MOVE) {
if (payload.ask === true) {
chooseTheMove();
} else {
showMessageWithSpinner(`Waiting Player ${payload.oppPose} to move`);
}
}
});
function chooseTheMove() {
const { steps, name } = gameFlow;
if (!steps.askName) {
readMove.question(`\nWhat's your name ( ${name} ) : `, playerName => {
handleNameChange(playerName);
});
} else {
readMove.question(`\nWhat's your move : `, move => {
handleTheMove(move);
});
}
}
function handleTheMove(data) {
const gameIndex = Number(data);
const isInValidIndex = gameIndex < 1 || gameIndex > 9;
if (Number.isNaN(gameIndex) || isInValidIndex) {
console.log("Invalid move !!");
chooseTheMove();
} else {
client.write(
JSON.stringify({
payload: {
move: gameIndex,
pos: gameFlow.pos
},
type: GAME_MOVE
})
);
}
}
function handleNameChange(playerName) {
gameFlow.steps.askName = true;
if (playerName) {
gameFlow.name = playerName;
}
const { name, pos } = gameFlow;
client.write(
JSON.stringify({
payload: {
name,
pos
},
type: NAME_CHANGE
})
);
console.log(`\nYour game name : ${name}`);
}
let cliSpinner;
function showMessageWithSpinner(message) {
cliSpinner = ora({
text: message,
spinner: "dots"
}).start();
}
function stopTheSpinner() {
if (cliSpinner) {
cliSpinner.stop();
}
}
function showGameBoard(gameBoard) {
stopTheSpinner();
clearTheScreen();
console.log(printResult(gameBoard));
}
function clearTheScreen() {
console.log(blank);
readline.cursorTo(process.stdout, 0, 0);
readline.clearScreenDown(process.stdout);
}
client.on("close", function() {
console.log("Connection closed");
});
client.on("error", function(err) {
console.log(
"\n************* Error on connecting to the server *************"
);
console.log(err);
console.log(
"************* Error on connecting to the server *************\n"
);
askForServerAddress();
});