shop.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. package model
  2. import (
  3. "math"
  4. "rocommon/util"
  5. "roserver/baseserver/model"
  6. "roserver/serverproto"
  7. "time"
  8. )
  9. const (
  10. ShopType_Normal = 1 //普通商店
  11. ShopType_Arane = 2 //英灵殿
  12. ShopType_Dark = 3 //黑市
  13. ShopType_Guild = 4 //公会
  14. ShopType_Pet = 5 //宠物
  15. ShopType_Dao = 6 //道场
  16. ShopType_Spring = 9 //新春
  17. ShopType_NewBee = 10 //新手
  18. ShopType_CashShop = 50 //cash shop
  19. )
  20. type RoleShop struct {
  21. SaveObject
  22. shopBuyInfo map[int32]*serverproto.BuyInfo
  23. specialShop map[int32]*serverproto.SpecialShop
  24. costRes []*serverproto.ShopCost
  25. totalBuyNum int32 //商店总购买次数
  26. }
  27. func newRoleShop(r *Role) *RoleShop {
  28. roleShop := &RoleShop{
  29. SaveObject: SaveObject{
  30. role: r,
  31. },
  32. }
  33. roleShop.shopBuyInfo = make(map[int32]*serverproto.BuyInfo)
  34. roleShop.specialShop = make(map[int32]*serverproto.SpecialShop)
  35. return roleShop
  36. }
  37. func (this *RoleShop) Load(msg interface{}) bool {
  38. proRole := msg.(*serverproto.Role)
  39. if proRole != nil && proRole.RoleShop != nil {
  40. for _, buyInfo := range proRole.RoleShop.Info {
  41. for _, itemInfo := range buyInfo.ItemInfo {
  42. this.shopBuyInfo[itemInfo.GoodsId] = itemInfo
  43. }
  44. //黑市类型商店
  45. shopType, ok := serverproto.ShopTypeCfgLoader[buyInfo.GoodsType]
  46. if ok && shopType.ShopType == 2 {
  47. this.specialShop[buyInfo.GoodsType] = buyInfo.RefData
  48. }
  49. }
  50. for _, costRes := range proRole.RoleShop.CostRes {
  51. this.costRes = append(this.costRes, costRes)
  52. }
  53. this.totalBuyNum = proRole.RoleShop.TotalBuyNum
  54. }
  55. return true
  56. }
  57. func (this *RoleShop) DailyReset(notify bool) {
  58. for _, shop := range this.specialShop {
  59. shop.RefreshCount = 0
  60. }
  61. this.SetDirty(true)
  62. }
  63. func (this *RoleShop) Save() {
  64. this.SetDirty(false)
  65. util.DebugF("uid=%v RoleShop save...", this.role.GetUUid())
  66. saveMsg := &serverproto.SSShopDataSaveReq{
  67. Shop: &serverproto.RoleShop{
  68. TotalBuyNum: this.totalBuyNum,
  69. },
  70. }
  71. //处理购买信息相关数据
  72. for _, buyData := range this.shopBuyInfo {
  73. goodsType := this.GetGoodsType(buyData.GoodsId)
  74. if goodsType == 0 {
  75. continue
  76. }
  77. var bFind = false
  78. for _, buyInfo := range saveMsg.Shop.Info {
  79. if buyInfo.GoodsType == goodsType {
  80. buyInfo.ItemInfo = append(buyInfo.ItemInfo, buyData)
  81. bFind = true
  82. break
  83. }
  84. }
  85. if bFind == false {
  86. buyInfo := &serverproto.ShopBuyInfo{
  87. GoodsType: goodsType,
  88. }
  89. buyInfo.ItemInfo = append(buyInfo.ItemInfo, buyData)
  90. saveMsg.Shop.Info = append(saveMsg.Shop.Info, buyInfo)
  91. }
  92. }
  93. //打包黑市商店售卖数据
  94. for goodsType, shopData := range this.specialShop {
  95. bFind := false
  96. for idx, msgShop := range saveMsg.Shop.Info {
  97. if goodsType == msgShop.GoodsType {
  98. saveMsg.Shop.Info[idx].RefData = shopData
  99. bFind = true
  100. }
  101. }
  102. if bFind == false {
  103. buyInfo := &serverproto.ShopBuyInfo{
  104. GoodsType: goodsType,
  105. }
  106. buyInfo.RefData = shopData
  107. saveMsg.Shop.Info = append(saveMsg.Shop.Info, buyInfo)
  108. }
  109. }
  110. for _, data := range this.costRes {
  111. saveMsg.Shop.CostRes = append(saveMsg.Shop.CostRes, data)
  112. }
  113. this.role.SendDb(saveMsg)
  114. }
  115. func (this *RoleShop) GetGoodsType(shopItem int32) int32 {
  116. cfg, ok := serverproto.ShopCfgLoader[shopItem]
  117. if !ok {
  118. return 0
  119. }
  120. return cfg.GoodsType
  121. }
  122. func (this *RoleShop) GetShopType(shopId int32) int32 {
  123. cfg, ok := serverproto.ShopTypeCfgLoader[shopId]
  124. if !ok {
  125. return 0
  126. }
  127. return cfg.ShopType
  128. }
  129. func (this *RoleShop) GetShopTotalBuyNum() int32 {
  130. return this.totalBuyNum
  131. }
  132. //循环限购-单独处理//计算当前轮次购买数量
  133. func (this *RoleShop) GetCurRoundBuyNum(buyItem *serverproto.BuyInfo, shopItem *serverproto.ShopItem) (int32, serverproto.ErrorCode) {
  134. if shopItem == nil {
  135. return 0, serverproto.ErrorCode_ERROR_SHOP_CONFIG_ITEM_NO_FOUND
  136. }
  137. curTime := util.GetTimeSeconds()
  138. //这个判定现在不要了。只针对限购类型3,时间段限购 //超过最大时间就不让买了
  139. /*
  140. if shopItem.EndTime > 0 && curTime > shopItem.EndTime {
  141. return 0, serverproto.ErrorCode_ERROR_SHOP_NOT_IN_RANGE_TIME
  142. }
  143. */
  144. if buyItem.BuyNum == 0 { //压根没买过
  145. return shopItem.Count, serverproto.ErrorCode_ERROR_OK
  146. }
  147. if shopItem.LimitType == LIMIT_TYPE_WEEK || shopItem.LimitType == LIMIT_TYPE_DAY {
  148. if curTime < buyItem.RefTime {
  149. if shopItem.Count < buyItem.BuyNum {
  150. return 0, serverproto.ErrorCode_ERROR_SHOP_MAX_BUY_NUM
  151. }
  152. return shopItem.Count - buyItem.BuyNum, serverproto.ErrorCode_ERROR_OK
  153. } else {
  154. buyItem.BuyNum = 0
  155. buyItem.BuyTime = 0
  156. this.SetDirty(true)
  157. if buyItem.RefTime != 0 {
  158. util.DebugF("uid=%v %s RoleShop refresh...", this.role.GetUUid(), this.role.GetNickName())
  159. }
  160. return shopItem.Count, serverproto.ErrorCode_ERROR_OK
  161. }
  162. }
  163. return 0, 0
  164. }
  165. func (this *RoleShop) GetShopItemConfig(shopId int32, goodsId int32) (serverproto.ErrorCode, *serverproto.ShopItem) {
  166. shopType, ok := serverproto.ShopTypeCfgLoader[shopId]
  167. if ok && shopType.ShopType == 2 {
  168. specialShop, ok2 := model.ConvertRoleShopData[shopId]
  169. if !ok2 {
  170. return serverproto.ErrorCode_ERROR_SHOP_CONFIG_NO_FOUND, nil
  171. }
  172. for _, data := range specialShop.ItemList {
  173. if data.ShopItem.GoodsId == goodsId {
  174. return serverproto.ErrorCode_ERROR_OK, data.ShopItem
  175. }
  176. }
  177. return serverproto.ErrorCode_ERROR_SHOP_CONFIG_ITEM_NO_FOUND, nil
  178. }
  179. //
  180. if shopId == ShopType_CashShop {
  181. shopItem, ok := model.ConvertRedBagShopItemData[goodsId]
  182. if !ok || shopItem == nil {
  183. return serverproto.ErrorCode_ERROR_SHOP_CONFIG_ITEM_NO_FOUND, nil
  184. }
  185. return serverproto.ErrorCode_ERROR_OK, shopItem.ShopItem
  186. }
  187. shopItem, ok := model.ConvertShopItemData[goodsId]
  188. if !ok || shopItem == nil {
  189. return serverproto.ErrorCode_ERROR_SHOP_CONFIG_ITEM_NO_FOUND, nil
  190. }
  191. return serverproto.ErrorCode_ERROR_OK, shopItem
  192. }
  193. //返回购买数量可错误码,-1表示不限购
  194. func (this *RoleShop) CheckBuyLimit(shopId, goodsId, goodsNum int32) (int32, serverproto.ErrorCode) {
  195. if goodsNum <= 0 || goodsNum >= 1000 {
  196. return -1, serverproto.ErrorCode_ERROR_SHOP_NOT_IN_RANGE_TIME
  197. }
  198. bRet, shopInfo := this.GetShopItemConfig(shopId, goodsId)
  199. if bRet != serverproto.ErrorCode_ERROR_OK || shopInfo == nil {
  200. return 0, serverproto.ErrorCode_ERROR_SHOP_CONFIG_ITEM_NO_FOUND
  201. }
  202. if shopInfo.LimitType == LIMIT_TYPE_NONE && shopInfo.HdItemId == 0 {
  203. return -1, serverproto.ErrorCode_ERROR_OK
  204. }
  205. //黑市类型商店需要检查,当前商店是否有此道具
  206. shopType := this.GetShopType(shopId)
  207. if shopType == 2 {
  208. shop := this.specialShop[shopId]
  209. if shop == nil {
  210. return -1, serverproto.ErrorCode_ERROR_SHOP_CONFIG_NO_FOUND
  211. }
  212. //判定下时间,过期了说明没刷新
  213. if shop.RefreshTime < int64(util.GetTimeSeconds()) {
  214. return -1, serverproto.ErrorCode_ERROR_SHOP_NOT_IN_RANGE_TIME
  215. }
  216. //判定商店是否有此件商品
  217. bFind := false
  218. for _, data := range shop.GoodsList {
  219. if data == goodsId {
  220. bFind = true
  221. break
  222. }
  223. }
  224. if !bFind {
  225. return -1, serverproto.ErrorCode_ERROR_SHOP_NOT_FOUND_ITEM
  226. }
  227. }
  228. //先判定时间//当前不在限购时间内
  229. if shopInfo.LimitType == LIMIT_TYPE_RANGE {
  230. curTime := util.GetTimeSeconds()
  231. if curTime > shopInfo.EndTime || curTime < shopInfo.StartTime {
  232. return 0, serverproto.ErrorCode_ERROR_SHOP_NOT_IN_RANGE_TIME
  233. }
  234. }
  235. if shopInfo.HdItemId != 0 {
  236. if !this.checkHDShopItem(shopInfo) {
  237. return 0, serverproto.ErrorCode_ERROR_SHOP_NOT_IN_RANGE_TIME
  238. }
  239. if shopInfo.LimitType == 0 {
  240. return -1, serverproto.ErrorCode_ERROR_OK
  241. }
  242. }
  243. //没有购买记录,则直接给上限
  244. buyInfo, ok := this.shopBuyInfo[goodsId]
  245. if !ok {
  246. return shopInfo.Count, serverproto.ErrorCode_ERROR_OK
  247. }
  248. if shopInfo.LimitType == LIMIT_TYPE_FOREVER || shopInfo.LimitType == LIMIT_TYPE_SPECIAL {
  249. //当前购买数量和限购数量差额
  250. if shopInfo.Count <= buyInfo.BuyNum {
  251. return 0, serverproto.ErrorCode_ERROR_SHOP_MAX_BUY_NUM
  252. }
  253. return shopInfo.Count - buyInfo.BuyNum, serverproto.ErrorCode_ERROR_OK
  254. }
  255. //固定天刷新
  256. if shopInfo.LimitType == LIMIT_TYPE_DAY || shopInfo.LimitType == LIMIT_TYPE_WEEK {
  257. //查看限购期间已购买数量和限购数量差额
  258. count, err := this.GetCurRoundBuyNum(buyInfo, shopInfo)
  259. return count, err
  260. }
  261. if shopInfo.LimitType == LIMIT_TYPE_RANGE {
  262. //在限购时间内,返回可购买数量
  263. if shopInfo.Count <= buyInfo.BuyNum {
  264. return 0, serverproto.ErrorCode_ERROR_SHOP_MAX_BUY_NUM
  265. }
  266. return shopInfo.Count - buyInfo.BuyNum, serverproto.ErrorCode_ERROR_OK
  267. }
  268. return 0, serverproto.ErrorCode_ERROR_OK
  269. }
  270. func (this *RoleShop) BuyItem(goodsType, goodsId, goodsNum int32, ackMsg *serverproto.SCShopBuyItemAck) (serverproto.ErrorCode, uint64, int32) {
  271. if ackMsg == nil {
  272. return serverproto.ErrorCode_ERROR_FAIL, 0, 0
  273. }
  274. //校验配置
  275. shopCfg, ok := serverproto.ShopCfgLoader[goodsId]
  276. if !ok {
  277. return serverproto.ErrorCode_ERROR_SHOP_CONFIG_ITEM_NO_FOUND, 0, 0
  278. }
  279. bRet, shopInfo := this.GetShopItemConfig(goodsType, goodsId)
  280. if bRet != serverproto.ErrorCode_ERROR_OK || shopInfo == nil {
  281. return bRet, 0, 0
  282. }
  283. //校验是否限购/限时
  284. nRet, err := this.CheckBuyLimit(goodsType, goodsId, goodsNum)
  285. if err != serverproto.ErrorCode_ERROR_OK {
  286. return err, 0, 0
  287. }
  288. if nRet != -1 && nRet < goodsNum {
  289. return serverproto.ErrorCode_ERROR_SHOP_MAX_BUY_NUM, 0, 0
  290. }
  291. //前置条件判定通过,资源扣除成功,给道具
  292. var addItemList = map[int32]int32{}
  293. if shopCfg.GoodsType != ShopType_CashShop {
  294. addItemList[int32(shopCfg.GoodsItem)] = int32(goodsNum)
  295. } else {
  296. shopData, ok := model.ConvertRedBagShopItemData[shopCfg.GoodsId]
  297. if !ok {
  298. return serverproto.ErrorCode_ERROR_SHOP_CONFIG_NO_FOUND, 0, 0
  299. }
  300. for key, value := range shopData.ItemList {
  301. addItemList[key] = value
  302. }
  303. }
  304. if ret := this.role.CanAddItemList(addItemList); ret != serverproto.ErrorCode_ERROR_OK {
  305. return ret, 0, 0
  306. }
  307. //下面下面这段枚举代码,是客户端限时要,非常蛋疼的代码
  308. addFrom := AddFrom_Shop_Buy
  309. if shopCfg.GoodsType == ShopType_Arane {
  310. addFrom = AddFrom_Shop_Arane
  311. } else if shopCfg.GoodsType == ShopType_Dark {
  312. addFrom = AddFrom_Shop_Dark
  313. } else if shopCfg.GoodsType == ShopType_Guild {
  314. addFrom = AddFrom_Shop_Guild
  315. } else if shopCfg.GoodsType == ShopType_Pet {
  316. addFrom = AddFrom_Shop_Pet
  317. } else if shopCfg.GoodsType == ShopType_CashShop {
  318. addFrom = AddFrom_RedBagShop
  319. }
  320. //校验钱是否够
  321. price := shopCfg.PayForNum
  322. if shopCfg.DiscountPayForNum != 0 {
  323. price = shopCfg.DiscountPayForNum
  324. }
  325. needRes := uint64(price) * uint64(goodsNum)
  326. if needRes >= math.MaxInt32 {
  327. util.DebugF("uid=%v %s RoleShop Bug, goodsNum:%v ...", this.role.GetUUid(), this.role.GetNickName(), uint64(goodsNum))
  328. return serverproto.ErrorCode_ERROR_SHOP_MAX_BUY_NUM, 0, 0
  329. }
  330. var costItemList = map[int32]int32{}
  331. costItemList[shopCfg.PayForType] = int32(needRes)
  332. if this.role.CheckResLitNum(costItemList) {
  333. this.role.DelItemList(costItemList, AddItemST{AddFrom: addFrom, ReasonParam: goodsId, ReasonParam2: goodsNum})
  334. } else {
  335. bRet := this.role.GetResNotice(shopCfg.PayForType)
  336. return bRet, 0, 0
  337. }
  338. //前置条件判定通过,资源扣除成功,给道具
  339. if len(addItemList) > 0 {
  340. // this.role.AddItemList(shopCfg.GoodsItem, goodsNum, addFrom)
  341. this.role.AddItemList(addItemList, addFrom, true)
  342. }
  343. //添加购买数量
  344. var buyNum int32 = goodsNum
  345. notTime := uint64(util.GetTimeSeconds())
  346. if nRet != -1 { //非无限购买的商品
  347. buyData, ok := this.shopBuyInfo[goodsId]
  348. if !ok {
  349. data := &serverproto.BuyInfo{
  350. GoodsId: goodsId,
  351. BuyNum: goodsNum,
  352. BuyTime: notTime,
  353. }
  354. this.shopBuyInfo[goodsId] = data
  355. buyData = this.shopBuyInfo[goodsId]
  356. } else {
  357. buyData.BuyNum += goodsNum
  358. buyData.BuyTime = notTime
  359. buyNum = buyData.BuyNum
  360. }
  361. //记录下次刷新时间
  362. if shopInfo.LimitType == LIMIT_TYPE_DAY {
  363. buyData.RefTime = int64(util.GetLatest5Hour()) / 1000
  364. } else if shopInfo.LimitType == LIMIT_TYPE_WEEK {
  365. buyData.RefTime = int64(util.GetLatestWeek5Hour(0)) / 1000
  366. } else {
  367. buyData.RefTime = 0
  368. }
  369. }
  370. this.totalBuyNum++
  371. TaskMagCheck(this.role, serverproto.TaskType_Shop_Buy_Count, 0)
  372. this.OnBuyEnd(goodsType, shopCfg.PayForType, int32(needRes))
  373. if shopCfg.PayForType == int32(serverproto.ResType_Res_Rmb) {
  374. nLog := &NeteaseLogGoldBuyItem{}
  375. nLog.ItemId = int(goodsId)
  376. nLog.ItemName = shopCfg.GoodsName
  377. nLog.BuyTime = uint64(util.GetTimeSeconds())
  378. nLog.Price = int(shopCfg.PayForNum)
  379. nLog.Gold = int(needRes)
  380. nLog.CostMoney = 0
  381. nLog.ExpireTime = 0
  382. nLog.ItemCount = int(goodsNum)
  383. nLog.LogRoleGoldBuyItem(this.role)
  384. }
  385. this.SetDirty(true)
  386. //打包数据
  387. if len(addItemList) > 0 {
  388. for key, count := range addItemList {
  389. if key != 0 && count != 0 {
  390. ackMsg.ItemList = append(ackMsg.ItemList, &serverproto.KeyValueType{
  391. Key: key,
  392. Value: count,
  393. })
  394. }
  395. }
  396. }
  397. return serverproto.ErrorCode_ERROR_OK, notTime, buyNum
  398. }
  399. func (this *RoleShop) HandleSpecialShopRefresh(goodsType int32, ackMsg *serverproto.SCShopRefreshAck) serverproto.ErrorCode {
  400. shopInfo, ok := this.specialShop[goodsType]
  401. if !ok {
  402. return serverproto.ErrorCode_ERROR_SHOP_CONFIG_ITEM_NO_FOUND
  403. }
  404. shopData, ok2 := model.ConvertRoleShopData[goodsType]
  405. if !ok2 {
  406. return serverproto.ErrorCode_ERROR_SHOP_CONFIG_ITEM_NO_FOUND
  407. }
  408. if len(shopData.RefreshCost) <= 0 {
  409. return serverproto.ErrorCode_ERROR_SHOP_RESOURCE_NOT_NEOUGH
  410. }
  411. //判定刷新次数是否够
  412. if shopData.RefreshCount <= shopInfo.RefreshCount {
  413. return serverproto.ErrorCode_ERROR_SHOP_REFRESH_COUNT_MAX
  414. }
  415. //配置文件错误
  416. if len(shopData.RefreshCost) <= 0 {
  417. return serverproto.ErrorCode_ERROR_SHOP_CONFIG_ITEM_NO_FOUND
  418. }
  419. costIndex := shopInfo.RefreshCount
  420. if shopInfo.RefreshCount >= int32(len(shopData.RefreshCost)) {
  421. costIndex = int32(len(shopData.RefreshCost)) - 1
  422. }
  423. if this.role.CheckResLitNum(shopData.RefreshCost[costIndex].ItemList) == false {
  424. return serverproto.ErrorCode_ERROR_SHOP_REMOVE_COST_FAIL
  425. }
  426. bRet := this.RefreshSpecialShop(goodsType, shopInfo)
  427. //刷新异常,策划配置表错误。
  428. if bRet == false {
  429. util.ErrorF("uid=%v %s refresh failed shopType:=%v", this.role.GetUUid(), this.role.GetNickName(), goodsType)
  430. return serverproto.ErrorCode_ERROR_SHOP_CONFIG_ITEM_NO_FOUND
  431. }
  432. this.role.DelItemList(shopData.RefreshCost[costIndex].ItemList, AddItemST{AddFrom: AddFrom_Shop_Buy})
  433. ackMsg.ShopData = &serverproto.ShopData{
  434. GoodsType: goodsType,
  435. }
  436. shopInfo.RefreshCount += 1
  437. //打包道具数据
  438. for _, goodsId := range shopInfo.GoodsList {
  439. for _, data := range shopData.ItemList {
  440. if goodsId == data.ShopItem.GoodsId {
  441. ackMsg.ShopData.ItemInfo = append(ackMsg.ShopData.ItemInfo, data.ShopItem)
  442. }
  443. }
  444. }
  445. ackMsg.ShopData.GoodsType = goodsType
  446. ackMsg.ShopData.RefreshTime = shopInfo.RefreshTime
  447. ackMsg.ShopData.RefreshCount = shopInfo.RefreshCount
  448. this.SetDirty(true)
  449. util.DebugF("uid=%v %s GetSpecialShopInfo possitive refresh shopType:=%v", this.role.GetUUid(), this.role.GetNickName(), goodsType)
  450. return serverproto.ErrorCode_ERROR_OK
  451. }
  452. //下一次刷新时间:精确到秒
  453. func (this *RoleShop) GetNextRefreshTime(goodsType int32) int64 {
  454. loc := util.GetLoc()
  455. nowTime := util.GetCurrentTimeNow()
  456. curHour := nowTime.Hour()
  457. dayTimeStr := nowTime.Format(util.DATE_FORMAT1)
  458. tempDayTime, _ := time.ParseInLocation(util.DATE_FORMAT1, dayTimeStr, loc)
  459. refreshHour := model.GetNextShopRefreshHour(goodsType, int32(curHour))
  460. if refreshHour > int32(curHour) {
  461. tempDayTime = tempDayTime.Add(time.Hour * time.Duration(refreshHour))
  462. } else {
  463. tempDayTime = tempDayTime.Add(time.Hour * (time.Duration(refreshHour) + 24))
  464. }
  465. return (tempDayTime.UnixNano() / 1e9)
  466. }
  467. //刷新接口,删除老的购买列表和购买记录,刷新最新的额购买列表
  468. func (this *RoleShop) RefreshSpecialShop(goodsType int32, refData *serverproto.SpecialShop) bool {
  469. for _, goodsId := range refData.GoodsList {
  470. //删除购买记录
  471. _, ok2 := this.shopBuyInfo[goodsId]
  472. if !ok2 {
  473. continue
  474. }
  475. delete(this.shopBuyInfo, goodsId)
  476. }
  477. //获取最新的道具
  478. shopWeight := this.role.GetRoleBase().GetVipData(model.Vip_System_ShopWeight)
  479. goodsList := model.GetShopItemByShopType(goodsType, shopWeight)
  480. if goodsList != nil {
  481. refData.GoodsList = []int32{}
  482. for _, goodId := range goodsList {
  483. refData.GoodsList = append(refData.GoodsList, goodId)
  484. }
  485. return true
  486. }
  487. return false
  488. }
  489. func (this *RoleShop) GetSpecialShopInfo(goodsType int32, ackMsg *serverproto.SCShopInfoAck) serverproto.ErrorCode {
  490. if ackMsg == nil {
  491. return serverproto.ErrorCode_ERROR_FAIL
  492. }
  493. ackMsg.ShopData = &serverproto.ShopData{
  494. GoodsType: goodsType,
  495. }
  496. //没有触发过特殊商店。或者需要刷新
  497. shop, ok := this.specialShop[goodsType]
  498. if ok {
  499. //打包刷新数据
  500. needRefresh := model.CheckShopNeedRefresh(shop.RefreshTime)
  501. if needRefresh == true {
  502. this.RefreshSpecialShop(goodsType, shop)
  503. shop.RefreshTime = this.GetNextRefreshTime(goodsType)
  504. this.SetDirty(true)
  505. util.ErrorF("uid=%v %s refresh failed shopType:=%v", this.role.GetUUid(), this.role.GetNickName(), goodsType)
  506. }
  507. } else {
  508. //初始化特殊类型商店
  509. this.specialShop[goodsType] = &serverproto.SpecialShop{
  510. RefreshTime: this.GetNextRefreshTime(goodsType),
  511. RefreshCount: 0,
  512. }
  513. shopWeight := this.role.GetRoleBase().GetVipData(model.Vip_System_ShopWeight)
  514. goodsList := model.GetShopItemByShopType(goodsType, shopWeight)
  515. if goodsList != nil {
  516. for _, goodId := range goodsList {
  517. this.specialShop[goodsType].GoodsList = append(this.specialShop[goodsType].GoodsList, goodId)
  518. }
  519. this.SetDirty(true)
  520. util.InfoF("uid=%v %s init success shopType:=%v", this.role.GetUUid(), this.role.GetNickName(), goodsType)
  521. } else {
  522. util.ErrorF("uid=%v %s refresh failed shopType:=%v", this.role.GetUUid(), this.role.GetNickName(), goodsType)
  523. }
  524. }
  525. if this.specialShop[goodsType].RefreshTime == 0 {
  526. return serverproto.ErrorCode_ERROR_SHOP_REFRESH_TIME
  527. }
  528. //打包商品数据
  529. ackMsg.ShopData.GoodsType = goodsType
  530. shopList, ok := model.ConvertRoleShopData[goodsType]
  531. if !ok || shopList == nil {
  532. return serverproto.ErrorCode_ERROR_SHOP_CONFIG_ITEM_NO_FOUND
  533. }
  534. for _, goodsId := range this.specialShop[goodsType].GoodsList {
  535. for _, data := range shopList.ItemList {
  536. if goodsId == data.ShopItem.GoodsId {
  537. ackMsg.ShopData.ItemInfo = append(ackMsg.ShopData.ItemInfo, data.ShopItem)
  538. buyInfo, ok2 := this.shopBuyInfo[goodsId]
  539. if !ok2 { //没有买过
  540. continue
  541. }
  542. ackMsg.ShopData.BuyInfo = append(ackMsg.ShopData.BuyInfo, buyInfo)
  543. }
  544. }
  545. }
  546. ackMsg.ShopData.RefreshTime = this.specialShop[goodsType].RefreshTime
  547. ackMsg.ShopData.RefreshCount = this.specialShop[goodsType].RefreshCount
  548. return serverproto.ErrorCode_ERROR_OK
  549. }
  550. func (this *RoleShop) GetShopConfig(goodsId, goodsType int32) *serverproto.ShopItem {
  551. if goodsType == 50 {
  552. shopInfo, ok := model.ConvertRedBagShopItemData[goodsId]
  553. if ok {
  554. return shopInfo.ShopItem
  555. }
  556. } else {
  557. shopInfo, ok := model.ConvertShopItemData[goodsId]
  558. if ok {
  559. return shopInfo
  560. }
  561. }
  562. return nil
  563. }
  564. func (this *RoleShop) GetShopInfo(goodsType int32, ackMsg *serverproto.SCShopInfoAck) serverproto.ErrorCode {
  565. if ackMsg == nil {
  566. return serverproto.ErrorCode_ERROR_FAIL
  567. }
  568. ackMsg.ShopData = &serverproto.ShopData{
  569. GoodsType: goodsType,
  570. }
  571. ackMsg.ShopData.DayEnd = int64(util.GetLatest5Hour()) / 1000
  572. ackMsg.ShopData.WeekEnd = int64(util.GetLatestWeek5Hour(0)) / 1000
  573. for _, buyData := range this.shopBuyInfo {
  574. gType := this.GetGoodsType(buyData.GoodsId)
  575. if gType == 0 {
  576. continue
  577. }
  578. //处理循环区间刷新的道具
  579. shopInfo := this.GetShopConfig(buyData.GoodsId, goodsType)
  580. if shopInfo == nil {
  581. continue
  582. }
  583. condition, ok := model.ConvertShopItemCondition[buyData.GoodsId]
  584. if ok {
  585. bRet := this.role.GetRoleBase().checkUnLockState(0, condition.Conditions)
  586. if bRet != serverproto.ErrorCode_ERROR_OK {
  587. continue
  588. }
  589. }
  590. /*
  591. shopInfo, ok := model.ConvertShopItemData[buyData.GoodsId]
  592. if !ok || shopInfo == nil {
  593. continue
  594. }
  595. */
  596. if goodsType != 0 && gType != goodsType {
  597. continue
  598. }
  599. //活动期间掉落道具判断
  600. if !this.checkHDShopItem(shopInfo) {
  601. continue
  602. }
  603. if shopInfo.LimitType == LIMIT_TYPE_DAY || shopInfo.LimitType == LIMIT_TYPE_WEEK {
  604. this.GetCurRoundBuyNum(buyData, shopInfo)
  605. }
  606. ackMsg.ShopData.BuyInfo = append(ackMsg.ShopData.BuyInfo, buyData)
  607. }
  608. //购买后更新购买信息(限购等)
  609. if goodsType == ShopType_CashShop {
  610. for _, shopData := range model.ConvertRedBagShopItemData {
  611. gType := this.GetGoodsType(shopData.ShopItem.GoodsId)
  612. if gType == 0 {
  613. continue
  614. }
  615. if goodsType != 0 && goodsType != gType {
  616. continue
  617. }
  618. condition, ok := model.ConvertShopItemCondition[shopData.ShopItem.GoodsId]
  619. if ok {
  620. bRet := this.role.GetRoleBase().checkUnLockState(0, condition.Conditions)
  621. if bRet != serverproto.ErrorCode_ERROR_OK {
  622. continue
  623. }
  624. }
  625. ackMsg.ShopData.ItemInfo = append(ackMsg.ShopData.ItemInfo, shopData.ShopItem)
  626. }
  627. } else {
  628. for _, shopData := range model.ConvertShopItemData {
  629. gType := this.GetGoodsType(shopData.GoodsId)
  630. if gType == 0 {
  631. continue
  632. }
  633. if goodsType != 0 && goodsType != gType {
  634. continue
  635. }
  636. //活动期间掉落道具判断
  637. if !this.checkHDShopItem(shopData) {
  638. continue
  639. }
  640. condition, ok := model.ConvertShopItemCondition[shopData.GoodsId]
  641. if ok {
  642. bRet := this.role.GetRoleBase().checkUnLockState(0, condition.Conditions)
  643. if bRet != serverproto.ErrorCode_ERROR_OK {
  644. continue
  645. }
  646. }
  647. ackMsg.ShopData.ItemInfo = append(ackMsg.ShopData.ItemInfo, shopData)
  648. }
  649. }
  650. return serverproto.ErrorCode_ERROR_OK
  651. }
  652. func (this *RoleShop) CheckAndAddCostRes(shopType int32, resType int32, resCount uint32) uint32 {
  653. var retCount uint32 = 0
  654. bFind := false
  655. for _, data := range this.costRes {
  656. if data.ShopType == shopType && data.ResType == resType {
  657. data.ResCount += resCount
  658. bFind = true
  659. retCount = data.ResCount
  660. break
  661. }
  662. }
  663. if bFind == false {
  664. this.costRes = append(this.costRes, &serverproto.ShopCost{
  665. ShopType: shopType,
  666. ResType: resType,
  667. ResCount: resCount,
  668. })
  669. retCount = resCount
  670. }
  671. this.SetDirty(true)
  672. return retCount
  673. }
  674. func (this *RoleShop) OnBuyEnd(shopType int32, useResType int32, useResNum int32) {
  675. st := &SuperChargeUnlockST{}
  676. unlockType := serverproto.UnlockChargeType_UChargeType_None
  677. switch shopType {
  678. case ShopType_Normal:
  679. unlockType = serverproto.UnlockChargeType_UChargeType_ShopNormalRMBCost
  680. if useResType != int32(serverproto.ResType_Res_Rmb) {
  681. return
  682. }
  683. case ShopType_Arane:
  684. unlockType = serverproto.UnlockChargeType_UChargeType_ArenaShopCost
  685. if useResType != int32(serverproto.ResType_Res_Sprite) {
  686. return
  687. }
  688. case ShopType_Dark:
  689. unlockType = serverproto.UnlockChargeType_UChargeType_SpecialShopRMB
  690. if useResType != int32(serverproto.ResType_Res_Rmb) {
  691. return
  692. }
  693. case ShopType_Guild:
  694. unlockType = serverproto.UnlockChargeType_UChargeType_GuildShopCost
  695. if useResType != int32(serverproto.ResType_Res_Guild) {
  696. return
  697. }
  698. case ShopType_Pet:
  699. unlockType = serverproto.UnlockChargeType_UChargeType_PetShopCost
  700. if useResType != int32(serverproto.ResType_Res_PetCoin) {
  701. return
  702. }
  703. default:
  704. return
  705. }
  706. totalRes := this.CheckAndAddCostRes(shopType, useResType, uint32(useResNum))
  707. st.oldValue = int32(totalRes)
  708. this.role.GetRoleActivity().SuperChargeUnlockCheck(unlockType, st)
  709. }
  710. //活动期间掉落道具判断
  711. func (this *RoleShop) checkHDShopItem(shopItem *serverproto.ShopItem) bool {
  712. if shopItem.HdItemId <= 0 {
  713. return true
  714. }
  715. if CheckHDItemAdd(shopItem.HdItemId, this.role) {
  716. return true
  717. }
  718. return false
  719. }
  720. func (this *RoleShop) ExchangeRedBagTicket(ackMsg *serverproto.SCRedBagExchangeAck) serverproto.ErrorCode {
  721. if ackMsg == nil {
  722. return serverproto.ErrorCode_ERROR_FAIL
  723. }
  724. var costItemList = map[int32]int32{}
  725. costItemList[model.GlobalCashShopCostId] = model.GlobalCashShopCostCount
  726. if this.role.CheckResLitNum(costItemList) {
  727. this.role.DelItemList(costItemList, AddItemST{AddFrom: AddFrom_RedBagExchange})
  728. } else {
  729. return serverproto.ErrorCode_ERROR_SHOP_RESOURCE_NOT_NEOUGH
  730. }
  731. //给道具
  732. var addItemList = map[int32]int32{}
  733. for _, items := range model.GlobalCashShopReward {
  734. addItemList[items.Key] = items.Value
  735. }
  736. if ret := this.role.CanAddItemList(addItemList); ret != serverproto.ErrorCode_ERROR_OK {
  737. return serverproto.ErrorCode_ERROR_SHOP_CONFIG_ITEM_NO_FOUND
  738. }
  739. if len(addItemList) > 0 {
  740. this.role.AddItemList(addItemList, AddFrom_RedBagExchange, true)
  741. }
  742. //回包数据
  743. for _, items := range model.GlobalCashShopReward {
  744. ackMsg.Reward = append(ackMsg.Reward, items)
  745. }
  746. return serverproto.ErrorCode_ERROR_OK
  747. }