GameTableServer: implement 2-level priveleges for publishing messages, publish map image changes
This commit is contained in:
parent
8f4eb7e958
commit
1fd828f6fd
1 changed files with 22 additions and 2 deletions
|
@ -34,6 +34,7 @@ type GameTableServer struct {
|
|||
subscribersLock sync.Mutex
|
||||
subscribers map[*Subscriber]models.TableKey
|
||||
dbAdapter mongodb.DbAdapter
|
||||
udb auth.UserStore
|
||||
}
|
||||
|
||||
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),
|
||||
publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
|
||||
dbAdapter: adapter,
|
||||
udb: udb,
|
||||
}
|
||||
srvr.serveMux.Handle("/table/", http.StripPrefix("/table/", renderer.Subtree("./static")))
|
||||
srvr.serveMux.Handle("/uploads/", http.StripPrefix("/uploads/", renderer.Subtree(uploads)))
|
||||
|
@ -162,8 +164,12 @@ func (self *GameTableServer) publish(msg []byte) {
|
|||
return
|
||||
}
|
||||
|
||||
self.writeToDB(tableMsg)
|
||||
err = self.writeToDB(tableMsg)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
self.publishLimiter.Wait(context.Background())
|
||||
|
||||
for s, k := range self.subscribers {
|
||||
|
@ -175,6 +181,7 @@ func (self *GameTableServer) publish(msg []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 {
|
||||
err := self.dbAdapter.InsertDiceRoll(key, *tableMsg.DiceRoll)
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue