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 return err
} }
app.mu.Lock()
defer app.mu.Unlock()
for _, file := range files { for _, file := range files {
id := file.Name() id := file.Name()
fileName := path.Join(dirName, id, "game.json") gameDirName := path.Join(dirName, id)
if gm, err := game.NewGameFromFile(id, fileName); err != nil { fileName := path.Join(gameDirName, "game.json")
return err gm, errGame := game.NewGameFromFile(id, fileName)
} else { if errGame != nil {
app.games[gm.GetId()] = gm return errGame
} }
errStatement := app.loadStatements(gm, gameDirName)
if errStatement != nil {
return errStatement
}
app.addGame(gm)
} }
return nil 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" "fmt"
"os" "os"
"sirlab.de/go/knyt/engine" "sirlab.de/go/knyt/engine"
"sirlab.de/go/knyt/statement"
) )
func NewGameFromFile(id, fileName string) (*Game, error) { 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) return nil, fmt.Errorf("%s: %v\n", fileName, err)
} else { } else {
gm := Game{ gm := Game{
id: id, id: id,
name: gmJson.Name, name: gmJson.Name,
eng: engine.NewEngine(), eng: engine.NewEngine(),
players: make(map[string]playerInfo), players: make(map[string]playerInfo),
state: STATE_IDLE, state: STATE_IDLE,
statements: make(map[string]map[string]*statement.Statement, 0),
} }
go gm.eng.Run(gm.populateSyncDataCb) go gm.eng.Run(gm.populateSyncDataCb)

View File

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

View File

@ -2,6 +2,7 @@ package game
import ( import (
"sirlab.de/go/knyt/engine" "sirlab.de/go/knyt/engine"
"sirlab.de/go/knyt/statement"
"sync" "sync"
) )
@ -19,12 +20,13 @@ type playerInfo struct {
} }
type Game struct { type Game struct {
mu sync.Mutex mu sync.Mutex
id string id string
name string name string
players map[string]playerInfo players map[string]playerInfo
eng *engine.Engine eng *engine.Engine
state string state string
statements map[string]map[string]*statement.Statement
} }
type GameJson struct { 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 package statement
type Statement struct { type Statement struct {
id string `json:"id"` id string
statement string `json:"statement"` text string
} }
type StatementJson struct { type StatementJson struct {
Id string `json:"id"` Id string `json:"id"`
Statement string `json:"statement"` Text string `json:"text"`
} }