Skip to content

Commit

Permalink
rewrite stringstack.go
Browse files Browse the repository at this point in the history
  • Loading branch information
sekaiwish committed Nov 27, 2023
1 parent 3c6067c commit a846a71
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 26 deletions.
19 changes: 2 additions & 17 deletions common/stringstack/stringstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,19 @@ import (

// StringStack is a basic LIFO "stack" for storing strings.
type StringStack struct {
Locked bool
stack []string
stack []string
}

// New creates a new instance of StringStack
func New() *StringStack {
return &StringStack{Locked: false}
return &StringStack{}
}

// Set sets up a new StringStack
func (s *StringStack) Set(v string) {
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.
func (s *StringStack) Push(v string) {
s.stack = append(s.stack, v)
Expand Down
12 changes: 3 additions & 9 deletions server/channelserver/handlers_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,11 @@ func handleMsgSysEnterStage(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgSysEnterStage)

// Push our current stage ID to the movement stack before entering another one.
if s.stage.id == "" {
s.stageMoveStack.Set(pkt.StageID)
} else {
if s.stage != nil {
s.stage.Lock()
s.stage.reservedClientSlots[s.charID] = false
s.stage.Unlock()
s.stageMoveStack.Push(s.stage.id)
s.stageMoveStack.Lock()
}

if s.reservationStage != nil {
Expand All @@ -173,7 +170,6 @@ func handleMsgSysBackStage(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgSysBackStage)

// Transfer back to the saved stage ID before the previous move or enter.
s.stageMoveStack.Unlock()
backStage, err := s.stageMoveStack.Pop()
if err != nil {
panic(err)
Expand All @@ -193,10 +189,8 @@ func handleMsgSysBackStage(s *Session, p mhfpacket.MHFPacket) {
func handleMsgSysMoveStage(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgSysMoveStage)

// Set a new move stack from the given stage ID if unlocked
if !s.stageMoveStack.Locked {
s.stageMoveStack.Set(pkt.StageID)
}
// Set a new move stack from the given stage ID
s.stageMoveStack.Set(pkt.StageID)

doStageTransfer(s, pkt.AckHandle, pkt.StageID)
}
Expand Down

0 comments on commit a846a71

Please sign in to comment.