优化好感度系统,修复重复人名问题 (#568)

This commit is contained in:
方柳煜 2023-01-31 16:23:27 +08:00 committed by GitHub
parent 39af90e63d
commit faf00dfea7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 93 additions and 8 deletions

View File

@ -60,7 +60,7 @@ var (
engine = control.Register("qqwife", &ctrl.Options[*zero.Ctx]{ engine = control.Register("qqwife", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false, DisableOnDefault: false,
Brief: "一群一天一夫一妻制群老婆", 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" + "--------------------------------\n以下指令存在CD,不跨天刷新,前两个受指令开关\n--------------------------------\n" +
"- (娶|嫁)@对方QQ\n自由选择对象, 自由恋爱(好感度越高成功率越高,保底30%概率)\n" + "- (娶|嫁)@对方QQ\n自由选择对象, 自由恋爱(好感度越高成功率越高,保底30%概率)\n" +
"- 当[对方Q号|@对方QQ]的小三\n我和你才是真爱, 为了你我愿意付出一切(好感度越高成功率越高,保底10%概率)\n" + "- 当[对方Q号|@对方QQ]的小三\n我和你才是真爱, 为了你我愿意付出一切(好感度越高成功率越高,保底10%概率)\n" +

View File

@ -84,16 +84,18 @@ func init() {
ctx.SendChain(message.Text("你钱包没钱啦!")) ctx.SendChain(message.Text("你钱包没钱啦!"))
return return
} }
moneyToFavor := rand.Intn(math.Min(walletinfo, 100)) moneyToFavor := rand.Intn(math.Min(walletinfo, 100)) + 1
// 计算钱对应的好感值 // 计算钱对应的好感值
newFavor := 1 newFavor := 1
moodMax := 2
if favor > 50 { if favor > 50 {
newFavor = moneyToFavor % 10 // 礼物厌倦 newFavor = moneyToFavor % 10 // 礼物厌倦
} else { } else {
moodMax = 5
newFavor += rand.Intn(moneyToFavor) newFavor += rand.Intn(moneyToFavor)
} }
// 随机对方心情 // 随机对方心情
mood := rand.Intn(2) mood := rand.Intn(moodMax)
if mood == 0 { if mood == 0 {
newFavor = -newFavor newFavor = -newFavor
} }
@ -191,6 +193,68 @@ func init() {
ctx.SendChain(message.ImageBytes(data)) ctx.SendChain(message.ImageBytes(data))
cl() 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) { func (sql *婚姻登记) 查好感度(uid, target int64) (int, error) {
@ -201,9 +265,19 @@ func (sql *婚姻登记) 查好感度(uid, target int64) (int, error) {
return 0, err return 0, err
} }
info := favorability{} info := favorability{}
uidstr := strconv.FormatInt(uid, 10) if uid > target {
targstr := strconv.FormatInt(target, 10) userinfo := strconv.FormatInt(uid, 10) + "+" + strconv.FormatInt(target, 10)
_ = sql.db.Find("favorability", &info, "where Userinfo glob '*"+uidstr+"+"+targstr+"*'") 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 return info.Favor, nil
} }
@ -256,8 +330,19 @@ func (sql *婚姻登记) 更新好感度(uid, target int64, score int) (favor in
info := favorability{} info := favorability{}
uidstr := strconv.FormatInt(uid, 10) uidstr := strconv.FormatInt(uid, 10)
targstr := strconv.FormatInt(target, 10) targstr := strconv.FormatInt(target, 10)
_ = sql.db.Find("favorability", &info, "where Userinfo glob '*"+uidstr+"+"+targstr+"*'") if uid > target {
info.Userinfo = uidstr + "+" + targstr + "+" + uidstr 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 info.Favor += score
if info.Favor > 100 { if info.Favor > 100 {
info.Favor = 100 info.Favor = 100