github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/game/internal/user_share.go (about) 1 package internal 2 3 import ( 4 "common" 5 "conf" 6 "encoding/json" 7 "fmt" 8 "game/circle" 9 10 "github.com/globalsign/mgo" 11 12 "msg" 13 "strconv" 14 "time" 15 16 "github.com/name5566/leaf/log" 17 "gopkg.in/mgo.v2/bson" 18 ) 19 20 type ShareTaskData struct { 21 TaskID int 22 UserID int 23 Total int 24 IsFinish bool 25 EndTime int64 26 } 27 28 func (user *User) shareTasksInfo() { 29 tasks := conf.GetCfgShareTask() 30 data := []*msg.ShareTasks{} 31 for _, v := range tasks { 32 temp := new(msg.ShareTasks) 33 temp.Desc = v.Desc 34 temp.FeeDes = v.Info 35 36 data = append(data, temp) 37 } 38 user.WriteMsg(&msg.S2C_ShareTasksInfo{ 39 ShareTasks: data, 40 }) 41 } 42 func (user *User) BindSharer(accountID string) { 43 db := mongoDB.Ref() 44 defer mongoDB.UnRef(db) 45 account, _ := strconv.Atoi(accountID) 46 parentUserdata := new(UserData) 47 if err := db.DB(DB).C("users").Find(bson.M{"accountid": account}).One(parentUserdata); err != nil { 48 log.Release("查找绑定人账号不合法:%v", err) 49 user.WriteMsg(&msg.S2C_BindSharer{ 50 Error: msg.BindSharerAbnormal, 51 }) 52 return 53 } 54 55 if user.isRobot() || parentUserdata.Role == roleRobot { 56 user.WriteMsg(&msg.S2C_BindSharer{ 57 Error: msg.BindRobot, 58 }) 59 return 60 } 61 62 //如果玩家已经绑定了 63 if user.baseData.userData.ParentId != 0 { 64 user.WriteMsg(&msg.S2C_BindSharer{ 65 Error: msg.BindDuplicate, 66 }) 67 return 68 } 69 70 //不能是自己 71 if account == user.baseData.userData.AccountID { 72 user.WriteMsg(&msg.S2C_BindSharer{ 73 Error: msg.BindSelf, 74 }) 75 return 76 } 77 78 //邀请人注册时间较晚,请绑定其他推荐码 79 if user.baseData.userData.CreatedAt < parentUserdata.CreatedAt { 80 user.WriteMsg(&msg.S2C_BindSharer{ 81 Error: msg.BindShareLate, 82 }) 83 return 84 } 85 86 userAgent := new(UserAgent) 87 query := bson.M{"accountid": user.baseData.userData.AccountID} 88 89 if err := db.DB(DB).C("userAgents").Find(query).One(userAgent); err != nil { 90 log.Error(err.Error()) 91 } 92 //不能绑定在自己的下级上面 93 for i := 0; i < len(userAgent.Agents); i++ { 94 for _, value := range userAgent.Agents[0].Datas { 95 if strconv.Itoa(int(value.AccountId)) == accountID { 96 user.WriteMsg(&msg.S2C_BindSharer{ 97 Error: msg.BindNotToLevel, 98 }) 99 return 100 } 101 } 102 } 103 104 //更新玩家绑定页面 105 user.WriteMsg(&msg.S2C_BindSharer{ 106 Error: msg.BindSharerOK, 107 ParentId: int64(account), 108 }) 109 user.baseData.userData.Chips += int64(conf.Server.Chips) 110 user.WriteMsg(&msg.S2C_UpdateUserChips{ 111 Chips: user.baseData.userData.Chips, 112 }) 113 //玩家绑定的上级是否完成新手任务(分享至好友并成为上级合伙人) 114 //userData := new(UserData) 115 //err := db.DB(DB).C("users").Find(bson.M{"accountid": account}).One(userData) 116 //if err == nil { 117 // tasks := new(TaskList) 118 // err := db.DB(DB).C("userDoRedpakcetTask").Find(bson.M{"userid": userData.UserID}).One(tasks) 119 // if err == nil { 120 // for key, value := range tasks.Tasks { 121 // //表示进行的任务是分享至好友并成为上级合伙人 122 // if value.State == 1 && value.ID != 1000 { 123 // break 124 // } 125 // if value.State == 1 && value.ID == 1000 { 126 // tasks.Tasks[key].State = 2 127 // tasks.Tasks[key].PlayTimes++ 128 // tasks.Tasks[key+1].PlayTimes = 0 129 // tasks.Tasks[key+1].StartTime = time.Now().Unix() 130 // tasks.Tasks[key+1].State = 1 131 // if v, ok := userIDUsers[userData.UserID]; ok { 132 // v.data.redPacketTaskList = tasks.Tasks 133 // v.WriteMsg(&msg.S2C_RedpacketTask{ 134 // Tasks: v.data.redPacketTaskList, 135 // Chips: ChangeChips[v.data.userData.Level], 136 // FreeChangeTime: v.data.userData.FreeChangeTime, 137 // }) 138 // } 139 // update := &struct { 140 // Tasks []msg.RedPacketTask 141 // UpdatedAt int64 142 // }{ 143 // Tasks: tasks.Tasks, 144 // UpdatedAt: time.Now().Unix(), 145 // } 146 // db.DB(DB).C("userDoRedpakcetTask").Upsert(bson.M{"userid": userData.UserID}, bson.M{"$set": update}) 147 // } 148 // } 149 // } 150 //} 151 user.baseData.userData.ParentId = int64(account) 152 updateUserData(user.baseData.userData.UserID, bson.M{"$set": bson.M{"parentid": account}}) 153 154 userAgentData := new(Data) 155 userAgentData.Createdat = time.Now().Unix() 156 userAgentData.AccountId = int64(user.baseData.userData.AccountID) 157 158 preId := user.baseData.userData.ParentId 159 160 for i := 0; i <= len(userAgent.Agents); i++ { 161 temppreID := preId 162 for j := 0; j < 8-i; j++ { 163 if i == 0 { 164 temppreID = user.pollingBindSharer(i+j+1, temppreID, *userAgentData) 165 } else { 166 length := len(userAgent.Agents[i-1].Datas) 167 for k := 0; k < length-1; k++ { 168 user.pollingBindSharer(i+j+1, temppreID, userAgent.Agents[i-1].Datas[k]) 169 } 170 temppreID = user.pollingBindSharer(i+j+1, temppreID, userAgent.Agents[i-1].Datas[length-1]) 171 } 172 if temppreID <= 0 { 173 break 174 } 175 } 176 } 177 if len(userAgent.Agents) > 0 { 178 userAgent.ParentId = int64(account) 179 db.DB(DB).C("userAgents").Upsert(bson.M{"accountid": user.baseData.userData.AccountID}, userAgent) 180 } 181 } 182 183 func (user *User) pollingBindSharer(level int, parentID int64, userAgent Data) int64 { 184 parentUserData := new(UserData) 185 if err := parentUserData.readByAccountID(parentID); err != nil { 186 log.Error(err.Error()) 187 return -1 188 } 189 userAgent.AllProfit = 0 190 userAgent.Profit = 0 191 userAgent.InsertAgent(level, parentID, int64(parentUserData.ParentId)) 192 193 return int64(parentUserData.ParentId) 194 } 195 196 func (userData *UserData) finishShareTask(shareTaskID int) { 197 198 shareTaskData := new(ShareTaskData) 199 err := shareTaskData.read(userData.UserID, shareTaskID) 200 if err != nil { 201 log.Error(err.Error()) 202 return 203 } 204 205 if !shareTaskData.IsFinish { 206 shareTaskData.EndTime = time.Now().Unix() 207 shareTaskData.IsFinish = true 208 shareTaskData.upsert(userData.UserID, shareTaskID) 209 } 210 } 211 212 func (ctx *ShareTaskData) read(userid int, taskid int) error { 213 db := mongoDB.Ref() 214 defer mongoDB.UnRef(db) 215 err := db.DB(DB).C("sharetask").Find(bson.M{"userid": userid, "taskid": taskid}).One(ctx) 216 if err != nil { 217 log.Error(err.Error()) 218 return err 219 } 220 return nil 221 } 222 223 func (ctx *ShareTaskData) upsert(userid int, taskid int) { 224 db := mongoDB.Ref() 225 defer mongoDB.UnRef(db) 226 if _, err := db.DB(DB).C("sharetask").Upsert(bson.M{"userid": userid, "taskid": taskid}, ctx); err != nil { 227 log.Error(err.Error()) 228 } 229 } 230 231 type ShareTakenRecord struct { 232 ID int 233 Fee float64 234 Level int 235 DirID int 236 CreatedAt int64 237 IsCopy bool 238 ExchangeCode string 239 } 240 241 type ShareTakenRecords struct { 242 AccountID int 243 TakenRecords []msg.ShareTakenRecord 244 } 245 246 func (userData *UserData) rebate(fee float64) { 247 if userData.ParentId == 0 { 248 log.Release("玩家%v进行充值操作,其无绑定任何代理", userData.AccountID) 249 return 250 } 251 grandParentID := userData.ParentId 252 253 //找到他有几个父节点 254 count := 0 255 for i := 0; i < 8; i++ { //往上轮询 256 count++ 257 grandParentID = userData.pollingGiveAchievement(0, 0, grandParentID, i+1) 258 if grandParentID <= 0 { 259 break 260 } 261 } 262 263 grandParentID = userData.ParentId 264 fee = common.Decimal(fee * conf.Server.RebateRate / float64(count)) 265 266 //更新上级的可领取收益 267 for i := 0; i < count; i++ { //往上轮询 268 grandParentID = userData.pollingGiveAchievement(fee, 0, grandParentID, i+1) 269 if grandParentID <= 0 { 270 break 271 } 272 } 273 } 274 275 func (userData *UserData) countRecharge(recharge float64) { 276 grandParentID := userData.ParentId 277 if userData.ParentId == 0 { 278 log.Release("玩家%v进行返利操作,其无绑定任何代理", userData.AccountID) 279 return 280 } 281 for i := 0; i < 8; i++ { //往上轮询 282 grandParentID = userData.pollingGiveAchievement(0, recharge, grandParentID, i+1) 283 if grandParentID <= 0 { 284 break 285 } 286 } 287 } 288 289 func (userData *UserData) giveShareAward(fee float64, dirID int64, level int) string { 290 parentUserData := new(UserData) 291 if err := parentUserData.readByAccountID(dirID); err != nil { 292 log.Error(err.Error()) 293 return "" 294 } 295 return parentUserData.shareAwardRecord(fee, level) 296 } 297 298 func (userData *UserData) pollingGiveAchievement(fee, recharge float64, parentID int64, level int) int64 { 299 parentUserData := new(UserData) 300 if err := parentUserData.readByAccountID(parentID); err != nil && err != mgo.ErrNotFound { 301 log.Error(err.Error()) 302 return -1 303 } 304 305 _, datas := shareAbleProfitAndDatas(level, parentID) 306 data := findAgentData(datas, int64(userData.AccountID)) 307 if data == nil { 308 log.Error("找不到代理") 309 return -1 310 } 311 312 data.updateIncAgent(level, parentID, map[string]float64{"profit": fee, "allprofit": fee, "recharge": recharge}) 313 314 return parentUserData.ParentId 315 } 316 317 func findAgentData(datas []Data, accountid int64) (rt *Data) { 318 for i := 0; i < len(datas); i++ { 319 if datas[i].AccountId == accountid { 320 return &datas[i] 321 } 322 } 323 return 324 } 325 326 func (userData *UserData) shareAwardRecord(fee float64, level int) string { 327 shareTakenRecord := new(ShareTakenRecord) 328 preID := 0 329 temp := new(ShareTakenRecords) 330 if err := temp.read(int64(userData.AccountID)); err != nil && err != mgo.ErrNotFound { 331 log.Error(err.Error()) 332 } else { 333 if len(temp.TakenRecords) > 0 { 334 preID = temp.TakenRecords[0].ID 335 } 336 } 337 338 shareTakenRecord.CreatedAt = time.Now().Unix() 339 shareTakenRecord.Fee = common.Decimal(fee) 340 shareTakenRecord.Level = level 341 shareTakenRecord.DirID = int(userData.AccountID) 342 shareTakenRecord.ExchangeCode = getRedPacketCode(fee) 343 shareTakenRecord.ID = preID + 1 344 345 if err := shareTakenRecord.push(int64(userData.AccountID)); err != nil { 346 log.Error(err.Error()) 347 return "" 348 } 349 return shareTakenRecord.ExchangeCode 350 } 351 352 func (user *User) shareAwardRecord(page, per int) { 353 temp := new(ShareTakenRecords) 354 temp.read(int64(user.baseData.userData.AccountID)) 355 //过滤掉已经过期的数据 356 r := new(ShareTakenRecords) 357 for i := 0; i < len(temp.TakenRecords); i++ { 358 if temp.TakenRecords[i].CreatedAt+86400*3 > time.Now().Unix() { 359 r.TakenRecords = append(r.TakenRecords, temp.TakenRecords[i]) 360 } 361 } 362 tr := r.TakenRecords 363 364 stRecord := []*msg.ShareTakenRecord{} 365 for i := (page - 1) * per; i < (page-1)*per+per; i++ { 366 if i >= len(tr) { 367 break 368 } 369 370 tempRecd := new(msg.ShareTakenRecord) 371 tempRecd.CreatedAt = tr[i].CreatedAt 372 tempRecd.ID = tr[i].ID 373 tempRecd.Fee = tr[i].Fee 374 tempRecd.Level = tr[i].Level 375 tempRecd.DirID = tr[i].DirID 376 tempRecd.IsCopy = tr[i].IsCopy 377 tempRecd.ExchangeCode = tr[i].ExchangeCode 378 379 stRecord = append(stRecord, tempRecd) 380 } 381 382 user.WriteMsg(&msg.S2C_ShareRecord{ 383 Page: page, 384 Per: per, 385 Total: len(r.TakenRecords), 386 ShareTakenRecords: stRecord, 387 }) 388 } 389 390 func (user *User) isExistAward() bool { 391 str := new(ShareTakenRecords) 392 db := mongoDB.Ref() 393 defer mongoDB.UnRef(db) 394 query := bson.M{"accountid": user.baseData.userData.AccountID, "takenrecords.iscopy": false} 395 if err := db.DB(DB).C("sharetakenrecords").Find(query).One(str); err != nil { 396 for i := 0; i < 8; i++ { 397 ableprofit, _ := shareAbleProfitAndDatas(i+1, int64(user.baseData.userData.AccountID)) 398 if ableprofit > 0 { 399 return true 400 } 401 } 402 return false 403 } else { 404 return true 405 } 406 } 407 408 func getRedPacketCode(Fee float64) (code string) { 409 //请求圈圈获取红包码 410 temp := &struct { 411 Code string 412 Data string 413 }{} 414 r := new(circle.RedPacketCodeInfo) 415 r.Sum = Fee 416 417 param, _ := json.Marshal(r) 418 json.Unmarshal(circle.DoRequestRepacketCode(string(param)), temp) 419 420 if temp.Code != "0" { 421 log.Error("请求圈圈红包错误") 422 return 423 } 424 return temp.Data 425 } 426 427 func (ctx *ShareTakenRecords) read(accountid int64) error { 428 db := mongoDB.Ref() 429 defer mongoDB.UnRef(db) 430 query := bson.M{"accountid": accountid} 431 err := db.DB(DB).C("sharetakenrecords").Find(query).One(ctx) 432 if err != nil { 433 log.Error(err.Error()) 434 return err 435 } 436 return nil 437 } 438 439 func (ctx *ShareTakenRecord) push(accountid int64) error { 440 db := mongoDB.Ref() 441 defer mongoDB.UnRef(db) 442 selector := bson.M{"accountid": accountid} 443 update := bson.M{"$push": bson.M{"takenrecords": bson.M{"$each": []ShareTakenRecord{*ctx}, "$sort": bson.M{"createdat": -1}}}} 444 _, err := db.DB(DB).C("sharetakenrecords").Upsert(selector, update) 445 if err != nil { 446 log.Error(err.Error()) 447 return err 448 } 449 return nil 450 } 451 452 func (user *User) copyExchangeCode(shareRecordID int) { 453 strecord := new(ShareTakenRecord) 454 log.Release("%+v", *strecord) 455 strecord.ID = shareRecordID 456 strecord.IsCopy = true 457 458 if err := strecord.update(user.baseData.userData.AccountID); err != nil { 459 user.WriteMsg(&msg.S2C_CopyExchangeCode{ 460 Error: msg.CopyFail, 461 }) 462 log.Error(err.Error()) 463 return 464 } 465 user.WriteMsg(&msg.S2C_CopyExchangeCode{ 466 Error: msg.CopyOK, 467 ShareRecordID: shareRecordID, 468 }) 469 } 470 471 func (ctx *ShareTakenRecord) update(accountid int) error { 472 db := mongoDB.Ref() 473 defer mongoDB.UnRef(db) 474 selector := bson.M{"accountid": accountid, "takenrecords.id": ctx.ID} 475 update := bson.M{"$set": bson.M{"takenrecords.$.iscopy": ctx.IsCopy}} 476 err := db.DB(DB).C("sharetakenrecords").Update(selector, update) 477 return err 478 } 479 480 func (user *User) achievement(level int, page int, per int) { 481 db := mongoDB.Ref() 482 defer mongoDB.UnRef(db) 483 userAgent := new(UserAgent) 484 err := db.DB(DB).C("userAgents").Find(bson.M{"accountid": user.baseData.userData.AccountID}).One(userAgent) 485 if err != nil && err != mgo.ErrNotFound { 486 log.Error(err.Error()) 487 return 488 } 489 achievements := []msg.Achievement{} 490 total := 0 491 if err == mgo.ErrNotFound { 492 user.WriteMsg(&msg.S2C_Achievement{ 493 Page: page, 494 Per: per, 495 Total: total, 496 Achievements: achievements, 497 }) 498 return 499 } 500 501 if level <= len(userAgent.Agents) { 502 datas := userAgent.Agents[level-1].Datas 503 total = len(datas) 504 for i := (page - 1) * per; i < (page-1)*per+per; i++ { 505 if i >= len(datas) { 506 break 507 } 508 achievements = append(achievements, msg.Achievement{ 509 Createdat: datas[i].Createdat, 510 AccountId: datas[i].AccountId, 511 Recharge: datas[i].Recharge, 512 AllProfit: datas[i].AllProfit, 513 Profit: datas[i].Profit, 514 Updatedat: datas[i].Updatedat, 515 }) 516 } 517 } 518 519 user.WriteMsg(&msg.S2C_Achievement{ 520 Page: page, 521 Per: per, 522 Total: total, 523 Achievements: achievements, 524 }) 525 } 526 527 func (user *User) ableProfit(level int) { 528 ableProfit, _ := shareAbleProfitAndDatas(level, int64(user.baseData.userData.AccountID)) 529 530 user.WriteMsg(&msg.S2C_AbleProfit{ 531 AbleProfit: ableProfit, 532 }) 533 } 534 535 func shareAbleProfitAndDatas(level int, accountid int64) (float64, []Data) { 536 db := mongoDB.Ref() 537 defer mongoDB.UnRef(db) 538 userAgent := new(UserAgent) 539 540 err := db.DB(DB).C("userAgents").Find(bson.M{"accountid": accountid}).One(userAgent) 541 if err != nil && err != mgo.ErrNotFound { 542 log.Error(err.Error()) 543 return 0, []Data{} 544 } 545 if err == mgo.ErrNotFound { 546 return 0, []Data{} 547 } 548 var ableProfit float64 549 if level <= len(userAgent.Agents) { 550 datas := userAgent.Agents[level-1].Datas 551 for _, v := range datas { 552 ableProfit += v.Profit 553 } 554 return ableProfit, userAgent.Agents[level-1].Datas 555 } else { 556 return ableProfit, []Data{} 557 } 558 } 559 560 func (user *User) agentNumbersProfit() { 561 numbersProfit := make([]msg.NumbersProfit, 8) 562 for i := 0; i < len(numbersProfit); i++ { 563 numbersProfit[i].Level = i + 1 564 } 565 566 db := mongoDB.Ref() 567 defer mongoDB.UnRef(db) 568 userAgent := new(UserAgent) 569 if err := db.DB(DB).C("userAgents").Find(bson.M{"accountid": user.baseData.userData.AccountID}).One(userAgent); err != nil && err != mgo.ErrNotFound { 570 log.Error(err.Error()) 571 } 572 573 for _, v := range userAgent.Agents { 574 numbersProfit[v.Level-1].Level = v.Level 575 numbersProfit[v.Level-1].Profit, _ = shareAbleProfitAndDatas(v.Level, int64(user.baseData.userData.AccountID)) 576 numbersProfit[v.Level-1].Number = len(v.Datas) 577 } 578 579 user.WriteMsg(&msg.S2C_AgentNumbersProfit{ 580 NumbersProfits: numbersProfit, 581 }) 582 } 583 584 func (user *User) receiveProfit(level int) { 585 fee, datas := shareAbleProfitAndDatas(level, int64(user.baseData.userData.AccountID)) 586 if fee == 0 { 587 user.WriteMsg(&msg.S2C_ReceiveShareProfit{ 588 Error: msg.ReceiveFail, 589 }) 590 return 591 } 592 exchangeCode := user.baseData.userData.giveShareAward(fee, int64(user.baseData.userData.AccountID), level) 593 WriteRedPacketGrantRecord(user.baseData.userData, 2, fmt.Sprintf("%v级下级充值“%v”元,进行返利", level, common.Decimal(fee/0.8)), fee) 594 for i := 0; i < len(datas); i++ { 595 datas[i].updateIncAgent(level, int64(user.baseData.userData.AccountID), map[string]float64{"profit": -datas[i].Profit}) 596 } 597 598 user.WriteMsg(&msg.S2C_ReceiveShareProfit{ 599 Error: msg.ReceiveOK, 600 Profit: fee, 601 ExchangeCode: exchangeCode, 602 }) 603 } 604 605 func (user *User) takenProfit() { 606 shareTakenRecords := new(ShareTakenRecords) 607 shareTakenRecords.read(int64(user.baseData.userData.AccountID)) 608 var takenProfit float64 609 for _, v := range shareTakenRecords.TakenRecords { 610 takenProfit += v.Fee 611 } 612 613 user.WriteMsg(&msg.S2C_TakenProfit{ 614 TakenProfit: common.Decimal(takenProfit), 615 }) 616 }