diff --git a/server/playerdb/accessor.go b/server/playerdb/accessor.go new file mode 100644 index 0000000..bf04d69 --- /dev/null +++ b/server/playerdb/accessor.go @@ -0,0 +1,14 @@ +package playerdb + +type Player struct { + X int `json:"x"` + Y int `json:"y"` + Z int `json:"z"` + Name string `json:"name"` + HP int `json:"hp"` + //TODO: stamina, skin, etc +} + +type DBAccessor interface { + GetActivePlayers() ([]*Player, error) +} diff --git a/server/playerdb/backend_file.go b/server/playerdb/backend_file.go new file mode 100644 index 0000000..699ec3d --- /dev/null +++ b/server/playerdb/backend_file.go @@ -0,0 +1,34 @@ +package playerdb + +import ( + "io/ioutil" + "time" +) + +//https://stackoverflow.com/questions/46746862/list-files-in-a-directory-sorted-by-creation-time + +type FilePlayerDB struct{} + +func (this *FilePlayerDB) GetActivePlayers() ([]*Player, error) { + files, err := ioutil.ReadDir("Players") + + if err != nil { + return nil, err + } + + now := time.Now().Unix() + activeTime := now - 5 + result := make([]*Player, 0) + + for _, file := range files { + if file.ModTime().Unix() > activeTime { + p := Player{} + + //TODO: parse + + result = append(result, &p) + } + } + + return result, nil +} diff --git a/server/playerdb/create.go b/server/playerdb/create.go new file mode 100644 index 0000000..a3e4d83 --- /dev/null +++ b/server/playerdb/create.go @@ -0,0 +1,15 @@ +package playerdb + +import ( + "errors" + "mapserver/worldconfig" +) + +func Create(cfg *worldconfig.WorldConfig) (DBAccessor, error) { + switch cfg.PlayerBackend { + case worldconfig.BACKEND_FILES: + return &FilePlayerDB{}, nil + default: + return nil, errors.New("player backend not supported: " + worldconfig.BACKEND_FILES) + } +}