rewrite stringstack.go

This commit is contained in:
wish
2023-11-27 20:45:03 +11:00
parent 3c6067c8a6
commit a846a71ca3
2 changed files with 5 additions and 26 deletions

View File

@@ -6,13 +6,12 @@ import (
// StringStack is a basic LIFO "stack" for storing strings. // StringStack is a basic LIFO "stack" for storing strings.
type StringStack struct { type StringStack struct {
Locked bool
stack []string stack []string
} }
// New creates a new instance of StringStack // New creates a new instance of StringStack
func New() *StringStack { func New() *StringStack {
return &StringStack{Locked: false} return &StringStack{}
} }
// Set sets up a new StringStack // Set sets up a new StringStack
@@ -20,20 +19,6 @@ func (s *StringStack) Set(v string) {
s.stack = []string{v} s.stack = []string{v}
} }
// Lock freezes the StringStack
func (s *StringStack) Lock() {
if !s.Locked {
s.Locked = true
}
}
// Unlock unfreezes the StringStack
func (s *StringStack) Unlock() {
if s.Locked {
s.Locked = false
}
}
// Push pushes a string onto the stack. // Push pushes a string onto the stack.
func (s *StringStack) Push(v string) { func (s *StringStack) Push(v string) {
s.stack = append(s.stack, v) s.stack = append(s.stack, v)

View File

@@ -152,14 +152,11 @@ func handleMsgSysEnterStage(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgSysEnterStage) pkt := p.(*mhfpacket.MsgSysEnterStage)
// Push our current stage ID to the movement stack before entering another one. // Push our current stage ID to the movement stack before entering another one.
if s.stage.id == "" { if s.stage != nil {
s.stageMoveStack.Set(pkt.StageID)
} else {
s.stage.Lock() s.stage.Lock()
s.stage.reservedClientSlots[s.charID] = false s.stage.reservedClientSlots[s.charID] = false
s.stage.Unlock() s.stage.Unlock()
s.stageMoveStack.Push(s.stage.id) s.stageMoveStack.Push(s.stage.id)
s.stageMoveStack.Lock()
} }
if s.reservationStage != nil { if s.reservationStage != nil {
@@ -173,7 +170,6 @@ func handleMsgSysBackStage(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgSysBackStage) pkt := p.(*mhfpacket.MsgSysBackStage)
// Transfer back to the saved stage ID before the previous move or enter. // Transfer back to the saved stage ID before the previous move or enter.
s.stageMoveStack.Unlock()
backStage, err := s.stageMoveStack.Pop() backStage, err := s.stageMoveStack.Pop()
if err != nil { if err != nil {
panic(err) panic(err)
@@ -193,10 +189,8 @@ func handleMsgSysBackStage(s *Session, p mhfpacket.MHFPacket) {
func handleMsgSysMoveStage(s *Session, p mhfpacket.MHFPacket) { func handleMsgSysMoveStage(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgSysMoveStage) pkt := p.(*mhfpacket.MsgSysMoveStage)
// Set a new move stack from the given stage ID if unlocked // Set a new move stack from the given stage ID
if !s.stageMoveStack.Locked {
s.stageMoveStack.Set(pkt.StageID) s.stageMoveStack.Set(pkt.StageID)
}
doStageTransfer(s, pkt.AckHandle, pkt.StageID) doStageTransfer(s, pkt.AckHandle, pkt.StageID)
} }