|
| 1 | +// Package robbery 打劫群友 基于“qqwife”插件魔改 |
| 2 | +package robbery |
| 3 | + |
| 4 | +import ( |
| 5 | + "math/rand" |
| 6 | + "strconv" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + fcext "github.com/FloatTech/floatbox/ctxext" |
| 11 | + sql "github.com/FloatTech/sqlite" |
| 12 | + ctrl "github.com/FloatTech/zbpctrl" |
| 13 | + "github.com/FloatTech/zbputils/control" |
| 14 | + "github.com/wdvxdr1123/ZeroBot/extension/single" |
| 15 | + |
| 16 | + "github.com/FloatTech/AnimeAPI/wallet" |
| 17 | + "github.com/FloatTech/floatbox/math" |
| 18 | + "github.com/FloatTech/zbputils/ctxext" |
| 19 | + zero "github.com/wdvxdr1123/ZeroBot" |
| 20 | + "github.com/wdvxdr1123/ZeroBot/message" |
| 21 | +) |
| 22 | + |
| 23 | +type robberyRepo struct { |
| 24 | + db *sql.Sqlite |
| 25 | + sync.RWMutex |
| 26 | +} |
| 27 | + |
| 28 | +type robberyRecord struct { |
| 29 | + UserID int64 `db:"user_id"` // 劫匪 |
| 30 | + VictimID int64 `db:"victim_id"` // 受害者 |
| 31 | + Time string `db:"time"` // 时间 |
| 32 | +} |
| 33 | + |
| 34 | +func init() { |
| 35 | + police := &robberyRepo{ |
| 36 | + db: &sql.Sqlite{}, |
| 37 | + } |
| 38 | + engine := control.AutoRegister(&ctrl.Options[*zero.Ctx]{ |
| 39 | + DisableOnDefault: false, |
| 40 | + Brief: "打劫别人的ATRI币", |
| 41 | + Help: "- 打劫[对方Q号|@对方QQ]\n" + |
| 42 | + "1. 受害者钱包少于1000不能被打劫\n" + |
| 43 | + "2. 打劫成功率 40%\n" + |
| 44 | + "4. 打劫失败罚款1000(钱不够不罚钱)\n" + |
| 45 | + "5. 保险赔付0-80%\n" + |
| 46 | + "6. 打劫成功获得对方0-5%+500的财产(最高1W)\n" + |
| 47 | + "7. 每日可打劫或被打劫一次\n" + |
| 48 | + "8. 打劫失败不计入次数\n", |
| 49 | + PrivateDataFolder: "robbery", |
| 50 | + }).ApplySingle(single.New( |
| 51 | + single.WithKeyFn(func(ctx *zero.Ctx) int64 { return ctx.Event.GroupID }), |
| 52 | + single.WithPostFn[int64](func(ctx *zero.Ctx) { |
| 53 | + ctx.Send( |
| 54 | + message.ReplyWithMessage(ctx.Event.MessageID, |
| 55 | + message.Text("别着急,警察局门口排长队了!"), |
| 56 | + ), |
| 57 | + ) |
| 58 | + }), |
| 59 | + )) |
| 60 | + getdb := fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool { |
| 61 | + police.db.DBPath = engine.DataFolder() + "robbery.db" |
| 62 | + err := police.db.Open(time.Hour) |
| 63 | + if err == nil { |
| 64 | + // 创建CD表 |
| 65 | + err = police.db.Create("criminal_record", &robberyRecord{}) |
| 66 | + if err != nil { |
| 67 | + ctx.SendChain(message.Text("[ERROR]:", err)) |
| 68 | + return false |
| 69 | + } |
| 70 | + return true |
| 71 | + } |
| 72 | + ctx.SendChain(message.Text("[ERROR]:", err)) |
| 73 | + return false |
| 74 | + }) |
| 75 | + |
| 76 | + // 打劫功能 |
| 77 | + engine.OnRegex(`^打劫\s?(\[CQ:at,qq=(\d+)\]|(\d+))`, getdb).SetBlock(true).Limit(ctxext.LimitByUser). |
| 78 | + Handle(func(ctx *zero.Ctx) { |
| 79 | + uid := ctx.Event.UserID |
| 80 | + fiancee := ctx.State["regex_matched"].([]string) |
| 81 | + victimID, _ := strconv.ParseInt(fiancee[2]+fiancee[3], 10, 64) |
| 82 | + if victimID == uid { |
| 83 | + ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, message.At(uid), message.Text("不能打劫自己"))) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + // 查询记录 |
| 88 | + ok, err := police.getRecord(victimID, uid) |
| 89 | + if err != nil { |
| 90 | + ctx.SendChain(message.Text("[ERROR]:", err)) |
| 91 | + return |
| 92 | + } |
| 93 | + if !ok { |
| 94 | + ctx.SendChain(message.Text("你已经打劫过了/对方已经被打劫过了")) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + // 穷人保护 |
| 99 | + victimWallet := wallet.GetWalletOf(victimID) |
| 100 | + if victimWallet < 1000 { |
| 101 | + ctx.SendChain(message.Text("对方太穷了!打劫失败")) |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + // 判断打劫是否成功 |
| 106 | + if rand.Intn(100) > 60 { |
| 107 | + ctx.SendChain(message.Text("打劫失败,罚款1000")) |
| 108 | + err := wallet.InsertWalletOf(uid, -1000) |
| 109 | + if err != nil { |
| 110 | + ctx.SendChain(message.Text("[ERROR]:罚款失败,钱包坏掉力:\n", err)) |
| 111 | + return |
| 112 | + } |
| 113 | + return |
| 114 | + } |
| 115 | + userIncrMonry := math.Min(rand.Intn(victimWallet/20)+500, 10000) |
| 116 | + victimDecrMonry := userIncrMonry / (rand.Intn(4) + 1) |
| 117 | + |
| 118 | + // 记录结果 |
| 119 | + err = wallet.InsertWalletOf(victimID, -victimDecrMonry) |
| 120 | + if err != nil { |
| 121 | + ctx.SendChain(message.Text("[ERROR]:钱包坏掉力:\n", err)) |
| 122 | + return |
| 123 | + } |
| 124 | + err = wallet.InsertWalletOf(uid, +userIncrMonry) |
| 125 | + if err != nil { |
| 126 | + ctx.SendChain(message.Text("[ERROR]:打劫失败,脏款掉入虚无\n", err)) |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + // 写入记录 |
| 131 | + err = police.insertRecord(victimID, uid) |
| 132 | + if err != nil { |
| 133 | + ctx.SendChain(message.At(uid), message.Text("[ERROR]:犯罪记录写入失败\n", err)) |
| 134 | + } |
| 135 | + |
| 136 | + ctx.SendChain(message.At(uid), message.Text("打劫成功,钱包增加:", userIncrMonry, "ATRI币")) |
| 137 | + ctx.SendChain(message.At(victimID), message.Text("保险公司对您进行了赔付,您实际损失:", victimDecrMonry, "ATRI币")) |
| 138 | + }) |
| 139 | +} |
| 140 | + |
| 141 | +func (sql *robberyRepo) getRecord(victimID, uid int64) (ok bool, err error) { |
| 142 | + sql.Lock() |
| 143 | + defer sql.Unlock() |
| 144 | + // 创建群表格 |
| 145 | + err = sql.db.Create("criminal_record", &robberyRecord{}) |
| 146 | + if err != nil { |
| 147 | + return false, err |
| 148 | + } |
| 149 | + limitID := "where victim_id is " + strconv.FormatInt(victimID, 10) + |
| 150 | + " or user_id is " + strconv.FormatInt(uid, 10) |
| 151 | + if !sql.db.CanFind("criminal_record", limitID) { |
| 152 | + // 没有记录即不用比较 |
| 153 | + return true, nil |
| 154 | + } |
| 155 | + cdinfo := robberyRecord{} |
| 156 | + _ = sql.db.Find("criminal_record", &cdinfo, limitID) |
| 157 | + if time.Now().Format("2006/01/02") != cdinfo.Time { |
| 158 | + // // 如果跨天了就删除 |
| 159 | + err = sql.db.Del("criminal_record", limitID) |
| 160 | + return true, err |
| 161 | + } |
| 162 | + return false, nil |
| 163 | +} |
| 164 | + |
| 165 | +func (sql *robberyRepo) insertRecord(vid int64, uid int64) error { |
| 166 | + sql.Lock() |
| 167 | + defer sql.Unlock() |
| 168 | + return sql.db.Insert("criminal_record", &robberyRecord{ |
| 169 | + UserID: uid, |
| 170 | + VictimID: vid, |
| 171 | + Time: time.Now().Format("2006/01/02"), |
| 172 | + }) |
| 173 | +} |
0 commit comments