add init world data

This commit is contained in:
lbenedar
2026-04-08 18:19:20 +03:00
parent e5deb5d76d
commit 468e027418
9 changed files with 464 additions and 74 deletions

View File

@@ -0,0 +1,9 @@
package types
import "sync"
type ExchangeChannels struct {
Msgs map[int](chan []byte)
ProgressMsg map[string](chan struct{})
ProgressMutex sync.Mutex
}

View File

@@ -0,0 +1,88 @@
package types
import (
"errors"
"fmt"
"reflect"
"strings"
)
func findByTag(obj any, tag string) (int, error) {
structType := reflect.TypeOf(obj).Elem()
for i := 0; i < structType.NumField(); i++ {
fieldTags := structType.Field(i).Tag.Get("json")
if tag == strings.Split(fieldTags, ",")[0] {
return i, nil
}
}
return -1, fmt.Errorf("No such field: %s in obj", tag)
}
func setField(obj any, name string, value any) error {
structValue := reflect.ValueOf(obj).Elem()
idx, err := findByTag(obj, name)
if err != nil {
return err
}
structFieldValue := structValue.Field(idx)
if !structFieldValue.IsValid() {
return fmt.Errorf("No such field: %s in obj", name)
}
if !structFieldValue.CanSet() {
return fmt.Errorf("Cannot set %s field value", name)
}
structFieldType := structFieldValue.Type()
val := reflect.ValueOf(value)
if structFieldType != val.Type() {
return errors.New("Provided value type didn't match obj field type")
}
if value == nil {
structFieldValue.Set(reflect.Zero(structFieldType))
} else {
structFieldValue.Set(val)
}
return nil
}
func isPointer(obj any) bool {
iv := reflect.ValueOf(obj)
if !iv.IsValid() {
return false
}
return iv.Kind() == reflect.Pointer
}
func FillStruct(m map[string]any, obj any) error {
if !isPointer(obj) {
return errors.New("Passed obj is not pointer")
}
for k, v := range m {
if v == nil {
continue
}
err := setField(obj, k, v)
if err != nil {
return err
}
}
return nil
}
func Dereference(input any) any {
v := reflect.ValueOf(input)
// Check if the value is a pointer
if v.Kind() == reflect.Pointer {
// v.Elem() returns the value the pointer points to
return v.Elem().Interface()
}
return input
}

View File

@@ -0,0 +1,72 @@
package types
import "fmt"
const (
WsSessionType = "session"
WsProgressType = "progress"
WsUserActivityType = "userActivity"
WsShutdownType = "shutdown"
)
var WsMsgActions = map[string]func() any{
"session": func() any { return &WsSessionMsg{} },
"progress": func() any { return &WsProgressMsg{} },
// "userActivity": WsUserActivityCursor{},
"shutdown": func() any { return &WsShutdownMsg{} },
}
func GetWsMsgAction(dataType string) any {
if factory, ok := WsMsgActions[dataType]; ok {
return factory()
}
return nil
}
type WsSessionMsg struct {
SessionId string `json:"sessionId"`
UserId *string `json:"userId,omitempty"`
}
func (msg WsSessionMsg) Action() error {
fmt.Println("WsSessionMsg")
return nil
}
type WsProgressMsg struct {
Id string `json:"id"`
Message string `json:"message"`
Pct float64 `json:"pct"`
HasChanged bool `json:"hasChanged,omitempty"`
Act string `json:"action"`
Step string `json:"step"`
}
func (msg WsProgressMsg) Action() error {
fmt.Println("WsProgressMsg")
return nil
}
type WsUserActivityMsg struct {
Cursor WsUserActivityCursor `json:"cursor"`
}
type WsUserActivityCursor struct {
X int `json:"x"`
Y int `json:"y"`
}
func (msg WsUserActivityMsg) Action() error {
fmt.Println("WsUserActivityMsg")
return nil
}
type WsShutdownMsg struct {
World string `json:"world"`
UserId *string `json:"userId,omitempty"`
}
func (msg WsShutdownMsg) Action() error {
fmt.Println("WsShutdownMsg")
return nil
}

View File

@@ -1,31 +1,31 @@
package types
type Channels struct {
type ReadChannels struct {
done chan struct{}
msg chan *WsMessage
err chan error
}
func (channels Channels) Err() chan error {
func (channels ReadChannels) Err() chan error {
return channels.err
}
func (channels Channels) Msg() chan *WsMessage {
func (channels ReadChannels) Msg() chan *WsMessage {
return channels.msg
}
func (channels Channels) Done() chan struct{} {
func (channels ReadChannels) Done() chan struct{} {
return channels.done
}
func (channels *Channels) Close() {
func (channels *ReadChannels) Close() {
close(channels.done)
close(channels.err)
close(channels.msg)
}
func InitWsChannels() *Channels {
channels := Channels{
func InitWsChannels() *ReadChannels {
channels := ReadChannels{
done: make(chan struct{}),
err: make(chan error, 10),
msg: make(chan *WsMessage, 10),