This commit is contained in:
hujiabin 2023-12-21 17:13:48 +08:00
parent 99a3b4a4e0
commit c943bb24eb
10 changed files with 385 additions and 68 deletions

View File

@ -0,0 +1,56 @@
package mt
import (
"f5"
"mtb"
"time"
)
type HashRateCommon struct {
mtb.HashRateCommon
_start_time int64
_end_time int64
}
type HashRateCommonTable struct {
f5.IdMetaTable[HashRateCommon]
}
func (this *HashRateCommon) Init1() {
dt1, _ := time.Parse("2006-1-02 15:04:05", this.GetStartTime())
this._start_time = dt1.Unix()
dt2, _ := time.Parse("2006-1-02 15:04:05", this.GetEndTime())
this._end_time = dt2.Unix()
}
func (this *HashRateCommonTable) PostInit1() {
}
func (this *HashRateCommonTable) GetLatestPeriod() *HashRateCommon {
var result *HashRateCommon
this.Traverse(
func(meta *HashRateCommon) bool {
if result == nil {
result = meta
return true
}
if f5.GetApp().GetNowSeconds() < meta._start_time {
return false
}
if f5.GetApp().GetNowSeconds() >= meta._end_time {
result = meta
}
return true
})
return result
}
func (this HashRateCommonTable) CheckAwaitTime() bool {
period := this.GetLatestPeriod()
if period != nil && f5.GetApp().GetNowSeconds() > period._end_time {
return true
}
return false
}

View File

@ -10,6 +10,7 @@ type table struct {
FriendDb *FriendDbTable FriendDb *FriendDbTable
Config *ConfigTable Config *ConfigTable
RankSeason *RankSeasonTable RankSeason *RankSeasonTable
HashRateCommon *HashRateCommonTable
} }
var Table = f5.New(func(this *table) { var Table = f5.New(func(this *table) {
@ -37,4 +38,9 @@ var Table = f5.New(func(this *table) {
this.FileName = "../res/rankSeason@rankSeason.json" this.FileName = "../res/rankSeason@rankSeason.json"
this.PrimKey = "id" this.PrimKey = "id"
}) })
this.HashRateCommon = f5.New(func(this *HashRateCommonTable) {
this.FileName = "../res/HashrateCommon@HashrateCommon.json"
this.PrimKey = "id"
})
}) })

View File

@ -63,6 +63,16 @@ type RankSeason struct {
_flags2_ uint64 _flags2_ uint64
} }
type HashRateCommon struct {
id int32
start_time string
end_time string
cec_pool int32
_flags1_ uint64
_flags2_ uint64
}
func (this *AdminCluster) GetInstanceId() int32 { func (this *AdminCluster) GetInstanceId() int32 {
return this.instance_id return this.instance_id
} }
@ -247,6 +257,38 @@ func (this *RankSeason) HasEndTime() bool {
return (this._flags1_ & (uint64(1) << 4)) > 0 return (this._flags1_ & (uint64(1) << 4)) > 0
} }
func (this *HashRateCommon) GetId() int32 {
return this.id
}
func (this *HashRateCommon) HasId() bool {
return (this._flags1_ & (uint64(1) << 1)) > 0
}
func (this *HashRateCommon) GetStartTime() string {
return this.start_time
}
func (this *HashRateCommon) HasStartTime() bool {
return (this._flags1_ & (uint64(1) << 2)) > 0
}
func (this *HashRateCommon) GetEndTime() string {
return this.end_time
}
func (this *HashRateCommon) HasEndTime() bool {
return (this._flags1_ & (uint64(1) << 3)) > 0
}
func (this *HashRateCommon) GetCecPool() int32 {
return this.cec_pool
}
func (this *HashRateCommon) HasCecPool() bool {
return (this._flags1_ & (uint64(1) << 4)) > 0
}
func (this *AdminCluster) LoadFromKv(kv map[string]interface{}) { func (this *AdminCluster) LoadFromKv(kv map[string]interface{}) {
f5.ReadMetaTableField(&this.instance_id, "instance_id", &this._flags1_, 1, kv) f5.ReadMetaTableField(&this.instance_id, "instance_id", &this._flags1_, 1, kv)
@ -288,3 +330,10 @@ func (this *RankSeason) LoadFromKv(kv map[string]interface{}) {
f5.ReadMetaTableField(&this.start_time, "start_time", &this._flags1_, 3, kv) f5.ReadMetaTableField(&this.start_time, "start_time", &this._flags1_, 3, kv)
f5.ReadMetaTableField(&this.end_time, "end_time", &this._flags1_, 4, kv) f5.ReadMetaTableField(&this.end_time, "end_time", &this._flags1_, 4, kv)
} }
func (this *HashRateCommon) LoadFromKv(kv map[string]interface{}) {
f5.ReadMetaTableField(&this.id, "id", &this._flags1_, 1, kv)
f5.ReadMetaTableField(&this.start_time, "start_time", &this._flags1_, 2, kv)
f5.ReadMetaTableField(&this.end_time, "end_time", &this._flags1_, 3, kv)
f5.ReadMetaTableField(&this.cec_pool, "cec_pool", &this._flags1_, 4, kv)
}

View File

@ -48,3 +48,11 @@ message RankSeason
optional string start_time = 3; optional string start_time = 3;
optional string end_time = 4; optional string end_time = 4;
} }
message HashRateCommon
{
optional int32 id = 1;
optional string start_time = 2;
optional string end_time = 3;
optional int32 cec_pool = 4;
}

