package game import ( "encoding/json" "fmt" "os" "sirlab.de/go/knyt/engine" ) func NewGameFromFile(id, fileName string) (*Game, error) { jsonBytes, err := os.ReadFile(fileName) if err != nil { return nil, err } var gmJson GameJson if err := json.Unmarshal(jsonBytes, &gmJson); err != nil { return nil, fmt.Errorf("%s: %v\n", fileName, err) } else { gm := Game{ id: id, name: gmJson.Name, eng: engine.NewEngine(id), } go gm.eng.Run() return &gm, nil } } func (gm *Game) GetId() string { gm.mu.Lock() defer gm.mu.Unlock() return gm.id } func (gm *Game) GetName() string { gm.mu.Lock() defer gm.mu.Unlock() return gm.name } func (gm *Game) GetEngine() *engine.Engine { gm.mu.Lock() defer gm.mu.Unlock() return gm.eng } // func (gm *Game) GetActivePlayers() []string { // gm.mu.Lock() // defer gm.mu.Unlock() // // 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 // }