完善了Web管理端
This commit is contained in:
+88
-6
@@ -8,12 +8,15 @@ import (
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
ID string
|
||||
Nickname string
|
||||
IsReady bool
|
||||
IsHost bool
|
||||
IsBot bool
|
||||
Send chan []byte
|
||||
ID string
|
||||
Username string
|
||||
Nickname string
|
||||
IsReady bool
|
||||
IsHost bool
|
||||
IsBot bool
|
||||
BotControlled bool
|
||||
Disconnected bool
|
||||
Send chan []byte
|
||||
}
|
||||
|
||||
type Room struct {
|
||||
@@ -298,6 +301,85 @@ func (r *Room) SendToPlayer(playerID string, data []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) FindRoomByUsername(username string) (string, string, bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
for _, rm := range m.rooms {
|
||||
rm.mu.RLock()
|
||||
for _, p := range rm.Players {
|
||||
if p.Username == username && !p.IsBot {
|
||||
rm.mu.RUnlock()
|
||||
return rm.ID, p.ID, true
|
||||
}
|
||||
}
|
||||
rm.mu.RUnlock()
|
||||
}
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
func (m *Manager) DisconnectPlayer(roomID, playerID string) {
|
||||
rm := m.GetRoom(roomID)
|
||||
if rm == nil {
|
||||
return
|
||||
}
|
||||
rm.mu.Lock()
|
||||
defer rm.mu.Unlock()
|
||||
if p, ok := rm.Players[playerID]; ok {
|
||||
p.Disconnected = true
|
||||
p.Send = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) IsPlayerDisconnected(roomID, playerID string) bool {
|
||||
rm := m.GetRoom(roomID)
|
||||
if rm == nil {
|
||||
return false
|
||||
}
|
||||
rm.mu.RLock()
|
||||
defer rm.mu.RUnlock()
|
||||
p, ok := rm.Players[playerID]
|
||||
return ok && p.Disconnected
|
||||
}
|
||||
|
||||
func (m *Manager) SetPlayerBotControlled(roomID, playerID string, v bool) {
|
||||
rm := m.GetRoom(roomID)
|
||||
if rm == nil {
|
||||
return
|
||||
}
|
||||
rm.mu.Lock()
|
||||
defer rm.mu.Unlock()
|
||||
if p, ok := rm.Players[playerID]; ok {
|
||||
p.BotControlled = v
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) ReconnectPlayer(roomID, oldPID, newPID string, send chan []byte) bool {
|
||||
rm := m.GetRoom(roomID)
|
||||
if rm == nil {
|
||||
return false
|
||||
}
|
||||
rm.mu.Lock()
|
||||
defer rm.mu.Unlock()
|
||||
p, ok := rm.Players[oldPID]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
p.ID = newPID
|
||||
p.Disconnected = false
|
||||
p.BotControlled = false
|
||||
p.Send = send
|
||||
delete(rm.Players, oldPID)
|
||||
rm.Players[newPID] = p
|
||||
for i, id := range rm.PlayerOrder {
|
||||
if id == oldPID {
|
||||
rm.PlayerOrder[i] = newPID
|
||||
break
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func generateRoomID() string {
|
||||
src := rand.NewSource(time.Now().UnixNano())
|
||||
r := rand.New(src)
|
||||
|
||||
Reference in New Issue
Block a user