forked from MTSR/mapserver
player backend
This commit is contained in:
parent
a36a881129
commit
782758bfa7
14
server/playerdb/accessor.go
Normal file
14
server/playerdb/accessor.go
Normal file
@ -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)
|
||||
}
|
34
server/playerdb/backend_file.go
Normal file
34
server/playerdb/backend_file.go
Normal file
@ -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
|
||||
}
|
15
server/playerdb/create.go
Normal file
15
server/playerdb/create.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user