Пример #1
0
func GetGameInfo(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
	}
	if !users.IsUserSignedIn(c) {
		fmt.Fprintf(w, "Not Registerd")
		return
	}
	id, pErr := strconv.ParseInt(r.FormValue("game_id"), 10, 32)
	if pErr != nil {
		c.Infof("Error parseing int: %v", pErr)
		http.Error(w, pErr.Error(), http.StatusInternalServerError)
		return
	}
	mGame, _, err := GetGame(c, int(id))
	if err != nil {
		c.Infof("Error getting game: %v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	mess, jErr := json.Marshal(mGame)
	if jErr != nil {
		c.Infof("Error json marshal: %v", jErr)
		http.Error(w, jErr.Error(), http.StatusInternalServerError)
		return
	}
	fmt.Fprintf(w, string(mess))

}
Пример #2
0
func ChallengerHandler(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
	}

	mUser, uKey, uErr := users.GetUser(c, u.ID)
	if uErr != nil {
		c.Infof("Error getting user: %v", uErr)
		http.Error(w, uErr.Error(), http.StatusInternalServerError)
		return
	}

	if !users.IsUserSignedIn(c) {
		fmt.Fprintf(w, "Not Registerd")
		return
	}
	opp := r.FormValue("Opponent")
	mUser.RemoveChallenge(opp)

	answer := r.FormValue("answer")
	if answer == "accept" {
		game, err := CreateGame(c, mUser, uKey)
		if err != nil {
			c.Infof("Error creating game: %v", err)
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		oppUser, oKey, oErr := users.GetUser(c, opp)
		if oErr != nil {
			c.Infof("Error getting user: %v", oErr)
			http.Error(w, oErr.Error(), http.StatusInternalServerError)
			return
		}
		game.SID = oppUser.Oid
		game.SName = oppUser.Name
		game.SPic = oppUser.Picture
		game.SaveGame(c)

		oppUser.AddGame(game.GID)
		oppUser.UpdateUser(c, oKey)
	}

}
Пример #3
0
func JoinGame(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	u := user.Current(c)
	if u == nil {
		url, _ := user.LoginURL(c, "/")
		fmt.Fprintf(w, `<a href="%s">Sign in or register</a>`, url)
		return
	}
	if !users.IsUserSignedIn(c) {
		fmt.Fprintf(w, "Not Registerd")
		return
	}
	mGame, _, err := FindFreeGame(c)
	if err != nil {
		c.Infof("Error joingGame: %v", err)
	} else {
		mGame.SaveGame(c)
	}
	GetStartPageMessage(w, r)
}
Пример #4
0
func GetStartPageMessage(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	u := user.Current(c)
	if u == nil {
		url, _ := user.LoginURL(c, "/")
		fmt.Fprintf(w, `<a href="%s">Sign in or register</a>`, url)
		return
	}
	if !users.IsUserSignedIn(c) {
		fmt.Fprintf(w, "Not Registerd")
		return
	}
	users, _, err := users.GetUser(c, u.ID)
	if err != nil {
		c.Infof("Error getting users: %v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	games, _, gErr := GetGames(c, users.Games)
	if gErr != nil {
		c.Infof("Error getting games: %v", gErr)
		http.Error(w, gErr.Error(), http.StatusInternalServerError)
	}

	startMess := new(messages.StartMessage)
	mess := make([]messages.GamesMessage, len(games))
	for i, mGame := range games {
		g := new(messages.GamesMessage)
		g.GID = mGame.GID
		g.MyTurn = mGame.IsUsersTurn(u.ID)
		g.Turn = mGame.NumberOfTurns
		pointOne, pointTwo := CalculateScore(mGame)
		c.Infof("score %v - %v", pointOne, pointTwo)
		c.Infof("%v", mGame.Rounds)
		if u.ID == mGame.FID {
			g.MPoints = pointOne
			g.OPoints = pointTwo
			g.OppID = mGame.SID
			g.OppName = mGame.SName
			g.OppPic = mGame.SPic
		} else {
			g.MPoints = pointTwo
			g.OPoints = pointOne
			g.OppID = mGame.FID
			g.OppName = mGame.FName
			g.OppPic = mGame.FPic
		}
		mess[i] = *g
	}
	startMess.Games = mess
	finished := make([]messages.FinishedGame, len(users.FinishedGames))
	for i, fGame := range users.FinishedGames {
		f := new(messages.FinishedGame)
		f.GID = fGame.GID
		f.MyScore = fGame.MyScore
		f.OppScore = fGame.OppScore
		f.OppName = fGame.OppName
		f.OppPic = fGame.OppPic
		finished[i] = *f
	}
	c.Infof("Finished %v", finished)
	startMess.Finished = finished

	chall := make([]messages.ChallengeMessage, len(users.ChallengeList))
	for i, challenge := range users.ChallengeList {
		f := new(messages.ChallengeMessage)
		f.OppName = challenge.OppName
		f.OppID = challenge.OppID
		chall[i] = *f

	}
	startMess.Challenges = chall
	c.Infof("Challenges %v", chall)
	m, err := json.Marshal(startMess)
	fmt.Fprintf(w, string(m))

}
Пример #5
0
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

	}
}