Browse Source

Finished support for sending presense/game status updates. Closes #18

Bruce Marriner 9 years ago
parent
commit
383b4645da
1 changed files with 30 additions and 11 deletions
  1. 30 11
      wsapi.go

+ 30 - 11
wsapi.go

@@ -56,24 +56,43 @@ func (s *Session) Handshake() (err error) {
 	return
 }
 
+type updateStatusData struct {
+	IdleSince json.Token `json:"idle_since"`
+	GameID    json.Token `json:"game_id"`
+}
+
+type updateStatusOp struct {
+	Op   int              `json:"op"`
+	Data updateStatusData `json:"d"`
+}
+
 // UpdateStatus is used to update the authenticated user's status.
-func (s *Session) UpdateStatus(idleSince, gameID string) (err error) {
+// If idle>0 then set status to idle.  If game>0 then set game.
+// if otherwise, set status to active, and no game.
+func (s *Session) UpdateStatus(idle int, gameID int) (err error) {
+
+	var usd updateStatusData
+	if idle > 0 {
+		usd.IdleSince = idle
+	} else {
+		usd.IdleSince = nil
+	}
 
-	err = s.wsConn.WriteJSON(map[string]interface{}{
-		"op": 2,
-		"d": map[string]interface{}{
-			"idle_since": idleSince,
-			"game_id":    gameID,
-		},
-	})
+	if gameID >= 0 {
+		usd.GameID = gameID
+	} else {
+		usd.GameID = nil
+	}
+
+	data := updateStatusOp{3, usd}
+	err = s.wsConn.WriteJSON(data)
 	return
 }
 
-// TODO: need a channel or something to communicate
-// to this so I can tell it to stop listening
-
 // Listen starts listening to the websocket connection for events.
 func (s *Session) Listen() (err error) {
+	// TODO: need a channel or something to communicate
+	// to this so I can tell it to stop listening
 
 	if s.wsConn == nil {
 		fmt.Println("No websocket connection exists.")