-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFortuneTellerServer.ts
113 lines (96 loc) · 3.82 KB
/
FortuneTellerServer.ts
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
// My first WebSocket app. :)
import { Socket } from "dgram";
class FortuneTeller {
connection : Socket
timerId : any;
constructor(connection : Socket) {
this.connection = connection;
connection.on('customer question', (question : string) => this.onCustomerQuestion(question));
connection.on("disconnect", () => this.onCustomerDisconnect());
console.log('Customer, customer... we have a customer. Places everyone.');
connection.emit('fortune teller answer', 'Hello there, I sense you are wondering something... do tell.');
this.startPatientlyWaiting();
}
private onCustomerQuestion(customerQuestion : string) {
this.startPatientlyWaiting();
if (!customerQuestion || customerQuestion.length > 500) {
this.connection.emit('fortune teller answer', "Goodness you're a strange one.");
return;
}
console.log(`Ahhh, so you would like to know "${customerQuestion}" ... let me see...`);
var theAnswer = this.getAnswer();
console.log(`The answer: ${theAnswer}`);
this.connection.emit('fortune teller answer', theAnswer);
}
private onCustomerDisconnect() {
clearTimeout(this.timerId);
console.log("The customer left.");
}
private startPatientlyWaiting() {
if (this.timerId) {
clearTimeout(this.timerId);
}
this.timerId = setTimeout(() => this.onPatienceHasRunOut(), 10*1000);
}
private onPatienceHasRunOut() {
// Drop a hint...
this.connection.emit('fortune teller comment', this.getImpatientComment());
// ...but remember they're the customer so let's wait patiently again.
this.startPatientlyWaiting();
}
private getAnswer() : string {
var maxChoice: number = this.allThereIsToKnow.length;
var choice: number = Math.floor(Math.random() * maxChoice);
return this.allThereIsToKnow[choice];
}
private getImpatientComment() : string {
var maxChoice: number = this.impatientComments.length;
var choice: number = Math.floor(Math.random() * maxChoice);
return this.impatientComments[choice];
}
private allThereIsToKnow: string[] = [
"Positively so.",
"Is this the planet earth?",
"Doubt has run away in great fear, rest assured, it will be.",
"Of course, of course. And don't forget we accept tips.",
"Count on it... but remember, no refunds.",
"Appears to be so.",
"As certain as it will rain in Seattle.",
"You'll be fine.",
"Affirmative capitain.",
"One second, let me have a sip of my medicine.",
"Wah? Can't hear ya.",
"If you say so.",
"Let's take a raincheck.",
"I dunno, wadda think?",
"Look over there while I examine your bag.",
"Have at it.",
"Personally, I wouldn't recommend it.",
"Forgetta 'bout it!",
"Nah, I don't think so.",
"Well, I could say yes against my own best judgement."
];
private impatientComments: string[] = [
"You there, hello?",
"Forgive my boredom.",
"<yawn> ... there's a thing called time, and thing a called money...",
"It's called a question... like you ask me something, and I answer.",
"Oh yawn, can someone order some java for da customer... hey, you have a question?"
];
}
const portToUse = process.env.PORT || 8080;
const path = require('path');
const express = require('express')
const app = express()
const server = require('http').createServer(app);
const io = require('socket.io')(server);
server.listen(portToUse, () => {
console.log("Listening on port " + portToUse);
});
app.use(express.static(path.join(__dirname, 'pub')));
console.log(`Running from ${__dirname}`);
io.on('connection', (connection : Socket) => {
var fortuneTeller = new FortuneTeller(connection);
});
console.log("Server listening on port " + portToUse);
console.log('The great fortune teller awaits a wise smart person to call for sound advice.');