Example #1
0
func getGameString(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := stringtoUint(vars["GameID"])
	if err != nil {
		WriteError(w, err, 400)
		return
	}

	game, err := game.GetGame(id)

	if err != nil {
		WriteError(w, err, 400)
		return
	}

	WriteJson(w, genMap("Board", game.Board.StringArray(true)))
}
Example #2
0
func getGame(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := stringtoUint(vars["GameID"])
	if err != nil {
		WriteError(w, err, 400)
		return
	}

	game, err := game.GetGame(id)

	if err != nil {
		WriteError(w, err, 400)
		return
	}

	info, err := game.InfoWithNames()

	if err != nil {
		WriteError(w, err, 500)
		return
	}

	WriteJson(w, genMap("Game", info))
}
Example #3
0
//func Balancer(r *http.Request, game Game) error {
func Balancer(r *http.Request, gameID int) error {
	c := appengine.NewContext(r)
	game, _, err := game.GetGame(c, gameID)

	if err != nil {
		c.Infof("Could not get the game")
	}

	var user users.Users
	var answers []int

	userMaxLevel, questionMaxLevel := GetMaxLevels(r)

	// Get the users
	user1, _, _ := users.GetUser(c, game.FID)
	user2, _, _ := users.GetUser(c, game.SID)

	// Loop over each game round
	for _, round := range game.Rounds {
		QID := round.QuestionSID

		questions, err := question.GetQuestionsWithID(c, QID)
		if err != nil {
			c.Infof("Error getting questions")
		}

		// Loop over both users (always two)
		i := 0
		for i < 2 {

			// Balance according to each user, one at the time.
			if i == 0 {
				user = user1
				answers = round.PlayerOneAnswers
			} else {
				user = user2
				answers = round.PlayerTwoAnswerss
			}

			updatedQuestions, err := humbleQuestionBalancer(questions, answers, user, userMaxLevel, questionMaxLevel) // TODO: Write the answer to datastore, it is returned here but does not go anywhere...

			if err != nil {
				return errors.New("Some error updating questions.")
			}

			for _, q := range updatedQuestions {
				question.UpdateQuestion(c, q)
			}

			i++
		}

		// Do the Elo user balancing
		new1, new2, err := userBalancer(user1.Level, user2.Level, round.PlayerOnePoints, round.PlayerTwoPoints)

		if err != nil {
			c.Infof("Error balancing users")
		}

		user1.UpdateLevel(new1)
		user2.UpdateLevel(new2)
	}

	return nil
}
Example #4
0
func makeGameMove(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := stringtoUint(vars["GameID"])
	if err != nil {
		WriteError(w, err, 400)
		return
	}

	player, err := stringtoUint(r.FormValue("Player"))
	if err != nil {
		WriteError(w, errors.New("Error parsing Player form value"), 400)
		return
	}

	box, err := stringtoUint(r.FormValue("Box"))
	if err != nil {
		WriteError(w, errors.New("Error parsing Box form value"), 400)
		return
	}

	square, err := stringtoUint(r.FormValue("Square"))
	if err != nil {
		WriteError(w, errors.New("Error parsing Square form value"), 400)
		return
	}

	if requireAuth {

		timeInt, path, messageHMACString, encoding, err := auth.ExtractAuthParamsNoUser(r)
		if err != nil {
			WriteError(w, err, 400)
			return
		}

		authed, err := auth.CheckAuthParams(player, timeInt, path, messageHMACString, encoding)
		if err != nil || !authed {
			if err != nil {
				log.Println(err)
			}
			WriteErrorString(w, "Not Authorized Request", 401)
			return
		}
	}

	game, err := game.GetGame(id)
	if err != nil {
		WriteError(w, err, 400)
		return
	}

	err = game.MakeMove(player, box, square)
	if err != nil {
		WriteError(w, err, 400)
		return
	}

	_, err = game.Update()
	WriteOutputError(w, genMap("Output", "Successful"), err)

	if err == nil {
		err = ws.BroadcastEvent(id, "Change", fmt.Sprintf("Changed %d, %d", box, square))
		if err != nil {
			log.Println(err)
		}

	}
}