-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
205 lines (194 loc) · 6.31 KB
/
server.js
File metadata and controls
205 lines (194 loc) · 6.31 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const http = require("http");
const path = require("path");
const socketio = require("socket.io");
const dotenv = require("dotenv");
const express = require("express");
const session = require("cookie-session");
dotenv.config({ path: "./config.env" });
const foodList = require("./food.json");
const formatMessage = require("./utils/formatMessage.js");
const app = express();
const server = http.createServer(app);
const io = socketio(server);
const publicDirectoryPath = path.join(__dirname, "./public");
app.use(express.static(publicDirectoryPath));
const PORT = process.env.PORT || 3000;
let sum = null;
const welcomeList = [
"Select 1 to Place an order",
"Select 99 to checkout order",
"Select 98 to see order history",
"Select 97 to see current order",
"Select 0 to cancel order",
];
const MENU = [
"Bacon cheeseburger",
"Chicken alfredo pasta",
"Caesar salad",
"Margherita pizza",
"Fish and chips",
"Grilled salmon",
"New York-style cheesecake",
"Chocolate lava cake",
"Chicken wings",
];
let history = [];
let cart = [];
const sessionMiddleWare = session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: true,
});
app.use(sessionMiddleWare);
io.use((socket, next) => {
return sessionMiddleWare(socket.request, socket.request.res, next);
});
io.on("connection", (socket) => {
console.log("a user connected", socket.id);
let session = socket.id;
let input = "";
socket.emit("welcome-message", {
welcomeList,
session,
input,
MENU,
history,
action: "welcome",
cart,
formatMessage: formatMessage("admin", welcomeList),
});
socket.on("chatMessage", (msg) => {
let userMealName = "";
let userInput = null;
let userMeal = {};
if (session === msg.session) {
socket.emit("admin-message", formatMessage("You", msg.input));
if (msg["action"] === "welcome") {
console.log(session);
if (msg.input === "98") {
if (msg.history.length === 0) {
socket.emit(
"admin-message",
formatMessage("admin", ["Oops", "Your history is empty!!!"])
);
} else {
// socket.emit("admin-message", ["Order history!!!", "Cheers!"]);
socket.emit("history", formatMessage("admin", msg.history));
return;
}
} else if (msg.input === "99") {
if (msg["cart"].length < 1) {
socket.emit(
"admin-message",
formatMessage("admin", ["Oops", "Your cart is empty!!!"])
);
return;
}
msg["history"].push(msg.cart);
socket.emit(
"admin-message",
formatMessage("admin", ["Order placed!!!", "Cheers!"])
);
} else if (msg.input === "97") {
console.log("Here");
if (msg["cart"].length < 1) {
socket.emit(
"admin-message",
formatMessage("admin", ["Oops", "Your cart is empty!!!"])
);
console.log("Are you here");
return;
}
socket.emit(
"admin-message",
formatMessage("admin", ["Current order:", msg.cart[-1]])
);
socket.emit(
"admin-message",
formatMessage("admin", ["Current order:", msg.cart[-1]])
);
} else if (msg.input === "0") {
if (msg["cart"].length < 1) {
socket.emit(
"admin-message",
formatMessage("admin", ["Oops", "Your cart is empty!!!"])
);
return;
}
socket.emit(
"admin-message",
formatMessage("admin", ["Order cancelled!!!", "Cheers!"])
);
msg.cart = [];
} else if (msg.input === "1") {
msg.action = "subMenu";
socket.emit("menu", formatMessage("admin", MENU));
}
} else if (msg.action === "subMenu" && !msg.checkOut) {
userMealName = msg.MENU[parseInt(msg.input) - 1];
console.log(userMealName);
userMeal = foodList[userMealName];
// { special: [ true, 'fries', 100 ], amount: 500 }
if (userMeal) {
let isSpecial = userMeal["special"];
let toSelect = [userMealName];
if (isSpecial[0]) {
toSelect.push(
`Select 1 to checkout without ${isSpecial[1]} - price = #${userMeal.amount}`
);
toSelect.push(
`Select 2 to checkout with ${isSpecial[1]} - +fee. #${
isSpecial[2] + userMeal.amount
}`
);
} else {
toSelect.push(
`Select 1 to checkout, total price - #${userMeal.amount}`
);
}
msg["checkOut"] = userMeal;
msg["userMealName"] = userMealName;
msg.action = "checkOut"
socket.emit("admin-message", formatMessage("admin", toSelect));
} else {
msg.action = "welcome";
socket.emit("admin-message", formatMessage("admin", "Invalid input"));
}
} else if (msg.checkOut && msg.action === "checkOut") {
let cartItem = [msg.userMealName];
let checkOutMessage = `Cheers! ${msg.userMealName} has been put in your shopping cart...`;
if (msg.input === "1") {
cartItem.push(msg["checkOut"].amount);
} else if (msg.input === "2") {
cartItem.push(msg["checkOut"].amount + msg["checkOut"]["special"][2]);
}
if (!cartItem[1]) {
socket.emit(
"admin-message",
formatMessage(
"admin",
`Invalid input... Select right option to continue`
)
);
return;
} else {
msg["cart"] = cartItem;
msg["action"] = "welcome";
socket.emit("admin-message", formatMessage("admin", checkOutMessage));
}
}
/*userInput = msg.input;
if (userInput !== "1" && userInput !== "98" && userInput !== "99" && userInput !== "97" && userInput !== "0" && (msg.action === "welcome" || msg.action === "subMenu")) {
socket.emit(
"admin-message",
formatMessage("admin", "Invalid input entered")
);
}*/
}
// msg.formatTime = formatTime();
socket.emit("saveToStorage", msg);
});
});
server.listen(PORT, () => {
console.log(`Server is up on port ${PORT}`);
});