コード例 #1
0
ファイル: game.go プロジェクト: f4rez/Cronos
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)
	}

}
コード例 #2
0
ファイル: game.go プロジェクト: f4rez/Cronos
func FindFreeGame(c appengine.Context) (Game, *datastore.Key, error) {
	qn := datastore.NewQuery("Game").
		Ancestor(GameKey(c)).
		Order("-Created").
		Filter("SID =", "")
	count, err := qn.Count(c)
	u := user.Current(c)
	user, uKey, uErr := users.GetUser(c, u.ID)
	if uErr != nil {
		return *new(Game), nil, uErr
	}

	if err != nil {
		return *new(Game), nil, err
	}
	if count == 0 {
		g, err := CreateGame(c, user, uKey)
		return g, nil, err
	} else {
		game := make([]Game, 1, 1)
		if key, err := qn.GetAll(c, &game); len(key) > 0 {
			if u.ID == game[1].FID {
				mErr := errors.New("Du är redan med i det senaste ska")
				return *new(Game), nil, mErr
			}
			temp := make([]Round, 1, 1)
			temp2 := game[1].RoundsJson
			err := json.Unmarshal(temp2, &temp)
			if err != nil {
				u := new(Game)
				return *u, nil, err
			}
			game[1].Rounds = temp

			game[1].SID = u.ID
			game[1].SName = user.Name
			game[1].SPic = user.Picture
			game[1].UpdateGame(c, key[0])
			user.AddGame(game[1].GID)
			user.UpdateUser(c, uKey)
			return game[1], key[0], nil
		} else {
			c.Infof("Err: %v", err)
			u := new(Game)
			return *u, nil, err
		}

	}
}
コード例 #3
0
ファイル: balancer.go プロジェクト: f4rez/Cronos
//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
}
コード例 #4
0
ファイル: game.go プロジェクト: f4rez/Cronos
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))

}