package server import ( "fmt" "strconv" "strings" "github.com/gorilla/websocket" "TerraOceanBot/db" ) var clients = make(map[*websocket.Conn]bool) var connectedUsers = make(map[string]bool) func newSession(who *websocket.Conn) { clients[who] = true who.WriteMessage(websocket.TextMessage, []byte("sync")) } func disconnectSession(who *websocket.Conn) { delete(clients, who) } func handleIncomingMessage(who *websocket.Conn, msg string) { message := strings.SplitN(msg, " ", 2) switch message[0] { case "query": if len(message) < 2 { return } if !connectedUsers[message[1]] { fmt.Println(message[1], "tried to connect. Disconnecting!") fmt.Println(connectedUsers) who.WriteMessage(websocket.TextMessage, []byte("leave "+message[1])) } case "active": if len(message) < 3 { return } arg := strings.Split(msg, " ") arg[1] = strings.Join(arg[1:], " ") id, err := db.GetDiscordID(arg[1]) if err != nil { fmt.Println(err) return } value, err := strconv.Atoi(arg[len(arg)-1]) if err != nil { fmt.Println(err) return } err = db.AddActiveLevel(id, value) if err != nil { fmt.Println(err) return } default: fmt.Println("Unknown message:" + msg) } } func broadcast(message string) { for k, _ := range clients { k.WriteMessage(websocket.TextMessage, []byte(message)) } } func ConnectUser(mcUsername string) { connectedUsers[mcUsername] = true } func DisconnectUser(mcUsername string) { delete(connectedUsers, mcUsername) broadcast("leave " + mcUsername) } func RequestSync() { broadcast("sync") } func VoteNotification() { broadcast("vote") } func WhitelistAdd(minecraftUsername string) { broadcast("whitelistadd " + minecraftUsername) } func WhitelistRemove(minecraftUsername string) { broadcast("whitelistremove " + minecraftUsername) }