diff --git a/server/marketserver/api/v1/activity/contribution.go b/server/marketserver/api/v1/activity/contribution.go new file mode 100644 index 00000000..4eae9f75 --- /dev/null +++ b/server/marketserver/api/v1/activity/contribution.go @@ -0,0 +1,105 @@ +package activity + +import ( + "f5" + "main/constant" + "q5" + "strings" + + "github.com/gin-gonic/gin" +) + +type ContriApi struct { +} + +func (cta *ContriApi) HistoryQuery(c *gin.Context) { + account := strings.ToLower(c.Param("account_address")) + if account == "" { + c.JSON(200, gin.H{ + "errcode": 1, + "errmsg": "", + }) + return + } + + rspObj := struct { + Errcode int32 `json:"errcode"` + Msg int32 `json:"errmsg"` + CP string `json:"contributionPoint"` + Rows []interface{} `json:"rows"` + }{} + + rspObj.CP = "12345.67" + item := struct { + Type int32 `json:"type"` + Time int32 `json:"give_time"` + CP string `json:"contribution_point"` + }{} + item.Type = 0 + item.Time = int32(f5.GetApp().GetRealSeconds()) + item.CP = "12345.67" + + q5.NewSlice(&rspObj.Rows, 0, 10) + rspObj.Rows = append(rspObj.Rows, item) + + { + sql := ` +SELECT * FROM t_contribution_history WHERE idx > 0 AND account_id = ? +` + params := []string{ + account, + } + f5.GetGoStyleDb().RawQuery( + constant.GAME_DB, + sql, + params, + func(err error, ds *f5.DataSet) { + if err != nil { + return + } + + for ds.Next() { + p := struct { + Type int32 `json:"type"` + Time int32 `json:"give_time"` + CP string `json:"contribution_point"` + }{} + p.Type = q5.ToInt32(ds.GetByName("net_id")) + p.Time = q5.ToInt32(ds.GetByName("")) + } + }) + } + + c.JSON(200, rspObj) +} + +func (cta *ContriApi) CECQuery(c *gin.Context) { + account := strings.ToLower(c.Param("account_address")) + if account == "" { + c.JSON(200, gin.H{ + "errcode": 1, + "errmsg": "", + }) + return + } + + rspObj := struct { + Errcode int32 `json:"errcode"` + Msg int32 `json:"errmsg"` + CP string `json:"contributionPoint"` + Info struct { + MyCP string `json:"my_contribution"` + GCP string `json:"global_contribution"` + GCEC int64 `json:"total_cec_pool"` + MyCEC int64 `json:"my_expected_cec"` + } `json:"info"` + }{} + + rspObj.CP = "12345.67" + rspObj.Info.MyCP = "12345.67" + rspObj.Info.GCP = "12345.67" + rspObj.Info.GCEC = 10000 + rspObj.Info.MyCEC = 10000 + + c.JSON(200, rspObj) +} diff --git a/server/marketserver/api/v1/activity/enter.go b/server/marketserver/api/v1/activity/enter.go index d0723326..77f8e14b 100644 --- a/server/marketserver/api/v1/activity/enter.go +++ b/server/marketserver/api/v1/activity/enter.go @@ -3,4 +3,5 @@ package activity type ApiGroup struct { StackingApi BitGetApi + ContriApi } diff --git a/server/marketserver/app/app.go b/server/marketserver/app/app.go index 3afb8551..575be92d 100644 --- a/server/marketserver/app/app.go +++ b/server/marketserver/app/app.go @@ -1,16 +1,16 @@ package app import ( - "q5" "f5" - "main/mt" "jccommon" "main/constant" + "main/mt" + "q5" ) type app struct { - initCb func() - unInitCb func() + initCb func() + unInitCb func() } func (this *app) GetPkgName() string { @@ -87,6 +87,16 @@ func (this *app) registerDataSources() { mt.Table.GameDb.GetById(0).GetMaxOpenConns(), mt.Table.GameDb.GetById(0).GetMaxIdleConns()) + f5.GetGoStyleDb().RegisterDataSource( + constant.ACCOUNT_DB, + mt.Table.AccountDb.GetById(0).GetHost(), + mt.Table.AccountDb.GetById(0).GetPort(), + mt.Table.AccountDb.GetById(0).GetUser(), + mt.Table.AccountDb.GetById(0).GetPasswd(), + mt.Table.AccountDb.GetById(0).GetDatabase(), + 1, + mt.Table.AccountDb.GetById(0).GetMaxOpenConns(), + mt.Table.AccountDb.GetById(0).GetMaxIdleConns()) } func (this *app) HasTask() bool { diff --git a/server/marketserver/constant/constant.go b/server/marketserver/constant/constant.go index a95fc68b..13006a48 100644 --- a/server/marketserver/constant/constant.go +++ b/server/marketserver/constant/constant.go @@ -1,6 +1,7 @@ package constant const ( + ACCOUNT_DB = "accountdb" BCNFT_DB = "bcnftdb" BCEVENT_DB = "bceventdb" GAME_DB = "gamedb" @@ -21,6 +22,6 @@ const ( const ( RECHARGE_CURRENCY_MAX_EXCHANGE_RAET = 100 - RECHARGE_CURRENCY_MAX_DECIMAL = 6 - RECHARGE_CURRENCY_MAX_BUY_NUM = 9999 + RECHARGE_CURRENCY_MAX_DECIMAL = 6 + RECHARGE_CURRENCY_MAX_BUY_NUM = 9999 ) diff --git a/server/marketserver/mt/AccountDb.go b/server/marketserver/mt/AccountDb.go new file mode 100644 index 00000000..2e1f5610 --- /dev/null +++ b/server/marketserver/mt/AccountDb.go @@ -0,0 +1,15 @@ +package mt + +import ( + "f5" + "main/mtb" +) + +type AccountDb struct { + mtb.AccountDb +} + +type AccountDbTable struct { + f5.IdMetaTable[AccountDb] + selfConf *AccountDb +} diff --git a/server/marketserver/mt/Currency.go b/server/marketserver/mt/Currency.go index 875e7918..0fd742de 100644 --- a/server/marketserver/mt/Currency.go +++ b/server/marketserver/mt/Currency.go @@ -1,18 +1,18 @@ package mt import ( - "q5" "f5" "fmt" - "strings" "main/constant" + "q5" + "strings" ) type Currency struct { - currencyName string - exchangeRate int64 + currencyName string + exchangeRate int64 currencyDecimal int64 - contract *Contract + contract *Contract } type CurrencyTable struct { @@ -77,7 +77,7 @@ func (this *CurrencyTable) GetByNetIdAddress(netId int32, currencyAddress string var result *Currency currencyAddress = strings.ToLower(currencyAddress) currencysMeta.Range( - func (key string, meta *Currency) bool { + func(key string, meta *Currency) bool { if meta.GetContract().GetAddress() == currencyAddress { result = meta return false @@ -106,9 +106,9 @@ func (this *CurrencyTable) Load() { if jsonStr, err := f5.ReadJsonFile(fileName); err == nil { f5.GetSysLog().Info("load currency %s", fileName) currencyCfg := struct { - CurrencyName string `json:"currency_name"` - ExchangeRate int64 `json:"exchange_rate"` - CurrencyDecimal int64 `json:"currency_decimal"` + CurrencyName string `json:"currency_name"` + ExchangeRate int64 `json:"exchange_rate"` + CurrencyDecimal int64 `json:"currency_decimal"` }{} if err := q5.DecodeJson(jsonStr, ¤cyCfg); err != nil { panic(fmt.Sprintf("load metafile json decode error %s %s", "currency.json", err)) @@ -118,7 +118,7 @@ func (this *CurrencyTable) Load() { p.check() currencysMeta := this.GetByNetId(netId) if currencysMeta == nil { - currencysMeta := new(q5.ConcurrentMap[string, *Currency]) + currencysMeta = new(q5.ConcurrentMap[string, *Currency]) this.netIdHash.Store(netId, currencysMeta) } currencysMeta.Store(p.GetCurrencyName(), p) @@ -129,13 +129,13 @@ func (this *CurrencyTable) Load() { func (this *CurrencyTable) PostInit1() { this.netIdHash.Range( - func (key int32, val *q5.ConcurrentMap[string, *Currency]) bool { + func(key int32, val *q5.ConcurrentMap[string, *Currency]) bool { netId := key val.Range( - func (key2 string, val2 *Currency) bool { + func(key2 string, val2 *Currency) bool { currencyMeta := val2 Table.Recharge.Traverse(func(ele *Recharge) bool { - if int64(ele.GetPrice()) * currencyMeta.GetExchangeRate() != int64(ele.GetDiamond()) { + if int64(ele.GetPrice())*currencyMeta.GetExchangeRate() != int64(ele.GetDiamond()) { panic(fmt.Sprintf("Currency verifyerror net_id:%d id:%d", netId, ele.GetId())) } return true @@ -146,7 +146,7 @@ func (this *CurrencyTable) PostInit1() { } currencyMeta.contract = contractMeta return true - }); + }) return true }) } diff --git a/server/marketserver/mt/export.go b/server/marketserver/mt/export.go index 9072795f..1fc05347 100644 --- a/server/marketserver/mt/export.go +++ b/server/marketserver/mt/export.go @@ -16,7 +16,8 @@ type table struct { Web3ServiceCluster *Web3ServiceClusterTable Web3SignCluster *Web3SignClusterTable Recharge *RechargeTable - Currency *CurrencyTable + Currency *CurrencyTable + AccountDb *AccountDbTable } var Table = f5.New(func(this *table) { @@ -40,6 +41,11 @@ var Table = f5.New(func(this *table) { this.PrimKey = "" }) + this.AccountDb = f5.New(func(this *AccountDbTable) { + this.FileName = "../config/accountdb.mysql.json" + this.PrimKey = "" + }) + this.Config = f5.New(func(this *ConfigTable) { this.FileName = "../config/config.json" this.PrimKey = "" diff --git a/server/marketserver/mtb/mtb.auto_gen.go b/server/marketserver/mtb/mtb.auto_gen.go index f91f1ea3..1e3a3560 100644 --- a/server/marketserver/mtb/mtb.auto_gen.go +++ b/server/marketserver/mtb/mtb.auto_gen.go @@ -13,6 +13,19 @@ type MarketServerCluster struct { _flags2_ uint64 } +type AccountDb struct { + host string + port int32 + user string + passwd string + database string + max_open_conns int32 + max_idle_conns int32 + + _flags1_ uint64 + _flags2_ uint64 +} + type NftDb struct { host string port int32 @@ -137,6 +150,62 @@ func (this *MarketServerCluster) HasHttpListenPort() bool { return (this._flags1_ & (uint64(1) << 3)) > 0 } +func (this *AccountDb) GetHost() string { + return this.host +} + +func (this *AccountDb) HasHost() bool { + return (this._flags1_ & (uint64(1) << 1)) > 0 +} + +func (this *AccountDb) GetPort() int32 { + return this.port +} + +func (this *AccountDb) HasPort() bool { + return (this._flags1_ & (uint64(1) << 2)) > 0 +} + +func (this *AccountDb) GetUser() string { + return this.user +} + +func (this *AccountDb) HasUser() bool { + return (this._flags1_ & (uint64(1) << 3)) > 0 +} + +func (this *AccountDb) GetPasswd() string { + return this.passwd +} + +func (this *AccountDb) HasPasswd() bool { + return (this._flags1_ & (uint64(1) << 4)) > 0 +} + +func (this *AccountDb) GetDatabase() string { + return this.database +} + +func (this *AccountDb) HasDatabase() bool { + return (this._flags1_ & (uint64(1) << 5)) > 0 +} + +func (this *AccountDb) GetMaxOpenConns() int32 { + return this.max_open_conns +} + +func (this *AccountDb) HasMaxOpenConns() bool { + return (this._flags1_ & (uint64(1) << 6)) > 0 +} + +func (this *AccountDb) GetMaxIdleConns() int32 { + return this.max_idle_conns +} + +func (this *AccountDb) HasMaxIdleConns() bool { + return (this._flags1_ & (uint64(1) << 7)) > 0 +} + func (this *NftDb) GetHost() string { return this.host } @@ -464,6 +533,16 @@ func (this *MarketServerCluster) LoadFromKv(kv map[string]interface{}) { f5.ReadMetaTableField(&this.http_listen_port, "http_listen_port", &this._flags1_, 3, kv) } +func (this *AccountDb) LoadFromKv(kv map[string]interface{}) { + f5.ReadMetaTableField(&this.host, "host", &this._flags1_, 1, kv) + f5.ReadMetaTableField(&this.port, "port", &this._flags1_, 2, kv) + f5.ReadMetaTableField(&this.user, "user", &this._flags1_, 3, kv) + f5.ReadMetaTableField(&this.passwd, "passwd", &this._flags1_, 4, kv) + f5.ReadMetaTableField(&this.database, "database", &this._flags1_, 5, kv) + f5.ReadMetaTableField(&this.max_open_conns, "max_open_conns", &this._flags1_, 6, kv) + f5.ReadMetaTableField(&this.max_idle_conns, "max_idle_conns", &this._flags1_, 7, kv) +} + func (this *NftDb) LoadFromKv(kv map[string]interface{}) { f5.ReadMetaTableField(&this.host, "host", &this._flags1_, 1, kv) f5.ReadMetaTableField(&this.port, "port", &this._flags1_, 2, kv) diff --git a/server/marketserver/proto/mt.proto b/server/marketserver/proto/mt.proto index 19ac74ab..07b956c6 100644 --- a/server/marketserver/proto/mt.proto +++ b/server/marketserver/proto/mt.proto @@ -9,6 +9,17 @@ message MarketServerCluster optional int32 http_listen_port = 3; } +message AccountDb +{ + optional string host = 1; + optional int32 port = 2; + optional string user = 3; + optional string passwd = 4; + optional string database = 5; + optional int32 max_open_conns = 6; + optional int32 max_idle_conns = 7; +} + message NftDb { optional string host = 1; diff --git a/server/marketserver/router/activity/contribution.go b/server/marketserver/router/activity/contribution.go new file mode 100644 index 00000000..655ff46e --- /dev/null +++ b/server/marketserver/router/activity/contribution.go @@ -0,0 +1,16 @@ +package activity + +import ( + "f5" + v1 "main/api/v1" +) + +type ContributionRouter struct{} + +func (this *ContributionRouter) InitRouter() { + api := v1.ApiGroupApp.ActivityApiGroup + f5.GetApp().GetGinEngine().GET("api/activity/contribution/history/:account_address", + api.ContriApi.HistoryQuery) + f5.GetApp().GetGinEngine().GET("/api/activity/cec/contribution/:account_address", + api.ContriApi.CECQuery) +} diff --git a/server/marketserver/router/activity/enter.go b/server/marketserver/router/activity/enter.go index 0618e0e7..b32b38ed 100644 --- a/server/marketserver/router/activity/enter.go +++ b/server/marketserver/router/activity/enter.go @@ -3,4 +3,5 @@ package activity type RouterGroup struct { StackingRouter BitGetRouter + ContributionRouter } diff --git a/server/marketserver/router/routermgr.go b/server/marketserver/router/routermgr.go index 9e00b763..f5a45371 100644 --- a/server/marketserver/router/routermgr.go +++ b/server/marketserver/router/routermgr.go @@ -42,6 +42,7 @@ func (this *routerMgr) Init() { this.user.UserRouter.InitRouter() this.activity.StackingRouter.InitRouter() this.activity.BitGetRouter.InitRouter() + this.activity.ContributionRouter.InitRouter() this.event.EventRouter.InitRouter() this.recharge.RechargeRouter.InitRouter() diff --git a/server/marketserver/service/contribution.go b/server/marketserver/service/contribution.go index 0006beec..70888bbf 100644 --- a/server/marketserver/service/contribution.go +++ b/server/marketserver/service/contribution.go @@ -1,21 +1,231 @@ package service -type contribution struct { +import ( + "f5" + "main/constant" + "q5" +) +type accountContricution struct { + history float64 + loadhistory bool + contribution float64 + gcTime int64 + loweremail string +} + +type contribution struct { + historyContribution float64 + globalContribution float64 + gcTime int64 + + accountContricutionlist q5.ConcurrentMap[string, *accountContricution] } func (this *contribution) init() { - + this.accountContricutionlist = q5.ConcurrentMap[string, *accountContricution]{} } func (this *contribution) unInit() { } -func (this *contribution) GetAddressContribution(accountAddress string) (float64, error){ - return 0, nil +func (this *contribution) GetAddressContribution(accountAddress string) (float64, error) { + accinfo, exist := this.accountContricutionlist.Load(accountAddress) + var beforcontribution float64 = 0 + if nowseconds := f5.GetApp().GetRealSeconds(); !exist || (*accinfo).gcTime+60 < nowseconds { + if !exist { + *accinfo = new(accountContricution) + } + beforcontribution = (*accinfo).contribution + + if (*accinfo).loweremail == "" { + sql := `SELECT account_id FROM t_user WHERE idx > 0 AND address = ?` + accountid := "" + f5.GetGoStyleDb().RawQuery( + constant.GAME_DB, + sql, + []string{accountAddress}, + func(err error, ds *f5.DataSet) { + if err != nil { + return + } + + if ds.Next() { + accountid = ds.GetByIndex(0) + } + }) + + if accountid != "" { + sql := `SELECT lower_case_email FROM t_immutable_account WHERE idx > 0 AND account_id = ?` + f5.GetGoStyleDb().RawQuery( + constant.ACCOUNT_DB, + sql, + []string{accountid}, + func(err error, ds *f5.DataSet) { + if err != nil { + return + } + + if ds.Next() { + (*accinfo).loweremail = ds.GetByIndex(0) + } + }) + } + } + + if !(*accinfo).loadhistory { + sql := `SELECT contribution FROM t_contribution WHERE idx > 0 and account_address = ?` + f5.GetGoStyleDb().RawQuery( + constant.BCNFT_DB, + sql, + []string{accountAddress}, + func(err error, ds *f5.DataSet) { + if err != nil { + return + } + + if ds.Next() { + (*accinfo).history, _ = q5.ToFloat64Ex(ds.GetByIndex(0)) + } + }) + + sql = `SELECT SUM(contribution) FROM t_staking_daily_settlement WHERE idx > 0 and account_address = ?` + f5.GetGoStyleDb().RawQuery( + constant.BCNFT_DB, + sql, + []string{accountAddress}, + func(err error, ds *f5.DataSet) { + if err != nil { + return + } + + if ds.Next() { + tmp, _ := q5.ToFloat64Ex(ds.GetByIndex(0)) + (*accinfo).history += tmp + } + }) + + (*accinfo).loadhistory = true + } + + { + sql := `SELECT SUM(contribution) FROM t_contribution WHERE idx > 0 AND user_identity = ?` + f5.GetGoStyleDb().RawQuery( + constant.GAME_DB, + sql, + []string{accountAddress}, + func(err error, ds *f5.DataSet) { + if err != nil { + return + } + + if ds.Next() { + (*accinfo).contribution, _ = q5.ToFloat64Ex(ds.GetByIndex(0)) + } + }) + + sql = `SELECT SUM(return_contribution) FROM t_recharge_return_contribution WHERE idx > 0 AND user_identity IN (?` + if (*accinfo).loweremail != "" { + sql += ", " + (*accinfo).loweremail + } + sql += ")" + + f5.GetGoStyleDb().RawQuery( + constant.BCNFT_DB, + sql, + []string{accountAddress}, + func(err error, ds *f5.DataSet) { + if err != nil { + return + } + + if ds.Next() { + tmp, _ := q5.ToFloat64Ex(ds.GetByIndex(0)) + (*accinfo).contribution += tmp + } + }) + } + + (*accinfo).contribution += (*accinfo).history + (*accinfo).gcTime = nowseconds + } + + if beforcontribution != (*accinfo).contribution { + this.GetGlobalContribution(true) + } + + return (*accinfo).contribution, nil } -func (this *contribution) GetGlobalContribution() (float64, error){ - return 0, nil +func (this *contribution) GetGlobalContribution(instant bool) (float64, error) { + if this.historyContribution < 0.000001 { + sql := `SELECT SUM(contribution) FROM t_contribution WHERE idx > 0` + f5.GetGoStyleDb().RawQuery( + constant.BCNFT_DB, + sql, + []string{}, + func(err error, ds *f5.DataSet) { + if err != nil { + return + } + + if ds.Next() { + this.historyContribution, _ = q5.ToFloat64Ex(ds.GetByIndex(0)) + } + }) + + sql = `SELECT SUM(contribution) FROM t_staking_daily_settlement WHERE idx > 0` + f5.GetGoStyleDb().RawQuery( + constant.BCNFT_DB, + sql, + []string{}, + func(err error, ds *f5.DataSet) { + if err != nil { + return + } + + if ds.Next() { + tmp, _ := q5.ToFloat64Ex(ds.GetByIndex(0)) + this.historyContribution += tmp + } + }) + } + + if nowseconds := f5.GetApp().GetRealSeconds(); nowseconds > this.gcTime+60 || instant { + sql := `SELECT SUM(contribution) FROM t_contribution WHERE idx > 0` + f5.GetGoStyleDb().RawQuery( + constant.GAME_DB, + sql, + []string{}, + func(err error, ds *f5.DataSet) { + if err != nil { + return + } + + if ds.Next() { + this.globalContribution, _ = q5.ToFloat64Ex(ds.GetByIndex(0)) + } + }) + sql = `SELECT SUM(return_contribution) FROM t_recharge_return_contribution WHERE idx > 0` + f5.GetGoStyleDb().RawQuery( + constant.BCNFT_DB, + sql, + []string{}, + func(err error, ds *f5.DataSet) { + if err != nil { + return + } + + if ds.Next() { + tmp, _ := q5.ToFloat64Ex(ds.GetByIndex(0)) + this.globalContribution += tmp + } + }) + + this.globalContribution += this.historyContribution + this.gcTime = nowseconds + } + + return this.globalContribution, nil }