Beispiel #1
0
func convertArticle(user *models.Account, article *models.Article, author *userJsonStruct) *articleJsonStruct {
	jsonStruct := &articleJsonStruct{}
	jsonStruct.Id = article.Id.Hex()
	jsonStruct.Parent = article.Parent
	//jsonStruct.Contents = article.Contents
	jsonStruct.PubTime = article.PubTime.Unix()
	jsonStruct.Thumbs = len(article.Thumbs)
	//jsonStruct.ThumbUsers = article.Thumbs
	for _, thumber := range article.Thumbs {
		if thumber == user.Id {
			jsonStruct.Thumbed = true
		}
	}

	jsonStruct.Reviews = article.ReviewCount
	jsonStruct.CoachReviews = article.CoachReviewCount
	jsonStruct.Rewards = article.TotalReward

	jsonStruct.Title = article.Title
	jsonStruct.Image = article.Image
	//jsonStruct.Images = article.Images
	jsonStruct.Content = article.Content
	jsonStruct.Contents = article.Contents

	jsonStruct.Author = author.Userid

	thumbers := article.Thumbs
	if len(article.Thumbs) > 6 {
		thumbers = article.Thumbs[len(article.Thumbs)-6:]
	}
	//jsonStruct.ThumbUsers = nil
	users, _ := models.FindUsersByIds(0, thumbers...)
	for i := len(thumbers); i > 0; i-- { // reverse
		for j, _ := range users {
			if users[j].Id == thumbers[i-1] {
				jsonStruct.ThumbUsers = append(jsonStruct.ThumbUsers, users[j].Profile)
				break
			}
		}
	}

	jsonStruct.AuthorInfo = author
	jsonStruct.Type = article.Type
	jsonStruct.Public = (article.Privilege == models.PrivPublic)
	jsonStruct.Location = article.Loc
	if jsonStruct.Location.Lat == 0 {
		jsonStruct.Location = author.Location
	}
	if bson.IsObjectIdHex(article.Record) {
		rec := &models.Record{Id: bson.ObjectIdHex(article.Record)}
		rec.Find()
		jsonStruct.Record = convertRecord(rec)
	}
	return jsonStruct
}
Beispiel #2
0
func thumbersHandler(r *http.Request, w http.ResponseWriter,
	form thumbersForm) {

	article := &models.Article{}
	article.FindById(form.Id)

	var respData struct {
		Users []*leaderboardResp `json:"members_list"`
	}

	if form.Index < 0 {
		form.Index = 0
	}
	thumbers := article.Thumbs
	end := len(thumbers) - form.Index*20
	start := end - 20

	if end <= 0 {
		respData.Users = []*leaderboardResp{}
		writeResponse(r.RequestURI, w, respData, nil)
		return
	}
	if start < 0 {
		start = 0
	}

	thumbers = article.Thumbs[start:end]
	users, _ := models.FindUsersByIds(1, thumbers...)

	for j := len(thumbers); j > 0; j-- { // reverse
		for i, _ := range users {
			if users[i].Id == thumbers[j-1] {
				respData.Users = append(respData.Users, &leaderboardResp{
					Userid:   users[i].Id,
					Score:    users[i].Props.Score,
					Level:    users[i].Level(),
					Profile:  users[i].Profile,
					Nickname: users[i].Nickname,
					Gender:   users[i].Gender,
					LastLog:  users[i].LastLogin.Unix(),
					Birth:    users[i].Birth,
					Location: users[i].Loc,
					Addr:     users[i].LocAddr,
					Phone:    users[i].Phone,
				})
				break
			}
		}
	}

	writeResponse(r.RequestURI, w, respData, nil)
	return

}
Beispiel #3
0
func gamelbHandler(r *http.Request, w http.ResponseWriter,
	redis *models.RedisLogger, user *models.Account, p Parameter) {

	form := p.(gamelbForm)
	if form.Index <= 0 {
		form.Index = 0
	}
	if form.Count <= 0 {
		form.Count = 20
	}

	gt := gameType(form.Game)
	var ids []string
	var kvs []models.KV

	switch form.Query {
	case "FRIEND":
		ids = redis.Friends(models.RelFriend, user.Id)
		if len(ids) == 0 {
			break
		}

		ids = append(ids, user.Id)

		if form.Index*form.Count >= len(ids) {
			break
		}

		start := form.Index * form.Count
		end := start + form.Count
		if end > len(ids) {
			end = len(ids)
		}

		scores := redis.UserGameScores(gt, ids...)
		if len(scores) != len(ids) {
			scores = make([]int64, len(ids))
		}

		kvs = make([]models.KV, len(ids))
		for i, _ := range kvs {
			kvs[i].K = ids[i]
			kvs[i].V = scores[i]
			if ids[i] == user.Id {
				kvs[i].V = int64(form.Score)
			}
		}
		sort.Sort(sort.Reverse(models.KVSlice(kvs)))

		kvs = kvs[start:end]
		ids = []string{}
		for _, kv := range kvs {
			ids = append(ids, kv.K)
		}
	case "TOP":
		fallthrough
	default:
		maxScore := 0
		if scores := redis.UserGameScores(gt, user.Id); len(scores) == 1 {
			maxScore = int(scores[0])
		}
		redis.SetGameScore(gt, user.Id, form.Score) // current score
		kvs = redis.GameScores(gt, form.Index*form.Count, form.Count)
		redis.SetGameScore(gt, user.Id, maxScore) // recover max score
		for i, kv := range kvs {
			ids = append(ids, kv.K)
			if kv.K == user.Id {
				kvs[i].V = int64(form.Score)
			}
		}
	}

	var respData struct {
		List []*leaderboardResp `json:"members_list"`
	}

	users, _ := models.FindUsersByIds(1, ids...)
	index := 0
	for _, kv := range kvs {
		for i, _ := range users {
			if users[i].Id == kv.K {
				respData.List = append(respData.List, &leaderboardResp{
					Userid:   users[i].Id,
					Score:    kv.V,
					Rank:     form.Index*form.Count + index + 1,
					Level:    users[i].Level(),
					Profile:  users[i].Profile,
					Nickname: users[i].Nickname,
					Actor:    users[i].Actor,
					Gender:   users[i].Gender,
					LastLog:  users[i].LastGameTime(gt).Unix(),
					Birth:    users[i].Birth,
					Location: users[i].Loc,
					Phone:    users[i].Phone,
				})
				index++

				break
			}
		}
	}

	writeResponse(r.RequestURI, w, respData, nil)
}
Beispiel #4
0
func leaderboardHandler(request *http.Request, resp http.ResponseWriter,
	redis *models.RedisLogger, user *models.Account, form leaderboardForm) {
	if form.Paging.Count == 0 {
		form.Paging.Count = models.DefaultPageSize
	}

	start := 0
	stop := 0

	switch form.Type {
	case "FRIEND":
		ids := redis.Friends("friend", user.Id)
		friends, err := models.Users(ids, &form.Paging)
		if err != nil {
			writeResponse(request.RequestURI, resp, nil, err)
			return
		}
		lb := make([]leaderboardResp, len(friends))
		for i, _ := range friends {
			lb[i].Userid = friends[i].Id
			lb[i].Score = friends[i].Props.Score
			lb[i].Level = friends[i].Level()
			lb[i].Profile = friends[i].Profile
			lb[i].Nickname = friends[i].Nickname
			lb[i].Gender = friends[i].Gender
			lb[i].Actor = friends[i].Actor
			lb[i].LastLog = friends[i].LastLogin.Unix()
			lb[i].Birth = friends[i].Birth
			lb[i].Location = friends[i].Loc
			lb[i].Phone = friends[i].Phone
		}

		respData := map[string]interface{}{
			"members_list":  lb,
			"page_frist_id": form.Paging.First,
			"page_last_id":  form.Paging.Last,
		}
		writeResponse(request.RequestURI, resp, respData, nil)

		return

	case "USER_AROUND":
		rank := redis.LBDisRank(form.Info)
		if rank < 0 {
			writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.NotExistsError, "user not exist"))
			return
		}

		if form.Paging.Count < 0 {
			start = rank
			stop = rank
			break
		}

		start = rank - form.Paging.Count
		if start < 0 {
			start = 0
		}
		stop = rank + form.Paging.Count
	case "TOP":
		fallthrough
	default:
		start, stop = leaderboardPaging(&form.Paging)
	}

	kv := redis.GetDisLB(start, stop)
	ids := make([]string, len(kv))
	for i, _ := range kv {
		ids[i] = kv[i].K
	}

	users, _ := models.FindUsersByIds(0, ids...)

	lb := make([]leaderboardResp, len(kv))
	for i, _ := range kv {
		lb[i].Userid = kv[i].K
		lb[i].Rank = start + i + 1
		lb[i].Score = kv[i].V
		for _, user := range users {
			if user.Id == kv[i].K {
				lb[i].Nickname = user.Nickname
				lb[i].Profile = user.Profile
				break
			}
		}
	}

	page_first := 0
	page_last := 0
	if len(lb) > 0 {
		page_first = lb[0].Rank
		page_last = lb[len(lb)-1].Rank
	}

	respData := map[string]interface{}{
		"members_list":  lb,
		"page_frist_id": strconv.Itoa(page_first),
		"page_last_id":  strconv.Itoa(page_last),
	}
	writeResponse(request.RequestURI, resp, respData, nil)
}
Beispiel #5
0
func gameResultHandler(r *http.Request, w http.ResponseWriter,
	redis *models.RedisLogger, user *models.Account, p Parameter) {

	form := p.(gameResultForm)
	var respData struct {
		Total     []*leaderboardResp `json:"total_list"`
		Friends   []*leaderboardResp `json:"friends_list"`
		Score     int                `json:"total_score"`
		Percent   int                `json:"percent"`
		PerFriend int                `json:"percentFri"`
	}

	if form.Count == 0 {
		form.Count = 3
	}

	gt := gameType(form.Type)
	if scores := redis.UserGameScores(gt, user.Id); len(scores) == 1 {
		respData.Score = int(scores[0])
	}
	redis.SetGameScore(gt, user.Id, form.Score) // current score

	kvs := redis.GameScores(gt, 0, form.Count)
	var ids []string
	for _, kv := range kvs {
		ids = append(ids, kv.K)
	}
	ranks := redis.UserGameRanks(gt, user.Id)

	redis.SetGameScore(gt, user.Id, respData.Score) // recover max score

	n := redis.GameUserCount(gt) - 1
	//log.Println(ranks, n)
	if len(ranks) == 1 && n > 0 && form.Score > 0 {
		respData.Percent = int(float64(n-ranks[0]) / float64(n) * 100.0)
	}

	//log.Println(ids)
	users, _ := models.FindUsersByIds(1, ids...)
	index := 0
	for _, kv := range kvs {
		for i, _ := range users {
			if users[i].Id == kv.K {
				respData.Total = append(respData.Total, &leaderboardResp{
					Userid:   users[i].Id,
					Score:    kv.V,
					Rank:     index + 1,
					Level:    users[i].Level(),
					Profile:  users[i].Profile,
					Nickname: users[i].Nickname,
					Gender:   users[i].Gender,
					LastLog:  users[i].LastGameTime(gt).Unix(),
					Birth:    users[i].Birth,
					Location: users[i].Loc,
					Phone:    users[i].Phone,
					Actor:    users[i].Actor,
				})
				index++

				break
			}
		}
	}

	ids = redis.Friends(models.RelFriend, user.Id)
	if len(ids) > 0 {
		total := len(ids)
		ids = append(ids, user.Id)
		scores := redis.UserGameScores(gt, ids...)

		if len(scores) != len(ids) {
			scores = make([]int64, total)
		}
		kvs = make([]models.KV, total+1)
		for i, _ := range ids {
			kvs[i].K = ids[i]
			kvs[i].V = scores[i]
			if ids[i] == user.Id {
				kvs[i].V = int64(form.Score)
			}
		}

		sort.Sort(sort.Reverse(models.KVSlice(kvs)))
		lb := kvs
		if len(kvs) > 3 {
			kvs = kvs[0:3]
		}
		ids = []string{}
		for _, kv := range kvs {
			ids = append(ids, kv.K)
		}
		users, _ = models.FindUsersByIds(1, ids...)
		index := 0
		rank := 0

		for i, _ := range lb {
			if lb[i].K == user.Id {
				rank = i
				break
			}
		}

		for _, kv := range kvs {
			for i, _ := range users {
				if users[i].Id == kv.K {
					respData.Friends = append(respData.Friends, &leaderboardResp{
						Userid:   users[i].Id,
						Score:    kv.V,
						Rank:     index + 1,
						Level:    users[i].Level(),
						Profile:  users[i].Profile,
						Nickname: users[i].Nickname,
						Gender:   users[i].Gender,
						LastLog:  users[i].LastGameTime(gt).Unix(),
						Birth:    users[i].Birth,
						Location: users[i].Loc,
						Phone:    users[i].Phone,
						Actor:    users[i].Actor,
					})
					index++

					break
				}
			}
		}

		if total > 0 {
			respData.PerFriend = int(float64(total-rank) / float64(total) * 100.0)
		}
	}

	writeResponse(r.RequestURI, w, respData, nil)
}