load statements

This commit is contained in:
Settel 2021-08-13 02:58:58 +02:00
parent d99e052234
commit 3cfd4ce795
10 changed files with 150 additions and 30 deletions

View File

@ -1,3 +1,3 @@
{
"statement": "Mit 5 Jahren bin ich vom Baum gefallen und habe seither eine Narbe quer über die Wade."
"text": "Mit 5 Jahren bin ich vom Baum gefallen und habe seither eine Narbe quer über die Wade."
}

View File

@ -1,3 +1,3 @@
{
"statement": "Mein erstes Haustier war ein Hamster namens Flecki, den ich mir mit 29 geholt habe."
"text": "Mein erstes Haustier war ein Hamster namens Flecki, den ich mir mit 29 geholt habe."
}

View File

@ -13,18 +13,29 @@ func (app Application) loadGames() error {
return err
}
app.mu.Lock()
defer app.mu.Unlock()
for _, file := range files {
id := file.Name()
fileName := path.Join(dirName, id, "game.json")
if gm, err := game.NewGameFromFile(id, fileName); err != nil {
return err
} else {
app.games[gm.GetId()] = gm
gameDirName := path.Join(dirName, id)
fileName := path.Join(gameDirName, "game.json")
gm, errGame := game.NewGameFromFile(id, fileName)
if errGame != nil {
return errGame
}
errStatement := app.loadStatements(gm, gameDirName)
if errStatement != nil {
return errStatement
}
app.addGame(gm)
}
return nil
}
func (app Application) addGame(gm *game.Game) {
app.mu.Lock()
defer app.mu.Unlock()
app.games[gm.GetId()] = gm
}

View File

@ -0,0 +1,48 @@
package application
import (
"os"
"path"
"sirlab.de/go/knyt/game"
"sirlab.de/go/knyt/statement"
)
func (app Application) loadStatements(gm *game.Game, dirName string) error {
files, err := os.ReadDir(dirName)
if err != nil {
return err
}
for _, dir := range files {
if !dir.IsDir() {
continue
}
userId := dir.Name()
statementDir := path.Join(dirName, userId)
err := app.loadUserStatements(gm, userId, statementDir)
if err != nil {
return err
}
}
return nil
}
func (app Application) loadUserStatements(gm *game.Game, userId, dirName string) error {
files, err := os.ReadDir(dirName)
if err != nil {
return err
}
for _, file := range files {
fileName := path.Join(dirName, file.Name())
statement, err := statement.NewStatementFromFile(fileName)
if err != nil {
return err
}
gm.AddStatement(userId, statement)
}
return nil
}

View File

@ -0,0 +1,15 @@
package game
import (
"sirlab.de/go/knyt/statement"
)
func (gm *Game) AddStatement(userId string, sm *statement.Statement) {
gm.mu.Lock()
defer gm.mu.Unlock()
if gm.statements[userId] == nil {
gm.statements[userId] = make(map[string]*statement.Statement, 0)
}
gm.statements[userId][sm.GetId()] = sm
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"os"
"sirlab.de/go/knyt/engine"
"sirlab.de/go/knyt/statement"
)
func NewGameFromFile(id, fileName string) (*Game, error) {
@ -18,11 +19,12 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
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),
state: STATE_IDLE,
id: id,
name: gmJson.Name,
eng: engine.NewEngine(),
players: make(map[string]playerInfo),
state: STATE_IDLE,
statements: make(map[string]map[string]*statement.Statement, 0),
}
go gm.eng.Run(gm.populateSyncDataCb)

View File

@ -12,6 +12,14 @@ func (gm *Game) populateGameInfo() syncdata.GameInfo {
gm.mu.Lock()
defer gm.mu.Unlock()
return syncdata.GameInfo{
GameId: gm.id,
State: gm.state,
Players: gm.getPlayers(),
}
}
func (gm *Game) getPlayers() []syncdata.PlayerInfo {
players := make([]syncdata.PlayerInfo, 0)
for _, p := range gm.players {
if p.isPlaying {
@ -23,9 +31,5 @@ func (gm *Game) populateGameInfo() syncdata.GameInfo {
}
}
return syncdata.GameInfo{
GameId: gm.id,
State: gm.state,
Players: players,
}
return players
}

View File

@ -2,6 +2,7 @@ package game
import (
"sirlab.de/go/knyt/engine"
"sirlab.de/go/knyt/statement"
"sync"
)
@ -19,12 +20,13 @@ type playerInfo struct {
}
type Game struct {
mu sync.Mutex
id string
name string
players map[string]playerInfo
eng *engine.Engine
state string
mu sync.Mutex
id string
name string
players map[string]playerInfo
eng *engine.Engine
state string
statements map[string]map[string]*statement.Statement
}
type GameJson struct {

View File

@ -0,0 +1,38 @@
package statement
import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
)
func NewStatementFromFile(fileName string) (*Statement, error) {
jsonBytes, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
var smJson StatementJson
if err := json.Unmarshal(jsonBytes, &smJson); err != nil {
return nil, fmt.Errorf("%s: %v\n", fileName, err)
} else {
_, fileNameShort := path.Split(fileName)
id := strings.TrimSuffix(fileNameShort, ".json")
sm := &Statement{
id: id,
text: smJson.Text,
}
return sm, nil
}
}
func (sm *Statement) GetId() string {
return sm.id
}
func (sm *Statement) GetStatementText() string {
return sm.text
}

View File

@ -1,11 +1,11 @@
package statement
type Statement struct {
id string `json:"id"`
statement string `json:"statement"`
id string
text string
}
type StatementJson struct {
Id string `json:"id"`
Statement string `json:"statement"`
Id string `json:"id"`
Text string `json:"text"`
}