ranklist.go 4.7 KB
package main

import (
	"client/sdkdb"
	"client/sdkredis"
	"common"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Msg struct {
	ErrCode string      `json:"code"`
	ErrMsg  string      `json:"msg"`
	Obj     interface{} `json:"obj,omitempty"`
}

func main() {
	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)

	// http服务配置
	sdkhttp := common.NewHttp(config)

	// app
	sdkhttp.RegisterFunc("RankList", RankList)
	sdkhttp.RegisterFunc("SubmitScore", SubmitScore)
	sdkhttp.RegisterFunc("SingleRank", SingleRank)
	// web
	sdkhttp.RegisterFunc("CreateRank", CreateRank)

	// 激活http服务
	sdkhttp.Activate("ranklist")
}

/**
 *  获取排行数据
 */
func RankList(c *gin.Context) {
	gtid := c.Query("gtid")
	gid := c.Query("gid")
	toptype := c.Query("toptype")
	start, _ := strconv.Atoi(c.Query("start"))
	stop, _ := strconv.Atoi(c.Query("stop"))

	log.Printf("RankList gtid[%s] gid[%s] toptype[%s] start[%d] stop[%d]\n", gtid, gid, toptype, start, stop)

	igts := sdkredis.IceGametopRedisSort(gtid, gid, toptype, start, stop)
	msg := new(Msg)
	if len(igts) == 0 {
		err := errors.New("ranklist has no user")
		msg.ErrCode = "105103"
		msg.ErrMsg = err.Error()
	} else if igts == nil {
		err := errors.New("gametop name or game name not exist")
		msg.ErrCode = "105102"
		msg.ErrMsg = err.Error()
	} else {
		msg.ErrCode = "0"
		msg.ErrMsg = "success"
		mapresult := make(map[string]interface{})
		mapresult["ranklist"] = igts
		msg.Obj = mapresult
	}
	jsonByte, jsonerr := json.Marshal(msg)
	if jsonerr != nil {
		log.Printf("marshal err[%v]\n", jsonerr)
	}
	jsonStr := string(jsonByte)

	c.String(http.StatusOK, jsonStr)
}

/**
 *  提交分数
 */
func SubmitScore(c *gin.Context) {
	uid := c.Request.FormValue("uid")
	gtid := c.Request.FormValue("gtid")
	gid := c.Request.FormValue("gid")
	score := c.Request.FormValue("score")

	log.Printf("SubmitScore uid[%s] gtid[%s] gid[%s] score[%s]\n", uid, gtid, gid, score)
	msg := new(Msg)
	err := sdkredis.IceGametopRedisSubmitScore(uid, gtid, gid, score)
	if err != nil {
		msg.ErrCode = "105101"
		msg.ErrMsg = err.Error()
	}
	err = sdkdb.IceGameTopMysqlSubmitScore(uid, gtid, gid, score)
	if err != nil {
		msg.ErrCode = "105102"
		msg.ErrMsg = err.Error()
	} else {
		msg.ErrCode = "0"
		msg.ErrMsg = "success"
	}
	jsonByte, jsonerr := json.Marshal(msg)
	if jsonerr != nil {
		log.Printf("marshal err[%v]\n", jsonerr)
	}
	jsonStr := string(jsonByte)

	c.String(http.StatusOK, jsonStr)

}

/**
 *  获得单个用户的排行信息
 */
func SingleRank(c *gin.Context) {
	uid := c.Query("uid")
	gtid := c.Query("gtid")
	gid := c.Query("gid")
	toptype := c.Query("toptype")
	prerank := c.Query("prerank")
	nextrank := c.Query("nextrank")
	log.Printf("SingleRank uid[%s] gtid[%s] gid[%s] toptype[%s] prerank[%s] nextrank[%s]\n", uid, gtid, gid, toptype, prerank, nextrank)

	igt := sdkredis.IceGameTopRedisGetSingleUserRankInfo(uid, gtid, gid, toptype, prerank, nextrank)
	fmt.Println(igt)
	msg := new(Msg)
	if igt == nil {
		err := errors.New("gametop name or game name not exist")
		msg.ErrMsg = err.Error()
		msg.ErrCode = "105101"
	} else {
		msg.ErrCode = "0"
		msg.ErrMsg = "success"
		msg.Obj = igt
	}
	jsonByte, jsonerr := json.Marshal(msg)
	if jsonerr != nil {
		log.Printf("marshal err[%v]\n", jsonerr)
	}
	jsonStr := string(jsonByte)

	c.String(http.StatusOK, jsonStr)
}

/**
 *  创建排行榜
 */
func CreateRank(c *gin.Context) {
	gtid := c.Request.FormValue("gtid")
	gid := c.Request.FormValue("gid")
	toptype := c.Request.FormValue("toptype")
	gtname := c.Request.FormValue("gtname")
	sorcetype := c.Request.FormValue("sorcetype")
	sorcesub := c.Request.FormValue("sorcesub")
	sorceorder := c.Request.FormValue("sorceorder")

	if err := sdkdb.IceGameTopSetInsert(gtid, gid, toptype, gtname, sorcetype, sorcesub, sorceorder); err != nil {
		c.String(http.StatusOK, err.Error())
	}
	sdkredis.IceGameTopRedisCreateRank(gtid, gid, toptype, gtname, sorcetype, sorcesub, sorceorder)

	log.Printf("CreateRank gtid[%s] gid[%s] toptype[%s] gtname[%s] sorcetype[%s] sorcesub[%s] sorceorder[%s]\n", gtid, gid, toptype, gtname, sorcetype, sorcesub, sorceorder)

	msg := new(Msg)
	msg.ErrCode = "0"
	msg.ErrMsg = "success"

	jsonByte, jsonerr := json.Marshal(msg)
	if jsonerr != nil {
		log.Printf("marshal err[%v]\n", jsonerr)
	}
	jsonStr := string(jsonByte)

	c.String(http.StatusOK, jsonStr)
}