package socket import ( "JISQueueing/common" "JISQueueing/db" "encoding/json" "strconv" "github.com/gorilla/websocket" ) var queue = make([]common.Ticket, 0) var config common.Configuration func SetConfiguration(configFile common.Configuration) { config = configFile } func newTicket(ticket common.Ticket, who *websocket.Conn) { queue = append(queue, ticket) jsonTicket, _ := json.Marshal(ticket) who.WriteMessage(websocket.TextMessage, []byte("success new "+string(jsonTicket))) for _, conn := range onlineStaff { if conn.Status == 1 { notifyTicket(conn.Conn) } } 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 } 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))) for _, conn := range onlineStaff { if conn.Status == 1 { conn.Conn.WriteMessage(websocket.TextMessage, []byte("info claimed "+string(jsonTicket))) notifyTicket(conn.Conn) } } } 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))) notifyTicket(who) //ticket, _ := db.GetTicket(id) sendDisplayMessage("complete " + strconv.Itoa(table)) } 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))) notifyTicket(who) sendDisplayMessage("cancel " + strconv.Itoa(table)) } func newStaff(table int, who *websocket.Conn) { who.WriteMessage(websocket.TextMessage, []byte("success pick "+strconv.Itoa(table))) for _, conn := range onlineStaff { sendTaken(conn.Conn) } sendDisplayMessage("pick " + strconv.Itoa(table)) notifyTicket(who) } func leaveStaff(table int) { sendDisplayMessage("unpick " + strconv.Itoa(table)) 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 } } }