Merge branch 'meixiongfeng' of http://git.legu.cc/liwei_3d/go_dreamfactory into dev

This commit is contained in:
meixiongfeng 2022-10-27 19:08:42 +08:00
commit 14be41a497
4 changed files with 131 additions and 38 deletions

View File

@ -123,6 +123,7 @@ func (this *apiComp) ChallengeOver(session comm.IUserSession, req *pb.PagodaChal
Lv: userinfo.Lv,
CostTime: req.Report.Costtime,
}
conn.Mgo.InsertOne(comm.TablePagodaRecord, newData)
}
}

View File

@ -1,6 +1,7 @@
package pagoda
import (
"context"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
@ -12,7 +13,9 @@ import (
"sort"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
var floorRankKey = "pagoda:floor"
@ -71,32 +74,38 @@ func (this *ModelRank) getPagodaRankList(uid string) []*pb.DBPagodaRecord {
}
return pagodaRank
}
func (this *ModelRank) addPagodaList(session comm.IUserSession, data *pb.DBPagoda, Leadpos int32, line []*pb.LineUp, costTime int32) {
userinfo := this.modulePagoda.ModuleUser.GetUser(session.GetUserId())
new := &pb.DBPagodaRecord{
Id: primitive.NewObjectID().Hex(),
Uid: session.GetUserId(),
PagodaId: data.PagodaId,
Type: data.Type,
Nickname: userinfo.Name,
Icon: "", // icon 暂无
Lv: userinfo.Lv,
CostTime: costTime,
Line: line,
}
this.AddList(session.GetUserId(), new.Id, new) //写爬塔记录
// 查询本层是否上榜
rankData, ilen, err1 := this.GetFloorLastRankData(data.PagodaId)
if err1 == nil {
if ilen < comm.MaxRankNum || rankData.CostTime < costTime {
this.ChangeFloorRankList(session, data.PagodaId, new)
}
}
return
}
// func (this *ModelRank) addPagodaList(session comm.IUserSession, data *pb.DBPagoda, Leadpos int32, line []*pb.LineUp, costTime int32) {
// userinfo := this.modulePagoda.ModuleUser.GetUser(session.GetUserId())
// new := &pb.DBPagodaRecord{
// Id: primitive.NewObjectID().Hex(),
// Uid: session.GetUserId(),
// PagodaId: data.PagodaId,
// Type: data.Type,
// Nickname: userinfo.Name,
// Icon: "", // icon 暂无
// Lv: userinfo.Lv,
// CostTime: costTime,
// Line: line,
// }
// this.AddList(session.GetUserId(), new.Id, new) //写爬塔记录
// // 查询本层是否上榜
// rankData, ilen, err1 := this.GetFloorLastRankData(data.PagodaId)
// if err1 == nil {
// if ilen < comm.MaxRankNum || rankData.CostTime < costTime {
// this.ChangeFloorRankList(session, data.PagodaId, new)
// }
// }
// return
// }
// 插入新的排行数据
func (this *ModelRank) addPagodaRankList(session comm.IUserSession, data *pb.DBSeasonPagoda, Leadpos int32, line []*pb.LineUp, costTime int32) {
uid := session.GetUserId()
var (
dbModel *db.DBModel
err error
)
userinfo := this.modulePagoda.ModuleUser.GetUser(session.GetUserId())
new := &pb.DBPagodaRecord{
Id: primitive.NewObjectID().Hex(),
@ -104,16 +113,32 @@ func (this *ModelRank) addPagodaRankList(session comm.IUserSession, data *pb.DBS
PagodaId: data.PagodaId,
Type: data.Type,
Nickname: userinfo.Name,
Icon: "", // icon 暂无
Icon: "", // 暂时没有数据
Lv: userinfo.Lv,
Leadpos: Leadpos,
Line: line,
CostTime: costTime,
}
this.AddList(session.GetUserId(), new.Id, new) //写爬塔记录
if this.modulePagoda.IsCross() {
if dbModel, err = this.modulePagoda.GetDBNoduleByUid(uid, this.TableName, this.Expired); err != nil {
this.modulePagoda.Errorln(err)
} else {
if err = dbModel.AddList(session.GetUserId(), new.Id, new); err != nil {
this.modulePagoda.Errorf("err:%v", err)
}
}
} else {
if err := this.AddList(session.GetUserId(), new.Id, new); err != nil {
this.modulePagoda.Errorf("err:%v", err)
}
}
// 查询本层是否上榜
rankData, ilen, err1 := this.GetFloorLastRankData(data.PagodaId)
rankData, ilen, err1 := this.GetFloorLastRankData(data.PagodaId, dbModel)
if err1 != nil {
if rankData.CostTime < costTime || ilen < comm.MaxRankNum {
this.ChangeFloorRankList(session, data.PagodaId, new)
this.ChangeFloorRankList(session, data.PagodaId, new, dbModel)
}
}
return
@ -137,30 +162,46 @@ func (this *ModelRank) GetFloorRankList(floor int32) (result []*pb.DBPagodaRecor
}
// 获取排行榜最后一个元素
func (this *ModelRank) GetFloorLastRankData(floor int32) (curData *pb.DBPagodaRecord, len int, err error) {
func (this *ModelRank) GetFloorLastRankData(floor int32, dbModel *db.DBModel) (curData *pb.DBPagodaRecord, len int, err error) {
key := fmt.Sprintf("%s-%d-rank", floorRankKey, floor)
len, err = this.Redis.Llen(key)
len, err = dbModel.Redis.Llen(key)
curData = &pb.DBPagodaRecord{}
if this.Redis.Lindex(key, -1, curData); err != nil {
if dbModel.Redis.Lindex(key, -1, curData); err != nil {
this.modulePagoda.Errorf("err:%v", err)
return
}
if curData == nil {
data := make([]interface{}, 0) // options.Find().SetLimit(comm.MaxRankList)
if _data, err := dbModel.DB.Find(comm.TableSeasonRecord, bson.M{"pagodaId": floor}, options.Find().SetSort(bson.M{"pagodaId": -1}).SetLimit(comm.MaxRankList)); err == nil {
for _data.Next(context.TODO()) {
temp := &pb.DBPagodaRecord{}
if err = _data.Decode(temp); err == nil {
data = append(data, temp)
curData = temp
}
}
}
err := dbModel.Redis.RPush(key, data...)
if err == nil {
this.modulePagoda.Errorf("rpush failed :%v", err)
}
}
return
}
// 修改某一层排行数据
func (this *ModelRank) ChangeFloorRankList(session comm.IUserSession, floor int32, curData *pb.DBPagodaRecord) (err error) {
func (this *ModelRank) ChangeFloorRankList(session comm.IUserSession, floor int32, curData *pb.DBPagodaRecord, dbModel *db.DBModel) (err error) {
key := fmt.Sprintf("%s-%d-rank", floorRankKey, floor)
lockkey := fmt.Sprintf("%s-%d-lock", floorRankKey, floor)
this.Redis.Lock(lockkey, time.Now().Second()*5)
defer this.Redis.UnLock(lockkey)
if this.Redis.RPush(key, curData); err != nil {
dbModel.Redis.Lock(lockkey, time.Now().Second()*5)
defer dbModel.Redis.UnLock(lockkey)
if dbModel.Redis.RPush(key, curData); err != nil {
this.modulePagoda.Errorf("err:%v", err)
return
}
temp := make([]*pb.DBPagodaRecord, 0)
if err = this.Redis.LRange(key, 0, -1, &temp); err != nil {
if err = dbModel.Redis.LRange(key, 0, -1, &temp); err != nil {
this.modulePagoda.Errorf("err:%v", err)
return
}
@ -169,14 +210,14 @@ func (this *ModelRank) ChangeFloorRankList(session comm.IUserSession, floor int3
return temp[i].CostTime < temp[j].CostTime
})
iLne := len(temp)
if this.Redis.RPush(key, temp); err != nil {
if dbModel.Redis.RPush(key, temp); err != nil {
this.modulePagoda.Errorf("err:%v", err)
return
}
if iLne > comm.MaxRankNum {
iLne = comm.MaxRankNum
}
err = this.Redis.Ltrim(key, -1*iLne, -1) //对一个列表进行修剪
err = dbModel.Redis.Ltrim(key, -1*iLne, -1) //对一个列表进行修剪
if err != nil {
log.Errorf("delete failed")
}

View File

@ -89,6 +89,57 @@ func Test_Modules(t *testing.T) {
}
func TestXxx(t *testing.T) {
// uid := session.GetUserId()
// if !this.module.IsCross() {
// if conn, err := db.Cross(); err == nil {
// userinfo := this.module.ModuleUser.GetUser(session.GetUserId())
// newData := &pb.DBPagodaRecord{
// Id: primitive.NewObjectID().Hex(),
// Uid: session.GetUserId(),
// PagodaId: 111,
// Type: 201,
// Nickname: userinfo.Name,
// Icon: "", // icon 暂无
// Lv: userinfo.Lv,
// CostTime: 1002,
// }
// conn.Mgo.InsertOne(comm.TablePagodaRecord, newData)
// users := make([]*pb.DBPagodaRecord, 0)
// users = append(users, newData)
// var (
// pipe *pipe.RedisPipe = conn.Redis.RedisPipe(context.TODO())
// menbers []*redis.Z
// )
// menbers = make([]*redis.Z, 10)
// for i := 0; i < 10; i++ {
// uid += strconv.Itoa(i)
// menbers[i] = &redis.Z{Score: float64(i), Member: uid}
// }
// if cmd := pipe.ZAdd("pagodaList", menbers...); cmd != nil {
// this.module.Errorln(err)
// dock, err1 := cmd.Result()
// this.module.Errorln(dock, err1)
// }
// if _, err = pipe.Exec(); err != nil {
// this.module.Errorln(err)
// return
// }
// rd := pipe.ZRevRank("pagodaList", "dfmxf_6358f3f1375f6a340a12e2ab01234567")
// //rd := pipe.ZRange("pagodaList", 1, 5)
// if _, err = pipe.Exec(); err != nil {
// this.module.Errorln(err)
// return
// }
// _data3 := rd.Val()
// _data, err2 := rd.Result()
// this.module.Errorln(_data, err2, _data3)
// }
// }
// if !this.module.IsCross() {
// if conn, err := db.Cross(); err == nil {
// userinfo := this.module.ModuleUser.GetUser(session.GetUserId())

View File

@ -11,7 +11,7 @@ import (
func (this *apiComp) CreateCheck(session comm.IUserSession, req *pb.UserCreateReq) (code pb.ErrorCode) {
name := strings.TrimSpace(req.NickName)
if name == "" || len(name) > 30 || req.Figure == 0 {
if name == "" || len(name) > 30 {
code = pb.ErrorCode_UserNickNameEmpty
}