2021-08-04 22:12:28 +00:00
|
|
|
package game
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-08-04 22:52:35 +00:00
|
|
|
"sirlab.de/go/knyt/engine"
|
2021-08-04 22:12:28 +00:00
|
|
|
)
|
|
|
|
|
2021-08-04 22:52:35 +00:00
|
|
|
func NewGameFromFile(id, fileName string) (*Game, error) {
|
2021-08-04 22:12:28 +00:00
|
|
|
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 {
|
2021-08-04 22:52:35 +00:00
|
|
|
gm.id = id
|
|
|
|
gm.eng = engine.NewEngine(id)
|
2021-08-04 22:12:28 +00:00
|
|
|
return &gm, nil
|
|
|
|
}
|
|
|
|
}
|
2021-08-04 22:40:31 +00:00
|
|
|
|
|
|
|
func (gm *Game) GetId() string {
|
|
|
|
return gm.id
|
|
|
|
}
|