-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
212 lines (168 loc) · 5.97 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"context"
"fmt"
"html/template"
"log"
"net/http"
"sync"
"time"
vrcinput "github.com/Jilwer/oscvrc"
"github.com/jfyne/live"
)
const (
Jump = "jump"
MoveForward = "up"
MoveForwardStop = "up-stop"
MoveBack = "down"
MoveBackStop = "down-stop"
MoveLeft = "left"
MoveLeftStop = "left-stop"
MoveRight = "right"
MoveRightStop = "right-stop"
LookLeft = "look-left"
LookLeftStop = "look-left-stop"
LookRight = "look-right"
LookRightStop = "look-right-stop"
Send = "send"
)
func main() {
// Set up the initial state
initialState := MustLoadConfig("config.toml")
initialState.StaticMessage.Timer = time.NewTicker(10 * time.Second)
initialState.Chat.LastMessageTime = time.Now().Add(-10 * time.Second)
initialState.Chat.RateLimit = 1 * time.Second
initialState.Chat.Mutex = &sync.Mutex{}
osc := initializeOscClient()
t := initializeTemplates()
h := initializeHandlers(&osc, t, initialState)
initializeStaticMessageSender(&osc, initialState)
go runServer(h, initialState)
fmt.Println("Server is running on http://localhost:" + initialState.Server.Port)
select {}
}
func runServer(h live.Handler, config *StateConfig) {
http.Handle("/", live.NewHttpHandler(live.NewCookieStore("session-name", []byte("weak-secret")), h))
http.Handle("/live.js", live.Javascript{})
http.Handle("/auto.js.map", live.JavascriptMap{})
err := http.ListenAndServe(":"+config.Server.Port, nil)
if err != nil {
log.Fatal(err)
}
}
// Initializers
func initializeOscClient() vrcinput.Client {
osc := vrcinput.NewOscClient(vrcinput.DefaultAddr, vrcinput.DefaultPort)
return osc
}
func initializeStaticMessageSender(osc *vrcinput.Client, config *StateConfig) {
go func() {
for range config.StaticMessage.Timer.C {
// Send the message if the timer is up and the user has enabled it
if config.StaticMessage.Send {
osc.Chat(config.StaticMessage.Message, true, false)
}
}
}()
}
func initializeTemplates() *template.Template {
t, err := template.ParseFiles("root.html", "view.html")
if err != nil {
log.Fatal(err)
}
return t
}
func initializeHandlers(osc *vrcinput.Client, t *template.Template, state *StateConfig) live.Handler {
h := live.NewHandler(live.WithTemplateRenderer(t))
h.HandleEvent(Send, func(ctx context.Context, s live.Socket, p live.Params) (interface{}, error) {
return handleChatEvent(osc, p, state)
})
h.HandleEvent(Jump, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleJumpEvent(osc)
})
// Input Start Events
h.HandleEvent(MoveForward, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleMoveEvent(osc, vrcinput.MoveForward)
})
h.HandleEvent(MoveBack, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleMoveEvent(osc, vrcinput.MoveBackward)
})
h.HandleEvent(MoveLeft, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleMoveEvent(osc, vrcinput.MoveLeft)
})
h.HandleEvent(MoveRight, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleMoveEvent(osc, vrcinput.MoveRight)
})
h.HandleEvent(LookLeft, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleLookEvent(osc, vrcinput.LookLeft)
})
h.HandleEvent(LookRight, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleLookEvent(osc, vrcinput.LookRight)
})
// Input Stop Events
h.HandleEvent(MoveForwardStop, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleStopMoveEvent(osc, vrcinput.MoveForward)
})
h.HandleEvent(MoveBackStop, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleStopMoveEvent(osc, vrcinput.MoveBackward)
})
h.HandleEvent(MoveLeftStop, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleStopMoveEvent(osc, vrcinput.MoveLeft)
})
h.HandleEvent(MoveRightStop, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleStopMoveEvent(osc, vrcinput.MoveRight)
})
h.HandleEvent(LookLeftStop, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleStopLookEvent(osc, vrcinput.LookLeft)
})
h.HandleEvent(LookRightStop, func(ctx context.Context, s live.Socket, _ live.Params) (interface{}, error) {
return handleStopLookEvent(osc, vrcinput.LookRight)
})
return h
}
// Event handlers
func handleChatEvent(osc *vrcinput.Client, p live.Params, config *StateConfig) (interface{}, error) {
if !config.Chat.Enabled {
return 1, nil
}
msg := p.String("message")
if msg == "" {
return 1, nil
}
if len(msg) > 143 {
msg = msg[:143]
}
config.Chat.Mutex.Lock()
defer config.Chat.Mutex.Unlock()
if time.Since(config.Chat.LastMessageTime) < config.Chat.RateLimit {
log.Println("Chat rate limit exceeded")
return nil, fmt.Errorf("rate limit exceeded")
}
osc.Chat(msg, true, false)
config.StaticMessage.Timer.Reset(10 * time.Second)
config.Chat.LastMessageTime = time.Now()
return 1, nil
}
func handleJumpEvent(osc *vrcinput.Client) (interface{}, error) {
osc.Jump()
return 1, nil
}
func handleMoveEvent(osc *vrcinput.Client, direction vrcinput.MoveDirection) (interface{}, error) {
osc.Move(direction, true)
// log.Println("Received move event: ", direction)
return 1, nil
}
func handleStopMoveEvent(osc *vrcinput.Client, direction vrcinput.MoveDirection) (interface{}, error) {
osc.Move(direction, false)
// log.Println("Received stop event: ", direction)
return 1, nil
}
func handleLookEvent(osc *vrcinput.Client, direction vrcinput.LookDirection) (interface{}, error) {
osc.Look(direction, true)
return 1, nil
}
func handleStopLookEvent(osc *vrcinput.Client, direction vrcinput.LookDirection) (interface{}, error) {
// log.Println("Received stop look event: ", direction)
osc.Look(direction, false)
return 1, nil
}