63 lines
1.0 KiB
Go
63 lines
1.0 KiB
Go
package game
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"sirlab.de/go/knyt/engine"
|
|
"sirlab.de/go/knyt/quote"
|
|
)
|
|
|
|
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(),
|
|
players: make(map[string]playerInfo, 0),
|
|
state: STATE_IDLE,
|
|
quotes: make(map[string]*quote.Quote, 0),
|
|
}
|
|
|
|
go gm.eng.Run(gm.populateSyncDataCb)
|
|
|
|
return &gm, nil
|
|
}
|
|
}
|
|
|
|
func (gm *Game) GetId() string {
|
|
gm.mu.Lock()
|
|
defer gm.mu.Unlock()
|
|
|
|
return gm.id
|
|
}
|
|
|
|
func (gm *Game) GetState() string {
|
|
gm.mu.Lock()
|
|
defer gm.mu.Unlock()
|
|
|
|
return gm.state
|
|
}
|
|
|
|
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
|
|
}
|