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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"nhooyr.io/websocket"
|
"errors"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"time"
|
|
||||||
"sync"
|
|
||||||
"net/http"
|
|
||||||
"log"
|
"log"
|
||||||
"errors"
|
"net/http"
|
||||||
|
"nhooyr.io/websocket"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Subscriber struct {
|
type Subscriber struct {
|
||||||
|
|
4
main.go
4
main.go
|
@ -2,13 +2,13 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"nilfm.cc/git/felt/gametable"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"nilfm.cc/git/felt/gametable"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -5,21 +5,21 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type TableKey struct {
|
type TableKey struct {
|
||||||
name: string
|
Name string
|
||||||
passcode: string
|
Passcode string
|
||||||
}
|
}
|
||||||
|
|
||||||
type DiceRoll struct {
|
type DiceRoll struct {
|
||||||
faces: uint8
|
Faces uint8
|
||||||
roll: uint8[]
|
Roll []uint8
|
||||||
player: string
|
Player string
|
||||||
note: string
|
Note string
|
||||||
timestamp: time.Time
|
Timestamp time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type Token struct {
|
type Token struct {
|
||||||
id: string
|
Id string
|
||||||
spriteUri: string
|
SpriteUri string
|
||||||
x: int
|
X int
|
||||||
y: int
|
Y int
|
||||||
}
|
}
|
|
@ -2,42 +2,46 @@ package dbengine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"nilfm.cc/git/felt/models"
|
"nilfm.cc/git/felt/models"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
interface DbAdapter {
|
type DbAdapter interface {
|
||||||
Init(mongoUri: string): error
|
Init(mongoUri string) error
|
||||||
|
|
||||||
CreateTable(table: models.TableKey): error
|
CreateTable(table models.TableKey) error
|
||||||
DestroyTable(table: models.TableKey): error
|
DestroyTable(table models.TableKey) error
|
||||||
|
|
||||||
InsertDiceRoll(table: models.TableKey, diceRoll: models.DiceRoll): error
|
InsertDiceRoll(table models.TableKey, diceRoll models.DiceRoll) error
|
||||||
GetDiceRolls(table: models.TableKey): models.DiceRoll[], error
|
GetDiceRolls(table models.TableKey) ([]models.DiceRoll, error)
|
||||||
|
|
||||||
SetMapImageUrl(table: models.TableKey, url: string): error
|
SetMapImageUrl(table models.TableKey, url string) error
|
||||||
GetMapImageUrl(table: models.TableKey): string, error
|
GetMapImageUrl(table models.TableKey) (string, error)
|
||||||
|
|
||||||
AddToken(table: models.TableKey, token: models.Token): error
|
AddToken(table models.TableKey, token models.Token) error
|
||||||
RemoveToken(table: models.TableKey, tokenId: string): error
|
RemoveToken(table models.TableKey, tokenId string) error
|
||||||
ModifyToken(table: models.TableKey, token: models.Token): error
|
ModifyToken(table models.TableKey, token models.Token) error
|
||||||
GetTokens(table: models.TableKey): models.Token[], error
|
GetTokens(table models.TableKey) ([]models.Token, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type DbEngine struct {
|
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))
|
client, err := mongo.NewClient(options.Client().ApplyURI(mongoUri))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
self.client = client
|
self.client = client
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, _ := self.mkCtx(10)
|
||||||
|
|
||||||
err = client.Connect(ctx)
|
err = client.Connect(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -51,7 +55,7 @@ func (self *DbEngine) Init(mongoUri: string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *DbEngine) ensureCollections(db: mongo.Database) error {
|
func (self *DbEngine) ensureCollections(db mongo.Database) error {
|
||||||
tables := db.Collection("tables")
|
tables := db.Collection("tables")
|
||||||
if tables == nil {
|
if tables == nil {
|
||||||
createCmd := bson.D{
|
createCmd := bson.D{
|
||||||
|
@ -59,13 +63,13 @@ func (self *DbEngine) ensureCollections(db: mongo.Database) error {
|
||||||
{"clusteredIndex", {
|
{"clusteredIndex", {
|
||||||
{"key", {"name"}},
|
{"key", {"name"}},
|
||||||
{"unique", true},
|
{"unique", true},
|
||||||
{"name", "idx_tables_unique_names"}
|
{"name", "idx_tables_unique_names"},
|
||||||
}}
|
}},
|
||||||
}
|
}
|
||||||
|
|
||||||
var createResult bson.M
|
var createResult bson.M
|
||||||
err := db.RunCommand(
|
err := db.RunCommand(
|
||||||
context.WithTimeout(context.Background(), 10*time.Second),
|
self.mkCtx(10),
|
||||||
createCmd).Decode(&createResult)
|
createCmd).Decode(&createResult)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -74,3 +78,18 @@ func (self *DbEngine) ensureCollections(db: mongo.Database) error {
|
||||||
}
|
}
|
||||||
return nil
|
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