-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.js
More file actions
164 lines (146 loc) · 3.91 KB
/
Copy pathclient.js
File metadata and controls
164 lines (146 loc) · 3.91 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
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
'use strict';
const io = require('socket.io-client');
const prompt = require('prompt');
// const socket = io.connect('http://172.16.5.198:4039');
// const client = require('../Game-Engine/wordWizard/remoteClient.js');
// const socket = io.connect('https://cdk-socket-io-test.herokuapp.com/');
const socket = io.connect('https://enseven-game-engine.herokuapp.com');
const readline = require('readline');
//setting readline to read from standard (keyboard) input and output streams. also setting a timeout limit if no input is received for any readline prompts / inputs.
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
escapeCodeTimeout: 20000,
});
function welcomePrompt() {
// Welcome user. Explain how to use app by setting the rl prompt, and then calling the prompt
rl.question('Welcome to N7 Games. To begin, type LOGIN to login to an existing account, or type CREATE to create a new account. ', function(answer) {
firstQuestionPromptLoop(answer);
});
}
function firstQuestionPromptLoop(answer) {
//this needs to be wrapped in an invokable function, it's auto kicking to this before doing the welcome prompt
if(answer){
switch(answer){
case 'LOGIN':
rl.close();
signin();
break;
case 'CREATE':
rl.close();
signup();
break;
default:
welcomePrompt();
}
}
}
function signin() {
prompt.start();
const userSchema = {
properties: {
email: {
description: 'Enter your email address',
required: true,
},
username: {
description: 'Enter your username',
pattern: /^[a-zA-Z0-9]+$/,
message: 'Name must be only letters and numbers',
required: true,
},
password: {
description: 'Enter in your password',
hidden: true,
replace: '*',
required: true,
},
},
};
prompt.get(userSchema, function (err, result) {
socket.emit('sign-in', result);
});
}
function signup() {
prompt.start();
const userSchema = {
properties: {
email: {
description: 'Enter your email address',
required: true,
},
username: {
description: 'Create a username',
pattern: /^[a-zA-Z0-9]+$/,
message: 'Name must be only letters and numbers',
required: true,
},
password: {
description: 'Create a password',
hidden: true,
replace: '*',
required: true,
},
},
};
prompt.get(userSchema, function (err, result) {
socket.emit('sign-up', result);
});
}
// username comes with this
socket.on('signed-in-newuser', payload => {
let user = payload;
socket.emit('join', user);
console.log('signed in new user');
});
socket.on('signed-in-user', payload => {
let user = payload;
socket.emit('join', user);
console.log('signed in returning user');
});
socket.on('player1-joined', payload => {
console.log('player1 joined');
socket.emit('play');
});
socket.on('input-request', (word) => {
let count = word.count + 1;
console.log(`You have ${count} guesses left`);
console.log(word.category);
console.log(word.string);
prompt.start();
const input = {
properties: {
letter: {
description: 'Enter a letter',
required: true,
},
},
};
prompt.get(input, function (err, result) {
socket.emit('input', result.letter);
});
});
socket.on('player2-joined', payload => {
console.log('player2 joined');
console.log(payload);
socket.emit('play');
});
socket.on('results', results => {
console.log('results:', results);
socket.emit('play');
});
socket.on('won', () => {
console.log('You have won!');
});
socket.on('lost', () => {
console.log('You have lost!');
});
socket.on('output', (output) => {
console.log(output);
});
// Sending to game engine to START the game
socket.emit('start');
socket.on('connected', payload => {
console.log(payload);
welcomePrompt();
});