package socket import ( "JISQueueing/common" "JISQueueing/db" "github.com/gorilla/websocket" "strconv" "strings" ) //Receive Commands: [status, pick, accept, complete] //Send Commands: [new, var connToTable = make(map[*websocket.Conn]int) var connToStaff = make(map[*websocket.Conn]common.Staff) func onNewStaff(who *websocket.Conn) { connToTable[who] = -1 sendTaken(who) } func onStaffMessage(who *websocket.Conn, msg string) { message := strings.SplitN(strings.TrimSpace(msg), " ", 2) switch message[0] { case "unpick": if table, ok := connToTable[who]; !ok || table == -1 { who.WriteMessage(websocket.TextMessage, []byte("error unpick you do not have a table assigned")) return } connToTable[who] = -1 delete(connToStaff, who) who.WriteMessage(websocket.TextMessage, []byte("success unpick")) leaveStaff(connToTable[who]) case "pick": args := strings.SplitN(msg, " ", 3) if len(args) < 3 { return } choice, err := strconv.Atoi(strings.TrimSpace(args[1])) if err != nil { return } staff, err := db.GetTokenOwner(strings.TrimSpace(args[2])) if err == db.ErrTokenNotFound { who.WriteMessage(websocket.TextMessage, []byte("error pick "+err.Error())) return } if isTaken(choice) { who.WriteMessage(websocket.TextMessage, []byte("error the chosen table is not available")) return } connToStaff[who] = staff connToTable[who] = choice newStaff(choice, who) case "accept": id, err := strconv.Atoi(strings.TrimSpace(message[1])) if err != nil { return } staff, ok := connToStaff[who] if !ok { who.WriteMessage(websocket.TextMessage, []byte("error accept your session token was found to be invalid. please relogin")) return } if connToTable[who] == -1 { who.WriteMessage(websocket.TextMessage, []byte("error accept you do not have a table chosen")) } ticket, err := db.GetTicket(id) if err == db.ErrTicketNotFound { who.WriteMessage(websocket.TextMessage, []byte("error accept "+err.Error())) return } if ticket.Staff != "{}" { who.WriteMessage(websocket.TextMessage, []byte("error accept this ticket is already claimed")) return } claimedTicket(ticket, staff, connToTable[who], who) case "complete": id, err := strconv.Atoi(strings.TrimSpace(message[1])) staff := connToStaff[who] table, ok := connToTable[who] if err != nil { return } if !ok { who.WriteMessage(websocket.TextMessage, []byte("error complete you are not assigned to any table")) } ticket, err := db.GetTicket(id) if err == db.ErrTicketNotFound { who.WriteMessage(websocket.TextMessage, []byte("error complete "+err.Error())) return } if staff.Username != ticket.Staff { who.WriteMessage(websocket.TextMessage, []byte("error complete you do not own this ticket")) return } finishedTicket(ticket, id, table, who) case "cancel": id, err := strconv.Atoi(strings.TrimSpace(message[1])) staff := connToStaff[who] table, ok := connToTable[who] if err != nil { return } if !ok { who.WriteMessage(websocket.TextMessage, []byte("error cancel you are not assigned to any table")) } ticket, err := db.GetTicket(id) if err == db.ErrTicketNotFound { who.WriteMessage(websocket.TextMessage, []byte("error cancel "+err.Error())) return } if staff.Username != ticket.Staff { who.WriteMessage(websocket.TextMessage, []byte("error cancel you do not own this ticket")) return } cancelTicket(ticket, id, table, who) default: who.WriteMessage(websocket.TextMessage, []byte("error "+message[0]+" not found")) } } func sendStaffMessage(msg string) { for who, num := range connToTable { if num == -1 { continue } who.WriteMessage(websocket.TextMessage, []byte(msg)) } } func onStaffDisconnect(who *websocket.Conn) { num, ok := connToTable[who] delete(connToTable, who) delete(connToStaff, who) if ok && num != -1 { leaveStaff(connToTable[who]) } } func sendTaken(who *websocket.Conn) { var taken = "" for _, num := range connToTable { if num != -1 { taken = taken + " " + strconv.Itoa(num) } } if taken != "" { who.WriteMessage(websocket.TextMessage, []byte("taken"+taken)) } } func isTaken(table int) bool { if table < 0 { return true } for _, num := range connToTable { if table == num { return true } } return false }