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

80 lines
2.4 KiB
Go

package socket
import (
"JISQueueing/common"
"JISQueueing/db"
"encoding/json"
"github.com/gorilla/websocket"
"strconv"
)
var tickets = make(map[common.Ticket]bool)
func newTicket(ticket common.Ticket, who *websocket.Conn) {
tickets[ticket] = false
jsonTicket, _ := json.Marshal(ticket)
sendStaffMessage("new " + string(jsonTicket))
who.WriteMessage(websocket.TextMessage, []byte("success new "+string(jsonTicket)))
}
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
}
delete(tickets, ticket)
ticket.Staff = staff.Username
tickets[ticket] = true
jsonTicket, _ := json.Marshal(ticket)
sendDisplayMessage("claimed " + staff.Username + " " + strconv.Itoa(table) + " " + string(jsonTicket))
who.WriteMessage(websocket.TextMessage, []byte("success claimed "+string(jsonTicket)))
for conn, s := range connToStaff {
if s.Username != staff.Username {
conn.WriteMessage(websocket.TextMessage, []byte("claimed "+string(jsonTicket)))
}
}
}
func finishedTicket(ticket common.Ticket, id int, table int, who *websocket.Conn) {
delete(tickets, ticket)
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)))
sendDisplayMessage("complete " + strconv.Itoa(table))
}
func cancelTicket(ticket common.Ticket, id int, table int, who *websocket.Conn) {
delete(tickets, ticket)
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)))
sendDisplayMessage("cancel " + strconv.Itoa(table))
}
func newStaff(table int, who *websocket.Conn) {
who.WriteMessage(websocket.TextMessage, []byte("success pick "+strconv.Itoa(table)))
for member, num := range connToTable {
if num == -1 {
sendTaken(member)
}
}
sendDisplayMessage("pick " + strconv.Itoa(table))
}
func leaveStaff(table int) {
sendDisplayMessage("unpick " + strconv.Itoa(table))
for member, num := range connToTable {
if num == -1 {
sendTaken(member)
}
}
}