View File

@ -0,0 +1,22 @@
package task
import (
"f5"
"fmt"
"q5"
"time"
)
type DailyTaskMgr struct {
SeasonRankMgr
}
func (this *DailyTaskMgr) Init() {
//sleepTime := f5.GetApp().GetNowDaySeconds() + 3600*24 - f5.GetApp().GetNowSeconds()
f5.GetTimer().SetInterval(
1000*3600*24,
func(e int32, args *q5.Args) {
this.SeasonRankMgr.CalcRanking()
fmt.Printf("DailyTask.sleepTime:%v \n", time.Now())
})
}

View File

@ -85,3 +85,10 @@ func minTopX(topX int64) float64 {
t := 20000 * math.Pow(0.6, q5.ToFloat64(topX)) * 0.8 * math.Pow(0.9, q5.ToFloat64(topX-1)) t := 20000 * math.Pow(0.6, q5.ToFloat64(topX)) * 0.8 * math.Pow(0.9, q5.ToFloat64(topX-1))
return _round(t, 0) return _round(t, 0)
} }
func ceilEx(number float64, decimals float64) float64 {
intPart := math.Floor(number)
floatPart := number - intPart
finallyFloatPart := math.Ceil(floatPart*math.Pow(q5.ToFloat64(10), decimals)) / math.Pow(q5.ToFloat64(10), decimals)
return intPart + finallyFloatPart
}

View File

