Skip to content

Commit

Permalink
Merge pull request #59 from youngeek-0410/fix/question-algorithm
Browse files Browse the repository at this point in the history
Trueが2以上の回答を配布するquestionに設定
  • Loading branch information
GoRuGoo authored Dec 21, 2023
2 parents 83ac0fc + 26fc0ad commit bc5af7b
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions lambda/room/{room_id}/start/POST/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"strconv"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
Expand All @@ -15,6 +13,10 @@ import (
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"math/rand"
"os"
"strconv"
"time"
)

type Answer struct {
Expand Down Expand Up @@ -146,6 +148,16 @@ func returnNumberOfTrueForEachQuestion(userData []UserData) map[string]int {
return totalCount
}

func carefullySelectionOfTrueAnsTwoOrMore(eachQueCount map[string]int) []string {
var twoOrMoreQueIDList []string
for key, count := range eachQueCount {
if count >= 2 {
twoOrMoreQueIDList = append(twoOrMoreQueIDList, key)
}
}
return twoOrMoreQueIDList
}

func DecidingSantaAndQuestion(santaCandidateList []UserData, allUserData []UserData) (string, string) {
trueQueMap := returnNumberOfTrueForEachQuestion(allUserData)

Expand Down Expand Up @@ -201,7 +213,6 @@ func getQuestionDescriptionFromQuestionID(cfg aws.Config, ctx context.Context, q
return "", err
}

fmt.Println(queRes)

for _, que := range queRes {
if que.QuestionID == questionID {
Expand Down Expand Up @@ -242,9 +253,7 @@ func gameStartHandler(ctx context.Context, event events.APIGatewayProxyRequest)
}

santaCandidateList := ReturnSantaCandidateList(allUserData)
santaUserID, torchQuestionID := DecidingSantaAndQuestion(santaCandidateList, allUserData)
fmt.Println("torch")
fmt.Println(torchQuestionID)
santaUserID, _ := DecidingSantaAndQuestion(santaCandidateList, allUserData)

var responseBody ResponseBody
//先ほど取得したサンタのuser_idとリクエストボディのuser_idが一致したらサンタである
Expand All @@ -253,7 +262,13 @@ func gameStartHandler(ctx context.Context, event events.APIGatewayProxyRequest)
} else {
responseBody.IsSanta = false
}
responseBody.QuestionID = torchQuestionID
trueQueMap := returnNumberOfTrueForEachQuestion(allUserData)
twoOrMoreQueIDList := carefullySelectionOfTrueAnsTwoOrMore(trueQueMap)

//回答者が2以上のquestion_idをリストアップして、その長さ分ランダムな整数値を生成し、その数字をインデックスにしてquestion_idを決定
rand.Seed(time.Now().UnixNano())
randNum := rand.Intn(len(twoOrMoreQueIDList))
responseBody.QuestionID = twoOrMoreQueIDList[randNum]

intQueID, err := strconv.Atoi(responseBody.QuestionID)
if err != nil {
Expand Down

0 comments on commit bc5af7b

Please sign in to comment.