Skip to content

Commit

Permalink
feat: 新增 space 包及 space.RoomMananger 结构体,提供了更便于使用的房间结构,用于取代 room 包
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Oct 16, 2023
1 parent 2d1e8f1 commit c3538ab
Show file tree
Hide file tree
Showing 9 changed files with 638 additions and 1 deletion.
6 changes: 6 additions & 0 deletions game/room/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func (slf *Manager[PID, P, R]) ReleaseRoom(guid int64) {
slf.pr.Atom(func(m map[PID]map[int64]struct{}) {
for playerId := range players {
delete(m[playerId], guid)
if len(m[playerId]) == 0 {
slf.players.Delete(playerId)
}
}
})
})
Expand Down Expand Up @@ -364,6 +367,9 @@ func (slf *Manager[PID, P, R]) Leave(roomId int64, player P) {
return
}
delete(rooms, roomId)
if len(rooms) == 0 {
slf.players.Delete(player.GetID())
}
})
slf.rp.Atom(func(m map[int64]map[PID]struct{}) {
players, exist := m[roomId]
Expand Down
26 changes: 26 additions & 0 deletions game/room/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package room_test

import (
"fmt"
"github.com/kercylan98/minotaur/game/room"
"testing"
)

func TestNewManager(t *testing.T) {
m := room.NewManager[string, *Player, *Room]()
r := &Room{}
m.CreateRoom(r)
helper := m.GetHelper(r)

helper.Join(&Player{ID: "player_01"})
helper.Join(&Player{ID: "player_02"})
helper.Join(&Player{ID: "player_03"})
helper.Leave(helper.GetPlayer("player_02"))
helper.Join(&Player{ID: "player_02"})

helper.BroadcastExcept(func(player *Player) {
fmt.Println(player.GetID())
}, func(player *Player) bool {
return false
})
}
3 changes: 2 additions & 1 deletion game/room/seat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

type Player struct {
ID string
}

func (slf *Player) GetID() string {
return ""
return slf.ID
}

func (slf *Player) GetConn() *server.Conn {
Expand Down
Loading

0 comments on commit c3538ab

Please sign in to comment.