participate.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package blockchain
  2. import (
  3. "errors"
  4. "ktogame/controller"
  5. "ktogame/models"
  6. "ktogame/util"
  7. "strconv"
  8. "github.com/go-xorm/xorm"
  9. )
  10. /*
  11. type UserInfo struct {
  12. Id int64
  13. Addr string
  14. Direct string //我的上级
  15. DirectNumber int //直推总人数
  16. IndirectRewards float64 `xorm:"Decimal"`
  17. Indirect string //上上级
  18. IndirectNumber int //间推总人数
  19. DirectRewards float64 `xorm:"Decimal"`
  20. Superiors string //所有上级
  21. AvailableClaim float64 `xorm:"Decimal"` //可领取收益
  22. TotalClaimed float64 `xorm:"Decimal"` //总共已领取的收益
  23. AvailableReinput float64 `xorm:"Decimal"` //可用复投
  24. TotalReinputed float64 `xorm:"Decimal"` //总复投
  25. ParticipateAmount float64 `xorm:"Decimal"` //总参与
  26. State int //身份 0 1正式 2 社区
  27. OpTime string
  28. Hash string
  29. }
  30. type Performance struct {
  31. Id int64
  32. Addr string
  33. TotalPerformance float64 `xorm:"Decimal"`
  34. ClaimedPerformanceLevel int
  35. PerformanceRewards float64 `xorm:"Decimal"`
  36. TotalTeamCultivate float64 `xorm:"Decimal"`
  37. ClaimedCultivateLevel int
  38. TeamCultivateRewards float64 `xorm:"Decimal"`
  39. CommunityGift float64 `xorm:"Decimal"`
  40. CommunityNode float64 `xorm:"Decimal"`
  41. }
  42. */
  43. func participate(engine *xorm.Engine, user, inviter, hash string, amount float64) error {
  44. var dui models.UserInfo
  45. ok, err := engine.Id(inviter).Get(&dui)
  46. if err != nil {
  47. return err
  48. }
  49. if !ok {
  50. return errors.New("participate get user info failed")
  51. }
  52. //insert new user info
  53. var ui models.UserInfo
  54. ui.Addr = user
  55. ui.Direct = inviter
  56. ui.Indirect = dui.Direct
  57. ui.Superiors = dui.Superiors + "," + strconv.FormatInt(dui.Id, 10)
  58. ui.ParticipateAmount = float64(controller.ParticipateValue * controller.Decimals)
  59. ui.State = 1
  60. ui.CreateTime = util.NowTimeString()
  61. ui.Hash = hash
  62. _, err = engine.Insert(&ui)
  63. if err != nil {
  64. return err
  65. }
  66. //insert new user Performance
  67. var uper models.Performance
  68. uper.Addr = user
  69. _, err = engine.Insert(&uper)
  70. if err != nil {
  71. return err
  72. }
  73. //update rewards pool
  74. err = controller.UpdateRewardsPool(engine, amount)
  75. if err != nil {
  76. return err
  77. }
  78. ///update Recommendation Rewards
  79. ///update direct recommend rewards(10%)
  80. rewards := amount * 10 / 100
  81. if ui.Direct != "" {
  82. err := controller.UpdateAvailableRewards(engine, rewards, ui.Direct, true, true)
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. ///update indirect recommend rewards(10%)
  88. if ui.Indirect != "" {
  89. err := controller.UpdateAvailableRewards(engine, rewards, ui.Indirect, true, false)
  90. if err != nil {
  91. return err
  92. }
  93. }
  94. //update all supers performance (20%)
  95. err = controller.UpdateSuperiorsPerformance(engine, ui.Superiors, amount)
  96. if err != nil {
  97. return err
  98. }
  99. //update Community Gift(50%)
  100. err = controller.UpdateCommunityGift(engine, amount, inviter)
  101. if err != nil {
  102. return err
  103. }
  104. //Update Community Nodes(5%)
  105. err = controller.UpdateCommunityNodes(engine, amount)
  106. if err != nil {
  107. return err
  108. }
  109. return nil
  110. }