-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
335 lines (302 loc) Β· 8.69 KB
/
server.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"errors"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
rl "github.com/gen2brain/raylib-go/raylib"
)
type EditMode int32
const (
EditRot EditMode = iota
EditPos
EditScale
ResetChanges
ToggleModel
HighlightCurrent
FocusCurrent
DeleteModel
Cancel
)
type (
msgPayload []byte
Editor struct {
con net.Conn
addr string
editContext struct {
modelName string
mode EditMode
}
}
Message struct {
from *Editor
payload msgPayload
}
Server struct {
quitch chan struct{}
msgch chan Message
ln net.Listener
logger *log.Logger
editorsOnline map[string]*Editor
addr string
}
)
func NewLogger(filename string) *log.Logger {
logFile, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
panic(err)
}
return log.New(logFile, "[deck_crawler_editor]", log.Ldate|log.Ltime)
}
func NewServer(addr string) *Server {
return &Server{
addr: addr,
msgch: make(chan Message, 100),
editorsOnline: make(map[string]*Editor, 10),
quitch: make(chan struct{}),
logger: NewLogger("./editor.log"),
}
}
func (s *Server) Start() error {
ln, err := net.Listen("tcp", s.addr)
if err != nil {
return err
}
defer ln.Close()
s.ln = ln
go s.acceptLoop()
go s.handleMessage()
<-s.quitch
close(s.msgch)
return nil
}
func (s *Server) acceptLoop() {
for {
con, err := s.ln.Accept()
if err != nil {
break
}
go s.handleConn(con)
}
}
func (s *Server) handleConn(con net.Conn) {
defer func() {
delete(s.editorsOnline, con.RemoteAddr().String())
fmt.Printf("[%s] has disconnected \n", con.RemoteAddr().String())
if len(s.editorsOnline) == 0 {
GameState.editMode = false
}
con.Close()
}()
buf := make([]byte, 1024)
editor := &Editor{
con: con,
addr: con.RemoteAddr().String(),
}
editor.editContext.mode = Cancel
s.editorsOnline[editor.addr] = editor
GameState.editMode = true
Scene.listItems(con)
con.Write([]byte("enter item name: "))
for {
n, err := con.Read(buf)
if err != nil {
break
}
s.msgch <- Message{
from: editor,
payload: buf[:n],
}
}
}
func (s *Server) handleMessage() {
for msg := range s.msgch {
input := string(msg.payload[:len(msg.payload)-1])
item, e := Scene.Items[string(input)]
_ = item
switch {
case Matches(msg.payload.String(), keys.Back):
if msg.from.editContext.modelName == "" {
msg.from.con.Write([]byte("to quite type 'q' or 'quit' \n"))
}
if msg.from.editContext.modelName != "" {
if msg.from.editContext.mode != Cancel {
msg.from.resetEditMode()
} else {
msg.from.editContext.modelName = ""
Scene.listItems(msg.from.con)
msg.from.con.Write([]byte("enter item name: "))
}
}
fmt.Println("editor - back")
continue
case Matches(msg.payload.String(), keys.Quit):
msg.from.con.Close()
fmt.Println("editor - quit")
continue
case Matches(msg.payload.String(), keys.Save):
fmt.Println("save is not implemented")
continue
case Matches(msg.payload.String(), keys.Help):
fmt.Println("help is not implemented")
continue
case Matches(msg.payload.String(), keys.FullScreen):
GameState.editFull = !GameState.editFull
continue
case Matches(msg.payload.String(), keys.CycleCamMode):
if GameState.camMode == rl.CameraFirstPerson {
GameState.camMode = rl.CameraThirdPerson
fmt.Println("switched cam mode: third")
} else {
GameState.camMode = rl.CameraFirstPerson
fmt.Println("switched cam mode: first")
}
continue
}
switch {
case msg.from.editContext.modelName == "" && !e:
errMsg := fmt.Sprintf("name not found: %s \n", input)
fmt.Print(errMsg)
msg.from.con.Write([]byte(errMsg))
case msg.from.editContext.modelName == "":
msg.from.editContext.modelName = string(input)
msg.from.askAttr()
case msg.from.editContext.mode == Cancel:
mode, err := msg.payload.parseEditMode()
if err != nil {
fmt.Println(err)
msg.from.con.Write([]byte("invalid mode"))
break
}
msg.from.editContext.mode = mode
selectedItem := Scene.Items[msg.from.editContext.modelName]
// NOTE: after item selection
switch msg.from.editContext.mode {
case EditPos:
fmt.Fprintf(msg.from.con, "Poisiton (%+v):", selectedItem.pos)
case EditRot:
rot := Scene.Items[msg.from.editContext.modelName].rot
fmt.Fprintf(msg.from.con, "Rotation (X:%f, Y: %f, Z: %f):", rot.X*rl.Rad2deg, rot.Y*rl.Rad2deg, rot.Z*rl.Rad2deg)
case EditScale:
fmt.Fprintf(msg.from.con, "Scale (%+v):", selectedItem.scale)
case ResetChanges:
msg.from.con.Write([]byte("reseting unsaved changes...\n"))
// TODO: reset the changes here
msg.from.resetEditMode()
case HighlightCurrent:
selectedItem.highlight = !selectedItem.highlight
msg.from.con.Write([]byte("element highlighted...\n"))
msg.from.resetEditMode()
case FocusCurrent:
if msg.from.editContext.modelName == GameState.editFocusedItemUid {
GameState.editFocusedItemUid = ""
} else {
GameState.editFocusedItemUid = msg.from.editContext.modelName
}
msg.from.con.Write([]byte("focused on element...\n"))
msg.from.resetEditMode()
case ToggleModel:
selectedItem.hidden = !Scene.Items[msg.from.editContext.modelName].hidden
fmt.Fprint(msg.from.con, "Item toggled \n")
msg.from.resetEditMode()
case DeleteModel:
msg.from.con.Write([]byte("Are you you want to DELETE the model? (y/n):"))
case Cancel:
msg.from.con.Write([]byte("canceled"))
}
default:
if msg.from.editContext.mode != Cancel {
vals := strings.Split(input, " ")
switch msg.from.editContext.mode {
case EditPos:
if len(vals) != 3 {
msg.from.con.Write([]byte("not enough arguments, need 3\n"))
break
}
v1, _ := strconv.ParseFloat(vals[0], 32)
v2, _ := strconv.ParseFloat(vals[1], 32)
v3, _ := strconv.ParseFloat(vals[2], 32)
msg.from.con.Write([]byte("Position: "))
Scene.Items[msg.from.editContext.modelName].pos = rl.NewVector3(float32(v1), float32(v2), float32(v3))
case EditRot:
if len(vals) != 3 {
msg.from.con.Write([]byte("invalid number of arguments, need 3\n"))
break
}
msg.from.con.Write([]byte("Rotation: "))
v1, _ := strconv.ParseFloat(vals[0], 32)
v2, _ := strconv.ParseFloat(vals[1], 32)
v3, _ := strconv.ParseFloat(vals[2], 32)
v := rl.NewVector3(float32(v1)*rl.Deg2rad, float32(v2)*rl.Deg2rad, float32(v3)*rl.Deg2rad)
Scene.Items[msg.from.editContext.modelName].model.Transform = rl.MatrixRotateXYZ(v)
case EditScale:
if len(vals) != 1 {
msg.from.con.Write([]byte("invalid number of arguments, need 1\n"))
break
}
msg.from.con.Write([]byte("scale edit"))
case ResetChanges:
msg.from.con.Write([]byte("reset all changes"))
v1, _ := strconv.ParseFloat(vals[0], 32)
Scene.Items[msg.from.editContext.modelName].scale = float32(v1)
case DeleteModel:
if input[0] == 'y' || input == "yes" {
delete(Scene.Items, msg.from.editContext.modelName)
msg.from.editContext.mode = Cancel
msg.from.editContext.modelName = ""
fmt.Fprint(msg.from.con, "Model deleted... \n")
Scene.listItems(msg.from.con)
} else {
fmt.Fprint(msg.from.con, "Canceled...\n")
msg.from.editContext.mode = Cancel
}
case Cancel:
msg.from.con.Write([]byte("Canceled...\n"))
default:
msg.from.con.Write([]byte("\nif you see this message open an issue\n"))
}
}
}
}
}
func (payload msgPayload) parseEditMode() (EditMode, error) {
if len(payload) == 0 {
return -1, fmt.Errorf("payload is empty")
}
b := payload[0] - 49
if b >= byte(EditRot) && b <= byte(Cancel) {
return EditMode(b), nil
}
return -1, fmt.Errorf("invalid byte for EditMode: %v", b)
}
func (data msgPayload) String() string {
return string(data[:len(data)-1])
}
func (input EditMode) validate() error {
if input < EditRot || input > Cancel {
return errors.New("invalid EditMode value")
}
return nil
}
func (e *Editor) askAttr() {
temp := []string{"rotation", "position", "scale", "reset", "hide model", "toggle highlight", "toggle focus", "delete model", "cancel"}
el := Scene.Items[e.editContext.modelName]
fmt.Fprintf(e.con, "[%s] Pos: %+v | Rot: %+v | Scale:%+v | Visible:%+v | %+v \n", e.editContext.modelName, el.pos, el.rot, el.scale, el.hidden, el.model.Materials.Shader)
for i, cmd := range temp {
fmt.Fprintf(e.con, "[%d] - %s \n", i+1, cmd)
}
}
func (s *Scene3D) listItems(con net.Conn) {
con.Write([]byte("\n-----Item List------\n"))
for i := range Scene.Items {
con.Write([]byte(i + "\n"))
}
con.Write([]byte("--------------------\n"))
}
func (editor *Editor) resetEditMode() {
editor.editContext.mode = Cancel
editor.askAttr()
}