xy 1 semana atrás
pai
commit
7851db0a22

+ 7 - 0
bin/client_msg/common.proto

@@ -259,6 +259,13 @@ message ResShop {
   repeated ShopItem list = 3;
 }
 
+
+message HistoryRecord{
+  int32 id = 1;
+  int32 room_id = 2;
+  string user_id = 3;
+}
+
 message BuyShopItem{
   string id = 1;
 }

+ 1 - 1
src/server/conf/conf.go

@@ -27,5 +27,5 @@ var (
 	MongoDBAddr = "mongodb://localhost:27017"
 	MongoDBName = "ludo_game"
 	RedisDBAddr = "localhost:6379"
-	MysqlDBAddr = "root:password@tcp(localhost:3306)/ludo_game?charset=utf8mb4&parseTime=True"
+	MysqlDBAddr = "root:4de12d49@tcp(localhost:3306)/ludo_game?charset=utf8mb4&parseTime=True"
 )

+ 4 - 2
src/server/game/ludo/battle.go

@@ -4,6 +4,7 @@ package ludo
 import (
 	"server/console"
 	"server/game"
+	"server/hall/history"
 	"server/msg"
 	"server/tools"
 	"server/user"
@@ -329,10 +330,10 @@ func (room_info *RoomInfoWrapper) kick_color_of_room(color msg.RoleType) {
 // 解散房间
 func (room_info *RoomInfoWrapper) dismiss_room() {
 	room_info.CancelCountdownPlayerOpt()
-	//将战绩给玩家存档
-
 	//移除房间
 	room_info.RoomStatus = msg.RoomStatus_END
+	//将战绩给玩家存档
+	history.AddHistory(room_info.RoomInfo)
 	for i := 0; i < len(room_info.Colors); i++ {
 		color_data := room_info.Colors[i]
 		user_agent := getAgentByUserId(color_data.MId)
@@ -341,6 +342,7 @@ func (room_info *RoomInfoWrapper) dismiss_room() {
 			ud.RoomId = 0
 			user_agent.SetUserData(ud)
 		}
+		user.ClearUserRoomId(color_data.MId)
 	}
 	ClearRoomInfoWrapperDisbandRoom()
 }

+ 114 - 0
src/server/hall/history/history.go

@@ -0,0 +1,114 @@
+package history
+
+import (
+	"database/sql"
+	"encoding/json"
+	"fmt"
+	mysqlmgr "server/db/mysql"
+	"server/msg"
+	"time"
+)
+
+func AddHistory(room_info *msg.RoomInfo) (int64, error) {
+	jsonData, err := json.Marshal(room_info)
+	if err != nil {
+		return 0, fmt.Errorf("failed to marshal room info: %v", err)
+	}
+
+	room_id, err := mysqlmgr.Insert("INSERT INTO ludo_roominfo (room_info) VALUES (?)", jsonData)
+	if err != nil {
+		return 0, fmt.Errorf("failed to insert room info: %v", err)
+	}
+	var userIDs []string = make([]string, 0, 4)
+
+	for _, v := range room_info.Colors {
+		userIDs = append(userIDs, v.MId)
+	}
+
+	err = addUsersHistoryToRoom(room_id, userIDs)
+	if err != nil {
+		return 0, fmt.Errorf("addUsersToRoom failed to insert room info: %v", err)
+	}
+
+	return room_id, nil
+}
+
+func addUsersHistoryToRoom(roomID int64, userIDs []string) error {
+	if len(userIDs) == 0 {
+		return nil
+	}
+
+	query := "INSERT INTO ludo_history (create_time, room_id, user_id) VALUES "
+	var args []interface{}
+	now := time.Now() // Same timestamp for all entries
+
+	for i, userID := range userIDs {
+		if i > 0 {
+			query += ", "
+		}
+		query += "(?, ?, ?)"
+		args = append(args, now, roomID, userID)
+	}
+
+	_, err := mysqlmgr.Insert(query, args...)
+	return err
+}
+
+type HistoryRecord struct {
+	ID         int       `db:"id"`
+	CreateTime time.Time `db:"create_time"`
+	RoomID     int       `db:"room_id"`
+	UserID     string    `db:"user_id"`
+}
+
+func GetHistoryByUserID(userID string) ([]*msg.HistoryRecord, error) {
+	rows, err := mysqlmgr.Query("SELECT id, create_time, room_id, user_id FROM ludo_history WHERE user_id = ?", userID)
+	if err != nil {
+		return nil, fmt.Errorf("query failed: %v", err)
+	}
+	defer rows.Close()
+
+	var results []*msg.HistoryRecord
+
+	for rows.Next() {
+		var record HistoryRecord
+		if err := rows.Scan(&record.ID, &record.CreateTime, &record.RoomID, &record.UserID); err != nil {
+			return nil, fmt.Errorf("failed to scan row: %v", err)
+		}
+		results = append(results, &msg.HistoryRecord{
+			Id:     int32(record.ID),
+			RoomId: int32(record.RoomID),
+			UserId: record.UserID,
+		})
+	}
+
+	if err := rows.Err(); err != nil {
+		return nil, fmt.Errorf("rows error: %v", err)
+	}
+
+	return results, nil
+}
+
+func GetRoomInfoByID(id int) (*msg.RoomInfo, error) {
+	// Query the database
+	row := mysqlmgr.QueryRow("SELECT room_info FROM ludo_roominfo WHERE id = ?", id)
+
+	// This will hold the raw JSON data from the database
+	var jsonData []byte
+
+	// Scan the result into jsonData
+	if err := row.Scan(&jsonData); err != nil {
+		if err == sql.ErrNoRows {
+			return nil, fmt.Errorf("room info not found for id %d", id)
+		}
+		return nil, fmt.Errorf("failed to scan room info: %v", err)
+	}
+
+	// Unmarshal the JSON into RoomInfo struct
+	var roomInfo msg.RoomInfo
+	if err := json.Unmarshal(jsonData, &roomInfo); err != nil {
+		return nil, fmt.Errorf("failed to unmarshal room info: %v", err)
+	}
+
+	return &roomInfo, nil
+}

+ 72 - 7
src/server/msg/common.pb.go

@@ -2332,6 +2332,66 @@ func (x *ResShop) GetList() []*ShopItem {
 	return nil
 }
 
+type HistoryRecord struct {
+	state         protoimpl.MessageState `protogen:"open.v1"`
+	Id            int32                  `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
+	RoomId        int32                  `protobuf:"varint,2,opt,name=room_id,json=roomId,proto3" json:"room_id,omitempty"`
+	UserId        string                 `protobuf:"bytes,3,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
+	unknownFields protoimpl.UnknownFields
+	sizeCache     protoimpl.SizeCache
+}
+
+func (x *HistoryRecord) Reset() {
+	*x = HistoryRecord{}
+	mi := &file_common_proto_msgTypes[32]
+	ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+	ms.StoreMessageInfo(mi)
+}
+
+func (x *HistoryRecord) String() string {
+	return protoimpl.X.MessageStringOf(x)
+}
+
+func (*HistoryRecord) ProtoMessage() {}
+
+func (x *HistoryRecord) ProtoReflect() protoreflect.Message {
+	mi := &file_common_proto_msgTypes[32]
+	if x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
+}
+
+// Deprecated: Use HistoryRecord.ProtoReflect.Descriptor instead.
+func (*HistoryRecord) Descriptor() ([]byte, []int) {
+	return file_common_proto_rawDescGZIP(), []int{32}
+}
+
+func (x *HistoryRecord) GetId() int32 {
+	if x != nil {
+		return x.Id
+	}
+	return 0
+}
+
+func (x *HistoryRecord) GetRoomId() int32 {
+	if x != nil {
+		return x.RoomId
+	}
+	return 0
+}
+
+func (x *HistoryRecord) GetUserId() string {
+	if x != nil {
+		return x.UserId
+	}
+	return ""
+}
+
 type BuyShopItem struct {
 	state         protoimpl.MessageState `protogen:"open.v1"`
 	Id            string                 `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
@@ -2341,7 +2401,7 @@ type BuyShopItem struct {
 
 func (x *BuyShopItem) Reset() {
 	*x = BuyShopItem{}
-	mi := &file_common_proto_msgTypes[32]
+	mi := &file_common_proto_msgTypes[33]
 	ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
 	ms.StoreMessageInfo(mi)
 }
@@ -2353,7 +2413,7 @@ func (x *BuyShopItem) String() string {
 func (*BuyShopItem) ProtoMessage() {}
 
 func (x *BuyShopItem) ProtoReflect() protoreflect.Message {
-	mi := &file_common_proto_msgTypes[32]
+	mi := &file_common_proto_msgTypes[33]
 	if x != nil {
 		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
 		if ms.LoadMessageInfo() == nil {
@@ -2366,7 +2426,7 @@ func (x *BuyShopItem) ProtoReflect() protoreflect.Message {
 
 // Deprecated: Use BuyShopItem.ProtoReflect.Descriptor instead.
 func (*BuyShopItem) Descriptor() ([]byte, []int) {
-	return file_common_proto_rawDescGZIP(), []int{32}
+	return file_common_proto_rawDescGZIP(), []int{33}
 }
 
 func (x *BuyShopItem) GetId() string {
@@ -2526,7 +2586,11 @@ const file_common_proto_rawDesc = "" +
 	"\aResShop\x12\x18\n" +
 	"\asuccess\x18\x01 \x01(\bR\asuccess\x12\"\n" +
 	"\aerr_msg\x18\x02 \x01(\v2\t.MsgErrorR\x06errMsg\x12\x1d\n" +
-	"\x04list\x18\x03 \x03(\v2\t.ShopItemR\x04list\"\x1d\n" +
+	"\x04list\x18\x03 \x03(\v2\t.ShopItemR\x04list\"Q\n" +
+	"\rHistoryRecord\x12\x0e\n" +
+	"\x02id\x18\x01 \x01(\x05R\x02id\x12\x17\n" +
+	"\aroom_id\x18\x02 \x01(\x05R\x06roomId\x12\x17\n" +
+	"\auser_id\x18\x03 \x01(\tR\x06userId\"\x1d\n" +
 	"\vBuyShopItem\x12\x0e\n" +
 	"\x02id\x18\x01 \x01(\tR\x02id*K\n" +
 	"\broleType\x12\x15\n" +
@@ -2579,7 +2643,7 @@ func file_common_proto_rawDescGZIP() []byte {
 }
 
 var file_common_proto_enumTypes = make([]protoimpl.EnumInfo, 7)
-var file_common_proto_msgTypes = make([]protoimpl.MessageInfo, 33)
+var file_common_proto_msgTypes = make([]protoimpl.MessageInfo, 34)
 var file_common_proto_goTypes = []any{
 	(RoleType)(0),                // 0: roleType
 	(OptType)(0),                 // 1: OptType
@@ -2620,7 +2684,8 @@ var file_common_proto_goTypes = []any{
 	(*BeKickLogin)(nil),          // 36: BeKickLogin
 	(*ReqShop)(nil),              // 37: ReqShop
 	(*ResShop)(nil),              // 38: ResShop
-	(*BuyShopItem)(nil),          // 39: BuyShopItem
+	(*HistoryRecord)(nil),        // 39: HistoryRecord
+	(*BuyShopItem)(nil),          // 40: BuyShopItem
 }
 var file_common_proto_depIdxs = []int32{
 	0,  // 0: round.m_color:type_name -> roleType
@@ -2687,7 +2752,7 @@ func file_common_proto_init() {
 			GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
 			RawDescriptor: unsafe.Slice(unsafe.StringData(file_common_proto_rawDesc), len(file_common_proto_rawDesc)),
 			NumEnums:      7,
-			NumMessages:   33,
+			NumMessages:   34,
 			NumExtensions: 0,
 			NumServices:   0,
 		},

+ 1 - 0
src/server/msg/msg.go

@@ -46,6 +46,7 @@ func init() {
 	Processor.Register(&ReqShop{})
 	Processor.Register(&ResShop{})
 	Processor.Register(&BuyShopItem{})
+	Processor.Register(&HistoryRecord{})
 	Processor.Range(func(id uint16, t reflect.Type) {
 		log.Debug("消息ID: %d, 消息类型: %s\n", id, t.Elem().Name())
 		msgList = append(msgList, MsgInfo{

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

@@ -88,3 +88,15 @@ func AddUserCoin(coin int32, userId string) {
 	// 打印更新日志
 	log.Debug("User %s's points updated: %d", userId, userData)
 }
+
+// 清空房间
+func ClearUserRoomId(userId string) {
+	// 查找用户数据
+	userData := GetUserInfoById(userId)
+
+	// 更新用户的金币
+	userData.RoomId = 0
+	SetUserInfo(userData)
+	// 打印更新日志
+	log.Debug("User %s's points updated: %d", userId, userData)
+}