ranksendreward.go 3.6 KB
package main

import (
	"client/sdkdb"
	"client/sdkredis"
	"common"
	"flag"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
	"time"
)

func main() {
	log.Printf("---------------------------ranksendreward begin--------------------------------")

	configPath := flag.String("config", "./sdkRoute.conf", "config path")
	flag.Parse()
	fmt.Printf("config[%v]", *configPath)
	config := common.NewConfig(*configPath)

	// db初始化
	sdkdb.DBInit()
	// 连接数据库
	dberr := sdkdb.DBRegis(config.DBMysql)
	if dberr != nil {
		log.Printf("DBRegis err[%v]\n", dberr)
		return
	}

	// 设置数据库日志
	sdkdb.SetLogDebug(true, os.Stdout)

	// redis 初始化
	sdkredis.RedisInit(config)

	// 连接redis
	redisConn := sdkredis.RedisClient.GetConn()
	defer sdkredis.RedisClose(redisConn)

	// 获得所有的排行
	log.Printf("get all ranks ...")
	/*
		keys, err := sdkredis.RedisGetKeys(redisConn, "ice_gametopset:*gtname")
		if err != nil {
			sdkredis.RedisClose(redisConn)
			log.Printf("RedisGetKeys keys[%s] err[%v]\n", keys, err)
			return
		}
	*/
	keys, err := sdkdb.IceGameTopSetQueryTotal()
	if err != nil {
		log.Printf("IceGameTopSetQueryTotal keys[%s] err[%v]\n", keys, err)
	}

	log.Printf("ranks num[%d]", len(keys))
	if len(keys) == 0 {
		return
	}

	for _, key := range keys {
		/*
			index0 := strings.Index(key, ":gtid:")
			index1 := strings.Index(key, ":gid:")
			index2 := strings.Index(key, ":toptype:")
			index3 := strings.Index(key, ":gtname")
			gtid := common.Substr2(key, index0+6, index1)
			gid := common.Substr2(key, index1+5, index2)
			toptype := common.Substr2(key, index2+9, index3)
		*/
		gtid := key.GTID
		gid := key.GID
		toptype := key.TOPTYPE

		// 获得奖励ID
		log.Printf("get rank reward id---gid[%v] gtid[%v] toptype[%v]", gid, gtid, toptype)
		iceReward, _ := sdkdb.IceRewardQueryIDWithRank(gtid, gid, toptype)
		if iceReward == nil {
			log.Printf("this rank has no reward---gid[%v] gtid[%v] toptype[%v]", gid, gtid, toptype)
			continue
		}
		for _, v := range iceReward {
			log.Printf("iceReward rewardId[%v] title[%v] content[%v] UserFilter[%v]", v.Id, v.Title, v.Content, v.UserFilter)

			// 判断奖励是否失效  update time :2017年9月4日11:22:27 update by 徐振浩
			// update start
			nowTime := time.Now().UnixNano() / 1e6
			if v.StartTime > nowTime || v.EndTime < nowTime {
				log.Printf("this rank reward has been expired-----rewardId[%v] nowtime[%d] starttime[%d] endtime[%d]", v.Id, nowTime, v.StartTime, v.EndTime)
				continue
			}
			// update end

			seIndex := strings.Index(v.UserFilter, ",")
			start, _ := strconv.Atoi(common.Substr2(v.UserFilter, 0, seIndex))
			end, _ := strconv.Atoi(common.Substr2(v.UserFilter, seIndex+1, len(v.UserFilter)))

			// 待发放奖励的用户列表
			log.Printf("get rank user--gtid[%v] gid[%v] toptype[%v] start[%d] end[%d]", gtid, gid, toptype, start, end)
			igts := sdkredis.IceGametopRedisSort(gtid, gid, toptype, start, end)
			rank := start
			log.Printf("send to user begin...")
			if igts != nil {
				for _, igt := range igts {
					userID := igt.UID

					nowTime := time.Now().UnixNano() / 1e6
					invalidTime := nowTime + v.ValidTime
					// 发放奖励
					srErr := sdkdb.IceRewardUserSendReward(userID, v.Id, 1, 0, nowTime, invalidTime, rank, v.Title, v.Content, v.GTName)
					if srErr != nil {
						sdkredis.RedisClose(redisConn)
						return
					}
					log.Printf("send to user[%v]", userID)
					rank++
				}
			}
			log.Printf("send to user end...")
		}
	}

	sdkredis.RedisRPush(redisConn, "IsRankClean", "clean")
	log.Printf("send rankclean command")

	log.Printf("---------------------------ranksendreward end--------------------------------")
}