gofmt, rid code of typescript confusion, little bit of db code
This commit is contained in:
parent
25e51fb2d5
commit
e59f3e1609
4 changed files with 140 additions and 121 deletions
|
@ -2,14 +2,14 @@ package gametable
|
|||
|
||||
import (
|
||||
"context"
|
||||
"nhooyr.io/websocket"
|
||||
"errors"
|
||||
"golang.org/x/time/rate"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
"sync"
|
||||
"net/http"
|
||||
"log"
|
||||
"errors"
|
||||
"net/http"
|
||||
"nhooyr.io/websocket"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Subscriber struct {
|
||||
|
@ -27,7 +27,7 @@ type GameTableServer struct {
|
|||
}
|
||||
|
||||
func New() *GameTableServer {
|
||||
srvr := &GameTableServer {
|
||||
srvr := &GameTableServer{
|
||||
subscribeMessageBuffer: 16,
|
||||
logf: log.Printf,
|
||||
subscribers: make(map[*Subscriber]struct{}),
|
||||
|
|
4
main.go
4
main.go
|
@ -2,13 +2,13 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"nilfm.cc/git/felt/gametable"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"nilfm.cc/git/felt/gametable"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -5,21 +5,21 @@ import (
|
|||
)
|
||||
|
||||
type TableKey struct {
|
||||
name: string
|
||||
passcode: string
|
||||
Name string
|
||||
Passcode string
|
||||
}
|
||||
|
||||
type DiceRoll struct {
|
||||
faces: uint8
|
||||
roll: uint8[]
|
||||
player: string
|
||||
note: string
|
||||
timestamp: time.Time
|
||||
Faces uint8
|
||||
Roll []uint8
|
||||
Player string
|
||||
Note string
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
type Token struct {
|
||||
id: string
|
||||
spriteUri: string
|
||||
x: int
|
||||
y: int
|
||||
Id string
|
||||
SpriteUri string
|
||||
X int
|
||||
Y int
|
||||
}
|
|
@ -2,42 +2,46 @@ package dbengine
|
|||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"nilfm.cc/git/felt/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
interface DbAdapter {
|
||||
Init(mongoUri: string): error
|
||||
type DbAdapter interface {
|
||||
Init(mongoUri string) error
|
||||
|
||||
CreateTable(table: models.TableKey): error
|
||||
DestroyTable(table: models.TableKey): error
|
||||
CreateTable(table models.TableKey) error
|
||||
DestroyTable(table models.TableKey) error
|
||||
|
||||
InsertDiceRoll(table: models.TableKey, diceRoll: models.DiceRoll): error
|
||||
GetDiceRolls(table: models.TableKey): models.DiceRoll[], error
|
||||
InsertDiceRoll(table models.TableKey, diceRoll models.DiceRoll) error
|
||||
GetDiceRolls(table models.TableKey) ([]models.DiceRoll, error)
|
||||
|
||||
SetMapImageUrl(table: models.TableKey, url: string): error
|
||||
GetMapImageUrl(table: models.TableKey): string, error
|
||||
SetMapImageUrl(table models.TableKey, url string) error
|
||||
GetMapImageUrl(table models.TableKey) (string, error)
|
||||
|
||||
AddToken(table: models.TableKey, token: models.Token): error
|
||||
RemoveToken(table: models.TableKey, tokenId: string): error
|
||||
ModifyToken(table: models.TableKey, token: models.Token): error
|
||||
GetTokens(table: models.TableKey): models.Token[], error
|
||||
AddToken(table models.TableKey, token models.Token) error
|
||||
RemoveToken(table models.TableKey, tokenId string) error
|
||||
ModifyToken(table models.TableKey, token models.Token) error
|
||||
GetTokens(table models.TableKey) ([]models.Token, error)
|
||||
}
|
||||
|
||||
type DbEngine struct {
|
||||
client: mongo.Client
|
||||
client mongo.Client
|
||||
}
|
||||
|
||||
func (self *DbEngine) Init(mongoUri: string) error {
|
||||
func (self *DbEngine) mkCtx(timeoutSec int) context.Context {
|
||||
return context.WithTimeout(context.Background(), 10*time.Second)
|
||||
}
|
||||
|
||||
func (self *DbEngine) Init(mongoUri string) error {
|
||||
client, err := mongo.NewClient(options.Client().ApplyURI(mongoUri))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
self.client = client
|
||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
ctx, _ := self.mkCtx(10)
|
||||
|
||||
err = client.Connect(ctx)
|
||||
if err != nil {
|
||||
|
@ -51,7 +55,7 @@ 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{
|
||||
|
@ -59,13 +63,13 @@ func (self *DbEngine) ensureCollections(db: mongo.Database) error {
|
|||
{"clusteredIndex", {
|
||||
{"key", {"name"}},
|
||||
{"unique", true},
|
||||
{"name", "idx_tables_unique_names"}
|
||||
}}
|
||||
{"name", "idx_tables_unique_names"},
|
||||
}},
|
||||
}
|
||||
|
||||
var createResult bson.M
|
||||
err := db.RunCommand(
|
||||
context.WithTimeout(context.Background(), 10*time.Second),
|
||||
self.mkCtx(10),
|
||||
createCmd).Decode(&createResult)
|
||||
|
||||
if err != nil {
|
||||
|
@ -74,3 +78,18 @@ func (self *DbEngine) ensureCollections(db: mongo.Database) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *DbEngine) CreateTable(table models.TableKey) error {
|
||||
tables := self.db.Collection("tables")
|
||||
if tables != nil {
|
||||
_, err := tables.insertOne(self.mkCtx(10), bson.D{
|
||||
{"name", table.Name},
|
||||
{"passcode", table.Passcode},
|
||||
{"mapUri", ""},
|
||||
{"diceRolls", bson.A{}},
|
||||
{"tokens", bson.A{}},
|
||||
})
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue