Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kisepichu committed Jun 16, 2024
1 parent 09e0e9f commit ff49c8a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
27 changes: 17 additions & 10 deletions internal/pkg/streamer/handle_post_word.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type rejectedPostWord struct {

var NotMatchShiritoriError = errors.New("ルール違反")

func addWord(db *sqlx.DB, roomId string, word string, reading string, basic_score int) (int, error) {
func addWord(db *sqlx.DB, roomId string, userId string, word string, reading string, basic_score int) (int, error) {
lastReading, err := getLastWordReading(db, roomId)
if err != nil {
fmt.Println("failed to get last word:", err)
Expand All @@ -45,7 +45,7 @@ func addWord(db *sqlx.DB, roomId string, word string, reading string, basic_scor
}

fmt.Println("word: %s, reading: %s, basic_score: %d", word, reading, basic_score)
res, err := db.Exec("INSERT INTO `words` (`room_id`, `word`, `reading`, `basic_score`) VALUES (?, ?, ?, ?)", roomId, word, reading, basic_score)
res, err := db.Exec("INSERT INTO words (room_id, user_id, word, reading, basic_score) VALUES (?, ?, ?, ?)", roomId, userId, word, reading, basic_score)
if err != nil {
fmt.Println("failed to insert word:", err)
return 0, err
Expand All @@ -61,7 +61,7 @@ func addWord(db *sqlx.DB, roomId string, word string, reading string, basic_scor

func getLastWordReading(db *sqlx.DB, roomId string) (string, error) {
var reading string
err := db.Get(&reading, "SELECT `reading` FROM `words` WHERE `room_id` = ? ORDER BY `word_id` DESC LIMIT 1", roomId)
err := db.Get(&reading, "SELECT reading FROM words WHERE room_id = ? ORDER BY word_id DESC LIMIT 1", roomId)
if err != nil {
return "", nil
}
Expand All @@ -70,20 +70,26 @@ func getLastWordReading(db *sqlx.DB, roomId string) (string, error) {

func (s *Streamer) handleInit(db *sqlx.DB, roomId string, clientID uuid.UUID) error {
var words []struct {
Word string `db:"word"`
Reading string `db:"reading"`
WordId int `db:"word_id"`
Word string `db:"word"`
Reading string `db:"reading"`
UserName string `db:"user_name"`
BasicScore int `db:"basic_score"`
}
err := db.Select(&words, "SELECT `word`, `reading` FROM `words` WHERE `room_id` = ? ORDER BY `word_id` LIMIT 10", roomId)
err := db.Select(&words, "SELECT `word_id`, `word`, `reading`, `user_name`, `basic_score` FROM `words` WHERE `room_id` = ? ORDER BY `word_id` LIMIT 10", roomId)
if err != nil {
fmt.Println("failed to get words:", err)
return err
}

for _, word := range words {
message := postWordResponse{
Type: "posted_word",
Word: word.Word,
Reading: word.Reading,
Type: "posted_word",
UserName: word.UserName,
WordId: word.WordId,
Word: word.Word,
Reading: word.Reading,
BasicScore: word.BasicScore,
}
messageBytes, err := json.Marshal(message)
if err != nil {
Expand All @@ -103,10 +109,11 @@ func (s *Streamer) handlePostWord(db *sqlx.DB, roomId string, clientID uuid.UUID
fmt.Println("failed to get last word:", err)
return err
}
userId := s.clients[clientID].name
roomIdUuid := uuid.FromStringOrNil(roomId)
rune_count, err := s.repo.GetRuneCount(roomIdUuid)
basicScore := util.GetScore(lastReading, args.Reading, rune_count)
wordId, err := addWord(db, roomId, args.Word, args.Reading, basicScore)
wordId, err := addWord(db, roomId, userId, args.Word, args.Reading, basicScore)
if err != nil {
if err == NotMatchShiritoriError {
message := rejectedPostWord{
Expand Down
1 change: 1 addition & 0 deletions sql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ DROP TABLE IF EXISTS `words`;
CREATE TABLE `words` (
`word_id` INTEGER NOT NULL AUTO_INCREMENT,
`room_id` VARCHAR(36) NOT NULL,
`user_id` VARCHAR(36) NOT NULL,
`word` varchar(30) NOT NULL,
`reading` varchar(30) NOT NULL,
`basic_score` INTEGER NOT NULL,
Expand Down

0 comments on commit ff49c8a

Please sign in to comment.