implement subscribe_mp_complete_match
This commit is contained in:
parent
6925ce4c6e
commit
b7c00722de
|
@ -56,11 +56,11 @@ func handler(rawConn *websocket.Conn) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type conn struct {
|
type conn struct {
|
||||||
Conn *websocket.Conn
|
Conn *websocket.Conn
|
||||||
Mtx sync.Mutex
|
Mtx sync.Mutex
|
||||||
ID uint64
|
ID uint64
|
||||||
RestrictedVisible bool
|
RestrictedVisible bool
|
||||||
User *websocketUser
|
User *websocketUser
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *conn) WriteJSON(t string, data interface{}) error {
|
func (c *conn) WriteJSON(t string, data interface{}) error {
|
||||||
|
@ -72,6 +72,7 @@ func (c *conn) WriteJSON(t string, data interface{}) error {
|
||||||
|
|
||||||
var messageHandler = map[string]func(c *conn, message incomingMessage){
|
var messageHandler = map[string]func(c *conn, message incomingMessage){
|
||||||
TypeSubscribeScores: SubscribeScores,
|
TypeSubscribeScores: SubscribeScores,
|
||||||
|
TypeSubscribeMultiMatches: SubscribeMultiMatches,
|
||||||
TypeSetRestrictedVisibility: SetRestrictedVisibility,
|
TypeSetRestrictedVisibility: SetRestrictedVisibility,
|
||||||
TypeIdentify: Identify,
|
TypeIdentify: Identify,
|
||||||
TypePing: pingHandler,
|
TypePing: pingHandler,
|
||||||
|
@ -79,20 +80,23 @@ var messageHandler = map[string]func(c *conn, message incomingMessage){
|
||||||
|
|
||||||
// Server Message Types
|
// Server Message Types
|
||||||
const (
|
const (
|
||||||
TypeConnected = "connected"
|
TypeConnected = "connected"
|
||||||
TypeInvalidMessage = "invalid_message_type"
|
TypeInvalidMessage = "invalid_message_type"
|
||||||
TypeUnexpectedError = "unexpected_error"
|
TypeUnexpectedError = "unexpected_error"
|
||||||
TypeNotFound = "not_found"
|
TypeNotFound = "not_found"
|
||||||
TypeSubscribedToScores = "subscribed_to_scores"
|
TypeSubscribedToScores = "subscribed_to_scores"
|
||||||
TypeNewScore = "new_score"
|
TypeNewScore = "new_score"
|
||||||
TypeIdentified = "identified"
|
TypeSubscribedToMultiMatches = "subscribed_mp_complete_match"
|
||||||
TypeRestrictedVisibilitySet = "restricted_visibility_set"
|
TypeNewMatch = "new_completed_match"
|
||||||
TypePong = "pong"
|
TypeIdentified = "identified"
|
||||||
|
TypeRestrictedVisibilitySet = "restricted_visibility_set"
|
||||||
|
TypePong = "pong"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Client Message Types
|
// Client Message Types
|
||||||
const (
|
const (
|
||||||
TypeSubscribeScores = "subscribe_scores"
|
TypeSubscribeScores = "subscribe_scores"
|
||||||
|
TypeSubscribeMultiMatches = "subscribe_mp_complete_match"
|
||||||
TypeIdentify = "identify"
|
TypeIdentify = "identify"
|
||||||
TypeSetRestrictedVisibility = "set_restricted_visibility"
|
TypeSetRestrictedVisibility = "set_restricted_visibility"
|
||||||
TypePing = "ping"
|
TypePing = "ping"
|
||||||
|
|
57
app/websockets/multi.go
Normal file
57
app/websockets/multi.go
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
package websockets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SubscribeMultiMatches subscribes to receiving information from completed
|
||||||
|
// games in multiplayer matches.
|
||||||
|
func SubscribeMultiMatches(c *conn, message incomingMessage) {
|
||||||
|
multiSubscriptionsMtx.Lock()
|
||||||
|
var found bool
|
||||||
|
for _, el := range multiSubscriptions {
|
||||||
|
if el.ID == c.ID {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if it was not found, we need to add it
|
||||||
|
if !found {
|
||||||
|
multiSubscriptions = append(multiSubscriptions, c)
|
||||||
|
}
|
||||||
|
multiSubscriptionsMtx.Unlock()
|
||||||
|
|
||||||
|
c.WriteJSON(TypeSubscribedToMultiMatches, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
var multiSubscriptions []*conn
|
||||||
|
var multiSubscriptionsMtx = new(sync.RWMutex)
|
||||||
|
|
||||||
|
func matchRetriever() {
|
||||||
|
ps, err := red.Subscribe("api:mp_complete_match")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
msg, err := ps.ReceiveMessage()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go handleNewMultiGame(msg.Payload)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleNewMultiGame(payload string) {
|
||||||
|
defer catchPanic()
|
||||||
|
multiSubscriptionsMtx.RLock()
|
||||||
|
cp := make([]*conn, len(multiSubscriptions))
|
||||||
|
copy(cp, multiSubscriptions)
|
||||||
|
multiSubscriptionsMtx.RUnlock()
|
||||||
|
|
||||||
|
for _, el := range cp {
|
||||||
|
el.WriteJSON(TypeNewMatch, json.RawMessage(payload))
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,6 +16,7 @@ func Start(r *redis.Client, _db *sqlx.DB) error {
|
||||||
red = r
|
red = r
|
||||||
db = _db
|
db = _db
|
||||||
go scoreRetriever()
|
go scoreRetriever()
|
||||||
|
go matchRetriever()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,4 +30,13 @@ func cleanup(connID uint64) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
scoreSubscriptionsMtx.Unlock()
|
scoreSubscriptionsMtx.Unlock()
|
||||||
|
multiSubscriptionsMtx.Lock()
|
||||||
|
for idx, el := range multiSubscriptions {
|
||||||
|
if el.ID == connID {
|
||||||
|
multiSubscriptions[idx] = multiSubscriptions[len(multiSubscriptions)-1]
|
||||||
|
multiSubscriptions = multiSubscriptions[:len(multiSubscriptions)-1]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
multiSubscriptionsMtx.Unlock()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user