refactoring: renamed Statement to Quote

This commit is contained in:
Settel 2021-08-13 21:37:32 +02:00
parent 189d77ae58
commit 0eb9bf3602
11 changed files with 115 additions and 129 deletions

View File

@ -22,7 +22,7 @@ func (app Application) loadGames() error {
return errGame return errGame
} }
errStatement := app.loadStatements(gm, gameDirName) errStatement := app.loadQuotes(gm, gameDirName)
if errStatement != nil { if errStatement != nil {
return errStatement return errStatement
} }

View File

@ -0,0 +1,29 @@
package application
import (
"os"
"path"
"sirlab.de/go/knyt/game"
"sirlab.de/go/knyt/quote"
)
func (app Application) loadQuotes(gm *game.Game, dirName string) error {
quoteDirName := path.Join(dirName, "quotes")
files, err := os.ReadDir(quoteDirName)
if err != nil {
return err
}
for _, file := range files {
fileName := path.Join(quoteDirName, file.Name())
quote, err := quote.NewQuoteFromFile(fileName)
if err != nil {
return err
}
gm.AddQuote(quote)
}
return nil
}

View File

@ -1,48 +0,0 @@
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,12 @@
package game
import (
"sirlab.de/go/knyt/quote"
)
func (gm *Game) AddQuote(qu *quote.Quote) {
gm.mu.Lock()
defer gm.mu.Unlock()
gm.quotes[qu.GetId()] = qu
}

View File

@ -1,15 +0,0 @@
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,7 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"sirlab.de/go/knyt/engine" "sirlab.de/go/knyt/engine"
"sirlab.de/go/knyt/statement" "sirlab.de/go/knyt/quote"
) )
func NewGameFromFile(id, fileName string) (*Game, error) { func NewGameFromFile(id, fileName string) (*Game, error) {
@ -24,7 +24,7 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
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), quotes: make(map[string]*quote.Quote, 0),
} }
go gm.eng.Run(gm.populateSyncDataCb) go gm.eng.Run(gm.populateSyncDataCb)

View File

@ -2,13 +2,14 @@ package game
import ( import (
"sirlab.de/go/knyt/engine" "sirlab.de/go/knyt/engine"
"sirlab.de/go/knyt/statement" "sirlab.de/go/knyt/quote"
"sync" "sync"
) )
const ( const (
STATE_IDLE = "idle" STATE_IDLE = "idle"
STATE_ENTER_STATEMENTS = "enter-statements" STATE_ENTER_STATEMENTS = "enter-quotes"
STATE_READY_SET_PLAY = "ready-set-play"
STATE_PLAY = "play" STATE_PLAY = "play"
) )
@ -26,7 +27,8 @@ type Game struct {
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 phase string
quotes map[string]*quote.Quote
} }
type GameJson struct { type GameJson struct {

43
server/src/quote/quote.go Normal file
View File

@ -0,0 +1,43 @@
package quote
import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
)
func NewQuoteFromFile(fileName string) (*Quote, error) {
jsonBytes, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
var quJson QuoteJson
if err := json.Unmarshal(jsonBytes, &quJson); err != nil {
return nil, fmt.Errorf("%s: %v\n", fileName, err)
} else {
_, fileNameShort := path.Split(fileName)
id := strings.TrimSuffix(fileNameShort, ".json")
qu := &Quote{
id: id,
source: quJson.Source,
quote: quJson.Quote,
}
return qu, nil
}
}
func (qu *Quote) GetId() string {
return qu.id
}
func (qu *Quote) GetSource() string {
return qu.source
}
func (qu *Quote) GetQuote() string {
return qu.quote
}

View File

@ -0,0 +1,12 @@
package quote
type Quote struct {
id string
source string
quote string
}
type QuoteJson struct {
Quote string `json:"quote"`
Source string `json:"source"`
}

View File

@ -1,38 +0,0 @@
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 +0,0 @@
package statement
type Statement struct {
id string
text string
}
type StatementJson struct {
Id string `json:"id"`
Text string `json:"text"`
}