@ -0,0 +1,179 @@
package task
import (
"f5"
"fmt"
"main/constant"
"mt"
"q5"
)
type HashRateMgr struct {
totalPowerNumber float64
period int32
hashRateMate *mt.HashRateCommon
}
func (this *HashRateMgr) CalPowerByCECReward() {
this.hashRateMate = mt.Table.HashRateCommon.GetLatestPeriod()
this.period = this.hashRateMate.GetId()
var totalPower float64 = 0
f5.NewAsyncTask(
func(task *f5.AsyncTask) {
sql := fmt.Sprintf("SELECT * FROM t_power_exchange_record WHERE period = %d",
this.period)
f5.GetJsStyleDb().SelectCustomQuery(
constant.GAME_DB,
sql,
func(err error, rows *f5.DataSet) {
if err != nil {
task.SetFail()
f5.GetSysLog().Error("HashRateMgr Error1:%v \n", err)
return
}
for rows.Next() {
totalPower += q5.ToFloat64(rows.GetByName("total_num"))
}
if rows.NumOfReaded() <= 0 {
task.SetFail()
} else {
task.SetSucc()
}
},
)
},
).OnSucc(
func(task *f5.AsyncTask) {
if totalPower != 0 {
this.totalPowerNumber = totalPower
this.getAccountGroup()
}
}).OnFail(
func(task *f5.AsyncTask) {
fmt.Printf("Cal Fail")
})
}
func (this *HashRateMgr) getAccountGroup() {
accountIds := []string{}
f5.NewAsyncTask(
func(task *f5.AsyncTask) {
sql := fmt.Sprintf("SELECT account_id,`period` FROM t_power_exchange_record WHERE period = %d group by account_id",
this.period)
f5.GetJsStyleDb().SelectCustomQuery(
constant.GAME_DB,
sql,
func(err error, rows *f5.DataSet) {
if err != nil {
task.SetFail()
f5.GetSysLog().Error("HashRateMgr Error2:%v \n", err)
return
}
for rows.Next() {
accountId := rows.GetByName("account_id")
accountIds = append(accountIds, accountId)
}
task.SetSucc()
},
)
}).OnSucc(
func(task *f5.AsyncTask) {
this.checkAccountIsSettle(accountIds)
}).OnFail(
func(task *f5.AsyncTask) {
})
}
func (this *HashRateMgr) checkAccountIsSettle(param []string) {
var index = 0
f5.NewAsyncTask(
func(task *f5.AsyncTask) {
if index >= len(param) {
task.SetSucc()
return
}
f5.GetJsStyleDb().OrmSelectOne(
constant.GAME_DB,
"t_hash_rate_reward",
[][]string{
{"account_id", q5.ToString(param[index])},
{"period", q5.ToString(this.period)},
},
func(err error, row *f5.DataSet) {
if err != nil {
f5.GetSysLog().Error("HashRateMgr Error3:%v \n", err)
task.SetFail()
return
}
if row.Next() {
index += 1
task.Continue()
} else {
//结算操作
this.getOwnerPower(param[index], func(ownerPower float64) {
target := ceilEx(q5.Min(this.totalPowerNumber/q5.ToFloat64(this.hashRateMate.GetCecPool()), 1), 6)
ratio := ownerPower / this.totalPowerNumber
cecNUm := ceilEx(q5.ToFloat64(this.hashRateMate.GetCecPool())*target*ratio, 2)
this.pushHashRateReward(param[index], cecNUm)
index += 1
task.Continue()
})
}
})
}).OnSucc(
func(task *f5.AsyncTask) {
}).OnFail(
func(task *f5.AsyncTask) {
})
}
func (this *HashRateMgr) getOwnerPower(account string, cb func(param float64)) {
var ownerPower float64 = 0
f5.NewAsyncTask(func(task *f5.AsyncTask) {
sql1 := fmt.Sprintf("SELECT * FROM t_power_exchange_record WHERE account_id = '%s' AND period = %d",
account,
this.period)
f5.GetJsStyleDb().SelectCustomQuery(
constant.GAME_DB,
sql1,
func(err error, rows *f5.DataSet) {
if err != nil {
task.SetFail()
f5.GetSysLog().Error("HashRateMgr Error4:%v \n", err)
return
}
for rows.Next() {
ownerPower += q5.ToFloat64(rows.GetByName("total_num"))
}
task.SetSucc()
},
)
}).OnSucc(func(task *f5.AsyncTask) {
cb(ownerPower)
}).OnFail(func(task *f5.AsyncTask) {
})
}
func (this *HashRateMgr) pushHashRateReward(account string, num float64) {
fields := [][]string{
{"account_id", account},
//q5.GenFieldKvEmptyAsNull("address", v.address),
{"address", q5.ToString("test123456789")},
{"period", q5.ToString(this.period)},
{"reward_cec", q5.ToString(num)},
{"createtime", q5.ToString(f5.GetApp().GetNowSeconds())},
{"modifytime", q5.ToString(f5.GetApp().GetNowSeconds())},
}
f5.GetJsStyleDb().Insert(
constant.GAME_DB,
"t_hash_rate_reward",
fields,
func(err error, id int64, affectedRows int64) {
if err != nil || affectedRows != 1 {
// 插入失败,处理错误逻辑
f5.GetSysLog().Error("HashRateMgr Error5:%v \n", err)
}
},
)
}

View File

@ -0,0 +1,21 @@
package task
import (
"f5"
"fmt"
"q5"
"time"
)
type HourlyTaskMgr struct {
HashRateMgr
}
func (this *HourlyTaskMgr) Init() {
f5.GetTimer().SetInterval(
1000*3600,
func(e int32, args *q5.Args) {
this.HashRateMgr.CalPowerByCECReward()
fmt.Printf("HourlyTask.sleepTime:%v \n", time.Now())
})
}

View File

