Beispiel #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)
	}
}