Skip to content

Commit

Permalink
Added to doc.go
Browse files Browse the repository at this point in the history
Also renamed some message-sending functions for clarity.
  • Loading branch information
curfman committed Feb 18, 2016
1 parent 385a252 commit 3f2935b
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 24 deletions.
13 changes: 13 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ package main
// its own source file and it is typically named room<cmd>.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
Expand Down
16 changes: 3 additions & 13 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions room.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)}
}
}
Expand Down
2 changes: 1 addition & 1 deletion roomexamine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions roomgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions roomhello.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion roominventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion roomlook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion roomwink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3f2935b

Please sign in to comment.