@ -8,23 +8,8 @@ import (
"mt" "mt"
"q5" "q5"
"sort" "sort"
"time"
) )
type RankingList struct {
idx int64
account_id string
address string
channel int32
rank int32
score int32
ranking int32
ranking_point int32
season int32
createtime int32
modifytime int32
}
type userDb struct { type userDb struct {
idx int64 idx int64
account_id string account_id string
@ -55,16 +40,12 @@ func (this sortUserDb) Less(i, j int) bool {
} else { } else {
return this[i].score > this[j].score return this[i].score > this[j].score
} }
} }
type SeasonRankMgr struct { type SeasonRankMgr struct {
} }
func (this *SeasonRankMgr) Init() { func (this *SeasonRankMgr) CalcRanking() {
f5.GetTimer().SetInterval(
1000*10,
func(e int32, args *q5.Args) {
seasonMt := mt.Table.RankSeason.GetCurrentSeason() seasonMt := mt.Table.RankSeason.GetCurrentSeason()
season := mt.Table.RankSeason.GetLastSeason() season := mt.Table.RankSeason.GetLastSeason()
f5.GetJsStyleDb().OrmSelectOne( f5.GetJsStyleDb().OrmSelectOne(
@ -75,26 +56,16 @@ func (this *SeasonRankMgr) Init() {
}, },
func(err error, row *f5.DataSet) { func(err error, row *f5.DataSet) {
if err != nil { if err != nil {
f5.GetSysLog().Error("select t_season_ranking one Error:%v \n", err) f5.GetSysLog().Error("SeasonRankMgr Error1:%v \n", err)
return return
} }
row.Next() row.Next()
if row.NumOfReaded() <= 0 && seasonMt == nil { if row.NumOfReaded() <= 0 && seasonMt == nil {
fmt.Println("OK") fmt.Println("OK")
//this.CalcRanking()
}
})
fmt.Printf("SeasonRanking.sleepTime:%v \n", time.Now())
})
}
func (this *SeasonRankMgr) UnInit() {
}
func (this *SeasonRankMgr) CalcRanking() {
this.goGetUsersRecords() this.goGetUsersRecords()
} }
})
}
func (this *SeasonRankMgr) goGetUsersRecords() { func (this *SeasonRankMgr) goGetUsersRecords() {
userHash := make(map[int64]*userDb) userHash := make(map[int64]*userDb)
@ -106,13 +77,13 @@ func (this *SeasonRankMgr) goGetUsersRecords() {
lastIdx, lastIdx,
constant.BASE_SCORE, constant.BASE_SCORE,
1000) 1000)
f5.GetGoStyleDb().SyncSelectCustomQuery( f5.GetJsStyleDb().SelectCustomQuery(
constant.GAME_DB, constant.GAME_DB,
sql, sql,
func(err error, rows *f5.DataSet) { func(err error, rows *f5.DataSet) {
if err != nil { if err != nil {
task.SetFail() task.SetFail()
f5.GetSysLog().Error("getUsersRecords Error:%v \n", err) f5.GetSysLog().Error("SeasonRankMgr Error2:%v \n", err)
return return
} }
for rows.Next() { for rows.Next() {
@ -131,20 +102,18 @@ func (this *SeasonRankMgr) goGetUsersRecords() {
} }
f5.GetSysLog().Info("get User:%s \n", user.idx) f5.GetSysLog().Info("get User:%s \n", user.idx)
} }
if rows.NumOfReaded() <= 0 {
task.SetSucc() task.SetSucc()
} else {
task.Continue()
}
}, },
) )
}).OnSucc( }).OnSucc(
func(task *f5.AsyncTask) { func(task *f5.AsyncTask) {
if userList != nil {
sort.Sort(sortUserDb(userList)) sort.Sort(sortUserDb(userList))
this.pushRankingResult(userList) this.pushRankingResult(userList)
}
}).OnFail( }).OnFail(
func(task *f5.AsyncTask) { func(task *f5.AsyncTask) {
f5.GetSysLog().Error("Sync Task SeasonRanking Error \n") f5.GetSysLog().Error("SeasonRankMgr Error3 \n")
return return
}) })
} }
@ -177,6 +146,6 @@ func (this *SeasonRankMgr) pushRankingResult(record []userDb) {
// } // }
// }, // },
//) //)
fmt.Println(fields) f5.GetSysLog().Info("ranking:%v", fields)
} }
} }

View File

@ -1,14 +1,14 @@
package task package task
type taskMgr struct { type taskMgr struct {
SeasonRankMgr HourlyTaskMgr
DailyTaskMgr
} }
func (t *taskMgr) Init() { func (t *taskMgr) Init() {
t.SeasonRankMgr.Init() t.HourlyTaskMgr.Init()
//t.DailyTaskMgr.Init()
} }
func (t *taskMgr) UnInit() { func (t *taskMgr) UnInit() {
t.SeasonRankMgr.UnInit()
} }