package game import ( "encoding/json" "fmt" "os" "sirlab.de/go/knyt/engine" "sirlab.de/go/knyt/user" "time" ) func NewGameFromFile(id, fileName string) (*Game, error) { jsonBytes, err := os.ReadFile(fileName) if err != nil { return nil, err } var gm Game if err := json.Unmarshal(jsonBytes, &gm); err != nil { return nil, fmt.Errorf("%s: %v\n", fileName, err) } else { gm.id = id gm.eng = engine.NewEngine(id) gm.playerTimestamp = make(map[string]time.Time) go gm.eng.Run() return &gm, nil } } func (gm *Game) GetId() string { return gm.id } func (gm *Game) GetEngine() *engine.Engine { return gm.eng } func (gm *Game) UpdatePlayerTimestamp(usr *user.User) { gm.playerTimestamp[usr.GetId()] = time.Now() } func (gm *Game) GetActivePlayers() []string { players := make([]string, 0) now := time.Now() for usrId, timestamp := range gm.playerTimestamp { elapsed := now.Sub(timestamp) fmt.Printf("%s: %.0f\n", usrId, elapsed.Seconds()) if elapsed.Seconds() < 30.0 { players = append(players, usrId) } } return players }