增加了一些功能
This commit is contained in:
+138
-5
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"Embalming_Girl_Server/db"
|
||||
"Embalming_Girl_Server/game"
|
||||
"Embalming_Girl_Server/room"
|
||||
"encoding/json"
|
||||
@@ -18,11 +19,15 @@ var upgrader = websocket.Upgrader{
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
ID string
|
||||
RoomID string
|
||||
Conn *websocket.Conn
|
||||
Send chan []byte
|
||||
mu sync.Mutex
|
||||
ID string
|
||||
RoomID string
|
||||
UserID int64
|
||||
Username string
|
||||
Nickname string
|
||||
Token string
|
||||
Conn *websocket.Conn
|
||||
Send chan []byte
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
type Hub struct {
|
||||
@@ -40,6 +45,12 @@ func NewHub() *Hub {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hub) OnlineCount() int {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
return len(h.clients)
|
||||
}
|
||||
|
||||
func (h *Hub) InitTestRooms() {
|
||||
for _, n := range []int{3, 4, 5, 6} {
|
||||
rid := "TEST" + string(rune('0'+n))
|
||||
@@ -113,6 +124,14 @@ func (h *Hub) handleMessage(c *Client, msg map[string]interface{}) {
|
||||
t, _ := msg["type"].(string)
|
||||
p, _ := msg["payload"].(map[string]interface{})
|
||||
switch t {
|
||||
case "register":
|
||||
h.handleRegister(c, p)
|
||||
case "login":
|
||||
h.handleLogin(c, p)
|
||||
case "update_profile":
|
||||
h.handleUpdateProfile(c, p)
|
||||
case "get_profile":
|
||||
h.handleGetProfile(c, p)
|
||||
case "create_room":
|
||||
h.handleCreateRoom(c, p)
|
||||
case "join_room":
|
||||
@@ -133,6 +152,8 @@ func (h *Hub) handleMessage(c *Client, msg map[string]interface{}) {
|
||||
h.handleEffectResponse(c, p)
|
||||
case "send_chat":
|
||||
h.handleChat(c, p)
|
||||
case "set_chaos":
|
||||
h.handleSetChaos(c, p)
|
||||
case "heartbeat":
|
||||
h.sendTo(c, "heartbeat_ack", nil)
|
||||
}
|
||||
@@ -144,6 +165,80 @@ func (h *Hub) handleListRooms(c *Client) {
|
||||
h.sendTo(c, "room_list", map[string]interface{}{"rooms": h.RoomMgr.ListRooms()})
|
||||
}
|
||||
|
||||
func (h *Hub) handleRegister(c *Client, p map[string]interface{}) {
|
||||
username, _ := p["username"].(string)
|
||||
password, _ := p["password"].(string)
|
||||
nickname, _ := p["nickname"].(string)
|
||||
user, err := db.Register(username, password, nickname)
|
||||
if err != nil {
|
||||
h.sendTo(c, "register_result", map[string]interface{}{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
h.sendTo(c, "register_result", map[string]interface{}{
|
||||
"success": true, "message": "注册成功", "username": user.Username, "nickname": user.Nickname,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Hub) handleLogin(c *Client, p map[string]interface{}) {
|
||||
username, _ := p["username"].(string)
|
||||
password, _ := p["password"].(string)
|
||||
token, _ := p["token"].(string)
|
||||
|
||||
var user *db.User
|
||||
var err error
|
||||
if token != "" {
|
||||
user, err = db.ValidateToken(token)
|
||||
if err == nil {
|
||||
token = token
|
||||
}
|
||||
} else {
|
||||
token, user, err = db.Login(username, password)
|
||||
}
|
||||
if err != nil {
|
||||
h.sendTo(c, "login_result", map[string]interface{}{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.UserID = user.ID
|
||||
c.Username = user.Username
|
||||
c.Nickname = user.Nickname
|
||||
c.Token = token
|
||||
log.Printf("User logged in: %s (%s)", user.Username, c.ID)
|
||||
|
||||
h.sendTo(c, "login_result", map[string]interface{}{
|
||||
"success": true, "token": token,
|
||||
"username": user.Username, "nickname": user.Nickname, "avatar": user.Avatar, "bio": user.Bio,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Hub) handleUpdateProfile(c *Client, p map[string]interface{}) {
|
||||
if c.UserID == 0 {
|
||||
h.sendTo(c, "error", map[string]interface{}{"message": "未登录"})
|
||||
return
|
||||
}
|
||||
nick, _ := p["nickname"].(string)
|
||||
bio, _ := p["bio"].(string)
|
||||
avatar, _ := p["avatar"].(string)
|
||||
if nick == "" {
|
||||
nick = c.Nickname
|
||||
}
|
||||
db.UpdateProfile(c.UserID, nick, bio, avatar)
|
||||
c.Nickname = nick
|
||||
h.sendTo(c, "profile_updated", map[string]interface{}{"success": true, "nickname": nick, "bio": bio, "avatar": avatar})
|
||||
}
|
||||
|
||||
func (h *Hub) handleGetProfile(c *Client, p map[string]interface{}) {
|
||||
username, _ := p["username"].(string)
|
||||
user, err := db.GetProfile(username)
|
||||
if err != nil {
|
||||
h.sendTo(c, "profile_info", map[string]interface{}{"success": false})
|
||||
return
|
||||
}
|
||||
h.sendTo(c, "profile_info", map[string]interface{}{
|
||||
"success": true, "username": user.Username, "nickname": user.Nickname, "bio": user.Bio, "avatar": user.Avatar,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Hub) handleCreateRoom(c *Client, p map[string]interface{}) {
|
||||
nick, _ := p["nickname"].(string)
|
||||
mp := 4
|
||||
@@ -190,6 +285,19 @@ func (h *Hub) handleLeaveRoom(c *Client) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hub) handleSetChaos(c *Client, p map[string]interface{}) {
|
||||
if c.RoomID == "" {
|
||||
return
|
||||
}
|
||||
rm := h.RoomMgr.GetRoom(c.RoomID)
|
||||
if rm == nil {
|
||||
return
|
||||
}
|
||||
enabled, _ := p["enabled"].(bool)
|
||||
rm.ChaosMode = enabled
|
||||
h.broadcastRoomState(rm)
|
||||
}
|
||||
|
||||
func (h *Hub) handleChat(c *Client, p map[string]interface{}) {
|
||||
if c.RoomID == "" {
|
||||
return
|
||||
@@ -255,6 +363,7 @@ func (h *Hub) handleStartGame(c *Client) {
|
||||
}
|
||||
|
||||
eng := game.NewEngine(ids, nicks, bots, len(ids))
|
||||
eng.ChaosMode = rm.ChaosMode
|
||||
eng.Deal()
|
||||
|
||||
h.mu.Lock()
|
||||
@@ -318,6 +427,7 @@ func (h *Hub) handleConfirmAction(c *Client, p map[string]interface{}) {
|
||||
}
|
||||
|
||||
h.broadcastSnapshots(c.RoomID)
|
||||
h.flushGameLogs(c.RoomID)
|
||||
|
||||
if eng.IsEffectResolving() {
|
||||
h.processBotEffectResponses(c.RoomID)
|
||||
@@ -431,6 +541,7 @@ func (h *Hub) processBotTurns(roomID string) {
|
||||
|
||||
eng.ConfirmAction(cp.ID, action, target)
|
||||
h.broadcastSnapshots(roomID)
|
||||
h.flushGameLogs(roomID)
|
||||
|
||||
if eng.IsEffectResolving() {
|
||||
h.processBotEffectResponses(roomID)
|
||||
@@ -515,6 +626,28 @@ func (h *Hub) handleAllExited(roomID string) {
|
||||
|
||||
// ---- Audio relay ----
|
||||
|
||||
func (h *Hub) flushGameLogs(roomID string) {
|
||||
h.mu.RLock()
|
||||
eng := h.engines[roomID]
|
||||
h.mu.RUnlock()
|
||||
if eng == nil {
|
||||
return
|
||||
}
|
||||
rm := h.RoomMgr.GetRoom(roomID)
|
||||
if rm == nil {
|
||||
return
|
||||
}
|
||||
for _, msg := range eng.FlushLogs() {
|
||||
for _, pid := range rm.PlayerOrder {
|
||||
p := rm.Players[pid]
|
||||
if p.IsBot || p.Send == nil {
|
||||
continue
|
||||
}
|
||||
h.sendToChannel(p.Send, "game_log", map[string]interface{}{"text": msg})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hub) relayAudio(sender *Client, pcm []byte) {
|
||||
if sender.RoomID == "" {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user