Ejemplo n.º 1
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
}
Ejemplo n.º 2
0
Archivo: game.go Proyecto: f4rez/Cronos
func MatchHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	u := user.Current(c)

	if u == nil {
		c.Infof("Not logged in")
		url, _ := user.LoginURL(c, "/")
		fmt.Fprintf(w, `<a href="%s">Sign in or register</a>`, url)
		return
	}
	gID, err := strconv.ParseInt(r.FormValue("game_id"), 10, 32)
	if err != nil {
		c.Infof("Error parsing gameID: %v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	game, key, err2 := GetGame(c, int(gID))
	if err2 != nil {
		c.Infof("Error getting game: %v", err)
		http.Error(w, err2.Error(), http.StatusInternalServerError)
		return
	}
	if !game.IsUsersTurn(u.ID) {
		err3 := errors.New("Det är inte din tur doe...")
		http.Error(w, err3.Error(), http.StatusInternalServerError)

		return
	}
	if !users.IsUserSignedIn(c) {
		fmt.Fprintf(w, "Not Registerd")
		return
	}
	action := r.FormValue("action")
	switch action {
	case "getQuestions":
		c.Infof("game %v", game)
		q, err4 := question.GetQuestionsWithID(c, game.getNewestRound(c).QuestionSID)
		if err4 != nil {
			if len(game.getNewestRound(c).QuestionSID) < 5 {
				game.findPrevAndAddQuestions(c)
				q, err4 = question.GetQuestionsWithID(c, game.getNewestRound(c).QuestionSID)
				if err4 != nil {
					c.Infof("Error getting questions: %v", err4)
					http.Error(w, err4.Error(), http.StatusInternalServerError)
					return
				}
			}

		}
		mess, errJson := json.Marshal(q)
		if errJson != nil {
			c.Infof("Error marshal: ", errJson)
			http.Error(w, errJson.Error(), http.StatusInternalServerError)
			return
		}
		c.Infof("mess: %v", string(mess))
		fmt.Fprint(w, string(mess))
		break
	case "answerQuestions":
		a1, errA1 := strconv.ParseInt(r.FormValue("a1"), 10, 32)
		a2, errA2 := strconv.ParseInt(r.FormValue("a2"), 10, 32)
		a3, errA3 := strconv.ParseInt(r.FormValue("a3"), 10, 32)
		a4, errA4 := strconv.ParseInt(r.FormValue("a4"), 10, 32)
		a5, errA5 := strconv.ParseInt(r.FormValue("a5"), 10, 32)
		if errA1 != nil || errA2 != nil || errA3 != nil || errA4 != nil || errA5 != nil {
			fmt.Fprintf(w, "error parsing input")
			http.Error(w, errA2.Error(), http.StatusInternalServerError)
		}

		points := calculatePoints(a1, a2, a3, a4, a5)
		one, two := CalculateScore(game)
		ParseRoundData(c, u.ID, game, key, int(a1), int(a2), int(a3), int(a4), int(a5), points)
		c.Infof("Points: %v", points)

		if game.NumberOfTurns >= 5 && game.SID == u.ID {

			uErr := users.GameEnded(c, game.GID, game.FID, game.SID, one, two, game.FPic, game.SPic)
			if uErr != nil {
				fmt.Fprintf(w, "error getting users to remove Game")
				http.Error(w, uErr.Error(), http.StatusInternalServerError)
			}
			if one > two {
				fmt.Fprintf(w, "Winner is %v with %v points, loser is %v with %v points", game.FID, one, game.SID, two)
			} else {
				if one < two {
					fmt.Fprintf(w, "Winner is %v with %v points, loser is %v with %v points", game.SID, two, game.FID, one)
				} else {
					if one == two {
						fmt.Fprintf(w, "It's a draw finalscore: %v - %v", one, two)
					}
				}
			}

			// Call balancer here!
			//Balancer(r, game)

			t := taskqueue.NewPOSTTask("/balancer", url.Values{
				"gameID": {string(game.GID)},
			})
			taskqueue.Add(c, t, "") // add t to the default queue; c is appengine.Context

		}

		fmt.Fprintf(w, "Dina svar har registrerats")
		break

	}
}