xy 13 hours ago
parent
commit
f95bf89e61

+ 1 - 0
bin/client_msg/common.proto

@@ -157,6 +157,7 @@ message UserInfo {
   int32 m_coin = 3;
   string name = 4;
   int32 room_id = 5; //如果用户在某一个房间玩,他的id就一直存在,
+  int32 online_status = 6;//在线=1 离线等0
   
 }
 //登录 请求

+ 7 - 9
src/server/common/user_common.go

@@ -1,12 +1,10 @@
 package common
 
-type UserData struct {
-	Id     string
-	Status int
-	GameId string
-	// Teen_Patti_Room *room.Room
-	Head_Image string
-	Nickname   string
-	Room_id    string
-	Points     int32
+import (
+	usercenter "server/datacenter"
+)
+
+// 更新玩家在线状态
+func UpdateUserOnlineStatus(online_status int32, userId string) {
+	usercenter.UpdateOnlineStatusToDB(userId, online_status)
 }

+ 28 - 0
src/server/datacenter/users.go

@@ -269,6 +269,20 @@ func GetUserCount() (int, error) {
 	}
 	return count, nil
 }
+
+func InitOnlineStatus() error {
+	_, err := mysqlmgr.Update(`
+        UPDATE users 
+        SET online_status = ? 
+        WHERE online_status = ?`,
+		0,
+		1,
+	)
+	if err != nil {
+		return fmt.Errorf("数据库更新失败: %v", err)
+	}
+	return nil
+}
 func UpdateCoinToDB(user_id string, coin int) error {
 	_, err := mysqlmgr.Update(`
         UPDATE users 
@@ -283,6 +297,20 @@ func UpdateCoinToDB(user_id string, coin int) error {
 	return nil
 }
 
+func UpdateOnlineStatusToDB(user_id string, online_status int32) error {
+	_, err := mysqlmgr.Update(`
+        UPDATE users 
+        SET online_status = ? 
+        WHERE user_id = ?`,
+		online_status,
+		user_id,
+	)
+	if err != nil {
+		return fmt.Errorf("数据库更新失败: %v", err)
+	}
+	return nil
+}
+
 // GetAllFriend 获取用户的所有好友信息
 func (u *User) GetAllFriend() ([]*User, error) {
 	// 1. 获取好友ID列表

+ 41 - 42
src/server/db/mongodb/userMongodb.go

@@ -1,49 +1,48 @@
 package mongodbmgr
 
-import (
-	"log"
-	"server/common"
-	"server/conf"
-	"server/msg"
+// import (
+// 	"log"
+// 	"server/conf"
+// 	"server/msg"
 
-	"gopkg.in/mgo.v2/bson"
-)
+// 	"gopkg.in/mgo.v2/bson"
+// )
 
-// 定义用户集合操作
-var userCollection = NewCollection(conf.MongoDBName, "users")
+// // 定义用户集合操作
+// var userCollection = NewCollection(conf.MongoDBName, "users")
 
-// 新增用户到 MongoDB
-func AddUser(userData *msg.UserInfo) error {
-	// 创建插入的用户数据
-	err := userCollection.Insert(userData)
-	if err != nil {
-		log.Printf("Error inserting user data into MongoDB: %v", err)
-		return err
-	}
-	log.Printf("User data added to MongoDB for userId: %s", userData.UserId)
-	return nil
-}
+// // 新增用户到 MongoDB
+// func AddUser(userData *msg.UserInfo) error {
+// 	// 创建插入的用户数据
+// 	err := userCollection.Insert(userData)
+// 	if err != nil {
+// 		log.Printf("Error inserting user data into MongoDB: %v", err)
+// 		return err
+// 	}
+// 	log.Printf("User data added to MongoDB for userId: %s", userData.UserId)
+// 	return nil
+// }
 
-// 更新用户信息到 MongoDB
-func UpdateUserInfo(userData *msg.UserInfo) error {
-	// 使用用户 ID 来更新用户数据
-	err := userCollection.Update(bson.M{"id": userData.UserId}, bson.M{"$set": userData})
-	if err != nil {
-		log.Printf("Error updating user data in MongoDB: %v", err)
-		return err
-	}
-	log.Printf("User data updated in MongoDB for userId: %s", userData.UserId)
-	return nil
-}
+// // 更新用户信息到 MongoDB
+// func UpdateUserInfo(userData *msg.UserInfo) error {
+// 	// 使用用户 ID 来更新用户数据
+// 	err := userCollection.Update(bson.M{"id": userData.UserId}, bson.M{"$set": userData})
+// 	if err != nil {
+// 		log.Printf("Error updating user data in MongoDB: %v", err)
+// 		return err
+// 	}
+// 	log.Printf("User data updated in MongoDB for userId: %s", userData.UserId)
+// 	return nil
+// }
 
-// 获取用户信息从 MongoDB
-func GetUserInfoFromMongoDB(userId string) (*common.UserData, error) {
-	var userData common.UserData
-	err := userCollection.FindOne(bson.M{"id": userId}, &userData) // 查找用户ID
-	if err != nil {
-		log.Printf("Error fetching user data from MongoDB: %v", err)
-		// 如果没有找到用户数据,可以返回 nil 或者处理错误
-		return nil, err
-	}
-	return &userData, nil
-}
+// // 获取用户信息从 MongoDB
+// func GetUserInfoFromMongoDB(userId string) (*common.UserData, error) {
+// 	var userData common.UserData
+// 	err := userCollection.FindOne(bson.M{"id": userId}, &userData) // 查找用户ID
+// 	if err != nil {
+// 		log.Printf("Error fetching user data from MongoDB: %v", err)
+// 		// 如果没有找到用户数据,可以返回 nil 或者处理错误
+// 		return nil, err
+// 	}
+// 	return &userData, nil
+// }

+ 10 - 1
src/server/db/redis/userRedis.go

@@ -44,7 +44,12 @@ func SaveUserInfoToRedis(userData *msg.UserInfo) error {
 	log.Debug("User data saved to Redis for userId: %s", userData.UserId)
 	return nil
 }
-func InitUserTota() {
+
+func InitDb() {
+	InitUserTotal()
+	InitUserOnlineStatus()
+}
+func InitUserTotal() {
 	conn := GetPool().Get()
 	defer conn.Close()
 	count, err := usercenter.GetUserCount()
@@ -53,6 +58,10 @@ func InitUserTota() {
 	}
 }
 
+func InitUserOnlineStatus() {
+	usercenter.InitOnlineStatus()
+}
+
 // IncrementUserTotal 将UserTotal的值原子性加1,返回增加后的新值
 func IncrementUserTotal() (int64, error) {
 	conn := GetPool().Get()

+ 3 - 0
src/server/game/agentManager/agentManager.go

@@ -1,6 +1,7 @@
 package agentmanager
 
 import (
+	"server/common"
 	"time"
 
 	"github.com/name5566/leaf/gate"
@@ -46,6 +47,8 @@ func CloseAgent(args []interface{}) {
 	}
 	// 清理已认证连接
 	for userID, a := range authedUsers {
+		//设置为离线状态
+		common.UpdateUserOnlineStatus(0, userID)
 		if a == agent {
 			// a.SetUserData(&common.UserData{
 			// 	Id:       userID,

+ 13 - 5
src/server/hall/internal/handler.go

@@ -5,6 +5,7 @@ import (
 	redismgr "server/db/redis"
 	"server/hall/friends"
 	"server/hall/shop"
+	"server/user"
 
 	agentmanager "server/game/agentManager"
 	"server/game/ludo"
@@ -59,12 +60,16 @@ func enterHall(args []interface{}) {
 	}
 	agentmanager.AddAuthedUsers(m.UserId, a)
 	a.SetUserData(&msg.UserInfo{
-		UserId: user_info.UserId,
-		MHead:  user_info.MHead,
-		MCoin:  user_info.MCoin,
-		Name:   user_info.Name,
-		RoomId: user_info.RoomId,
+		UserId:       user_info.UserId,
+		MHead:        user_info.MHead,
+		MCoin:        user_info.MCoin,
+		Name:         user_info.Name,
+		RoomId:       user_info.RoomId,
+		OnlineStatus: 1,
 	})
+	//设置为在线状态
+	user.UpdateUserOnlineStatus(1, user_info.UserId)
+
 	room_id := user_info.RoomId
 	var user_room *msg.RoomInfo = nil
 	//如果玩家在某个游戏里,就进行重连
@@ -96,5 +101,8 @@ func enterHall(args []interface{}) {
 func leaveHall(args []interface{}) {
 	m := args[0].(*msg.LeaveHall)
 	a := args[1].(gate.Agent)
+
+	//设置为离线状态
+	user.UpdateUserOnlineStatus(1, m.UserId)
 	log.Debug("leaveHallHandler, msg: %v, agent: %v", m, a)
 }

+ 1 - 1
src/server/login/internal/handler.go

@@ -11,5 +11,5 @@ func handleMsg(m interface{}, h interface{}) {
 
 func init() {
 	print("login:handler")
-	redismgr.InitUserTota()
+	redismgr.InitDb()
 }

+ 12 - 3
src/server/msg/common.pb.go

@@ -1340,7 +1340,8 @@ type UserInfo struct {
 	MHead         string                 `protobuf:"bytes,2,opt,name=m_head,json=mHead,proto3" json:"m_head,omitempty"`
 	MCoin         int32                  `protobuf:"varint,3,opt,name=m_coin,json=mCoin,proto3" json:"m_coin,omitempty"`
 	Name          string                 `protobuf:"bytes,4,opt,name=name,proto3" json:"name,omitempty"`
-	RoomId        int32                  `protobuf:"varint,5,opt,name=room_id,json=roomId,proto3" json:"room_id,omitempty"` //如果用户在某一个房间玩,他的id就一直存在,
+	RoomId        int32                  `protobuf:"varint,5,opt,name=room_id,json=roomId,proto3" json:"room_id,omitempty"`                   //如果用户在某一个房间玩,他的id就一直存在,
+	OnlineStatus  int32                  `protobuf:"varint,6,opt,name=online_status,json=onlineStatus,proto3" json:"online_status,omitempty"` //在线=1 离线等0
 	unknownFields protoimpl.UnknownFields
 	sizeCache     protoimpl.SizeCache
 }
@@ -1410,6 +1411,13 @@ func (x *UserInfo) GetRoomId() int32 {
 	return 0
 }
 
+func (x *UserInfo) GetOnlineStatus() int32 {
+	if x != nil {
+		return x.OnlineStatus
+	}
+	return 0
+}
+
 // 登录 请求
 type ReqLogin struct {
 	state         protoimpl.MessageState `protogen:"open.v1"`
@@ -3498,13 +3506,14 @@ const file_common_proto_rawDesc = "" +
 	"\rfinish_colors\x18\x02 \x03(\v2\n" +
 	".ColorDataR\ffinishColors\">\n" +
 	"\x14NotifyUpdateRoomInfo\x12&\n" +
-	"\troom_info\x18\x01 \x01(\v2\t.RoomInfoR\broomInfo\"}\n" +
+	"\troom_info\x18\x01 \x01(\v2\t.RoomInfoR\broomInfo\"\xa2\x01\n" +
 	"\bUserInfo\x12\x16\n" +
 	"\x06UserId\x18\x01 \x01(\tR\x06UserId\x12\x15\n" +
 	"\x06m_head\x18\x02 \x01(\tR\x05mHead\x12\x15\n" +
 	"\x06m_coin\x18\x03 \x01(\x05R\x05mCoin\x12\x12\n" +
 	"\x04name\x18\x04 \x01(\tR\x04name\x12\x17\n" +
-	"\aroom_id\x18\x05 \x01(\x05R\x06roomId\"@\n" +
+	"\aroom_id\x18\x05 \x01(\x05R\x06roomId\x12#\n" +
+	"\ronline_status\x18\x06 \x01(\x05R\fonlineStatus\"@\n" +
 	"\bReqLogin\x12\x18\n" +
 	"\aaccount\x18\x01 \x01(\tR\aaccount\x12\x1a\n" +
 	"\bpassword\x18\x02 \x01(\tR\bpassword\"\x89\x01\n" +

+ 13 - 0
src/server/user/user.go

@@ -92,6 +92,19 @@ func AddUserCoin(coin int32, userId string) {
 	log.Debug("User %s's points updated: %d", userId, userData)
 }
 
+// 更新玩家在线状态
+func UpdateUserOnlineStatus(online_status int32, userId string) {
+	// 查找用户数据
+	userData := GetUserInfoById(userId)
+
+	userData.OnlineStatus = online_status
+	SetUserInfo(userData)
+
+	usercenter.UpdateOnlineStatusToDB(userData.UserId, int32(userData.OnlineStatus))
+	// 打印更新日志
+	log.Debug("User %s's points updated: %d", userId, userData)
+}
+
 // 清空房间
 func ClearUserRoomId(userId string) {
 	// 查找用户数据