Skip to content

Commit

Permalink
优化好感度系统,修复重复人名问题 (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
fangliuyu committed Jan 31, 2023
1 parent 39af90e commit faf00df
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
2 changes: 1 addition & 1 deletion plugin/qqwife/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var (
engine = control.Register("qqwife", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Brief: "一群一天一夫一妻制群老婆",
Help: "- 娶群友\n- 群老婆列表\n- [允许|禁止]自由恋爱\n- [允许|禁止]牛头人\n- 设置CD为xx小时 →(默认12小时)\n- 重置花名册\n- 重置所有花名册(用于清除所有群数据及其设置)\n- 查好感度[对方Q号|@对方QQ]\n- 好感度列表\n" +
Help: "- 娶群友\n- 群老婆列表\n- [允许|禁止]自由恋爱\n- [允许|禁止]牛头人\n- 设置CD为xx小时 →(默认12小时)\n- 重置花名册\n- 重置所有花名册(用于清除所有群数据及其设置)\n- 查好感度[对方Q号|@对方QQ]\n- 好感度列表\n- 好感度数据整理 (当好感动列表出现重复名字时使用)\n" +
"--------------------------------\n以下指令存在CD,不跨天刷新,前两个受指令开关\n--------------------------------\n" +
"- (娶|嫁)@对方QQ\n自由选择对象, 自由恋爱(好感度越高成功率越高,保底30%概率)\n" +
"- 当[对方Q号|@对方QQ]的小三\n我和你才是真爱, 为了你我愿意付出一切(好感度越高成功率越高,保底10%概率)\n" +
Expand Down
99 changes: 92 additions & 7 deletions plugin/qqwife/favorSystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,18 @@ func init() {
ctx.SendChain(message.Text("你钱包没钱啦!"))
return
}
moneyToFavor := rand.Intn(math.Min(walletinfo, 100))
moneyToFavor := rand.Intn(math.Min(walletinfo, 100)) + 1
// 计算钱对应的好感值
newFavor := 1
moodMax := 2
if favor > 50 {
newFavor = moneyToFavor % 10 // 礼物厌倦
} else {
moodMax = 5
newFavor += rand.Intn(moneyToFavor)
}
// 随机对方心情
mood := rand.Intn(2)
mood := rand.Intn(moodMax)
if mood == 0 {
newFavor = -newFavor
}
Expand Down Expand Up @@ -191,6 +193,68 @@ func init() {
ctx.SendChain(message.ImageBytes(data))
cl()
})

engine.OnFullMatch("好感度数据整理", zero.SuperUserPermission, getdb).SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) {
民政局.Lock()
defer 民政局.Unlock()
count, err := 民政局.db.Count("favorability")
if err != nil {
ctx.SendChain(message.Text("[ERROR]: ", err))
return
}
if count == 0 {
ctx.SendChain(message.Text("[ERROR]: 不存在好感动数据."))
return
}
favor := favorability{}
delInfo := make([]string, 0, count*2)
favorInfo := make(map[string]int, count*2)
_ = 民政局.db.FindFor("favorability", &favor, "group by Userinfo", func() error {
delInfo = append(delInfo, favor.Userinfo)
// 解析旧数据
userList := strings.Split(favor.Userinfo, "+")
if userList[0] > userList[1] {
favor.Userinfo = userList[0] + "+" + userList[1]
} else {
favor.Userinfo = userList[1] + "+" + userList[0]
}
// 判断是否是重复的
score, ok := favorInfo[favor.Userinfo]
if ok {
if score < favor.Favor {
favorInfo[favor.Userinfo] = favor.Favor
}
} else {
favorInfo[favor.Userinfo] = favor.Favor
}
return nil
})
for _, updateinfo := range delInfo {
// 删除旧数据
err = 民政局.db.Del("favorability", "where Userinfo = '"+updateinfo+"'")
if err != nil {
userList := strings.Split(favor.Userinfo, "+")
uid1, _ := strconv.ParseInt(userList[0], 10, 64)
uid2, _ := strconv.ParseInt(userList[1], 10, 64)
ctx.SendChain(message.Text("[ERROR]: 删除", ctx.CardOrNickName(uid1), "和", ctx.CardOrNickName(uid2), "的好感动时发生了错误。\n错误信息:", err))
}
}
for userInfo, favor := range favorInfo {
favorInfo := favorability{
Userinfo: userInfo,
Favor: favor,
}
err = 民政局.db.Insert("favorability", &favorInfo)
if err != nil {
userList := strings.Split(userInfo, "+")
uid1, _ := strconv.ParseInt(userList[0], 10, 64)
uid2, _ := strconv.ParseInt(userList[1], 10, 64)
ctx.SendChain(message.Text("[ERROR]: 更新", ctx.CardOrNickName(uid1), "和", ctx.CardOrNickName(uid2), "的好感动时发生了错误。\n错误信息:", err))
}
}
ctx.SendChain(message.Text("清理好了哦"))
})
}

func (sql *婚姻登记) 查好感度(uid, target int64) (int, error) {
Expand All @@ -201,9 +265,19 @@ func (sql *婚姻登记) 查好感度(uid, target int64) (int, error) {
return 0, err
}
info := favorability{}
uidstr := strconv.FormatInt(uid, 10)
targstr := strconv.FormatInt(target, 10)
_ = sql.db.Find("favorability", &info, "where Userinfo glob '*"+uidstr+"+"+targstr+"*'")
if uid > target {
userinfo := strconv.FormatInt(uid, 10) + "+" + strconv.FormatInt(target, 10)
err = sql.db.Find("favorability", &info, "where Userinfo is '"+userinfo+"'")
if err != nil {
_ = sql.db.Find("favorability", &info, "where Userinfo glob '*"+userinfo+"*'")
}
} else {
userinfo := strconv.FormatInt(target, 10) + "+" + strconv.FormatInt(uid, 10)
err = sql.db.Find("favorability", &info, "where Userinfo is '"+userinfo+"'")
if err != nil {
_ = sql.db.Find("favorability", &info, "where Userinfo glob '*"+userinfo+"*'")
}
}
return info.Favor, nil
}

Expand Down Expand Up @@ -256,8 +330,19 @@ func (sql *婚姻登记) 更新好感度(uid, target int64, score int) (favor in
info := favorability{}
uidstr := strconv.FormatInt(uid, 10)
targstr := strconv.FormatInt(target, 10)
_ = sql.db.Find("favorability", &info, "where Userinfo glob '*"+uidstr+"+"+targstr+"*'")
info.Userinfo = uidstr + "+" + targstr + "+" + uidstr
if uid > target {
info.Userinfo = uidstr + "+" + targstr
err = sql.db.Find("favorability", &info, "where Userinfo is '"+info.Userinfo+"'")
} else {
info.Userinfo = targstr + "+" + uidstr
err = sql.db.Find("favorability", &info, "where Userinfo is '"+info.Userinfo+"'")
}
if err != nil {
err = sql.db.Find("favorability", &info, "where Userinfo glob '*"+targstr+"+"+uidstr+"*'")
if err == nil { // 如果旧数据存在就删除旧数据
err = 民政局.db.Del("favorability", "where Userinfo = '"+info.Userinfo+"'")
}
}
info.Favor += score
if info.Favor > 100 {
info.Favor = 100
Expand Down

0 comments on commit faf00df

Please sign in to comment.