load statements
This commit is contained in:
parent
d99e052234
commit
3cfd4ce795
@ -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."
|
||||
}
|
||||
|
@ -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."
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
48
server/src/application/loadStatements.go
Normal file
48
server/src/application/loadStatements.go
Normal 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
|
||||
}
|
15
server/src/game/addStatement.go
Normal file
15
server/src/game/addStatement.go
Normal 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
|
||||
}
|
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sirlab.de/go/knyt/engine"
|
||||
"sirlab.de/go/knyt/statement"
|
||||
)
|
||||
|
||||
func NewGameFromFile(id, fileName string) (*Game, error) {
|
||||
@ -23,6 +24,7 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
|
||||
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)
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package game
|
||||
|
||||
import (
|
||||
"sirlab.de/go/knyt/engine"
|
||||
"sirlab.de/go/knyt/statement"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@ -25,6 +26,7 @@ type Game struct {
|
||||
players map[string]playerInfo
|
||||
eng *engine.Engine
|
||||
state string
|
||||
statements map[string]map[string]*statement.Statement
|
||||
}
|
||||
|
||||
type GameJson struct {
|
||||
|
38
server/src/statement/statement.go
Normal file
38
server/src/statement/statement.go
Normal 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
|
||||
}
|
@ -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"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user