2026-01-14 19:33:06 +08:00

161 lines
4.7 KiB
Go

package model
import (
"fmt"
"roserver/baseserver/model"
model2 "roserver/game/model"
"roserver/serverproto"
)
type LiveSellRoom struct {
LiveRoom
currSellIds []int32
currSellInfos []*SellInfo
}
type SellInfo struct {
sellId int32
sellItemNum int32
buyUsers []uint64
}
func newLiveSellRoom(roomId uint32, tickInterval uint32) *LiveSellRoom {
room := &LiveSellRoom{
LiveRoom: newLiveRoom(roomId, LIVEROOM_TYPE_SELL, tickInterval),
}
room.SetVF()
return room
}
func (self *LiveSellRoom) SetVF() {
self.VF_GetMaxReadyNum = self.GetMaxReadyNum
self.VF_GetCMDPlayDataList = self.GetCMDPlayDataList
self.VF_GetShowResultList = self.GetShowResultList
self.VF_GetShowRewardList = self.GetShowRewardList
self.VF_HasShowData = self.HasShowData
self.VF_GetShowData = self.GetShowData
self.VF_HandleShowSubmit = self.HandleShowSubmit
self.VF_GetStageEnterParams = self.GetStageEnterParams
self.VF_ReadyStart = self.ReadyStart
self.VF_ShowStart = self.ShowStart
self.VF_CheckIfShowEnd = self.CheckIfShowEnd
self.VF_ShowEnd = self.ShowEnd
self.VF_HandlePlayCMD = self.HandlePlayCMD
}
func (self *LiveSellRoom) ReadyStart() {
liveRoomSellCfg := serverproto.LiveRoomSellCfgLoader
self.currSellIds = RandomKeys(liveRoomSellCfg, 1)
self.roomStateData["sellId"] = fmt.Sprintf("%d", self.currSellIds[0])
self.currSellInfos = make([]*SellInfo, 0)
for _, sellId := range self.currSellIds {
self.currSellInfos = append(self.currSellInfos, &SellInfo{
sellId: sellId,
sellItemNum: liveRoomSellCfg[sellId].SellNum,
buyUsers: make([]uint64, 0),
})
}
}
func (self *LiveSellRoom) ShowStart() {
}
// 结算阶段:给玩家背包里添加东西
func (self *LiveSellRoom) ShowEnd() {
for _, v := range self.currSellInfos {
for _, currUid := range v.buyUsers {
liveRoomSellCfg := serverproto.LiveRoomSellCfgLoader
cfg := liveRoomSellCfg[v.sellId]
items := make([]*serverproto.KeyValueType, 0)
for _, itemInfo := range cfg.Items {
itemId, itemNum := model.Str2Res(itemInfo)
items = append(items, &serverproto.KeyValueType{
Key: itemId,
Value: itemNum,
})
}
self.LiveRoom.AddItemList(currUid, items, int32(model2.AddFrom_LiveRoom_Card), true)
}
}
}
func (self *LiveSellRoom) GetMaxReadyNum(roomType uint32) int {
return LIVEROOM_SELL_MAX_READY_NUM
}
// 返回客户端显示秒杀的商品数据
func (self *LiveSellRoom) GetCMDPlayDataList(readyUidList []uint64, uid uint64, cfg *serverproto.LiveRoomTalkCfg) []*serverproto.KeyValueType64 {
return nil
}
// 返回客户端显示秒杀成功的玩家信息
func (self *LiveSellRoom) GetShowResultList(readyUidList []uint64, uid uint64) []*serverproto.LiveRoomPlayerInfo {
playerResults := make([]*serverproto.LiveRoomPlayerInfo, 0)
maxResultNum := 5
// 首先检查自己的秒杀信息
isBuySucc := false
for _, v := range self.currSellInfos {
if SliceIsExist(v.buyUsers, uid) {
isBuySucc = true
}
}
if isBuySucc {
currPlayerInfo := MakeLiveRoomPlayerInfo(uid)
currPlayerInfo.Data = ""
playerResults = append(playerResults, currPlayerInfo)
}
for _, v := range self.currSellInfos {
for _, currUid := range v.buyUsers {
if currUid != uid && len(playerResults) < maxResultNum {
currPlayerInfo := MakeLiveRoomPlayerInfo(currUid)
currPlayerInfo.Data = ""
playerResults = append(playerResults, currPlayerInfo)
}
}
}
ShuffleInPlace(playerResults)
return playerResults
}
func (self *LiveSellRoom) GetShowRewardList(readyUidList []uint64, uid uint64) []*serverproto.KeyValueType64 {
return nil
}
func (self *LiveSellRoom) HasShowData() bool {
return true
}
func (self *LiveSellRoom) GetShowData(readyUidList []uint64, uid uint64) []*serverproto.KeyValueType64 {
submitResult := make([]*serverproto.KeyValueType64, 0)
for _, v := range self.currSellInfos {
submitResult = append(submitResult, &serverproto.KeyValueType64{Key: uint64(v.sellId), Value: int32(len(v.buyUsers))})
}
return submitResult
}
// 玩家抢购秒杀
func (self *LiveSellRoom) HandleShowSubmit(uid uint64, data []*serverproto.KeyValueType64) []*serverproto.KeyValueType64 {
submitResult := make([]*serverproto.KeyValueType64, 0)
for _, v := range self.currSellInfos {
if SliceIsExist(v.buyUsers, uid) {
return nil
}
if len(v.buyUsers) >= int(v.sellItemNum) {
return nil
}
v.buyUsers = append(v.buyUsers, uid)
submitResult = append(submitResult, &serverproto.KeyValueType64{Key: uint64(v.sellId), Value: int32(len(v.buyUsers))})
}
return submitResult
}
func (self *LiveSellRoom) GetStageEnterParams(stage uint32) string {
stageEnterParams := ""
switch stage {
case LIVEROOM_STAGE_READY:
stageEnterParams = fmt.Sprintf("sellId:%d", self.currSellIds[0])
}
return stageEnterParams
}