1
0
forked from MTSR/mapserver

player backend

This commit is contained in:
NatureFreshMilk 2019-01-25 15:02:21 +01:00
parent a36a881129
commit 782758bfa7
3 changed files with 63 additions and 0 deletions

View 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)
}

View 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
View 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)
}
}