refactoring: renamed Statement to Quote
This commit is contained in:
parent
189d77ae58
commit
0eb9bf3602
@ -22,7 +22,7 @@ func (app Application) loadGames() error {
|
||||
return errGame
|
||||
}
|
||||
|
||||
errStatement := app.loadStatements(gm, gameDirName)
|
||||
errStatement := app.loadQuotes(gm, gameDirName)
|
||||
if errStatement != nil {
|
||||
return errStatement
|
||||
}
|
||||
|
29
server/src/application/loadQuotes.go
Normal file
29
server/src/application/loadQuotes.go
Normal 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
|
||||
}
|
@ -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
|
||||
}
|
12
server/src/game/addQuote.go
Normal file
12
server/src/game/addQuote.go
Normal 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
|
||||
}
|
@ -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
|
||||
}
|
@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sirlab.de/go/knyt/engine"
|
||||
"sirlab.de/go/knyt/statement"
|
||||
"sirlab.de/go/knyt/quote"
|
||||
)
|
||||
|
||||
func NewGameFromFile(id, fileName string) (*Game, error) {
|
||||
@ -24,7 +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),
|
||||
quotes: make(map[string]*quote.Quote, 0),
|
||||
}
|
||||
|
||||
go gm.eng.Run(gm.populateSyncDataCb)
|
||||
|
@ -2,13 +2,14 @@ package game
|
||||
|
||||
import (
|
||||
"sirlab.de/go/knyt/engine"
|
||||
"sirlab.de/go/knyt/statement"
|
||||
"sirlab.de/go/knyt/quote"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
STATE_IDLE = "idle"
|
||||
STATE_ENTER_STATEMENTS = "enter-statements"
|
||||
STATE_ENTER_STATEMENTS = "enter-quotes"
|
||||
STATE_READY_SET_PLAY = "ready-set-play"
|
||||
STATE_PLAY = "play"
|
||||
)
|
||||
|
||||
@ -26,7 +27,8 @@ type Game struct {
|
||||
players map[string]playerInfo
|
||||
eng *engine.Engine
|
||||
state string
|
||||
statements map[string]map[string]*statement.Statement
|
||||
phase string
|
||||
quotes map[string]*quote.Quote
|
||||
}
|
||||
|
||||
type GameJson struct {
|
||||
|
43
server/src/quote/quote.go
Normal file
43
server/src/quote/quote.go
Normal 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
|
||||
}
|
12
server/src/quote/struct.go
Normal file
12
server/src/quote/struct.go
Normal 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"`
|
||||
}
|
@ -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
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package statement
|
||||
|
||||
type Statement struct {
|
||||
id string
|
||||
text string
|
||||
}
|
||||
|
||||
type StatementJson struct {
|
||||
Id string `json:"id"`
|
||||
Text string `json:"text"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user