-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
103 lines (85 loc) · 2.64 KB
/
server.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
require('dotenv').config()
const http = require("http")
const app = require("./app")
const socketio = require("socket.io")
const formatMessage = require("./utils/formatMessage")
const bot = "bot"
const server = http.createServer(app)
const io = socketio(server, { cors: { origin: "*" } })
const sessionMiddleware = require("./middleware/expressSession")
const {
commandMessages,
getOrderAndQuantity,
getItemsIndex
} = require("./utils/order")
const {
getOrder,
placeOrder,
checkOutOrder,
orderHistory,
currentOrder,
cancelOrder,
invalidReply
} = require("./controller/order")
// using session middleware with express app and socket.io server
app.use(sessionMiddleware)
io.use(function (socket, next) {
sessionMiddleware(socket.request, socket.request.res || {}, next)
})
// run when a client connects
io.on("connection", async (socket) => {
console.log(`User ${socket.id} connected`)
/**
* socket.request.session becomes available for session storage
* due to the express-session middleware that was used and linked
* with the socket.io server
*/
const session = socket.request.session
// Store order in session
session.order = { items: {}, total: 0 }
// Welcome current client and show order
socket.emit(
"message",
formatMessage(
`Welcome to OrderBot, <br> ${commandMessages.join("<br>")}`,
bot
)
)
// Listen on server for reply from client
socket.on("reply", async (msg) => {
const userReply = msg
// if user respponse is 1
if (userReply === "1") {
getOrder(io, socket, userReply)
}
// if user response is the key (item number) of the item
else if (
getItemsIndex().hasOwnProperty(getOrderAndQuantity(userReply).order)
) {
placeOrder(io, socket, userReply, session)
}
// if user reply is 99, checkout the order
else if (userReply === "99") {
checkOutOrder(io, socket, userReply, session)
}
else if (userReply === "97") {
currentOrder(io, socket, userReply, session)
}
// cancel order and set session order to null
else if (userReply === "0") {
cancelOrder(io, socket, userReply, session)
// all other values asides 98 and special values above
} else if (userReply !== "98") {
invalidReply(io, socket, userReply)
}
})
socket.on("printOrderHistory", async (order) => {
userReply = order.msg
// order history modified to check by month or year
if (userReply.split(",")[0] === "98") {
orderHistory(io, socket, userReply, order.orderHistory)
}
})
})
const PORT = process.env.PORT || 3000
server.listen(PORT, () => console.log(`Server running on port ${PORT}`))