diff --git a/server/src/application/loadGames.go b/server/src/application/loadGames.go index 933a75b..a45637d 100644 --- a/server/src/application/loadGames.go +++ b/server/src/application/loadGames.go @@ -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 } diff --git a/server/src/application/loadQuotes.go b/server/src/application/loadQuotes.go new file mode 100644 index 0000000..a618f67 --- /dev/null +++ b/server/src/application/loadQuotes.go @@ -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 +} diff --git a/server/src/application/loadStatements.go b/server/src/application/loadStatements.go deleted file mode 100644 index 918fe7e..0000000 --- a/server/src/application/loadStatements.go +++ /dev/null @@ -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 -} diff --git a/server/src/game/addQuote.go b/server/src/game/addQuote.go new file mode 100644 index 0000000..74aa540 --- /dev/null +++ b/server/src/game/addQuote.go @@ -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 +} diff --git a/server/src/game/addStatement.go b/server/src/game/addStatement.go deleted file mode 100644 index f7a312d..0000000 --- a/server/src/game/addStatement.go +++ /dev/null @@ -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 -} diff --git a/server/src/game/game.go b/server/src/game/game.go index d19eab9..932f5f2 100644 --- a/server/src/game/game.go +++ b/server/src/game/game.go @@ -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) { @@ -19,12 +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, - statements: make(map[string]map[string]*statement.Statement, 0), + id: id, + name: gmJson.Name, + eng: engine.NewEngine(), + players: make(map[string]playerInfo), + state: STATE_IDLE, + quotes: make(map[string]*quote.Quote, 0), } go gm.eng.Run(gm.populateSyncDataCb) diff --git a/server/src/game/struct.go b/server/src/game/struct.go index 21846c3..cdf1f00 100644 --- a/server/src/game/struct.go +++ b/server/src/game/struct.go @@ -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" ) @@ -20,13 +21,14 @@ type playerInfo struct { } type Game struct { - mu sync.Mutex - id string - name string - players map[string]playerInfo - eng *engine.Engine - state string - statements map[string]map[string]*statement.Statement + mu sync.Mutex + id string + name string + players map[string]playerInfo + eng *engine.Engine + state string + phase string + quotes map[string]*quote.Quote } type GameJson struct { diff --git a/server/src/quote/quote.go b/server/src/quote/quote.go new file mode 100644 index 0000000..ae2e493 --- /dev/null +++ b/server/src/quote/quote.go @@ -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 +} diff --git a/server/src/quote/struct.go b/server/src/quote/struct.go new file mode 100644 index 0000000..ca45891 --- /dev/null +++ b/server/src/quote/struct.go @@ -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"` +} diff --git a/server/src/statement/statement.go b/server/src/statement/statement.go deleted file mode 100644 index 4d63480..0000000 --- a/server/src/statement/statement.go +++ /dev/null @@ -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 -} diff --git a/server/src/statement/struct.go b/server/src/statement/struct.go deleted file mode 100644 index 0a50575..0000000 --- a/server/src/statement/struct.go +++ /dev/null @@ -1,11 +0,0 @@ -package statement - -type Statement struct { - id string - text string -} - -type StatementJson struct { - Id string `json:"id"` - Text string `json:"text"` -}