88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
package over
|
|
|
|
import "sync"
|
|
|
|
// 战斗结束原因池,用于管理战斗结束的各种原因数据
|
|
type BattleOverPool struct {
|
|
dataMap map[EnumBattleOverReason]BattleOverData
|
|
size int
|
|
value BattleOverData
|
|
mutex sync.RWMutex // 用于并发安全
|
|
}
|
|
|
|
// 初始化战斗结束原因池
|
|
func NewBattleOverPool() *BattleOverPool {
|
|
return &BattleOverPool{
|
|
dataMap: make(map[EnumBattleOverReason]BattleOverData),
|
|
}
|
|
}
|
|
|
|
// 添加战斗结束原因
|
|
func (p *BattleOverPool) PushReason(reason EnumBattleOverReason, data BattleOverData) {
|
|
p.mutex.Lock()
|
|
defer p.mutex.Unlock()
|
|
|
|
p.dataMap[reason] = data
|
|
p.value = data
|
|
p.size++
|
|
}
|
|
|
|
// 获取并移除指定的战斗结束原因
|
|
func (p *BattleOverPool) PopReason(reason EnumBattleOverReason) BattleOverData {
|
|
p.mutex.Lock()
|
|
defer p.mutex.Unlock()
|
|
|
|
data, exists := p.dataMap[reason]
|
|
if exists {
|
|
delete(p.dataMap, reason)
|
|
p.size--
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 获取并移除所有战斗结束原因
|
|
func (p *BattleOverPool) PopReasons() []BattleOverData {
|
|
p.mutex.Lock()
|
|
defer p.mutex.Unlock()
|
|
|
|
reasons := make([]BattleOverData, 0, p.size)
|
|
for _, data := range p.dataMap {
|
|
reasons = append(reasons, data)
|
|
}
|
|
p.dataMap = make(map[EnumBattleOverReason]BattleOverData)
|
|
p.size = 0
|
|
p.value = nil
|
|
return reasons
|
|
}
|
|
|
|
// 获取最后添加的战斗结束原因
|
|
func (p *BattleOverPool) PopReasonLast() BattleOverData {
|
|
p.mutex.Lock()
|
|
defer p.mutex.Unlock()
|
|
|
|
data := p.value
|
|
p.value = nil
|
|
if p.size > 0 {
|
|
p.size--
|
|
}
|
|
// 清空map
|
|
if p.size == 0 {
|
|
p.dataMap = make(map[EnumBattleOverReason]BattleOverData)
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 判断战斗是否应该结束
|
|
func (p *BattleOverPool) IsShouldOver() bool {
|
|
p.mutex.RLock()
|
|
defer p.mutex.RUnlock()
|
|
return p.size > 0
|
|
}
|
|
|
|
// 获取当前战斗结束原因的数量
|
|
func (p *BattleOverPool) Size() int {
|
|
p.mutex.RLock()
|
|
defer p.mutex.RUnlock()
|
|
return p.size
|
|
}
|