diff --git a/doc.go b/doc.go index 5240c51..719a03b 100644 --- a/doc.go +++ b/doc.go @@ -83,6 +83,19 @@ package main // its own source file and it is typically named room.go, as in // roomchat.go, roomlook.go, etc. +// Chat (broadcast messages) +// +// It is up to the room to implement the notion of global chat to +// all parties in a given room. This requires us to track player +// movement to/from rooms and to remember which websocket connection +// is currently associated with a given player in a given room. Tracker +// functions (tracker.go) handle this for us and they use channels to +// isolate shared data structures. BroadcastMessage(r, m, sender, +// receiver string) should be used to send broadcast messages. +// Messages to a player who is still in the room should be sent +// more directly using SendMessageToPlayer(conn *websocket.Conn, +// mUser, uid string). + // Deleting rooms // // At the time that this code was being written, Game On! did not diff --git a/message.go b/message.go index 560f80c..ab29e69 100644 --- a/message.go +++ b/message.go @@ -20,8 +20,7 @@ type PlayerMessage struct { var bookmark = 1 // Sends an event message to a player using the current websocket. -func sendMessageToPlayer(conn *websocket.Conn, mUser, uid string) (e error) { - //TODO//cleanup//locus := "SEND.MSG-TO-PLAYER" +func SendMessageToPlayer(conn *websocket.Conn, mUser, uid string) (e error) { var msg PlayerMessage var j []byte msg.Rtype = "event" @@ -35,21 +34,12 @@ func sendMessageToPlayer(conn *websocket.Conn, mUser, uid string) (e error) { if e != nil { return } - e = sendMsg(conn, uid, j, MTPlayer) - //TODO cleanup - //m := fmt.Sprintf("%s,%s,%s", MTPlayer, target, string(j)) - //e = conn.WriteMessage(ExpectedMessageType, []byte(m)) - //if config.debug { - // checkpoint(locus, fmt.Sprintf("MSG=%s", m)) - //} - //if e != nil { - // checkpoint(locus, fmt.Sprintf("FAILED err=%s", e.Error())) - //} + e = SendMessage(conn, uid, j, MTPlayer) return } // Sends a message with a JSON payload. -func sendMsg(conn *websocket.Conn, targetid string, j []byte, messageType string) (e error) { +func SendMessage(conn *websocket.Conn, targetid string, j []byte, messageType string) (e error) { locus := "SEND.MSG" var m = fmt.Sprintf("%s,%s,%s", messageType, targetid, string(j)) e = conn.WriteMessage(ExpectedMessageType, []byte(m)) diff --git a/room.go b/room.go index dc175a9..2abfa27 100644 --- a/room.go +++ b/room.go @@ -63,7 +63,7 @@ func handleSlashCommand(conn *websocket.Conn, req *GameonRequest, room string) e locus := "HANDLE.SLASH" cmd, tail, err := parseCommandPrefix(req.Content) if err != nil { - sendMessageToPlayer(conn, "What? I didn't understand that.", req.UserId) + SendMessageToPlayer(conn, "What? I didn't understand that.", req.UserId) return err } checkpoint(locus, fmt.Sprintf("cmd=%s tail=%s", cmd, tail)) @@ -79,7 +79,7 @@ func handleSlashCommand(conn *websocket.Conn, req *GameonRequest, room string) e case slashWink: return wink(conn, req, tail, room) default: - sendMessageToPlayer(conn, "What? I didn't understand that.", req.UserId) + SendMessageToPlayer(conn, "What? I didn't understand that.", req.UserId) return JSPayloadError{fmt.Sprintf("Unrecognized command: '%s'", cmd)} } } diff --git a/roomexamine.go b/roomexamine.go index 7283517..77f53dd 100644 --- a/roomexamine.go +++ b/roomexamine.go @@ -42,5 +42,5 @@ func examineObject(conn *websocket.Conn, req *GameonRequest, tail, room string) if err != nil { return err } - return sendMsg(conn, req.UserId, j, MTPlayer) + return SendMessage(conn, req.UserId, j, MTPlayer) } diff --git a/roomgo.go b/roomgo.go index abb1657..eb812be 100644 --- a/roomgo.go +++ b/roomgo.go @@ -47,14 +47,14 @@ func exitRoom(conn *websocket.Conn, req *GameonRequest, tail, room string) (e er banter = fmt.Sprintf("'%s'?!? There is no exit with that name. Try again.", dir) } - sendMessageToPlayer(conn, banter, req.UserId) + SendMessageToPlayer(conn, banter, req.UserId) if validExit { j, err := json.MarshalIndent(lresp, "", " ") if err != nil { return err } - e = sendMsg(conn, req.UserId, j, MTPlayerLocation) + e = SendMessage(conn, req.UserId, j, MTPlayerLocation) } return } diff --git a/roomhello.go b/roomhello.go index 145e867..ce201de 100644 --- a/roomhello.go +++ b/roomhello.go @@ -55,7 +55,7 @@ func handleHello(conn *websocket.Conn, req *HelloMessage, room string) (e error) mUser := fmt.Sprintf("Welcome to %s, %s. Take your time. Look around.", MyRooms[room], req.Username) - sendMessageToPlayer(conn, mUser, req.UserId) + SendMessageToPlayer(conn, mUser, req.UserId) // Send back the required response. Do not ignore these errors. var resp HelloResponse @@ -89,7 +89,7 @@ func handleHello(conn *websocket.Conn, req *HelloMessage, room string) (e error) if e != nil { return } - e = sendMsg(conn, req.UserId, j, MTPlayer) + e = SendMessage(conn, req.UserId, j, MTPlayer) return } diff --git a/roominventory.go b/roominventory.go index 1d14371..9d094d9 100644 --- a/roominventory.go +++ b/roominventory.go @@ -47,7 +47,7 @@ func checkInventory(conn *websocket.Conn, req *GameonRequest, tail, room string) if tt.msPause > 0 { time.Sleep(time.Duration(tt.msPause) * time.Millisecond) } - err = sendMsg(conn, req.UserId, j, MTPlayer) + err = SendMessage(conn, req.UserId, j, MTPlayer) if err != nil { return err } diff --git a/roomlook.go b/roomlook.go index cbb1522..074a295 100644 --- a/roomlook.go +++ b/roomlook.go @@ -42,7 +42,7 @@ func lookAroundRoom(conn *websocket.Conn, req *GameonRequest, tail, room string) if tt.msPause > 0 { time.Sleep(time.Duration(tt.msPause) * time.Millisecond) } - err = sendMsg(conn, req.UserId, j, MTPlayer) + err = SendMessage(conn, req.UserId, j, MTPlayer) if err != nil { return err } diff --git a/roomwink.go b/roomwink.go index 8710397..e1bc1e3 100644 --- a/roomwink.go +++ b/roomwink.go @@ -25,5 +25,5 @@ func wink(conn *websocket.Conn, req *GameonRequest, tail, room string) error { if err != nil { return err } - return sendMsg(conn, req.UserId, j, MTPlayer) + return SendMessage(conn, req.UserId, j, MTPlayer) } diff --git a/tracker.go b/tracker.go index 7247c26..ffd7bd4 100644 --- a/tracker.go +++ b/tracker.go @@ -142,7 +142,7 @@ func broadcast(bc *Broadcast) { fmt.Printf("BROADCAST JSON ERROR\n") return } - sendMsg(c, bc.receiver, j, MTPlayer) + SendMessage(c, bc.receiver, j, MTPlayer) } else { if config.debug { fmt.Printf("BROADCAST.%s REJECT\n", r)