Add state
This commit is contained in:
@ -11,27 +11,29 @@ import (
|
||||
type MapScoreList []MapScore
|
||||
|
||||
type Capture struct {
|
||||
Id uint
|
||||
ID int64 `bun:",pk,autoincrement"`
|
||||
Host string
|
||||
Port string
|
||||
Name string
|
||||
Active bool
|
||||
Start time.Time
|
||||
MapScores MapScoreList
|
||||
MapScores MapScoreList `bun:"rel:has-many,join:id=capture_id"`
|
||||
}
|
||||
|
||||
type MapScore struct {
|
||||
Id uint
|
||||
ID int64 `bun:",pk,autoincrement"`
|
||||
CaptureID int64 `bun:"capture_id"`
|
||||
StartTime time.Time
|
||||
Map string
|
||||
ScoreList []Score
|
||||
ScoreList []Score `bun:"rel:has-many,join:id=mapscore_id"`
|
||||
}
|
||||
|
||||
type Score struct {
|
||||
Id uint
|
||||
Name string
|
||||
Score int
|
||||
Ping int
|
||||
ID int64 `bun:",pk,autoincrement"`
|
||||
MapScoreID int64 `bun:"mapscore_id"`
|
||||
Name string
|
||||
Score int
|
||||
Ping int
|
||||
}
|
||||
|
||||
type ResultTable struct {
|
||||
|
||||
@ -12,7 +12,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var timeLayout = "Mon Jun 2 15:04:05 2006"
|
||||
var timeLayout = "Mon Jan 2 15:04:05 2006"
|
||||
|
||||
type CoD4Server struct {
|
||||
server string
|
||||
@ -109,7 +109,7 @@ func parseServerData(data string) (*CoD4ServerStatus, error) {
|
||||
c.serverData["_Maps"] = strings.Join(strings.Split(c.serverData["_Maps"], "-"), ",")
|
||||
c.MapName = c.serverData["mapname"]
|
||||
|
||||
startTime, err := time.Parse(timeLayout, c.serverData["g_mapStartTime"])
|
||||
startTime, err := time.ParseInLocation(timeLayout, c.serverData["g_mapStartTime"], time.Local)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -1,4 +1,58 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/dialect/sqlitedialect"
|
||||
"github.com/uptrace/bun/driver/sqliteshim"
|
||||
)
|
||||
|
||||
type Persistence interface {
|
||||
CreateCapture(context.Context, *Capture) error
|
||||
GetCaptures(context.Context) ([]Capture, error)
|
||||
}
|
||||
|
||||
type SQLitePersistence struct {
|
||||
db *bun.DB
|
||||
}
|
||||
|
||||
func NewSQLitePersistence(dbloc string) (*SQLitePersistence, error) {
|
||||
sqldb, err := sql.Open(sqliteshim.ShimName, fmt.Sprintf("file:%s", dbloc))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
db := bun.NewDB(sqldb, sqlitedialect.New())
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
for _, model := range []interface{}{(*Capture)(nil), (*MapScore)(nil), (*Score)(nil)} {
|
||||
_, err := db.NewCreateTable().Model(model).IfNotExists().Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &SQLitePersistence{
|
||||
db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *SQLitePersistence) GetCaptures(ctx context.Context) ([]Capture, error) {
|
||||
var captures []Capture
|
||||
err := s.db.NewSelect().Model(&captures).Scan(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return captures, nil
|
||||
}
|
||||
|
||||
func (s *SQLitePersistence) CreateCapture(ctx context.Context, capture *Capture) error {
|
||||
_, err := s.db.NewInsert().Model(capture).Exec(ctx)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user