diff --git a/server/gameservice/mt/HashRateCommon.go b/server/gameservice/mt/HashRateCommon.go new file mode 100644 index 00000000..ec405aa9 --- /dev/null +++ b/server/gameservice/mt/HashRateCommon.go @@ -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 +} diff --git a/server/gameservice/mt/export.go b/server/gameservice/mt/export.go index 4f8360b0..228123e4 100644 --- a/server/gameservice/mt/export.go +++ b/server/gameservice/mt/export.go @@ -5,11 +5,12 @@ import ( ) type table struct { - AdminCluster *AdminClusterTable - GameDb *GameDbTable - FriendDb *FriendDbTable - Config *ConfigTable - RankSeason *RankSeasonTable + AdminCluster *AdminClusterTable + GameDb *GameDbTable + FriendDb *FriendDbTable + Config *ConfigTable + RankSeason *RankSeasonTable + HashRateCommon *HashRateCommonTable } 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.PrimKey = "id" }) + + this.HashRateCommon = f5.New(func(this *HashRateCommonTable) { + this.FileName = "../res/HashrateCommon@HashrateCommon.json" + this.PrimKey = "id" + }) }) diff --git a/server/gameservice/mtb/mtb.auto_gen.go b/server/gameservice/mtb/mtb.auto_gen.go index 706d4dc3..5b1ddfe6 100644 --- a/server/gameservice/mtb/mtb.auto_gen.go +++ b/server/gameservice/mtb/mtb.auto_gen.go @@ -63,6 +63,16 @@ type RankSeason struct { _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 { return this.instance_id } @@ -247,6 +257,38 @@ func (this *RankSeason) HasEndTime() bool { 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{}) { 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.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) +} diff --git a/server/gameservice/proto/mt.proto b/server/gameservice/proto/mt.proto index ef9b4127..762fd660 100644 --- a/server/gameservice/proto/mt.proto +++ b/server/gameservice/proto/mt.proto @@ -48,3 +48,11 @@ message RankSeason optional string start_time = 3; 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; +} \ No newline at end of file diff --git a/server/gameservice/task/dailyTask.go b/server/gameservice/task/dailyTask.go new file mode 100644 index 00000000..cc619ad9 --- /dev/null +++ b/server/gameservice/task/dailyTask.go @@ -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()) + }) +} diff --git a/server/gameservice/task/formula.go b/server/gameservice/task/formula.go index 28424d56..5fdbf979 100644 --- a/server/gameservice/task/formula.go +++ b/server/gameservice/task/formula.go @@ -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)) 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 +} diff --git a/server/gameservice/task/hashRate.go b/server/gameservice/task/hashRate.go new file mode 100644 index 00000000..dfa18071 --- /dev/null +++ b/server/gameservice/task/hashRate.go @@ -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) + } + }, + ) +} diff --git a/server/gameservice/task/hourlyTask.go b/server/gameservice/task/hourlyTask.go new file mode 100644 index 00000000..78275f28 --- /dev/null +++ b/server/gameservice/task/hourlyTask.go @@ -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()) + }) +} diff --git a/server/gameservice/task/seasonRanking.go b/server/gameservice/task/seasonRanking.go index 569c045f..cfd04281 100644 --- a/server/gameservice/task/seasonRanking.go +++ b/server/gameservice/task/seasonRanking.go @@ -8,23 +8,8 @@ import ( "mt" "q5" "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 { idx int64 account_id string @@ -55,45 +40,31 @@ func (this sortUserDb) Less(i, j int) bool { } else { return this[i].score > this[j].score } - } type SeasonRankMgr struct { } -func (this *SeasonRankMgr) Init() { - f5.GetTimer().SetInterval( - 1000*10, - func(e int32, args *q5.Args) { - seasonMt := mt.Table.RankSeason.GetCurrentSeason() - season := mt.Table.RankSeason.GetLastSeason() - f5.GetJsStyleDb().OrmSelectOne( - constant.GAME_DB, - "t_season_ranking", - [][]string{ - {"season", q5.ToString(season.GetId())}, - }, - func(err error, row *f5.DataSet) { - if err != nil { - f5.GetSysLog().Error("select t_season_ranking one Error:%v \n", err) - return - } - row.Next() - if row.NumOfReaded() <= 0 && seasonMt == nil { - fmt.Println("OK") - //this.CalcRanking() - } - }) - fmt.Printf("SeasonRanking.sleepTime:%v \n", time.Now()) - }) -} - -func (this *SeasonRankMgr) UnInit() { - -} - func (this *SeasonRankMgr) CalcRanking() { - this.goGetUsersRecords() + seasonMt := mt.Table.RankSeason.GetCurrentSeason() + season := mt.Table.RankSeason.GetLastSeason() + f5.GetJsStyleDb().OrmSelectOne( + constant.GAME_DB, + "t_season_ranking", + [][]string{ + {"season", q5.ToString(season.GetId())}, + }, + func(err error, row *f5.DataSet) { + if err != nil { + f5.GetSysLog().Error("SeasonRankMgr Error1:%v \n", err) + return + } + row.Next() + if row.NumOfReaded() <= 0 && seasonMt == nil { + fmt.Println("OK") + this.goGetUsersRecords() + } + }) } func (this *SeasonRankMgr) goGetUsersRecords() { @@ -106,13 +77,13 @@ func (this *SeasonRankMgr) goGetUsersRecords() { lastIdx, constant.BASE_SCORE, 1000) - f5.GetGoStyleDb().SyncSelectCustomQuery( + f5.GetJsStyleDb().SelectCustomQuery( constant.GAME_DB, sql, func(err error, rows *f5.DataSet) { if err != nil { task.SetFail() - f5.GetSysLog().Error("getUsersRecords Error:%v \n", err) + f5.GetSysLog().Error("SeasonRankMgr Error2:%v \n", err) return } for rows.Next() { @@ -131,20 +102,18 @@ func (this *SeasonRankMgr) goGetUsersRecords() { } f5.GetSysLog().Info("get User:%s \n", user.idx) } - if rows.NumOfReaded() <= 0 { - task.SetSucc() - } else { - task.Continue() - } + task.SetSucc() }, ) }).OnSucc( func(task *f5.AsyncTask) { - sort.Sort(sortUserDb(userList)) - this.pushRankingResult(userList) + if userList != nil { + sort.Sort(sortUserDb(userList)) + this.pushRankingResult(userList) + } }).OnFail( func(task *f5.AsyncTask) { - f5.GetSysLog().Error("Sync Task SeasonRanking Error \n") + f5.GetSysLog().Error("SeasonRankMgr Error3 \n") return }) } @@ -177,6 +146,6 @@ func (this *SeasonRankMgr) pushRankingResult(record []userDb) { // } // }, //) - fmt.Println(fields) + f5.GetSysLog().Info("ranking:%v", fields) } } diff --git a/server/gameservice/task/taskMgr.go b/server/gameservice/task/taskMgr.go index f707f339..1da6bc62 100644 --- a/server/gameservice/task/taskMgr.go +++ b/server/gameservice/task/taskMgr.go @@ -1,14 +1,14 @@ package task type taskMgr struct { - SeasonRankMgr + HourlyTaskMgr + DailyTaskMgr } func (t *taskMgr) Init() { - t.SeasonRankMgr.Init() - + t.HourlyTaskMgr.Init() + //t.DailyTaskMgr.Init() } func (t *taskMgr) UnInit() { - t.SeasonRankMgr.UnInit() }