diff options
Diffstat (limited to 'internal/net/server.go')
| -rw-r--r-- | internal/net/server.go | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/internal/net/server.go b/internal/net/server.go index 2a10a9a..fa0b509 100644 --- a/internal/net/server.go +++ b/internal/net/server.go @@ -24,6 +24,7 @@ const ( StateDeleteChar StatePurgeAccount StateGame + StateChangeDescription ) type Session struct { @@ -32,8 +33,10 @@ type Session struct { State SessionState Account *AccountEntry Player interface{} // *player.Player once character is selected - PendingChar string // char being renamed/deleted - PendingPass string // first password during signup + PendingChar string // char being renamed/deleted + PendingPass string // first password during signup + Disconnecting bool + DisconnectTicks int } type AccountEntry struct { @@ -48,9 +51,9 @@ type Server struct { } type Hub struct { - sessions map[*Session]bool - // roomID -> sessions - rooms map[int]map[*Session]bool + sessions map[*Session]bool + rooms map[int]map[*Session]bool + onRemove func(*Session) } func NewHub() *Hub { @@ -60,15 +63,31 @@ func NewHub() *Hub { } } +func (h *Hub) OnRemove(cb func(*Session)) { + h.onRemove = cb +} + func (h *Hub) Add(s *Session) { h.sessions[s] = true } func (h *Hub) Remove(s *Session) { + if s.Player != nil && s.State == StateGame && !s.Disconnecting { + s.Disconnecting = true + s.DisconnectTicks = 10 + return + } + h.HardRemove(s) +} + +func (h *Hub) HardRemove(s *Session) { delete(h.sessions, s) for _, room := range h.rooms { delete(room, s) } + if h.onRemove != nil { + h.onRemove(s) + } } func (h *Hub) EnterRoom(s *Session, roomID int) { @@ -85,6 +104,14 @@ func (h *Hub) LeaveRoom(s *Session) { } } +func (h *Hub) AllSessions() []*Session { + var out []*Session + for s := range h.sessions { + out = append(out, s) + } + return out +} + func (h *Hub) PlayersInRoom(roomID int) []*Session { var out []*Session if room, ok := h.rooms[roomID]; ok { |
