Removed graphql dependency

graph-rework
noah metz 2023-05-31 00:37:51 -06:00
parent b7365f7dfb
commit 26a2a63d83
5 changed files with 34 additions and 36 deletions

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"log" "log"
"errors" "errors"
graphql "github.com/graph-gophers/graphql-go"
"reflect" "reflect"
"sort" "sort"
) )
@ -54,7 +53,7 @@ type Event interface {
RequiredResources() []Resource RequiredResources() []Resource
DoneResource() Resource DoneResource() Resource
AddChild(child Event, info EventInfo) error AddChild(child Event, info EventInfo) error
FindChild(id graphql.ID) Event FindChild(id string) Event
Run() error Run() error
Abort() error Abort() error
Signal(action string) error Signal(action string) error
@ -177,7 +176,7 @@ func NewBaseEvent(name string, description string, required_resources []Resource
BaseNode: BaseNode{ BaseNode: BaseNode{
name: name, name: name,
description: description, description: description,
id: gql_randid(), id: randid(),
listeners: []chan error{}, listeners: []chan error{},
}, },
parent: nil, parent: nil,
@ -315,7 +314,7 @@ func (event * BaseEvent) ChildInfo(idx Event) EventInfo {
return val return val
} }
func (event * BaseEvent) FindChild(id graphql.ID) Event { func (event * BaseEvent) FindChild(id string) Event {
if id == event.ID() { if id == event.ID() {
return event return event
} }

@ -3,21 +3,20 @@ package main
import ( import (
"errors" "errors"
"sync" "sync"
graphql "github.com/graph-gophers/graphql-go"
"github.com/google/uuid" "github.com/google/uuid"
) )
// Generate a random graphql id // Generate a random graphql id
func gql_randid() graphql.ID{ func randid() string{
uuid_str := uuid.New().String() uuid_str := uuid.New().String()
return graphql.ID(uuid_str) return uuid_str
} }
// GraphNode is the interface common to both DAG nodes and Event tree nodes // GraphNode is the interface common to both DAG nodes and Event tree nodes
type GraphNode interface { type GraphNode interface {
Name() string Name() string
Description() string Description() string
ID() graphql.ID ID() string
UpdateListeners() error UpdateListeners() error
UpdateChannel() chan error UpdateChannel() chan error
Update() error Update() error
@ -28,7 +27,7 @@ type GraphNode interface {
type BaseNode struct { type BaseNode struct {
name string name string
description string description string
id graphql.ID id string
listeners []chan error listeners []chan error
listeners_lock sync.Mutex listeners_lock sync.Mutex
} }
@ -41,7 +40,7 @@ func (node * BaseNode) Description() string {
return node.description return node.description
} }
func (node * BaseNode) ID() graphql.ID { func (node * BaseNode) ID() string {
return node.id return node.id
} }

@ -4,11 +4,10 @@ import (
"fmt" "fmt"
"log" "log"
"errors" "errors"
graphql "github.com/graph-gophers/graphql-go"
) )
type EventManager struct { type EventManager struct {
dag_nodes map[graphql.ID]Resource dag_nodes map[string]Resource
root_event Event root_event Event
} }
@ -16,7 +15,7 @@ type EventManager struct {
func NewEventManager(root_event Event, dag_nodes []Resource) * EventManager { func NewEventManager(root_event Event, dag_nodes []Resource) * EventManager {
manager := &EventManager{ manager := &EventManager{
dag_nodes: map[graphql.ID]Resource{}, dag_nodes: map[string]Resource{},
root_event: nil, root_event: nil,
} }
@ -41,7 +40,7 @@ func (manager * EventManager) Run() error {
return manager.root_event.Run() return manager.root_event.Run()
} }
func (manager * EventManager) FindResource(id graphql.ID) Resource { func (manager * EventManager) FindResource(id string) Resource {
resource, exists := manager.dag_nodes[id] resource, exists := manager.dag_nodes[id]
if exists == false { if exists == false {
return nil return nil
@ -50,7 +49,7 @@ func (manager * EventManager) FindResource(id graphql.ID) Resource {
return resource return resource
} }
func (manager * EventManager) FindEvent(id graphql.ID) Event { func (manager * EventManager) FindEvent(id string) Event {
event := manager.root_event.FindChild(id) event := manager.root_event.FindChild(id)
return event return event

@ -153,7 +153,7 @@ func NewResource(name string, description string, children []Resource) * BaseRes
BaseNode: BaseNode{ BaseNode: BaseNode{
name: name, name: name,
description: description, description: description,
id: gql_randid(), id: randid(),
listeners: []chan error{}, listeners: []chan error{},
}, },
parents: []Resource{}, parents: []Resource{},

@ -3,19 +3,11 @@ package main
import ( import (
"fmt" "fmt"
"log" "log"
"time"
) )
type ArenaDriver interface {
}
type VirtualArenaDriver struct {
}
type Arena struct { type Arena struct {
BaseResource BaseResource
driver ArenaDriver
} }
func NewVirtualArena(name string) * Arena { func NewVirtualArena(name string) * Arena {
@ -24,13 +16,12 @@ func NewVirtualArena(name string) * Arena {
BaseNode: BaseNode{ BaseNode: BaseNode{
name: name, name: name,
description: "A virtual vex arena", description: "A virtual vex arena",
id: gql_randid(), id: randid(),
listeners: []chan error{}, listeners: []chan error{},
}, },
parents: []Resource{}, parents: []Resource{},
children: []Resource{}, children: []Resource{},
}, },
driver: VirtualArenaDriver{},
} }
return arena return arena
@ -46,7 +37,7 @@ func NewMember(name string) * Member {
BaseNode: BaseNode{ BaseNode: BaseNode{
name: name, name: name,
description: "A Team Member", description: "A Team Member",
id: gql_randid(), id: randid(),
listeners: []chan error{}, listeners: []chan error{},
}, },
parents: []Resource{}, parents: []Resource{},
@ -80,7 +71,7 @@ func NewTeam(org string, team string, members []*Member) * Team {
BaseNode: BaseNode{ BaseNode: BaseNode{
name: name, name: name,
description: description, description: description,
id: gql_randid(), id: randid(),
listeners: []chan error{}, listeners: []chan error{},
}, },
parents: []Resource{}, parents: []Resource{},
@ -108,7 +99,7 @@ func NewAlliance(team0 * Team, team1 * Team) * Alliance {
BaseNode: BaseNode{ BaseNode: BaseNode{
name: name, name: name,
description: description, description: description,
id: gql_randid(), id: randid(),
listeners: []chan error{}, listeners: []chan error{},
}, },
parents: []Resource{}, parents: []Resource{},
@ -121,8 +112,12 @@ func NewAlliance(team0 * Team, team1 * Team) * Alliance {
type Match struct { type Match struct {
BaseEvent BaseEvent
state string state string
control string
control_start time.Time
} }
const start_slack = 3000 * time.Millisecond
func NewMatch(alliance0 * Alliance, alliance1 * Alliance, arena * Arena) * Match { func NewMatch(alliance0 * Alliance, alliance1 * Alliance, arena * Arena) * Match {
name := fmt.Sprintf("Match: %s vs. %s", alliance0.Name(), alliance1.Name() ) name := fmt.Sprintf("Match: %s vs. %s", alliance0.Name(), alliance1.Name() )
description := "A vex match" description := "A vex match"
@ -130,22 +125,28 @@ func NewMatch(alliance0 * Alliance, alliance1 * Alliance, arena * Arena) * Match
match := &Match{ match := &Match{
BaseEvent: NewBaseEvent(name, description, []Resource{alliance0, alliance1, arena}), BaseEvent: NewBaseEvent(name, description, []Resource{alliance0, alliance1, arena}),
state: "init", state: "init",
control: "init",
control_start: time.UnixMilli(0),
} }
match.LockDone() match.LockDone()
match.actions["start"] = func() (string, error) { match.actions["start"] = func() (string, error) {
// put the match into "scheduled" state
log.Printf("Starting match") log.Printf("Starting match")
match.control = "none"
match.state = "scheduled" match.state = "scheduled"
return "wait", nil return "wait", nil
} }
match.actions["start_autonomous"] = func() (string, error) { match.actions["queue_autonomous"] = func() (string, error) {
if match.state != "scheduled" { match.control = "none"
log.Printf("Cannot start_autonomous when the match is in %s", match.state) match.state = "autonomous_queued"
match.control_start = time.Now().Add(start_slack)
return "wait", nil return "wait", nil
} }
log.Printf("Starting autonomous")
match.actions["start_autonomous"] = func() (string, error) {
match.control = "autonomous"
match.state = "autonomous_running"
return "wait", nil return "wait", nil
} }