cleanup- we can compile again

This commit is contained in:
Iris Lightshard 2022-12-06 21:58:42 -07:00
parent 8c5eeeaa08
commit 6911337ffd
Signed by: nilix
GPG key ID: 3B7FBC22144E6398
6 changed files with 190 additions and 48 deletions

View file

@ -8,7 +8,7 @@ import (
"log"
"net/http"
"nhooyr.io/websocket"
"nilfm.cc/git/felt/dbengine"
"nilfm.cc/git/felt/mongodb"
"nilfm.cc/git/felt/models"
"nilfm.cc/git/quartzgun/cookie"
"sync"
@ -26,15 +26,15 @@ type GameTableServer struct {
logf func(f string, v ...interface{})
serveMux http.ServeMux
subscribersLock sync.Mutex
subscribers map[*Subscriber]models.tableKey
dbAdapter dbengine.DbAdapter
subscribers map[*Subscriber]models.TableKey
dbAdapter mongodb.DbAdapter
}
func New(adapter dbengine.DbAdapter) *GameTableServer {
func New(adapter mongodb.DbAdapter) *GameTableServer {
srvr := &GameTableServer{
subscribeMessageBuffer: 16,
logf: log.Printf,
subscribers: make(map[*Subscriber]models.TableKey{}),
subscribers: make(map[*Subscriber]models.TableKey),
publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
dbAdapter: adapter,
}
@ -57,7 +57,7 @@ func (self *GameTableServer) subscribeHandler(w http.ResponseWriter, r *http.Req
}
defer c.Close(websocket.StatusInternalError, "")
err = self.subscribe(r.Context(), c)
err = self.subscribe(r, c)
if errors.Is(err, context.Canceled) {
return
}
@ -91,7 +91,7 @@ func (self *GameTableServer) subscribe(r *http.Request, c *websocket.Conn) error
return tblPassErr
}
tableKey = models.TableKey{
tableKey := models.TableKey{
Name: tableName,
Passcode: tablePasscode,
}
@ -100,7 +100,7 @@ func (self *GameTableServer) subscribe(r *http.Request, c *websocket.Conn) error
return errors.New("Table with matching key was not found on this server")
}
self.addSubscriber(s)
self.addSubscriber(s, tableKey)
defer self.deleteSubscriber(s)
select {

21
go.mod
View file

@ -3,7 +3,22 @@ module nilfm.cc/git/felt
go 1.19
require (
github.com/klauspost/compress v1.10.3 // indirect
golang.org/x/time v0.1.0 // indirect
nhooyr.io/websocket v1.8.7 // indirect
go.mongodb.org/mongo-driver v1.11.0
golang.org/x/time v0.1.0
nhooyr.io/websocket v1.8.7
nilfm.cc/git/quartzgun v0.2.0
)
require (
github.com/golang/snappy v0.0.1 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/text v0.3.7 // indirect
)

70
go.sum
View file

@ -1,41 +1,109 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls=
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8=
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E=
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
github.com/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs=
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
go.mongodb.org/mongo-driver v1.11.0 h1:FZKhBSTydeuffHj9CBjXlR8vQLee1cQyTWYPA6/tqiE=
go.mongodb.org/mongo-driver v1.11.0/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA=
golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
nilfm.cc/git/quartzgun v0.2.0 h1:JJjkCbRLaMmfGLe5GQHWhBkkY6Is0roTMyWC76VtBPI=
nilfm.cc/git/quartzgun v0.2.0/go.mod h1:/DDvt1DtzNuUf3HHaP29WMei/kkdaRW+ySmEzybvVto=

11
main.go
View file

@ -5,6 +5,7 @@ import (
"log"
"net"
"net/http"
"nilfm.cc/git/felt/mongodb"
"nilfm.cc/git/felt/gametable"
"os"
"os/signal"
@ -24,7 +25,15 @@ func run() error {
return err
}
gt := gametable.New()
dbEngine := &mongodb.DbEngine{}
err = dbEngine.Init(os.Args[2])
if err != nil {
return err
}
gt := gametable.New(dbEngine)
s := &http.Server{
Handler: gt,
ReadTimeout: time.Second * 10,

View file

@ -32,4 +32,12 @@ type Table struct {
DiceRolls []DiceRoll
Tokens []Token
AvailableTokens []Token
AuxMessage string
}
type TableMessage struct {
Roll DiceRoll
Token Token
MapImg string
AuxMsg string
}

View file

@ -1,4 +1,4 @@
package dbengine
package mongodb
import (
"context"
@ -7,6 +7,8 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
"nilfm.cc/git/felt/models"
"time"
"errors"
"fmt"
)
const errNoCollection string = "collection not found: felt.%s"
@ -20,7 +22,7 @@ type DbAdapter interface {
CreateTable(table models.TableKey) error
DestroyTable(table models.TableKey) error
CheckTable(table models.TableKey) boolean
CheckTable(table models.TableKey) bool
InsertDiceRoll(table models.TableKey, diceRoll models.DiceRoll) error
GetDiceRolls(table models.TableKey) ([]models.DiceRoll, error)
@ -28,15 +30,18 @@ type DbAdapter interface {
SetMapImageUrl(table models.TableKey, url string) error
GetMapImageUrl(table models.TableKey) (string, error)
AddToken(table models.TableKey, token models.Token, active boolean) error
RemoveToken(table models.TableKey, tokenId string, active boolean) error
ModifyToken(table models.TableKey, token models.Token, active boolean) error
GetTokens(table models.TableKey, active boolean) ([]models.Token, error)
SetAuxMessage(table models.TableKey, message string) error
GetAuxMessage(table models.TableKey) (string, error)
AddToken(table models.TableKey, token models.Token, active bool) error
RemoveToken(table models.TableKey, tokenId string, active bool) error
ModifyToken(table models.TableKey, token models.Token, active bool) error
GetTokens(table models.TableKey, active bool) ([]models.Token, error)
}
type DbEngine struct {
client mongo.Client
db mongo.Database
client *mongo.Client
db *mongo.Database
}
func (self *DbEngine) mkCtx(timeoutSec int) context.Context {
@ -64,13 +69,13 @@ func (self *DbEngine) Init(mongoUri string) error {
return err
}
func (self *DbEngine) ensureCollections(db mongo.Database) error {
func (self *DbEngine) ensureCollections(db *mongo.Database) error {
tables := db.Collection("tables")
if tables == nil {
createCmd := bson.D{
{"create", "tables"},
{"clusteredIndex", {
{"key", {"name"}},
{"clusteredIndex", bson.D{
{"key", "name"},
{"unique", true},
{"name", "idx_tables_unique_names"},
}},
@ -117,18 +122,14 @@ func (self *DbEngine) DestroyTable(table models.TableKey) error {
return errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) CheckTable(table models.TableKey) boolean {
func (self *DbEngine) CheckTable(table models.TableKey) bool {
tables := self.db.Collection("tables")
if tables != nil {
_, err := tables.FindOne(self.mkCtx(10), bson.D{
res := tables.FindOne(self.mkCtx(10), bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
})
if err != nil {
return false
} else {
return true
}
return res != nil
}
return false
}
@ -145,10 +146,10 @@ func (self *DbEngine) InsertDiceRoll(table models.TableKey, diceRoll models.Dice
},
bson.D{
{"$push", bson.D{
"diceRolls", bson.D{
{"diceRolls", bson.D{
{"$each", []models.DiceRoll{diceRoll}},
{"$slice", 1000},
},
}},
}},
},
).Decode(&result)
@ -158,10 +159,10 @@ func (self *DbEngine) InsertDiceRoll(table models.TableKey, diceRoll models.Dice
}
func (self *DbEngine) GetDiceRolls(table models.TableKey) ([]models.DiceRoll, error) {
tables := self.db.Colletion("tables")
tables := self.db.Collection("tables")
if tables != nil {
var result models.Table
err := tables.findOne(
err := tables.FindOne(
self.mkCtx(10),
bson.D{
{"name", table.Name},
@ -170,10 +171,10 @@ func (self *DbEngine) GetDiceRolls(table models.TableKey) ([]models.DiceRoll, er
if err == nil {
return result.DiceRolls, nil
} else {
return "", errors.New(fmt.Sprintf(errNoDocument, table.Name, "tables"))
return nil, errors.New(fmt.Sprintf(errNoDocument, table.Name, "tables"))
}
}
return "", errors.New(fmt.Sprintf(errNoCollection, "tables"))
return nil, errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) SetMapImageUrl(table models.TableKey, url string) error {
@ -217,7 +218,48 @@ func (self *DbEngine) GetMapImageUrl(table models.TableKey) (string, error) {
return "", errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) AddToken(table models.TableKey, token models.Token, active boolean) error {
func (self *DbEngine) SetAuxMessage(table models.TableKey, message string) error {
tables := self.db.Collection("tables")
if tables != nil {
var result models.Table
err := tables.FindOneAndUpdate(
self.mkCtx(10),
bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
},
bson.D{
{"auxMessage", message},
},
).Decode(&result)
return err
}
return errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) GetAuxMessage(table models.TableKey) (string, error) {
tables := self.db.Collection("tables")
if tables != nil {
var result models.Table
err := tables.FindOne(
self.mkCtx(10),
bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
}).Decode(&result)
if err == nil {
return result.AuxMessage, nil
} else {
return "", errors.New(fmt.Sprintf(errNoDocument, table.Name, "tables"))
}
}
return "", errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) AddToken(table models.TableKey, token models.Token, active bool) error {
tables := self.db.Collection("tables")
if tables != nil {
tokenArrKey := "tokens"
@ -232,7 +274,7 @@ func (self *DbEngine) AddToken(table models.TableKey, token models.Token, active
{"passcode", table.Passcode},
},
bson.D{
{"$push", bson.D{tokenArrKey, token}},
{"$push", bson.D{{tokenArrKey, token}}},
},
).Decode(&result)
return err
@ -240,7 +282,7 @@ func (self *DbEngine) AddToken(table models.TableKey, token models.Token, active
return errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) RemoveToken(table models.TableKey, tokenId string, active boolean) error {
func (self *DbEngine) RemoveToken(table models.TableKey, tokenId string, active bool) error {
tables := self.db.Collection("tables")
if tables != nil {
tokenArrKey := "tokens"
@ -255,7 +297,7 @@ func (self *DbEngine) RemoveToken(table models.TableKey, tokenId string, active
{"passcode", table.Passcode},
},
bson.D{
{"$pull", bson.D{tokenArrKey, bson.D{"_id", tokenId}}},
{"$pull", bson.D{{tokenArrKey, bson.E{"_id", tokenId}}}},
},
).Decode(&result)
return err
@ -263,7 +305,7 @@ func (self *DbEngine) RemoveToken(table models.TableKey, tokenId string, active
return errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) ModifyToken(table models.TableKey, token models.Token, active boolean) error {
func (self *DbEngine) ModifyToken(table models.TableKey, token models.Token, active bool) error {
tables := self.db.Collection("tables")
if tables != nil {
tokenArrKey := "tokens"
@ -276,15 +318,15 @@ func (self *DbEngine) ModifyToken(table models.TableKey, token models.Token, act
bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
{tokenArrKey, bson.D{"_id", tokenId}},
{tokenArrKey, bson.E{"_id", token.Id}},
},
bson.D{
{"$set", bson.D{tokenArrKey + ".$", bson.D{
{"$set", bson.D{{tokenArrKey + ".$", bson.D{
{"name", token.Name},
{"spriteUri", token.SpriteUri},
{"x", token.X},
{"y", token.Y},
}}},
}}}},
},
).Decode(&result)
return err
@ -292,7 +334,7 @@ func (self *DbEngine) ModifyToken(table models.TableKey, token models.Token, act
return errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) GetTokens(table models.TableKey, active boolean) ([]models.Token, error) {
func (self *DbEngine) GetTokens(table models.TableKey, active bool) ([]models.Token, error) {
tables := self.db.Collection("tables")
if tables != nil {
var result models.Table
@ -304,9 +346,9 @@ func (self *DbEngine) GetTokens(table models.TableKey, active boolean) ([]models
}).Decode(&result)
if err == nil {
if active {
return result.Tokens
return result.Tokens, nil
} else {
return result.AvailableTokens
return result.AvailableTokens, nil
}
} else {
return nil, errors.New(fmt.Sprintf(errNoDocument, table.Name, "tables"))