package main import ( "encoding/json" "math/rand" "mongo.games.com/game/common" "mongo.games.com/game/model" player_proto "mongo.games.com/game/protocol/player" "mongo.games.com/game/protocol/webapi" "mongo.games.com/game/srvdata" "mongo.games.com/goserver/core/logger" "mongo.games.com/goserver/core/netlib" ) type CSPhoneLotteryInfoPacketFactory struct { } type CSPhoneLotteryInfoHandler struct { } func (this *CSPhoneLotteryInfoPacketFactory) CreatePacket() interface{} { pack := &player_proto.CSPhoneLotteryInfo{} return pack } // 获取抽奖信息 func (this *CSPhoneLotteryInfoHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error { if _, ok := data.(*player_proto.CSPhoneLotteryInfo); ok { p := PlayerMgrSington.GetPlayer(sid) logger.Logger.Trace("客户端请求抽奖信息!snid = ", p.SnId) if p == nil { logger.Logger.Warn("CSPhoneLotteryInfo p == nil") return nil } if WelfareMgrSington.GetPhoneLotteryStatus(p.Platform) == model.WelfareClose { return nil } pack := &player_proto.SCPhoneLotteryInfo{} pack.PhoneScore = p.PhoneScore pack.Count = p.LotteryCount pool := srvdata.PBDB_PhoneLotteryMgr.Datas.GetArr() for _, lottery := range pool { if p.AppChannel == common.ChannelGooglePlay { if lottery.Type == 1 { continue } } else { if lottery.Type == 2 { continue } } date := &player_proto.LotteryItem{ Id: lottery.Id, ItemId: lottery.Item_Id, ItemNum: int64(lottery.Grade), } pack.Item = append(pack.Item, date) } p.SendToClient(int(player_proto.PlayerPacketID_PACKET_SC_PhoneLotteryInfo), pack) logger.Logger.Trace("返回抽奖信息:", pack.String()) } return nil } // 请求抽奖 type CSPhoneLotteryPacketFactory struct { } type CSPhoneLotteryHandler struct { } func (this *CSPhoneLotteryPacketFactory) CreatePacket() interface{} { pack := &player_proto.CSPhoneLottery{} return pack } func (this *CSPhoneLotteryHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error { if msg, ok := data.(*player_proto.CSPhoneLottery); ok { p := PlayerMgrSington.GetPlayer(sid) if p == nil { logger.Logger.Warn("CSPhoneLottery p == nil") return nil } if !PlatformMgrSingleton.IsOn(p.Platform, common.ChannelSwitchDiamondLottery, p.LastChannel) { return nil } countType := msg.GetLotteryType() count := int32(0) if countType == 1 { count = 1 } else { count = 10 } logger.Logger.Tracef("玩家请求抽奖,snid =%d,count = %d ", p.SnId, count) if p.LotteryCount < count { logger.Logger.Trace("剩余抽奖次数不足,无法抽奖 count = ", count) return nil } //p.LotteryCount -= count p.addLotteryCount(-count) pool := srvdata.PBDB_PhoneLotteryMgr.Datas.GetArr() pack := &player_proto.SCPhoneLottery{} items := make([]*model.Item, 0) for i := 1; i <= int(count); i++ { //抽奖 rate := 0 weight := 0 for _, lottery := range pool { if p.AppChannel == common.ChannelGooglePlay { if lottery.Type == 1 { continue } } else { if lottery.Type == 2 { continue } } if p.PhoneScore < int64(lottery.Odd) { rate = 1 weight += int(lottery.Oddrate1) } else if p.PhoneScore >= int64(lottery.Odd) && p.PhoneScore < int64(lottery.Odd2) { rate = 2 weight += int(lottery.Oddrate2) } else if p.PhoneScore >= int64(lottery.Odd2) && p.PhoneScore < int64(lottery.Odd3) { rate = 3 weight += int(lottery.Oddrate3) } else if p.PhoneScore >= int64(lottery.Odd3) { rate = 4 weight += int(lottery.Oddrate4) } } random := rand.Intn(weight) + 1 num := 0 logger.Logger.Tracef("玩家抽奖 当前权重区间 rate = %d,weight = %d ,随机到的值:%d", rate, weight, random) for _, lottery := range pool { if p.AppChannel == common.ChannelGooglePlay { if lottery.Type == 1 { continue } } else { if lottery.Type == 2 { continue } } if rate == 1 { if lottery.Oddrate1 == 0 { continue } num += int(lottery.Oddrate1) } else if rate == 2 { if lottery.Oddrate2 == 0 { continue } num += int(lottery.Oddrate2) } else if rate == 3 { if lottery.Oddrate3 == 0 { continue } num += int(lottery.Oddrate3) } else if rate == 4 { if lottery.Oddrate4 == 0 { continue } num += int(lottery.Oddrate4) } if random <= num { itemArr := &player_proto.LotteryItem{} itemArr.ItemId = lottery.Item_Id itemArr.ItemNum = int64(lottery.Grade) itemArr.Id = lottery.Id items = append(items, &model.Item{ ItemId: lottery.Item_Id, // 物品id ItemNum: int64(lottery.Grade), // 数量 }) logger.Logger.Tracef("玩家抽奖获得物品 itemId = %d,itemNum = %d", lottery.Item_Id, lottery.Grade) pack.Item = append(pack.Item, itemArr) break } } } //增加到玩家背包 BagMgrSingleton.AddItems(&model.AddItemParam{ Platform: p.Platform, SnId: p.SnId, Change: items, GainWay: common.GainWay_PhoneScore, Operator: "system", Remark: "玩游戏积分", }) pack.Count = p.LotteryCount pack.PhoneScore = p.PhoneScore logger.Logger.Tracef("获取玩家抽奖权重 score = %d,抽奖获得的物品:%v", p.PhoneScore, pack) p.SendToClient(int(player_proto.PlayerPacketID_PACKET_SC_PhoneLottery), pack) //抽奖统计 jsonData, err := json.Marshal(items) if err != nil { return err } LogChannelSingleton.WriteMQData(model.GeneratePhoneLottery(p.SnId, p.Platform, string(jsonData), 1, 0, 0, 0)) for _, v := range items { tp1 := int32(-1) if v.ItemId == common.ItemIDCoin { tp1 = model.SystemFreeGive_CoinType_Coin } else if v.ItemId == common.ItemIDDiamond { tp1 = model.SystemFreeGive_CoinType_Diamond } if !p.IsRob && tp1 >= 0 { LogChannelSingleton.WriteMQData( model.GenerateSystemFreeGive(p.SnId, p.Name, p.Platform, p.Channel, model.SystemFreeGive_PhoneLottery, tp1, v.ItemNum)) } } } return nil } type CSDiamondLotteryInfoPacketFactory struct { } type CSDiamondLotteryInfoHandler struct { } func (this *CSDiamondLotteryInfoPacketFactory) CreatePacket() interface{} { pack := &player_proto.CSDiamondLotteryInfo{} return pack } // 获取钻石抽奖信息 func (this *CSDiamondLotteryInfoHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error { if _, ok := data.(*player_proto.CSDiamondLotteryInfo); ok { p := PlayerMgrSington.GetPlayer(sid) logger.Logger.Trace("客户端请求钻石抽奖信息!snid = ", p.SnId) if p == nil { logger.Logger.Warn("CSDiamondLotteryInfo p == nil") return nil } // 渠道开关 if !PlatformMgrSingleton.IsOn(p.Platform, common.ChannelSwitchDiamondLottery, p.LastChannel) { return nil } pack := &player_proto.SCDiamondLotteryInfo{} pack.LuckyScore = p.DiamondLotteryScore info := WelfareMgrSington.GetConfig(p.Platform) if info != nil && info.DiamondLotteryConfig != nil { for _, value := range info.DiamondLotteryConfig.LotteryData { if value.Channel == p.LastChannel { pack.MaxScore = value.MaxScore pack.DiamondNum = value.DiamondNum for _, lotteryInfo := range value.Info { item := &player_proto.LotteryItem{ Id: lotteryInfo.Id, ItemId: lotteryInfo.ItemId, ItemNum: int64(lotteryInfo.Grade), TypeId: lotteryInfo.Type, } pack.Item = append(pack.Item, item) } } } } p.SendToClient(int(player_proto.PlayerPacketID_PACKET_SC_DiamondLotteryInfo), pack) logger.Logger.Trace("返回钻石抽奖信息:", pack.String()) } return nil } type CSDiamondLotteryPacketFactory struct { } type CSDiamondLotteryHandler struct { } func (this *CSDiamondLotteryPacketFactory) CreatePacket() interface{} { pack := &player_proto.CSDiamondLottery{} return pack } // 玩家钻石抽奖 func (this *CSDiamondLotteryHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error { if msg, ok := data.(*player_proto.CSDiamondLottery); ok { p := PlayerMgrSington.GetPlayer(sid) if p == nil { logger.Logger.Warn("CSDiamondLottery p == nil") return nil } // 渠道开关 if !PlatformMgrSingleton.IsOn(p.Platform, common.ChannelSwitchDiamondLottery, p.LastChannel) { return nil } countType := msg.GetLotteryType() count := int32(0) if countType == 1 { count = 1 } else { count = 10 } logger.Logger.Tracef("玩家请求钻石抽奖,snid =%d,count = %d ", p.SnId, count) info := WelfareMgrSington.GetConfig(p.Platform) var config *webapi.DiamondLotteryData if info != nil && info.LotteryData != nil { for _, value := range info.LotteryData { if value.Channel == p.LastChannel { config = value break } } if config == nil { logger.Logger.Error("钻石抽奖,未找到配置!p.LastChannel = ", p.LastChannel) return nil } //判断钻石数量 diamondNum := int64(config.DiamondNum * count) if p.Diamond < diamondNum { logger.Logger.Trace("钻石抽奖 ,钻石数量不足!") return nil } p.AddDiamond(-diamondNum, 0, common.GainWayDiamondLottery, "sys", "钻石抽奖") pack := &player_proto.SCDiamondLottery{} var items []*model.Item for i := 1; i <= int(count); i++ { weight := 0 for _, lotteryInfo := range config.Info { if lotteryInfo.Type == 1 { weight += int(lotteryInfo.Oddrate) } } random := rand.Intn(weight) + 1 value := 0 p.DiamondLotteryScore += 100 add := p.GetSkillAdd(common.SkillIdDiamondLottery) if add > 0 { p.DiamondLotteryScore += int64(add) } //判断是否白名单用户 whiteList := config.Players status := false var playerData *webapi.DiamondLotteryPlayers for _, players := range whiteList { if players.Uid == p.SnId { playerData = players break } } if playerData != nil && p.DiamondLotteryScore%int64(playerData.Count*100) == 0 { status = true } if status { //白名单从列表里获取奖励 weight = 0 awardId := int32(0) for _, awardData := range playerData.Award { weight += int(awardData.Weight) } random = rand.Intn(weight) + 1 for _, awardData := range playerData.Award { value += int(awardData.Weight) if random <= value { awardId = awardData.AwardId break } } for _, lotteryInfo := range config.Info { if lotteryInfo.Id == awardId { items = append(items, &model.Item{ ItemId: lotteryInfo.ItemId, // 物品id ItemNum: int64(lotteryInfo.Grade), // 数量 }) itemData := &player_proto.LotteryItem{ Id: lotteryInfo.Id, ItemId: lotteryInfo.ItemId, ItemNum: int64(lotteryInfo.Grade), } pack.Item = append(pack.Item, itemData) break } } } else { for _, lotteryInfo := range config.Info { value += int(lotteryInfo.Oddrate) if lotteryInfo.Type == 1 { if random <= value { items = append(items, &model.Item{ ItemId: lotteryInfo.ItemId, // 物品id ItemNum: int64(lotteryInfo.Grade), // 数量 }) itemData := &player_proto.LotteryItem{ Id: lotteryInfo.Id, ItemId: lotteryInfo.ItemId, ItemNum: int64(lotteryInfo.Grade), } pack.Item = append(pack.Item, itemData) break } } } } } BagMgrSingleton.AddItems(&model.AddItemParam{ Platform: p.Platform, SnId: p.SnId, Change: items, Cost: []*model.Item{ { ItemId: common.ItemIDDiamond, ItemNum: diamondNum, }, }, Add: 0, GainWay: common.GainWayDiamondLottery, Operator: "system", Remark: "钻石抽奖", GameId: 0, GameFreeId: 0, NoLog: false, }) pack.LuckyScore = p.DiamondLotteryScore p.SendToClient(int(player_proto.PlayerPacketID_PACKET_SC_DiamondLottery), pack) logger.Logger.Trace("返回钻石抽奖信息:", pack.String()) } } return nil } // 领取保底奖励 type CSDiamondLotteryLuckyAwardPacketFactory struct { } type CSDiamondLotteryLuckyAwardHandler struct { } func (this *CSDiamondLotteryLuckyAwardPacketFactory) CreatePacket() interface{} { pack := &player_proto.CSDiamondLotteryLuckyAward{} return pack } // 玩家钻石抽奖保底奖励 func (this *CSDiamondLotteryLuckyAwardHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error { if _, ok := data.(*player_proto.CSDiamondLotteryLuckyAward); ok { p := PlayerMgrSington.GetPlayer(sid) if p == nil { logger.Logger.Warn("CSDiamondLotteryLuckyAward p == nil") return nil } // 渠道开关 if !PlatformMgrSingleton.IsOn(p.Platform, common.ChannelSwitchDiamondLottery, p.LastChannel) { return nil } info := WelfareMgrSington.GetConfig(p.Platform) var config *webapi.DiamondLotteryData if info != nil && info.LotteryData != nil { for _, value := range info.LotteryData { if value.Channel == p.LastChannel { config = value break } } if config == nil { logger.Logger.Error("钻石抽奖,未找到配置!p.LastChannel = ", p.LastChannel) return nil } if p.DiamondLotteryScore < int64(config.MaxScore) { return nil } p.DiamondLotteryScore -= int64(config.MaxScore) pack := &player_proto.SCDiamondLotteryLuckyAward{} pack.LuckyScore = p.DiamondLotteryScore //获取奖励 for _, lotteryInfo := range config.Info { if lotteryInfo.Type == 2 { var items []*model.Item items = append(items, &model.Item{ ItemId: lotteryInfo.ItemId, // 物品id ItemNum: int64(lotteryInfo.Grade), // 数量 }) itemData := &player_proto.LotteryItem{ Id: lotteryInfo.Id, ItemId: lotteryInfo.ItemId, ItemNum: int64(lotteryInfo.Grade), } BagMgrSingleton.AddItems(&model.AddItemParam{ Platform: p.Platform, SnId: p.SnId, Change: items, GainWay: common.GainWayDiamondLottery, Operator: "system", Remark: "钻石抽奖保底奖励", }) pack.Item = append(pack.Item, itemData) break } } p.SendToClient(int(player_proto.PlayerPacketID_PACKET_SCDiamondLotteryLuckyAward), pack) logger.Logger.Trace("返回钻石抽奖领取保底奖励信息:", pack.String()) } } return nil } func init() { // 抽奖信息 common.RegisterHandler(int(player_proto.PlayerPacketID_PACKET_CS_PhoneLotteryInfo), &CSPhoneLotteryInfoHandler{}) netlib.RegisterFactory(int(player_proto.PlayerPacketID_PACKET_CS_PhoneLotteryInfo), &CSPhoneLotteryInfoPacketFactory{}) // 抽奖 common.RegisterHandler(int(player_proto.PlayerPacketID_PACKET_CS_PhoneLottery), &CSPhoneLotteryHandler{}) netlib.RegisterFactory(int(player_proto.PlayerPacketID_PACKET_CS_PhoneLottery), &CSPhoneLotteryPacketFactory{}) //钻石抽奖信息 common.RegisterHandler(int(player_proto.PlayerPacketID_PACKET_CS_DiamondLotteryInfo), &CSDiamondLotteryInfoHandler{}) netlib.RegisterFactory(int(player_proto.PlayerPacketID_PACKET_CS_DiamondLotteryInfo), &CSDiamondLotteryInfoPacketFactory{}) // 钻石抽奖 common.RegisterHandler(int(player_proto.PlayerPacketID_PACKET_CS_DiamondLottery), &CSDiamondLotteryHandler{}) netlib.RegisterFactory(int(player_proto.PlayerPacketID_PACKET_CS_DiamondLottery), &CSDiamondLotteryPacketFactory{}) //钻石抽奖保底奖励 common.RegisterHandler(int(player_proto.PlayerPacketID_PACKET_CSDiamondLotteryLuckyAward), &CSDiamondLotteryLuckyAwardHandler{}) netlib.RegisterFactory(int(player_proto.PlayerPacketID_PACKET_CSDiamondLotteryLuckyAward), &CSDiamondLotteryLuckyAwardPacketFactory{}) }