***
This commit is contained in:
1 parent
688213e39f
commit
7e40b34df5
3 files changed
+158
-17
No files matched your search
+156
-5
@@ -1,7 +1,11 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"hk4e/gs/model"
|
||||
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type PlayerGorm struct {
|
||||
@@ -14,7 +18,7 @@ func (p PlayerGorm) TableName() string {
|
||||
}
|
||||
|
||||
type ChatMsgGorm struct {
|
||||
ID uint32 `gorm:"column:id;type:bigint(20);primaryKey"`
|
||||
ID uint32 `gorm:"column:id;type:bigint(20);primaryKey;autoIncrement"`
|
||||
Sequence uint32 `gorm:"column:sequence;type:bigint(20)"`
|
||||
Time uint32 `gorm:"column:time;type:bigint(20)"`
|
||||
Uid uint32 `gorm:"column:uid;type:bigint(20)"`
|
||||
@@ -41,61 +45,208 @@ func (s SceneBlockGorm) TableName() string {
|
||||
}
|
||||
|
||||
func (d *Dao) InsertPlayerGorm(player *model.Player) error {
|
||||
data, err := msgpack.Marshal(player)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = d.gormDb.Create(&PlayerGorm{
|
||||
Uid: player.PlayerId,
|
||||
Data: data,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) InsertPlayerListGorm(playerList []*model.Player) error {
|
||||
for _, player := range playerList {
|
||||
err := d.InsertPlayerGorm(player)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) DeletePlayerGorm(playerId uint32) error {
|
||||
d.gormDb.Where("uid = ?", playerId).Delete(&PlayerGorm{})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) DeletePlayerListGorm(playerIdList []uint32) error {
|
||||
for _, playerId := range playerIdList {
|
||||
err := d.DeletePlayerGorm(playerId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) UpdatePlayerGorm(player *model.Player) error {
|
||||
data, err := msgpack.Marshal(player)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = d.gormDb.Updates(&PlayerGorm{
|
||||
Uid: player.PlayerId,
|
||||
Data: data,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) UpdatePlayerListGorm(playerList []*model.Player) error {
|
||||
for _, player := range playerList {
|
||||
err := d.UpdatePlayerGorm(player)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) QueryPlayerByIdGorm(playerId uint32) (*model.Player, error) {
|
||||
return nil, nil
|
||||
playerGorm := new(PlayerGorm)
|
||||
err := d.gormDb.Where("uid = ?", playerId).First(playerGorm).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
player := new(model.Player)
|
||||
err = msgpack.Unmarshal(playerGorm.Data, player)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return player, nil
|
||||
}
|
||||
|
||||
func (d *Dao) QueryPlayerListGorm() ([]*model.Player, error) {
|
||||
return nil, nil
|
||||
var playerGormList []*PlayerGorm = nil
|
||||
err := d.gormDb.Find(&playerGormList).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
playerList := make([]*model.Player, 0)
|
||||
for _, playerGorm := range playerGormList {
|
||||
player := new(model.Player)
|
||||
err = msgpack.Unmarshal(playerGorm.Data, player)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
playerList = append(playerList, player)
|
||||
}
|
||||
return playerList, nil
|
||||
}
|
||||
|
||||
func (d *Dao) InsertChatMsgGorm(chatMsg *model.ChatMsg) error {
|
||||
err := d.gormDb.Create(&ChatMsgGorm{
|
||||
Sequence: chatMsg.Sequence,
|
||||
Time: chatMsg.Time,
|
||||
Uid: chatMsg.Uid,
|
||||
ToUid: chatMsg.ToUid,
|
||||
IsRead: chatMsg.IsRead,
|
||||
MsgType: chatMsg.MsgType,
|
||||
Text: chatMsg.Text,
|
||||
Icon: chatMsg.Icon,
|
||||
IsDelete: chatMsg.IsDelete,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) DeleteUpdateChatMsgByUidGorm(uid uint32) error {
|
||||
err := d.gormDb.Model(&ChatMsgGorm{}).Where("to_uid = ? or uid = ?", uid, uid).Update("is_delete", true).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) UpdateChatMsgByUidAndToUidActionReadGorm(uid uint32, toUid uint32) error {
|
||||
err := d.gormDb.Model(&ChatMsgGorm{}).Where("to_uid = ? and uid = ?", uid, toUid).Update("is_read", true).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) QueryChatMsgListByUidGorm(uid uint32) ([]*model.ChatMsg, error) {
|
||||
return nil, nil
|
||||
var chatMsgGormList []*ChatMsgGorm = nil
|
||||
err := d.gormDb.Where("to_uid = ? or uid = ? and is_delete = ?", uid, uid, false).Find(&chatMsgGormList).
|
||||
Order("time DESC").Limit(MaxQueryChatMsgLen).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
chatMsgList := make([]*model.ChatMsg, 0)
|
||||
for _, chatMsgGorm := range chatMsgGormList {
|
||||
chatMsgList = append(chatMsgList, &model.ChatMsg{
|
||||
Sequence: chatMsgGorm.Sequence,
|
||||
Time: chatMsgGorm.Time,
|
||||
Uid: chatMsgGorm.Uid,
|
||||
ToUid: chatMsgGorm.ToUid,
|
||||
IsRead: chatMsgGorm.IsRead,
|
||||
MsgType: chatMsgGorm.MsgType,
|
||||
Text: chatMsgGorm.Text,
|
||||
Icon: chatMsgGorm.Icon,
|
||||
IsDelete: chatMsgGorm.IsDelete,
|
||||
})
|
||||
}
|
||||
return chatMsgList, nil
|
||||
}
|
||||
|
||||
func (d *Dao) InsertSceneBlockGorm(sceneBlock *model.SceneBlock) error {
|
||||
data, err := msgpack.Marshal(sceneBlock)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = d.gormDb.Create(&SceneBlockGorm{
|
||||
Uid: sceneBlock.Uid,
|
||||
BlockId: sceneBlock.BlockId,
|
||||
Data: data,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) UpdateSceneBlockGorm(sceneBlock *model.SceneBlock) error {
|
||||
data, err := msgpack.Marshal(sceneBlock)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = d.gormDb.Updates(&SceneBlockGorm{
|
||||
Uid: sceneBlock.Uid,
|
||||
BlockId: sceneBlock.BlockId,
|
||||
Data: data,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) QuerySceneBlockByUidAndBlockIdGorm(uid uint32, blockId uint32) (*model.SceneBlock, error) {
|
||||
return nil, nil
|
||||
sceneBlockGorm := new(SceneBlockGorm)
|
||||
err := d.gormDb.Where("uid = ? and block_id = ?", uid, blockId).First(sceneBlockGorm).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
sceneBlock := new(model.SceneBlock)
|
||||
err = msgpack.Unmarshal(sceneBlockGorm.Data, sceneBlock)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sceneBlock, nil
|
||||
}
|
||||
@@ -201,14 +201,6 @@ func (d *Dao) UpdateChatMsgByUidAndToUidActionRead(uid uint32, toUid uint32) err
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = db.UpdateMany(
|
||||
context.TODO(),
|
||||
bson.D{{"uid", uid}, {"to_uid", toUid}},
|
||||
bson.D{{"$set", bson.D{{"is_read", true}}}},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -226,8 +218,8 @@ func (d *Dao) QueryChatMsgListByUid(uid uint32) ([]*model.ChatMsg, error) {
|
||||
{{"is_delete", false}},
|
||||
}},
|
||||
},
|
||||
options.Find().SetSort(bson.M{"time": -1}),
|
||||
options.Find().SetLimit(MaxQueryChatMsgLen),
|
||||
options.Find().SetSort(bson.M{"time": 1}),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -122,11 +122,9 @@ func (g *Game) ReadPrivateChatReq(player *model.Player, payloadMsg pb.Message) {
|
||||
if !exist {
|
||||
return
|
||||
}
|
||||
for index, chatMsg := range msgList {
|
||||
for _, chatMsg := range msgList {
|
||||
chatMsg.IsRead = true
|
||||
msgList[index] = chatMsg
|
||||
}
|
||||
player.ChatMsgMap[targetUid] = msgList
|
||||
|
||||
// 更新db
|
||||
USER_MANAGER.AsyncWriteDb(func(u *UserManager) {
|
||||
|
||||
Reference in new issue
Block a user