GameTableServer: implement 2-level priveleges for publishing messages, publish map image changes

This commit is contained in:
Iris Lightshard 2023-05-05 20:55:07 -06:00
parent 8f4eb7e958
commit 1fd828f6fd
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398

View file

@ -34,6 +34,7 @@ type GameTableServer struct {
subscribersLock sync.Mutex subscribersLock sync.Mutex
subscribers map[*Subscriber]models.TableKey subscribers map[*Subscriber]models.TableKey
dbAdapter mongodb.DbAdapter dbAdapter mongodb.DbAdapter
udb auth.UserStore
} }
func New(adapter mongodb.DbAdapter, udb auth.UserStore, uploads string, uploadMaxMB int) *GameTableServer { func New(adapter mongodb.DbAdapter, udb auth.UserStore, uploads string, uploadMaxMB int) *GameTableServer {
@ -43,6 +44,7 @@ func New(adapter mongodb.DbAdapter, udb auth.UserStore, uploads string, uploadMa
subscribers: make(map[*Subscriber]models.TableKey), subscribers: make(map[*Subscriber]models.TableKey),
publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8), publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
dbAdapter: adapter, dbAdapter: adapter,
udb: udb,
} }
srvr.serveMux.Handle("/table/", http.StripPrefix("/table/", renderer.Subtree("./static"))) srvr.serveMux.Handle("/table/", http.StripPrefix("/table/", renderer.Subtree("./static")))
srvr.serveMux.Handle("/uploads/", http.StripPrefix("/uploads/", renderer.Subtree(uploads))) srvr.serveMux.Handle("/uploads/", http.StripPrefix("/uploads/", renderer.Subtree(uploads)))
@ -162,8 +164,12 @@ func (self *GameTableServer) publish(msg []byte) {
return return
} }
self.writeToDB(tableMsg) err = self.writeToDB(tableMsg)
if err != nil {
fmt.Println(err.Error())
return
}
self.publishLimiter.Wait(context.Background()) self.publishLimiter.Wait(context.Background())
for s, k := range self.subscribers { for s, k := range self.subscribers {
@ -175,6 +181,7 @@ func (self *GameTableServer) publish(msg []byte) {
} }
} }
} }
} }
func (self *GameTableServer) getCurrentState(tableKey models.TableKey) []byte { func (self *GameTableServer) getCurrentState(tableKey models.TableKey) []byte {
@ -210,7 +217,20 @@ func (self *GameTableServer) writeToDB(tableMsg models.TableMessage) error {
if tableMsg.DiceRoll != nil { if tableMsg.DiceRoll != nil {
err := self.dbAdapter.InsertDiceRoll(key, *tableMsg.DiceRoll) err := self.dbAdapter.InsertDiceRoll(key, *tableMsg.DiceRoll)
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
}
}
// map image change and token addition/removal require admin authorization
if tableMsg.Auth != nil {
authorized, _ := self.udb.ValidateToken(*tableMsg.Auth)
if authorized {
if tableMsg.MapImg != nil {
err := self.dbAdapter.SetMapImageUrl(key, *tableMsg.MapImg)
if err != nil {
return err
}
}
} }
} }
return nil return nil