Ejemplo n.º 1
0
func EchoJeopardy(w http.ResponseWriter, r *http.Request) {
	echoReq := context.Get(r, "echoRequest").(*alexa.EchoRequest)

	// Start up Mongo!
	mongodb, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}

	col := mongodb.DB("echo").C("jeopardy")
	defer mongodb.Close()

	log.Println(echoReq.GetRequestType())
	log.Println(echoReq.GetSessionID())

	if echoReq.GetRequestType() == "LaunchRequest" {
		session := getJeopardySession(col, echoReq.GetSessionID())

		echoResp, session := jeopardyStart(echoReq, session)

		json, _ := echoResp.String()
		w.Header().Set("Content-Type", "application/json;charset=UTF-8")
		w.Write(json)
	} else if echoReq.GetRequestType() == "IntentRequest" {
		session := getJeopardySession(col, echoReq.GetSessionID())

		log.Println(echoReq.GetIntentName())

		var echoResp *alexa.EchoResponse

		switch echoReq.GetIntentName() {
		case "StartJeopardy":
			echoResp, session = jeopardyStart(echoReq, session)
		case "PickCategory":
			if session.CurrentQuestion.Category == "" {
				echoResp, session = jeopardyCategory(echoReq, session)
			} else {
				echoResp, session = jeopardyAnswer(echoReq, session)
			}

			session.Update(col)
		case "AnswerQuestion":
			echoResp, session = jeopardyAnswer(echoReq, session)
			session.Update(col)
		case "QuitGame":
			echoResp = alexa.NewEchoResponse().OutputSpeech("Ok. You ended with " + strconv.Itoa(session.Dollars) + " after " + strconv.Itoa(session.NumQuestions) + " questions.").EndSession(true)
		default:
			echoResp = alexa.NewEchoResponse().OutputSpeech("I'm sorry, I didn't get that. Can you say that again?").EndSession(false)
		}

		json, _ := echoResp.String()
		w.Header().Set("Content-Type", "application/json;charset=UTF-8")
		w.Write(json)
	} else if echoReq.GetRequestType() == "SessionEndedRequest" {
		//session.Delete(col)
	}
}
Ejemplo n.º 2
0
func jeopardyAnswer(echoReq *alexa.EchoRequest, session *JeopardySession) (*alexa.EchoResponse, *JeopardySession) {
	msg := ""
	echoResp := alexa.NewEchoResponse()

	if session.CurrentQuestion.Answer == "" {
		echoResp.OutputSpeech("You need to get a question before answering!").EndSession(false)
		return echoResp, session
	}

	answer, err := echoReq.GetSlotValue("Answer")
	log.Println(answer)
	if err != nil {
		echoResp.OutputSpeech("We need an answer!").EndSession(false)
		return echoResp, session
	}

	if strings.ToLower(answer) == strings.ToLower(session.CurrentQuestion.Answer) {
		msg += getRandom(JeopardyRightAnswer)
		session.Dollars = session.Dollars + session.CurrentQuestion.Value
	} else {
		msg += getRandom(JeopardyWrongAnswer) + " The correct answer was " + session.CurrentQuestion.Answer + ". "
	}

	session.NumQuestions++
	session.CurrentQuestion = JeopardyQuestion{}

	msg += "You're at " + strconv.Itoa(session.Dollars) + " after " + strconv.Itoa(session.NumQuestions) + " questions. Please pick another category."

	echoResp.OutputSpeech(msg).Card("Answer", session.CurrentQuestion.Answer).EndSession(false)

	return echoResp, session
}
Ejemplo n.º 3
0
func jeopardyCategory(echoReq *alexa.EchoRequest, session *JeopardySession) (*alexa.EchoResponse, *JeopardySession) {
	msg := ""
	echoResp := alexa.NewEchoResponse()

	// Declare the category
	category, err := echoReq.GetSlotValue("Category")
	_, catExists := JeopardyCategories[category]
	if err != nil || !catExists {
		catNames := []string{}
		for k, _ := range JeopardyCategories {
			catNames = append(catNames, k)
		}

		category = getRandom(catNames)

		msg = msg + getRandom(JeopardyCatSelect) + category + ". "
	} else {
		category = strings.ToLower(category)
	}

	clue, err := getJServiceClue(JeopardyCategories[category])
	if err != nil {
		clue, err = getJServiceClue(JeopardyCategories[category])
		if err != nil {
			echoResp := alexa.NewEchoResponse().OutputSpeech("I'm sorry, but I can't seem to get a question right now.").EndSession(true)
			return echoResp, session
		}
	}

	msg += "From " + category + " for " + strconv.Itoa(clue.Value) + ". " + clue.Question + ". I need your answer in the form of a question."

	session.CurrentQuestion.Category = category
	session.CurrentQuestion.Answer = sanitize.HTML(clue.Answer)
	session.CurrentQuestion.Question = clue.Question
	session.CurrentQuestion.Value = clue.Value

	log.Println(session.CurrentQuestion.Question)
	log.Println(session.CurrentQuestion.Answer)

	echoResp.OutputSpeech(msg).Card("Question", msg).Reprompt("Times up. I need your answer in the form of a question.").EndSession(false)

	return echoResp, session
}
Ejemplo n.º 4
0
func jeopardyStart(echoReq *alexa.EchoRequest, session *JeopardySession) (*alexa.EchoResponse, *JeopardySession) {
	catNames := []string{}
	for k, _ := range JeopardyCategories {
		catNames = append(catNames, k)
	}

	msg := JeopardyGreetings[rand.Intn(len(JeopardyGreetings))] + " Please pick one of the following categories: " + strings.Join(catNames, ", ")
	echoResp := alexa.NewEchoResponse().OutputSpeech(msg).EndSession(false)

	return echoResp, session
}