-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscripting.go
132 lines (115 loc) · 4.16 KB
/
scripting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"reflect"
"github.com/underlx/disturbancesmlx/scraper"
"github.com/gbl08ma/ankiddie"
"github.com/gbl08ma/sqalx"
"github.com/underlx/disturbancesmlx/compute"
"github.com/underlx/disturbancesmlx/types"
"github.com/underlx/disturbancesmlx/discordbot"
"github.com/underlx/disturbancesmlx/mqttgateway"
"github.com/underlx/disturbancesmlx/posplay"
"github.com/underlx/disturbancesmlx/resource"
"github.com/underlx/disturbancesmlx/utils"
"github.com/underlx/disturbancesmlx/website"
_ "github.com/gbl08ma/anko/packages"
)
type ankoInterop struct {
node sqalx.Node
}
func (ai *ankoInterop) ConfigurePackages(packages map[string]map[string]reflect.Value, packageTypes map[string]map[string]reflect.Type) {
type pkgInfo struct {
Name string
Types map[string]reflect.Type
Functions map[string]reflect.Value
Consts map[string]reflect.Value
Variables map[string]reflect.Value
}
processPkg := func(pkg pkgInfo) {
packages[pkg.Name] = make(map[string]reflect.Value)
dopkg := packages[pkg.Name]
for name, function := range pkg.Functions {
dopkg[name] = function
}
for name, item := range pkg.Consts {
dopkg[name] = item
}
for name, item := range pkg.Variables {
dopkg[name] = item
}
packageTypes[pkg.Name] = make(map[string]reflect.Type)
dotypes := packageTypes[pkg.Name]
for name, item := range pkg.Types {
dotypes[name] = item
}
}
processPkg(pkgInfo{"compute", compute.Types, compute.Functions, compute.Consts, compute.Variables})
processPkg(pkgInfo{"types", types.Types, types.Functions, types.Consts, types.Variables})
processPkg(pkgInfo{"discordbot", discordbot.Types, discordbot.Functions, discordbot.Consts, discordbot.Variables})
processPkg(pkgInfo{"resource", resource.Types, resource.Functions, resource.Consts, resource.Variables})
processPkg(pkgInfo{"posplay", posplay.Types, posplay.Functions, posplay.Consts, posplay.Variables})
processPkg(pkgInfo{"utils", utils.Types, utils.Functions, utils.Consts, utils.Variables})
processPkg(pkgInfo{"website", website.Types, website.Functions, website.Consts, website.Variables})
processPkg(pkgInfo{"underlx", Types, Functions, Consts, Variables})
processPkg(pkgInfo{"discordgo", extpkgDiscordGoTypes, extpkgDiscordGoFunctions, extpkgDiscordGoConsts, extpkgDiscordGoVariables})
processPkg(pkgInfo{"uuid", extpkgUUIDTypes, extpkgUUIDFunctions, extpkgUUIDConsts, extpkgUUIDVariables})
packages["underlx"]["RootSqalxNode"] = reflect.ValueOf(func() sqalx.Node {
return rootSqalxNode
})
packages["underlx"]["VehicleHandler"] = reflect.ValueOf(func() *compute.VehicleHandler {
return vehicleHandler
})
packages["underlx"]["VehicleETAHandler"] = reflect.ValueOf(func() *compute.VehicleETAHandler {
return vehicleETAHandler
})
packages["underlx"]["StatsHandler"] = reflect.ValueOf(func() *compute.StatsHandler {
return statsHandler
})
packages["underlx"]["ReportHandler"] = reflect.ValueOf(func() *compute.ReportHandler {
return reportHandler
})
packages["underlx"]["MQTTGateway"] = reflect.ValueOf(func() *mqttgateway.MQTTGateway {
return mqttGateway
})
packages["underlx"]["ContestScraper"] = reflect.ValueOf(func() scraper.AnnouncementScraper {
return contestscr
})
discordbot.AnkoPackageConfigurator(packages, packageTypes)
}
func (ai *ankoInterop) GetScript(id string) (*ankiddie.Script, error) {
script, err := types.GetScript(ai.node, id)
if err != nil {
return nil, err
}
s := ankiddie.Script(*script)
return &s, nil
}
func (ai *ankoInterop) GetAutorunScripts(autorunLevel int) ([]*ankiddie.Script, error) {
scripts, err := types.GetAutorunScriptsWithType(ai.node, "anko", autorunLevel)
if err != nil {
return []*ankiddie.Script{}, err
}
as := make([]*ankiddie.Script, len(scripts))
for i, s := range scripts {
t := ankiddie.Script(*s)
as[i] = &t
}
return as, nil
}
func (ai *ankoInterop) StoreScript(script *ankiddie.Script) error {
tx, err := ai.node.Beginx()
if err != nil {
return err
}
defer tx.Rollback()
s := types.Script(*script)
err = s.Update(tx)
if err != nil {
return err
}
return tx.Commit()
}
func defaultAnkoOut(env *ankiddie.Environment, msg string) error {
mainLog.Printf("[AnkoEnv%d] %s", env.EID(), msg)
return nil
}