forked from KarisAya/nonebot_plugin_game_collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
975 lines (831 loc) · 31.2 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
from nonebot.adapters.onebot.v11 import (
GROUP,
GROUP_ADMIN,
GROUP_OWNER,
Bot,
MessageEvent,
GroupMessageEvent,
Message,
)
from nonebot import get_driver
from nonebot.permission import SUPERUSER
from nonebot import on_message, on_command, on_regex, on_fullmatch
from nonebot.params import CommandArg, Arg
from nonebot.typing import T_State
from nonebot.matcher import Matcher
from nonebot.rule import to_me
from nonebot import require
from nonebot.log import logger
try:
import ujson as json
except ModuleNotFoundError:
import json
import random
import time
import datetime
import re
import shutil
from . import Manager
from . import Account
from . import Market
from . import Game
from . import Prop
from . import Alchemy
from .HorseRace.start import load_dlcs
from .utils.utils import get_message_at, number
from .data import ExchangeInfo,menu_data
from .config import Config, revolt_cd, bet_gold, path, backup
from nonebot.plugin import PluginMetadata
__plugin_meta__ = PluginMetadata(
name = "小游戏合集",
description = "各种群内小游戏",
usage = "金币签到",
config = Config,
extra = {'menu_data':menu_data,'menu_template':'default'})
data = Manager.data
company_index = Manager.company_index
current_games = Game.current_games
def to_int(arg:Message, default:int = bet_gold):
num = arg.extract_plain_text().strip()
if num.isdigit():
return int(num)
else:
return default
RaceReload = on_command("赛马事件重载", priority=5, permission=SUPERUSER, block=True)
@RaceReload.handle()
async def _():
events_list, logs = load_dlcs()
out = '\n'.join(logs)
await RaceReload.finish(out)
AllGameTips = {
"HorseRace":"赛马活动未开始,请输入【赛马创建】创建赛马场",
"Fortress":"要塞战未开始,请输入【要塞战创建】创建要塞战游戏",
}
# 加入游戏
AllJoinGameCommand = {
"HorseRace":{"赛马加入","加入赛马"},
"Fortress":{"要塞加入","堡垒加入","加入要塞","加入堡垒"},
}
AllJoinGameCommand = {cmd: name for name, cmds in AllJoinGameCommand.items() for cmd in cmds}
async def join_game_rule(bot:Bot, event:GroupMessageEvent, state:T_State) -> bool:
"""
规则:加入游戏
"""
msg = event.message.extract_plain_text()
for cmd in AllJoinGameCommand:
if msg.startswith(cmd):
game = current_games.get(event.group_id)
name = AllJoinGameCommand[cmd]
if game:
if game.name == name:
state["Game"] = game
state["arg"] = msg[len(cmd):].strip()
return True
return False
else:
await bot.send(event,AllGameTips[name])
return False
else:
return False
join_game = on_message(rule = join_game_rule, permission = GROUP, priority = 20, block = True)
@join_game.handle()
async def _(event:GroupMessageEvent, state:T_State):
game = state["Game"]
msg = game.join(event,state["arg"])
await join_game.finish(msg)
# 开始游戏
AllRunGameCommand = {
"HorseRace":{"赛马开始","开始赛马"},
"Fortress":{"游戏开始","开始游戏"},
}
AllRunGameCommand = {cmd: name for name, cmds in AllRunGameCommand.items() for cmd in cmds}
async def run_game_rule(bot:Bot, event:GroupMessageEvent, state:T_State) -> bool:
"""
规则:开始游戏
"""
msg = event.message.extract_plain_text()
for cmd in AllRunGameCommand:
if msg.startswith(cmd):
game = current_games.get(event.group_id)
name = AllRunGameCommand[cmd]
if game:
if game.name == name:
state["Game"] = game
return True
return False
else:
await bot.send(event,AllGameTips[name])
return False
else:
return False
async def AROF_check(event:GroupMessageEvent, state:T_State):
"""
本群有AROF
"""
group_id = event.group_id
if group_id in current_games and current_games[group_id].name in {"Fortress"}:
state["game"] = current_games[group_id]
return True
else:
return False
# 回合开始
AROF_start = on_command("回合开始", rule = AROF_check, priority = 20, block = True)
@AROF_start.handle()
async def _(bot:Bot, event:GroupMessageEvent):
game = current_games[event.group_id]
msg = await game.AROF_start(bot,event.user_id)
await AROF_start.finish(msg)
# 回合结束
AROF_end = on_command("回合结束", rule = AROF_check, priority = 20, block = True)
@AROF_end.handle()
async def _(bot:Bot, event:GroupMessageEvent):
game = current_games[event.group_id]
msg = await game.AROF_end(bot,event.user_id)
await AROF_end.finish(msg)
# 行动
AROF_action = on_command("行动", rule = AROF_check, priority = 20, block = True)
@AROF_action.handle()
async def _(bot:Bot, event:GroupMessageEvent, arg:Message = CommandArg()):
game = current_games[event.group_id]
msg = await game.AROF_action(bot,event.user_id,*arg.extract_plain_text().strip().split())
await AROF_action.finish(msg)
run_game = on_message(rule = run_game_rule, permission = GROUP, priority = 20, block = True)
@run_game.handle()
async def _(bot:Bot, state:T_State):
game = state["Game"]
msg = await game.run(bot)
await run_game.finish(msg)
# 赛马暂停
RaceStop = on_command(
"赛马暂停",
aliases = {"暂停赛马"},
rule = lambda event:isinstance(event,GroupMessageEvent) and event.group_id in current_games and current_games[event.group_id].name == "HorseRace",
permission = SUPERUSER | GROUP_ADMIN | GROUP_OWNER,
priority = 20,
block = True
)
@RaceStop.handle()
async def _(event:GroupMessageEvent):
current_games[event.group_id].race_group.start = 2
# GameClear
GameClear = on_command(
"GameClear",
aliases = {"清除游戏", "清除对局", "清除对决", "清除对战"},
rule = lambda event:isinstance(event,GroupMessageEvent) and event.group_id in current_games,
permission = SUPERUSER | GROUP_ADMIN | GROUP_OWNER,
priority = 20,
block = True
)
@GameClear.handle()
async def _(event:GroupMessageEvent):
del current_games[event.group_id]
# 获取金币
gold_create = on_command("获取金币", permission = SUPERUSER, priority = 20, block = True)
@gold_create.handle()
async def _(event:MessageEvent, arg:Message = CommandArg()):
gold = to_int(arg,bet_gold)
msg = Account.gold_create(event,gold)
await gold_create.finish(msg, at_sender=True)
# 获取道具
props_create = on_command("获取道具", permission = SUPERUSER, priority = 20, block = True)
@props_create.handle()
async def _(event:MessageEvent, arg:Message = CommandArg(),):
arg = arg.extract_plain_text().strip().split()
test = len(arg)
if test == 1:
prop_name = arg[0]
count = 1
elif test >1:
prop_name = arg[0]
count = number(arg[1])
if count < 1:
count = 1
else:
return
msg = Account.props_create(event, prop_name, count)
await give_props.finish(msg, at_sender = True)
# 金币签到
sign = on_command("金币签到", aliases = {"轮盘签到"}, priority = 20, block = True)
@sign.handle()
async def _(event:MessageEvent):
msg = Account.sign(event)
await sign.finish(msg, at_sender = True)
# 重置签到
revolt_sign = on_command("重置签到", aliases = {"revolt签到"}, priority = 20, block = True)
@revolt_sign.handle()
async def _(event:MessageEvent):
msg = Account.revolt_sign(event)
await revolt_sign.finish(msg, at_sender = True)
# 发动革命
from .config import revolt_cd
if revolt_cd:
revolution = on_command("发起重置", permission = GROUP, priority = 20, block = True)
@revolution.handle()
async def _(event:GroupMessageEvent):
msg = Account.revolution(event.group_id)
await revolution.finish(msg)
else:
logger.info("重置已被禁用")
# 发红包
give_gold = on_command("打钱", aliases = {"发红包", "赠送金币"}, permission = GROUP, priority = 20, block = True)
@give_gold.handle()
async def _(bot:Bot, event:GroupMessageEvent, arg:Message = CommandArg()):
gold = arg.extract_plain_text().strip()
if gold.isdigit():
gold = int(gold)
else:
return
if at := get_message_at(event.message):
at = int(at[0])
else:
return
target = Manager.locate_user_at(event, at)
msg = Account.transfer_gold(event, target, gold)
await give_gold.finish(msg, at_sender = True)
# 送道具
give_props = on_command("送道具", aliases = {"赠送道具"}, permission = GROUP, priority = 20, block = True)
@give_props.handle()
async def _(event:GroupMessageEvent, arg:Message = CommandArg(),):
arg = arg.extract_plain_text().strip().split()
at = get_message_at(event.message)
test = len(arg)
if test and at:
at = at[0]
if test == 1:
prop_name = arg[0]
count = 1
elif test >1:
prop_name = arg[0]
count = number(arg[1])
if count < 1:
count = 1
else:
return
target = Manager.locate_user_at(event, int(at))
msg = Account.transfer_props(event, target, prop_name, count)
await give_props.finish(msg, at_sender = True)
from .Game import current_games
# 创建游戏
AllCreateGameCommand = {
"Russian":{"俄罗斯轮盘","装弹"},
"Dice":{"掷色子", "摇色子", "掷骰子", "摇骰子"},
"Poker":{"扑克对战", "扑克对决", "扑克决斗"},
"LuckyNumber":{"猜数字"},
"Cantrell":{"同花顺","港式五张","梭哈"},
"Blackjack":{"21点"},
"ABCard":{"AB牌","ab牌"},
"GunFight":{"西部枪战","西部对战","牛仔对战","牛仔对决"},
"HorseRace":{"赛马创建","创建赛马"},
"Fortress":{"堡垒战创建","要塞战创建","创建堡垒战","创建要塞战"},
}
AllCreateGameCommand = {cmd: name for name, cmds in AllCreateGameCommand.items() for cmd in cmds}
AllGames = {
"Russian":Game.Russian,
"Dice":Game.Dice,
"Poker":Game.Poker,
"LuckyNumber":Game.LuckyNumber,
"Cantrell":Game.Cantrell,
"Blackjack":Game.Blackjack,
"ABCard":Game.ABCard,
"GunFight":Game.GunFight,
"HorseRace":Game.HorseRace,
"Fortress":Game.Fortress,
}
def create_game_rule(event:GroupMessageEvent, state:T_State)-> bool:
"""
规则:创建对局
"""
msg = event.message.extract_plain_text()
for cmd in AllCreateGameCommand:
if msg.startswith(cmd):
state["Game"] = AllGames[AllCreateGameCommand[cmd]]
state["arg"] = msg[len(cmd):].strip()
return True
else:
return False
create_game = on_message(rule = create_game_rule, permission = GROUP, priority = 20, block = True)
@create_game.handle()
async def _(event:GroupMessageEvent, state:T_State):
game = state["Game"]
kwargs = game.parse_arg(state["arg"])
msg = game.creat(event,**kwargs)
await create_game.finish(msg)
# 进行游戏
AllPlayGameCommand = {
"Russian":{"开枪","咔", "嘭", "嘣"},
"Dice":{"取出","开数", "开点"},
"Poker":{"出牌"},
"LuckyNumber":{"^\d{1,3}$"},
"Cantrell":{"看牌","加注","跟注","开牌"},
"Blackjack":{"停牌","抽牌","双倍下注"},
"ABCard":{"A","a","B","b","1","2","3"},
"GunFight":{"装弹","开枪","闪避","闪枪","预判开枪"},
}
def game_play_rule(event:MessageEvent, state:T_State)-> bool:
"""
规则:游戏进行
"""
user,group_account = Manager.locate_user(event)
if not group_account:
return False
group_id = user.connect
game = current_games.get(group_id)
if game and (Name := game.name) not in {"HorseRace"}:
cmdlst = AllPlayGameCommand.get(Name)
if not cmdlst:
return False
msg = event.message.extract_plain_text()
state["game"] = game
if Name == "LuckyNumber":
if msg.isdigit() and 0 < (N := int(msg)) <= 100:
state["arg"] = (N,)
return True
elif Name in {"Blackjack","ABCard","GunFight"}:
if msg in cmdlst:
state["arg"] = (msg,)
return True
else:
for cmd in cmdlst:
if msg.startswith(cmd):
msg = msg[len(cmd):].strip()
if Name == "Cantrell":
if msg.isdigit():
gold = int(msg)
else:
gold = None
state["arg"] = ({"看牌":0,"加注":1,"跟注":1,"开牌":1}[cmd], gold)
elif msg.isdigit():
state["arg"] = (int(msg),)
else:
state["arg"] = (None,)
return True
return False
game_play = on_message(rule = game_play_rule, priority = 15, block = True)
@game_play.handle()
async def _(bot:Bot, event:MessageEvent, state:T_State):
game = state["game"]
msg = await game.play(bot, event.user_id, *state["arg"])
await game_play.finish(msg)
# 随机对战
random_game = on_command("随机对战", permission = GROUP, priority = 5, block = True)
@random_game.handle()
async def _(event:GroupMessageEvent, arg:Message = CommandArg()):
gold = to_int(arg,-1)
msg = Game.random_game(event, gold)
await random_game.finish(msg)
async def session_check(event:GroupMessageEvent, state:T_State):
"""
本群有对局
"""
group_id = event.group_id
if group_id in current_games:
state["game"] = current_games[group_id]
return True
else:
return False
# 接受挑战
accept = on_command("接受挑战", aliases = {"接受决斗", "接受对决"}, rule = session_check, permission = GROUP, priority = 20, block = True)
@accept.handle()
async def _(event:GroupMessageEvent, state:T_State):
game = state["game"]
msg = game.accept(event)
await accept.finish(msg)
# 拒绝挑战
refuse = on_command("拒绝挑战", aliases={"拒绝决斗", "拒绝对决"}, rule = session_check, permission = GROUP, priority = 20, block = True)
@refuse.handle()
async def _(event:GroupMessageEvent, state:T_State):
game = state["game"]
msg = game.refuse(event.user_id)
await refuse.finish(msg)
# 超时结算
overtime = on_command("超时结算", rule = session_check, permission = GROUP, priority = 20, block = True)
@overtime.handle()
async def _(bot:Bot, state:T_State):
game = state["game"]
msg = await game.overtime(bot)
await overtime.finish(msg)
# 认输结算
fold = on_fullmatch(("认输", "投降", "结束"), rule = session_check, permission = GROUP, priority = 20, block = True)
@fold.handle()
async def _(bot:Bot, event:GroupMessageEvent, state:T_State):
game = state["game"]
await game.fold(bot, event.user_id)
# 游戏重置
restart = on_command("游戏重置", aliases={"重置游戏"}, rule = session_check, permission = GROUP, priority = 20, block = True)
@restart.handle()
async def _(state:T_State):
game = state["game"]
msg = game.restart()
await restart.finish(msg)
# 抽卡
gacha = on_regex("^.+连抽?卡?|单抽", rule = to_me(), priority = 20, block = True)
@gacha.handle()
async def _(bot:Bot, event:MessageEvent):
cmd = event.get_plaintext()
N = re.search(r"^(.*)连抽?卡?$",cmd)
if N:
N = N.group(1)
N = number(N)
else:
N = 1
if N and 0 < N <= 200:
msg = Prop.gacha(event,N)
await gacha.finish(msg)
# 使用道具
use_prop = on_command("使用道具", priority = 20, block = True)
@use_prop.handle()
async def _(event:MessageEvent, arg:Message = CommandArg()):
msg = arg.extract_plain_text().strip().split()
if len(msg) == 1:
prop_name = msg[0]
count = 1
elif len(msg) == 2 and msg[1].isdigit():
prop_name = msg[0]
count = int(msg[1])
else:
return
msg = Prop.use_prop(event, prop_name, count)
await use_prop.finish(msg, at_sender=True)
# 关联账户
connect = on_command("连接账户", aliases = {"关联账户"}, rule = to_me(), priority = 20, block = True)
@connect.handle()
async def _(event:MessageEvent, matcher:Matcher):
if isinstance(event,GroupMessageEvent):
matcher.set_arg("group_id", str(event.group_id))
else:
group_accounts = Manager.locate_user(event)[0].group_accounts
msg = "你的账户\n"
for group_id in group_accounts:
msg += f'{group_id} 金币:{group_accounts[group_id].gold}枚\n'
msg += "请输入你要关联的群号"
await connect.send(msg)
@connect.got("group_id")
async def _(event:MessageEvent, group_id:Message = Arg()):
group_id = str(group_id).strip()
msg = Account.connect(event,group_id)
await connect.finish(msg)
# 我的金币
my_gold = on_command("我的金币", priority = 20, block = True)
@my_gold.handle()
async def _(event:MessageEvent):
msg = Account.my_gold(event)
await my_gold.finish(msg, at_sender = True)
# 我的资料卡
my_info = on_command("我的信息", aliases = {"我的资料"}, priority = 20, block = True)
@my_info.handle()
async def _(event:MessageEvent):
msg = await Account.my_info(event)
await my_info.finish(msg)
# 我的交易信息
my_exchange = on_fullmatch(("我的交易信息","我的报价","我的股票"), priority = 20, block = True)
@my_exchange.handle()
async def _(event:MessageEvent):
msg = await Account.my_exchange(event)
await my_exchange.finish(msg)
# 元素精炼
alchemy_refine = on_command("元素精炼", priority = 20, block = True)
@alchemy_refine.handle()
async def _(event:MessageEvent, arg:Message = CommandArg()):
Products = arg.extract_plain_text().strip().split()
msg = Alchemy.alchemy_refine(event,Products)
await alchemy_refine.finish(msg)
# 炼金资料卡
alchemy_info = on_command("炼金账户", aliases = {"炼金资料"}, priority = 20, block = True)
@alchemy_info.handle()
async def _(event:MessageEvent):
msg = await Alchemy.my_info(event)
await alchemy_info.finish(msg)
# 我的道具
my_props = on_command("我的道具", aliases = {"我的仓库"}, priority = 20, block = True)
@my_props.handle()
async def _(event:MessageEvent):
msg = Account.my_props(event)
await my_props.finish(msg)
# 设置背景图片
add_BG_image = on_command("设置背景图片", aliases = {"add_BG"}, priority = 20, block = True)
@add_BG_image.handle()
async def _(event:MessageEvent):
msg = await Manager.add_BG_image(event)
await add_BG_image.finish(msg, at_sender = True)
# 删除背景图片
del_BG_image = on_command("删除背景图片", aliases = {"del_BG"}, priority = 20, block = True)
@del_BG_image.handle()
async def _(event:MessageEvent):
msg = await Manager.del_BG_image(event)
await del_BG_image.finish(msg, at_sender = True)
# 查看排行榜
russian_rank = on_regex(
r"^(总金币|总资产|金币|资产|财富|胜率|胜场|败场|路灯挂件)(排行|榜)",
priority = 20,
block = True
)
@russian_rank.handle()
async def _(event:MessageEvent):
cmd = event.get_plaintext().strip().split()
title = re.search(r"^(总金币|总资产|金币|资产|财富|胜率|胜场|败场|路灯挂件)(排行|榜)",cmd[0]).group(1)
msg = await Account.group_rank(event, title)
await russian_rank.finish(msg)
# 查看总排行
russian_All_rank = on_regex(
r"^(金币|资产|财富|胜率|胜场|败场|路灯挂件)(总排行|总榜)",
priority = 20,
block = True
)
@russian_All_rank.handle()
async def _(bot:Bot, event:MessageEvent):
cmd = event.get_plaintext().strip().split()
title = re.search(r"^(金币|资产|财富|胜率|胜场|败场|路灯挂件)(总排行|总榜)",cmd[0]).group(1)
msg = await Account.All_rank(event, title)
await russian_All_rank.finish(msg)
# 公司上市
Market_public = on_command(
"市场注册",
aliases = {"公司注册","注册公司"},
rule = to_me(),
permission = SUPERUSER | GROUP_ADMIN | GROUP_OWNER,
priority = 20,
block = True
)
@Market_public.handle()
async def _(event:GroupMessageEvent, arg:Message = CommandArg()):
company_name = arg.extract_plain_text().strip()
msg = Market.public(event,company_name)
await Market_public.finish(msg)
# 公司重命名
Market_rename = on_command(
"公司重命名",
rule = to_me(),
permission = SUPERUSER | GROUP_ADMIN | GROUP_OWNER,
priority = 20,
block = True
)
@Market_rename.handle()
async def _(event:GroupMessageEvent, arg:Message = CommandArg()):
company_name = arg.extract_plain_text().strip()
msg = Market.rename(event,company_name)
await Market_rename.finish(msg)
def arg_check(msg:str):
if len(msg) == 2:
if msg[0].isdigit():
count = int(msg[0])
company_name = msg[1]
elif msg[1].isdigit():
count = int(msg[1])
company_name = msg[0]
else:
return None
else:
return None
if count > 0:
return count,company_name
else:
return None
# 发行购买
Market_buy = on_command("购买", aliases = {"发行购买"}, priority = 20, block = True)
@Market_buy.handle()
async def _(event:MessageEvent, arg:Message = CommandArg()):
msg = arg.extract_plain_text().strip().split()
info = arg_check(msg)
if not info:
return
else:
buy, company_name = info
msg = Market.buy(event,buy,company_name)
await Market_buy.finish(msg)
# 官方结算
Market_settle = on_command("结算", aliases = {"官方结算"}, priority = 20, block = True)
@Market_settle.handle()
async def _(event:MessageEvent, arg:Message = CommandArg()):
msg = arg.extract_plain_text().strip().split()
info = arg_check(msg)
if not info:
return
else:
settle, company_name = info
msg = Market.settle(event,settle,company_name)
await Market_settle.finish(msg)
# 市场购买
Exchange_buy = on_command("市场购买", priority = 20, block = True)
@Exchange_buy.handle()
async def _(event:MessageEvent, arg:Message = CommandArg()):
msg = arg.extract_plain_text().strip().split()
info = arg_check(msg)
if not info:
return
else:
buy, company_name = info
msg = Market.Exchange_buy(event,buy,company_name)
await Exchange_buy.finish(msg)
# 市场出售
Exchange_sell = on_command("出售", aliases = {"市场出售", "发布交易信息"}, priority = 20, block = True)
@Exchange_sell.handle()
async def _(matcher:Matcher, event:MessageEvent, arg:Message = CommandArg()):
msg = arg.extract_plain_text().strip().split()
if msg and (company_id := company_index.get(msg[0])):
if len(msg) == 3:
if msg[1].isdigit():
quote = msg[2]
n = int(msg[1])
elif msg[2].isdigit():
quote = (msg[1])
n = int(msg[2])
else:
await Exchange_sell.finish()
try:
quote = float(quote)
except:
await Exchange_sell.finish()
info = (company_id, ExchangeInfo(quote = quote, n = n))
msg = Market.Exchange_sell(event,info)
await Exchange_sell.finish(msg)
else:
matcher.set_arg("company_id", company_id)
else:
await Exchange_sell.finish()
@Exchange_sell.got("info", prompt = "请输入出售数量和单价,用空格隔开。")
async def _(matcher:Matcher, event:MessageEvent, info:Message = Arg()):
info = info.extract_plain_text().strip().split()
if len(info) == 2 and info[0].isdigit():
n = int(info[0])
elif info[0] == "取消":
await Exchange_sell.finish()
else:
await Exchange_sell.reject("格式错误,请重新输入正确格式或输入【取消】中止对话。")
try:
quote = float(info[1])
except:
await Exchange_sell.reject("格式错误,请重新输入正确格式或输入【取消】中止对话。")
company_id = matcher.get_arg("company_id")
info = (company_id, ExchangeInfo(quote = quote, n = n))
msg = Market.Exchange_sell(event,info)
await Exchange_sell.finish(msg)
# 群资料卡
group_info = on_command("群资料卡", priority = 20, block = True)
@group_info.handle()
async def _(bot:Bot, event:MessageEvent, arg:Message = CommandArg()):
group_id = arg.extract_plain_text().strip()
if group_id.isdigit():
group_id = int(group_id)
elif isinstance(event,GroupMessageEvent):
group_id = event.group_id
else:
return
msg = await Market.group_info(bot,event,group_id)
await group_info.finish(msg)
# 市场信息
Market_info = on_command("市场信息",aliases={"查看市场"}, priority = 20, block = True)
@Market_info.handle()
async def _(bot:Bot, event:MessageEvent, arg:Message = CommandArg()):
company_name = arg.extract_plain_text().strip()
if company_name:
if company_name in company_index:
company_id = company_index[company_name]
msg = await Market.group_info(bot,event,company_id)
else:
msg = f"没有 {company_name} 的注册信息"
await Market_info.send(msg)
else:
if msg := Market.Market_info_All(event):
if len(msg) == 1:
await Market_info.send(msg[0]["data"]["content"])
elif isinstance(event, GroupMessageEvent):
await bot.send_group_forward_msg(group_id = event.group_id, messages = msg)
else:
await bot.send_private_forward_msg(user_id = event.user_id, messages = msg)
else:
await Market_info.send("市场为空")
# 市场价格表
Market_pricelist = on_command("市场价格表",aliases={"股票价格表"}, priority = 20, block = True)
@Market_pricelist.handle()
async def _(event:MessageEvent):
msg = Market.pricelist(event.user_id)
await Market_pricelist.finish(msg)
# 更新公司简介
update_intro = on_command(
"更新公司简介",
aliases = {"添加公司简介", "修改公司简介"},
permission = SUPERUSER | GROUP_ADMIN | GROUP_OWNER,
priority = 20,
block = True
)
@update_intro.handle()
async def _(event:GroupMessageEvent, arg:Message = CommandArg()):
intro = arg.extract_plain_text().strip()
msg = Market.update_intro(str(event.group_id), intro)
await group_info.finish(msg)
# 管理公司简介
update_intro_superuser = on_command("管理公司简介", permission = SUPERUSER, priority = 20, block = True)
@update_intro_superuser.handle()
async def _(event:MessageEvent, arg:Message = CommandArg()):
msg = arg.extract_plain_text().strip().split(" ",1)
if len(msg) == 2:
msg = Market.update_intro(msg[0], msg[1])
await update_intro_superuser.finish(msg)
# 跨群转移金币到自己的账户
transfer_gold = on_command("金币转移", priority = 20, block = True)
@transfer_gold.handle()
async def _(event:MessageEvent, arg:Message = CommandArg()):
msg = arg.extract_plain_text().strip().split()
info = arg_check(msg)
if not info:
return
else:
gold, company_name = info
msg = Account.intergroup_transfer_gold(event,gold,company_name)
await transfer_gold.finish(msg, at_sender = True)
# 冻结个人资产
freeze = on_command("冻结资产", permission = SUPERUSER, priority = 20, block = True)
@freeze.handle()
async def _(bot:Bot, event:MessageEvent, matcher:Matcher, arg:Message = CommandArg()):
at = get_message_at(event.message)
if at:
at = at[0]
elif (at := arg.extract_plain_text().strip()) and at.isdigit():
at = int(at)
if at not in data.user:
await freeze.finish(f"用户 {at} 不存在。")
else:
await freeze.finish("没有选择冻结对象。")
confirm = random.randint(1000,9999)
matcher.set_arg("freeze", (int(at),str(confirm)))
nickname = (await bot.get_group_member_info(group_id = event.group_id, user_id = at))["nickname"]
await freeze.send(f"您即将冻结 {nickname}({at}),请输入{confirm}来确认。")
@freeze.got("code")
async def _(event:MessageEvent, matcher:Matcher, code :Message = Arg()):
at,confirm = matcher.get_arg("freeze")
if confirm == str(code):
target = Manager.locate_user_at(event, at)
msg = Account.freeze(target[0])
await freeze.finish(msg)
else:
await freeze.finish("【冻结】已取消。")
# 清理无效账户
delist = on_fullmatch("清理无效账户", rule = to_me(), permission = SUPERUSER, priority = 20, block = True)
@delist.handle()
async def _():
await delist.send("正在启动清理程序。")
log = await Account.delist()
logger.info("\n" + log)
with open(path / "delist.log","a",encoding = "utf8") as f:
f.write(
f"\n{datetime.datetime.fromtimestamp(time.time()).strftime('%Y 年 %m 月 %d 日 %H:%M:%S')}\n"
"——————————————\n"
+ log + "\n"
"——————————————\n"
)
await delist.finish("清理完成!")
# 市场重置
Market_reset = on_fullmatch("市场重置", permission = SUPERUSER, priority = 20, block = True)
@Market_reset.handle()
async def _():
Market.reset()
await Market_reset.finish("市场已重置。")
require("nonebot_plugin_apscheduler")
from nonebot_plugin_apscheduler import scheduler
# 市场更新
@scheduler.scheduled_job("cron", minute = "*/5", misfire_grace_time = 120)
async def _():
log = Market.update()
if log:
logger.info("\n" + log)
# 数据备份
Backup = on_command("Backup", aliases = {"数据备份", "游戏备份"}, permission = SUPERUSER, priority = 20, block = True)
@Backup.handle()
@scheduler.scheduled_job("cron", hour = "*/4", misfire_grace_time = 120)
async def _():
now = time.strftime('%Y-%m-%d %H-%M-%S', time.localtime(time.time()))
now = now.split()
backup_today = backup / now[0]
if not backup_today.exists():
backup_today.mkdir()
data.save()
shutil.copy(f"{path}/russian_data.json",f"{backup_today}/russian_data {now[1]}.json")
logger.info(f'russian_data.json 备份成功!')
# 刷新每日
Newday = on_command("Newday", aliases = {"刷新每日", "刷新签到"}, permission = SUPERUSER, priority = 20, block = True)
@Newday.handle()
@scheduler.scheduled_job("cron", hour = 0, misfire_grace_time = 120)
async def _():
Manager.update_company_index()
log = data.verification()
logger.info(f"\n{log}")
data.Newday()
with open(path / "Newday.log","a",encoding = "utf8") as f:
f.write(f"\n{datetime.datetime.fromtimestamp(time.time()).strftime('%Y 年 %m 月 %d 日 %H:%M:%S')}\n"
"——————————————\n"
f"{log}\n"
"——————————————\n")
folders = [f for f in backup.iterdir() if f.is_dir()]
for folder in folders:
if time.time() - folder.stat().st_ctime > 604800:
shutil.rmtree(folder)
logger.info(f'备份 {folder} 已删除!')
# 保存数据
DataSave = on_command("DataSave", aliases = {"保存数据", "保存游戏"},permission = SUPERUSER, priority = 20, block = True)
@DataSave.handle()
@scheduler.scheduled_job("cron", minute = "*/10", misfire_grace_time = 120)
async def _():
data.save()
with open(Market.market_history_file, "w", encoding = "utf8") as f:
json.dump(Market.market_history, f, ensure_ascii = False, indent = 4)
logger.info(f'游戏数据已保存!!')