55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package schema
|
|
|
|
import (
|
|
"encoding/json"
|
|
"regexp"
|
|
"time"
|
|
|
|
"entgo.io/ent"
|
|
"entgo.io/ent/schema/edge"
|
|
"entgo.io/ent/schema/field"
|
|
"omctf.ru/block-game-backend/utils/xy"
|
|
)
|
|
|
|
// Level holds the schema definition for the Level entity.
|
|
type Level struct {
|
|
ent.Schema
|
|
}
|
|
|
|
// JSON data
|
|
type LevelData struct {
|
|
Size int `json:"size"`
|
|
Tiles []Tile `json:"tiles"`
|
|
}
|
|
|
|
type Tile struct {
|
|
Kind string `json:"kind"`
|
|
Pos xy.Point `json:"pos"`
|
|
Data json.RawMessage `json:"data"`
|
|
}
|
|
|
|
// Fields of the Level.
|
|
func (Level) Fields() []ent.Field {
|
|
return []ent.Field{
|
|
field.String("name").Unique().Match(regexp.MustCompile(`^\w{1,64}$`)),
|
|
field.String("description"),
|
|
field.Enum("visibility").Values("private", "public"),
|
|
field.JSON("data", LevelData{}),
|
|
field.String("prize"), // only for players who passed the level!!!
|
|
field.Time("createdAt").Default(func() (t time.Time) {
|
|
return time.Now().UTC()
|
|
}),
|
|
}
|
|
}
|
|
|
|
// Edges of the Level.
|
|
func (Level) Edges() []ent.Edge {
|
|
return []ent.Edge{
|
|
edge.From("owner", User.Type).
|
|
Ref("ownedLevels").
|
|
Unique(),
|
|
edge.From("invitedPlayers", User.Type).
|
|
Ref("invitedToLevels"),
|
|
}
|
|
}
|