This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
JISQueueing/server/socket/hub.go

106 lines
3.0 KiB
Go

package socket
import (
"JISQueueing/common"
"JISQueueing/db"
"encoding/json"
"strconv"
"github.com/gorilla/websocket"
)
2020-01-31 03:03:02 +07:00
var queue = make([]common.Ticket, 0)
2020-02-05 04:55:03 +07:00
var config common.Configuration
func SetConfiguration(configFile common.Configuration) {
config = configFile
}
func newTicket(ticket common.Ticket, who *websocket.Conn) {
2020-01-31 03:03:02 +07:00
queue = append(queue, ticket)
jsonTicket, _ := json.Marshal(ticket)
who.WriteMessage(websocket.TextMessage, []byte("success new "+string(jsonTicket)))
2020-01-31 03:03:02 +07:00
for _, conn := range onlineStaff {
if conn.Status == 1 {
notifyTicket(conn.Conn)
}
}
2020-01-31 03:24:23 +07:00
sendMessageToRedisChannel(Message{ticket.ID, ticket.Name, ticket.Email})
}
func claimedTicket(ticket common.Ticket, staff common.Staff, table int, who *websocket.Conn) {
if !db.SetClaimer(ticket.ID, staff.Username) {
who.WriteMessage(websocket.TextMessage, []byte("error accept the ticket has already been completed or cancelled"))
return
}
2020-01-31 03:03:02 +07:00
removeTicket(ticket.ID)
ticket.Staff = staff.Username
jsonTicket, _ := json.Marshal(ticket)
sendDisplayMessage("claimed " + strconv.Itoa(table) + " " + strconv.Itoa(ticket.ID))
who.WriteMessage(websocket.TextMessage, []byte("success claimed "+string(jsonTicket)))
2020-01-31 03:03:02 +07:00
for _, conn := range onlineStaff {
if conn.Status == 1 {
conn.Conn.WriteMessage(websocket.TextMessage, []byte("info claimed "+string(jsonTicket)))
notifyTicket(conn.Conn)
}
}
}
2020-01-31 03:03:02 +07:00
func finishedTicket(id int, table int, who *websocket.Conn) {
if success := db.FinishTicket(id); !success {
who.WriteMessage(websocket.TextMessage, []byte("error complete the ticket has already been completed or cancelled"))
return
}
who.WriteMessage(websocket.TextMessage, []byte("success complete "+strconv.Itoa(id)))
2020-01-31 03:03:02 +07:00
notifyTicket(who)
2020-02-05 04:55:03 +07:00
//ticket, _ := db.GetTicket(id)
sendDisplayMessage("complete " + strconv.Itoa(table))
}
2020-01-31 03:03:02 +07:00
func cancelTicket(id int, table int, who *websocket.Conn) {
if success := db.CancelTicket(id); !success {
who.WriteMessage(websocket.TextMessage, []byte("error cancel the ticket has already been completed or cancelled"))
return
}
who.WriteMessage(websocket.TextMessage, []byte("success cancel "+strconv.Itoa(id)))
2020-01-31 03:03:02 +07:00
notifyTicket(who)
sendDisplayMessage("cancel " + strconv.Itoa(table))
}
func newStaff(table int, who *websocket.Conn) {
who.WriteMessage(websocket.TextMessage, []byte("success pick "+strconv.Itoa(table)))
2020-01-31 03:03:02 +07:00
for _, conn := range onlineStaff {
sendTaken(conn.Conn)
}
sendDisplayMessage("pick " + strconv.Itoa(table))
2020-01-31 03:03:02 +07:00
notifyTicket(who)
}
func leaveStaff(table int) {
sendDisplayMessage("unpick " + strconv.Itoa(table))
2020-01-31 03:03:02 +07:00
for _, conn := range onlineStaff {
sendTaken(conn.Conn)
}
}
func notifyTicket(who *websocket.Conn) {
if len(queue) == 0 {
return
}
jsonTicket, _ := json.Marshal(queue[0])
who.WriteMessage(websocket.TextMessage, []byte("info new "+string(jsonTicket)))
}
func removeTicket(id int) {
for i, t := range queue {
if id == t.ID {
copy(queue[i:], queue[i+1:])
queue[len(queue)-1] = common.Ticket{}
queue = queue[:len(queue)-1]
return
}
}
}