241 lines
5.4 KiB
Go
241 lines
5.4 KiB
Go
package socket
|
|
|
|
import (
|
|
"JISQueueing/common"
|
|
"JISQueueing/db"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
//Receive Commands: [status, pick, accept, complete]
|
|
//Send Commands: [new,
|
|
|
|
type staffConnection struct {
|
|
Conn *websocket.Conn
|
|
Staff common.Staff
|
|
Table int
|
|
Status int
|
|
CurrentTicket int
|
|
}
|
|
|
|
var onlineStaff = make(map[*websocket.Conn]staffConnection)
|
|
|
|
func onNewStaff(who *websocket.Conn) {
|
|
onlineStaff[who] = staffConnection{
|
|
Conn: who,
|
|
Staff: common.Staff{
|
|
Username: "{}",
|
|
},
|
|
Table: -1,
|
|
Status: 0,
|
|
CurrentTicket: -1,
|
|
}
|
|
sendTaken(who)
|
|
}
|
|
|
|
func onStaffMessage(who *websocket.Conn, msg string) {
|
|
message := strings.SplitN(strings.TrimSpace(msg), " ", 2)
|
|
switch message[0] {
|
|
|
|
case "accept":
|
|
id, err := strconv.Atoi(strings.TrimSpace(message[1]))
|
|
conn, ok := onlineStaff[who]
|
|
|
|
if err != nil || !ok {
|
|
return
|
|
}
|
|
if conn.Staff.Username == "{}" || conn.Status == 0 {
|
|
who.WriteMessage(websocket.TextMessage, []byte("error accept your session token was found to be invalid. please relogin"))
|
|
return
|
|
}
|
|
if conn.Table == -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
|
|
}
|
|
|
|
conn.Status = 2
|
|
conn.CurrentTicket = id
|
|
onlineStaff[who] = conn
|
|
claimedTicket(ticket, conn.Staff, conn.Table, who)
|
|
|
|
case "cancel":
|
|
id, err := strconv.Atoi(strings.TrimSpace(message[1]))
|
|
conn, ok := onlineStaff[who]
|
|
|
|
if err != nil || !ok {
|
|
return
|
|
}
|
|
|
|
ticket, err := db.GetTicket(id)
|
|
if err == db.ErrTicketNotFound {
|
|
who.WriteMessage(websocket.TextMessage, []byte("error cancel "+err.Error()))
|
|
return
|
|
}
|
|
if conn.Staff.Username != ticket.Staff {
|
|
who.WriteMessage(websocket.TextMessage, []byte("error cancel you do not own this ticket"))
|
|
return
|
|
}
|
|
|
|
conn.Status = 1
|
|
conn.CurrentTicket = -1
|
|
onlineStaff[who] = conn
|
|
|
|
cancelTicket(id, conn.Table, who)
|
|
|
|
case "complete":
|
|
id, err := strconv.Atoi(strings.TrimSpace(message[1]))
|
|
conn, ok := onlineStaff[who]
|
|
|
|
if err != nil || !ok {
|
|
return
|
|
}
|
|
|
|
ticket, err := db.GetTicket(id)
|
|
if err == db.ErrTicketNotFound {
|
|
who.WriteMessage(websocket.TextMessage, []byte("error complete "+err.Error()))
|
|
return
|
|
}
|
|
if conn.Staff.Username != ticket.Staff {
|
|
who.WriteMessage(websocket.TextMessage, []byte("error complete you do not own this ticket"))
|
|
return
|
|
}
|
|
|
|
conn.Status = 1
|
|
conn.CurrentTicket = -1
|
|
onlineStaff[who] = conn
|
|
|
|
finishedTicket(id, conn.Table, 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
|
|
}
|
|
if onlineStaff[who].Table != -1 {
|
|
leaveStaff(onlineStaff[who].Table)
|
|
}
|
|
|
|
onlineStaff[who] = staffConnection{
|
|
Conn: who,
|
|
Staff: staff,
|
|
Table: choice,
|
|
Status: 1,
|
|
CurrentTicket: -1,
|
|
}
|
|
newStaff(choice, who)
|
|
|
|
case "status":
|
|
if conn, ok := onlineStaff[who]; !ok || conn.Table == -1 {
|
|
who.WriteMessage(websocket.TextMessage, []byte("error unpick you do not have a table assigned"))
|
|
return
|
|
}
|
|
notifyTicket(who)
|
|
|
|
case "unpick":
|
|
conn, ok := onlineStaff[who]
|
|
if !ok || conn.Table == -1 {
|
|
who.WriteMessage(websocket.TextMessage, []byte("error unpick you do not have a table assigned"))
|
|
return
|
|
}
|
|
|
|
table := conn.Table
|
|
conn.Table = -1
|
|
conn.Status = 0
|
|
|
|
staff := conn.Staff
|
|
staff.Username = "{}"
|
|
conn.Staff = staff
|
|
|
|
onlineStaff[who] = conn
|
|
leaveStaff(table)
|
|
who.WriteMessage(websocket.TextMessage, []byte("success unpick"))
|
|
case "valid":
|
|
if len(message) < 2 {
|
|
return
|
|
}
|
|
|
|
_, err := db.GetTokenOwner(strings.TrimSpace(message[1]))
|
|
if err == db.ErrTokenNotFound {
|
|
who.WriteMessage(websocket.TextMessage, []byte("error valid "+err.Error()))
|
|
return
|
|
}
|
|
who.WriteMessage(websocket.TextMessage, []byte("success valid"))
|
|
|
|
default:
|
|
who.WriteMessage(websocket.TextMessage, []byte("error "+message[0]+" not found"))
|
|
}
|
|
}
|
|
|
|
func sendStaffMessage(msg string) {
|
|
for who, conn := range onlineStaff {
|
|
if conn.Table == -1 {
|
|
continue
|
|
}
|
|
who.WriteMessage(websocket.TextMessage, []byte(msg))
|
|
}
|
|
}
|
|
|
|
func onStaffDisconnect(who *websocket.Conn) {
|
|
conn, ok := onlineStaff[who]
|
|
if !ok || conn.Table == -1 {
|
|
return
|
|
}
|
|
if conn.Status == 2 {
|
|
cancelTicket(conn.CurrentTicket, conn.Table, who)
|
|
}
|
|
|
|
delete(onlineStaff, who)
|
|
leaveStaff(conn.Table)
|
|
}
|
|
|
|
func sendTaken(who *websocket.Conn) {
|
|
var taken = ""
|
|
for _, conn := range onlineStaff {
|
|
if conn.Table != -1 {
|
|
taken = taken + " " + strconv.Itoa(conn.Table)
|
|
}
|
|
}
|
|
if taken != "" {
|
|
who.WriteMessage(websocket.TextMessage, []byte("info taken"+taken))
|
|
} else {
|
|
who.WriteMessage(websocket.TextMessage, []byte("info taken"))
|
|
}
|
|
}
|
|
|
|
func isTaken(table int) bool {
|
|
if table < 1 || table > 5 {
|
|
return true
|
|
}
|
|
for _, conn := range onlineStaff {
|
|
if table == conn.Table {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|