forked from KarisAya/nonebot_plugin_game_collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
2013 lines (1822 loc) · 69.8 KB
/
Game.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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from typing import Tuple,Dict
from pydantic import BaseModel
from abc import ABC, abstractmethod
from nonebot.adapters.onebot.v11 import (
Bot,
MessageEvent,
GroupMessageEvent,
Message,
MessageSegment
)
import random
import time
import asyncio
from .utils.utils import get_message_at
from .utils.chart import linecard_to_png
from .data import props_library
from .config import bot_name, security_gold, bet_gold, max_bet_gold, max_player, min_player
from . import Manager
data = Manager.data
user_data = data.user
group_data = data.group
class GameOverException(Exception):
"""
GameOverException
"""
class Session(BaseModel):
"""
游戏场次信息
"""
time:float = 0.0
group_id = 0
player1_id:int = None
player2_id:int = None
at:int = None
round = 0
next:int = None
win:int = None
gold:int = 0
bet_limit:int = 0
def create(self, event:GroupMessageEvent):
"""
创建游戏
"""
at = get_message_at(event.message)
at = int(at[0]) if at else None
self.__init__(
time = time.time(),
group_id = event.group_id,
player1_id = event.user_id,
at = at,
gold = self.gold
)
def create_check(self, event:GroupMessageEvent):
"""
检查是否可以根据event创建游戏
如果不能创建则返回提示
如果可以创建则返回None
"""
overtime = time.time() - self.time
if overtime > 60:
return None
user_id = event.user_id
if player1_id := self.player1_id:
if player1_id == user_id:
if not self.player2_id:
return None
else:
return "你已发起了一场对决"
if player2_id := self.player2_id:
if player2_id == user_id:
return "你正在进行一场对决"
else:
player1_name = user_data[player1_id].group_accounts[event.group_id].nickname
player2_name = user_data[player2_id].group_accounts[event.group_id].nickname
return f"{player1_name} 与 {player2_name} 的对决还未结束!"
else:
if overtime > 30:
return None
else:
return f'现在是 {user_data[player1_id].group_accounts[event.group_id].nickname} 发起的对决,请等待比赛结束后再开始下一轮...'
def try_create_game(self, event:GroupMessageEvent):
"""
根据event创建游戏
如果创建失败则返回提示
如果创建成功则返回None
"""
if msg := self.create_check(event):
return msg
else:
self.create(event)
def try_join_game(self, event:GroupMessageEvent):
"""
根据event加入游戏
如果加入失败则返回提示
如果加入成功则返回None
"""
if time.time() - self.time > 60:
return "这场对决邀请已经过时了,请重新发起决斗..."
user_id = event.user_id
if self.player1_id and self.player1_id != user_id and not self.next:
if not self.at or self.at == user_id:
self.time = time.time()
self.player2_id = user_id
return None
else:
return f'现在是 {user_data[self.player1_id].group_accounts[event.group_id].nickname} 发起的对决,请等待比赛结束后再开始下一轮...'
else:
return " "
def leave(self):
"""
玩家2离开游戏
"""
self.time = time.time()
self.player2_id = None
return None
def nextround(self):
"""
把session状态切换到下一回合
"""
self.time = time.time()
self.round += 1
self.next = self.player1_id if self.next == self.player2_id else self.player2_id
def shot_check(self, user_id:int):
"""
开枪前检查游戏是否合法
如果不合法则返回提示
如果合法则返回None
"""
if time.time() - self.time > 60:
return "这场对决邀请已经过时了,请重新发起决斗..."
if not self.player1_id:
return " "
if not self.player2_id:
if self.player1_id == user_id:
return "目前无人接受挑战哦"
else:
return "请这位勇士先接受挑战"
if user_id == self.player1_id or user_id == self.player2_id:
if user_id == self.next:
return None
else:
return f"现在是{user_data[self.next].group_accounts[self.group_id].nickname}的回合"
else:
player1_name = user_data[self.player1_id].group_accounts[self.group_id].nickname
player2_name = user_data[self.player2_id].group_accounts[self.group_id].nickname
return f"{player1_name} v.s. {player2_name}\n正在进行中..."
class Game(ABC):
"""
对战游戏类:
需要定义的方法:
action:游戏进行
game_tips:游戏开始的提示
session_tips:场次提示信息
end_tips:结束附件
"""
name:str = "undefined"
max_bet_gold:int = max_bet_gold
def __init__(self):
self.gold:int = 0
self.session:Session = Session()
@staticmethod
def parse_arg(arg:str):
if arg.isdigit():
gold = int(arg)
else:
gold = bet_gold
return {"gold":gold}
@classmethod
def creat(cls, event:GroupMessageEvent, **kwargs):
"""
发起游戏
"""
flag, msg = cls.start(event, **kwargs)
if flag == False:
return msg
return current_games[event.group_id].game_tips(msg)
@abstractmethod
def game_tips(self, msg):
"""
游戏开始的提示
"""
@abstractmethod
async def action(self, bot:Bot, user_id:int):
"""
游戏进行
"""
async def play(self, bot:Bot, user_id:int, *args):
try:
return await self.action(bot, user_id, *args)
except GameOverException:
pass
def accept(self, event:GroupMessageEvent):
"""
接受挑战
"""
session = self.session
group_id = session.group_id
if msg := session.try_join_game(event):
return None if msg == " " else msg
user,group_account = Manager.locate_user(event)
if group_account.gold < session.gold:
session.leave()
return Message(MessageSegment.at(event.user_id) + f"你的金币不足以接受这场对决!\n——你还有{group_account.gold}枚金币。")
user.connect = group_id
bet_limit = min(
user_data[session.player1_id].group_accounts[group_id].gold,
user_data[session.player2_id].group_accounts[group_id].gold)
bet_limit = bet_limit if bet_limit > 0 else 0
if session.gold == -1:
session.gold = random.randint(0, bet_limit)
self.gold = session.gold
session.bet_limit = bet_limit
session.next = session.player1_id
return self.session_tips()
@abstractmethod
def session_tips(self):
"""
场次提示信息
"""
def acceptmessage(self, tip1, tip2):
"""
合成接受挑战的提示信息
"""
session = self.session
return Message(
f"{MessageSegment.at(session.player2_id)}接受了对决!\n" +
tip1 +
f"赌注为 {session.gold} 金币\n" +
f"请{MessageSegment.at(session.player1_id)}{tip2}"
)
def refuse(self, user_id:int):
"""
拒绝挑战
"""
session = self.session
group_id = session.group_id
if time.time() - session.time > 60:
del current_games[group_id]
if session.at == user_id:
if session.player2_id:
return "对决已开始,拒绝失败。"
else:
del current_games[group_id]
return "拒绝成功,对决已结束。"
async def overtime(self, bot:Bot):
"""
超时结算
"""
session = self.session
if (time.time() - session.time > 30 and
session.player1_id and
session.player2_id):
try:
await self.end(bot)
except GameOverException:
pass
async def fold(self, bot:Bot, user_id:int):
"""
认输
"""
session = self.session
if (time.time() - session.time < 60 and
session.player1_id and
session.player2_id and
user_id == session.player1_id or user_id == session.player2_id):
session.win = session.player1_id if user_id == session.player2_id else session.player2_id
try:
await self.end(bot)
except GameOverException:
pass
def restart(self):
"""
游戏重置
"""
session = self.session
group_id = session.group_id
overtime = time.time() - session.time
if overtime < 60:
return f"当前游戏已创建 {int(overtime)} 秒,未超时。"
del current_games[group_id]
return "游戏已重置。"
@classmethod
def start(cls, event:GroupMessageEvent, **kwargs) -> Tuple[bool,Message]:
"""
发起游戏
"""
gold = kwargs["gold"]
limit = cls.max_bet_gold
if gold > limit:
return False, Message(MessageSegment.at(event.user_id) + f"对战金额不能超过{limit}")
user,group_account = Manager.locate_user(event)
if gold > group_account.gold:
return False, Message(MessageSegment.at(event.user_id) + f"你没有足够的金币支撑这场对决。\n——你还有{group_account.gold}枚金币。")
group_id = event.group_id
if group_id in current_games:
game = current_games[group_id]
if msg := game.session.create_check(event):
return False, msg
game = current_games[group_id] = cls(**kwargs)
session = game.session
session.create(event)
assert session is current_games[group_id].session
if at := session.at:
if at not in group_data[group_id].namelist:
del current_games[group_id]
return False, "没有对方的注册信息。"
player1_name = user_data[session.player1_id].group_accounts[event.group_id].nickname
player2_name = user_data[at].group_accounts[event.group_id].nickname
msg = (f"{player1_name} 向 {player2_name} 发起挑战!\n"
f"请 {player2_name} 回复 接受挑战 or 拒绝挑战\n"
"【30秒内有效】")
else:
player1_name = user_data[session.player1_id].group_accounts[event.group_id].nickname
msg = (f"{player1_name} 发起挑战!\n"
"回复 接受挑战 即可开始对局。\n"
"【30秒内有效】")
user.connect = group_id
session.round = 1
return True, msg
def settle(self):
"""
游戏结束结算
return:结算界面
"""
session = self.session
group_id = session.group_id
win = session.win if session.win else session.player1_id if session.next == session.player2_id else session.player2_id
winner = user_data[win]
winner_group_account = winner.group_accounts[group_id]
lose = session.player1_id if session.player2_id == win else session.player2_id
loser = user_data[lose]
loser_group_account = loser.group_accounts[group_id]
gold = session.gold
if winner_group_account.props.get("42001",0) > 0:
fee = 0
fee_tip = f"『{props_library['42001']['name']}』免手续费"
else:
rand = random.randint(0, 5)
fee = int(gold * rand / 100)
fee_tip = f"手续费:{fee}({rand}%)"
maxgold = int(max_bet_gold/5)
if winner_group_account.props.get("52002",0) > 0:
extra = int(gold *0.1)
if extra < maxgold:
extra_tip = f"◆『{props_library['52002']['name']}』\n"
else:
extra = maxgold
extra_tip = f"◆『{props_library['52002']['name']}』最大奖励\n"
else:
extra_tip = ""
extra = 0
if gold == loser_group_account.gold and loser_group_account.security < 3:
loser_group_account.security += 1
security = random.randint(security_gold[0], security_gold[1])
security_tip = f"◇『金币补贴』(+{security})\n"
else:
security = 0
security_tip = ""
if loser_group_account.props.get("52001",0) > 0:
off = int(gold * 0.1)
if off < maxgold:
off_tip = f"◇『{props_library['52001']['name']}』\n"
else:
off = maxgold
off_tip = f"◇『{props_library['52001']['name']}』最大补贴\n"
else:
off = 0
off_tip = ""
win_gold = gold - fee + extra
winner.win += 1
winner.Achieve_win += 1
winner.Achieve_lose = 0
lose_gold = gold - off
loser.lose += 1
loser.Achieve_lose += 1
loser.Achieve_win = 0
msg = (
f"结算:\n"
"----\n"
+ extra_tip
+ (tmp + "\n" if (tmp := "\n".join(x for x in Manager.Achieve_list((winner,winner_group_account)))) else "") +
f"◆胜者 {winner_group_account.nickname}\n"
f"◆结算 {winner_group_account.gold}(+{win_gold})\n"
f"◆战绩 {winner.win}:{winner.lose}\n"
f"◆胜率 {round(winner.win * 100 / (winner.win + winner.lose), 2) if winner.win > 0 else 0}%\n"
"----\n"
+ off_tip
+ (tmp + "\n" if (tmp := "\n".join(x for x in Manager.Achieve_list((loser,loser_group_account)))) else "") +
f"◇败者 {loser_group_account.nickname}\n"
f"◇结算 {loser_group_account.gold}(-{lose_gold})\n"
+ security_tip +
f"◇战绩 {loser.win}:{loser.lose}\n"
f"◇胜率 {round(loser.win * 100 / (loser.win + loser.lose), 2) if loser.win > 0 else 0}%\n"
"----\n" +
fee_tip
)
winner.gold += win_gold
winner_group_account.gold += win_gold
loser.gold -= lose_gold - security
loser_group_account.gold -= lose_gold - security
del current_games[group_id]
return f"这场对决是 {winner_group_account.nickname} 胜利了", msg, self.end_tips()
@abstractmethod
def end_tips(self):
"""
结束附件
"""
async def end(self, bot:Bot):
"""
输出结算界面
"""
group_id = self.session.group_id
result = self.settle()
await bot.send_group_msg(group_id = group_id, message = result[0])
await bot.send_group_msg(group_id = group_id, message = MessageSegment.image(linecard_to_png(result[1], width = 880)))
if result[2]:
await asyncio.sleep(0.5)
await bot.send_group_msg(group_id = group_id, message = result[2])
raise GameOverException
current_games:Dict[int,Game] = {}
class Russian(Game):
"""
俄罗斯轮盘
"""
name = "Russian"
max_bet_gold:int = max_bet_gold
def __init__(self, **kwargs):
super().__init__()
gold = kwargs["gold"]
bullet_num = kwargs.get("bullet_num",1)
self.gold:int = gold
self.bullet_num:int = bullet_num
self.bullet:list = self.random_bullet(bullet_num)
self.index:int = 0
self.session.gold = gold
@staticmethod
def parse_arg(arg:str):
gold = bet_gold
bullet_num = 1
if arg:
arg = arg.split()
if len(arg) == 1:
arg = arg[0]
if arg.isdigit():
if 0 < (tmp := int(arg)) < 7:
bullet_num = tmp
else:
gold = tmp
else:
if arg[0].isdigit():
if 0 < (tmp := int(arg[0])) < 7:
bullet_num = tmp
if arg[1].isdigit():
gold = int(arg[1])
else:
gold = tmp
return {"gold":gold,"bullet_num":bullet_num}
@staticmethod
def random_bullet(bullet_num:int):
"""
随机子弹排列
bullet_num:装填子弹数量
"""
bullet_lst = [0, 0, 0, 0, 0, 0, 0]
for i in random.sample([0, 1, 2, 3, 4, 5, 6], bullet_num):
bullet_lst[i] = 1
return bullet_lst
async def action(self, bot:Bot, user_id:int, *args):
"""
开枪!!!
"""
session = self.session
if msg := session.shot_check(user_id):
return None if msg == " " else msg
group_id = session.group_id
index = self.index
MAG = self.bullet[index:]
count = args[0] if args[0] else 1
count = len(MAG) if count < 1 else count
msg = f"连开{count}枪!\n" if count > 1 else ""
if 1 in MAG[:count]:
session.win = session.player1_id if user_id == session.player2_id else session.player2_id
await bot.send_group_msg(group_id = group_id, message = (
MessageSegment.at(user_id) + msg +
random.choice(["嘭!,你直接去世了","眼前一黑,你直接穿越到了异世界...(死亡)","终究还是你先走一步..."]) +
f"\n第 {index + MAG.index(1) + 1} 发子弹送走了你..."
))
await self.end(bot)
else:
session.nextround()
self.index += count
next_name = user_data[session.next].group_accounts[group_id].nickname
await bot.send_group_msg(group_id = group_id, message = (
random.choice(["呼呼,没有爆裂的声响,你活了下来","虽然黑洞洞的枪口很恐怖,但好在没有子弹射出来,你活下来了",f'{"咔 "*count},看来运气不错,你活了下来']) +
f"\n下一枪中弹的概率:{round(self.bullet_num * 100 / (len(MAG) - count),2)}%\n"
f"轮到 {next_name}了"
))
def game_tips(self, msg):
"""
发起游戏:俄罗斯轮盘
"""
return (("咔 " * self.bullet_num)[:-1] + ",装填完毕\n"
f'挑战金额:{self.gold}\n'
f'第一枪的概率为:{round(self.bullet_num * 100 / 7,2)}%\n'
f'{msg}')
def session_tips(self):
tip1 = "本场对决为【俄罗斯轮盘】\n"
tip2 = "开枪!"
return self.acceptmessage(tip1, tip2);
def end_tips(self):
return " ".join(("—" if x == 0 else "|") for x in self.bullet)
class Dice(Game):
"""
掷色子
"""
name = "Dice"
max_bet_gold:int = max_bet_gold
def __init__(self, **kwargs):
super().__init__()
gold = kwargs["gold"]
self.gold:int = gold
self.dice_array1:list = [random.randint(1,6) for i in range(5)]
self.dice_array2:list = [random.randint(1,6) for i in range(5)]
self.session.gold = gold
@staticmethod
def dice_pt(dice_array:list) -> int:
"""
计算骰子排列pt
"""
pt = 0
for i in range(1,7):
if dice_array.count(i) <= 1:
pt += i * dice_array.count(i)
elif dice_array.count(i) == 2:
pt += (100 + i) * (10 ** dice_array.count(i))
else:
pt += i * (10 ** (2 + dice_array.count(i)))
else:
return pt
@staticmethod
def dice_pt_analyses(pt:int) -> str:
"""
分析骰子pt
"""
array_type = ""
if (yiman := int(pt/10000000)) > 0:
pt -= yiman * 10000000
array_type += f"役满 {yiman} + "
if (chuan := int(pt/1000000)) > 0:
pt -= chuan * 1000000
array_type += f"串 {chuan} + "
if (tiao := int(pt/100000)) > 0:
pt -= tiao * 100000
array_type += f"条 {tiao} + "
if (dui := int(pt/10000)) > 0:
if dui == 1:
pt -= 10000
dui = int(pt/100)
array_type += f"对 {dui} + "
else:
pt -= 20000
dui = int(pt/100)
array_type += f"两对 {dui} + "
pt -= dui * 100
if pt>0:
array_type += f"散 {pt} + "
return array_type[:-3]
@staticmethod
def dice_list(dice_array:list) -> str:
"""
把骰子列表转成字符串
"""
lst_dict = {0:"〇",1:"1",2:"2",3:"3",4:"4",5:"5",6:"6",7:"7",8:"8",9:"9"}
return " ".join(lst_dict[x] for x in dice_array)
async def action(self, bot:Bot, user_id:int, *args):
"""
开数!!!
"""
session = self.session
if msg := session.shot_check(user_id):
return None if msg == " " else msg
group_id = session.group_id
session.gold += self.gold
session.gold = min(session.gold, session.bet_limit)
player1_id = session.player1_id
player2_id = session.player2_id
dice_array1 = (self.dice_array1[:int(session.round/2+0.5)] + [0, 0, 0, 0, 0])[:5]
dice_array2 = (self.dice_array2[:int(session.round/2)] + [0, 0, 0, 0, 0])[:5]
dice_array1.sort(reverse=True)
dice_array2.sort(reverse=True)
pt1 = self.dice_pt(dice_array1)
pt2 = self.dice_pt(dice_array2)
session.win = player1_id if pt1 > pt2 else player2_id
session.nextround()
next_name = "结算" if session.round > 10 else user_data[session.next].group_accounts[group_id].nickname
msg = (
f'玩家:{user_data[player1_id].group_accounts[group_id].nickname}\n'
f"组合:{self.dice_list(dice_array1)}\n"
f"点数:{self.dice_pt_analyses(pt1)}\n"
"----\n"
f'玩家:{user_data[player2_id].group_accounts[group_id].nickname}\n'
f"组合:{self.dice_list(dice_array2)}\n"
f"点数:{self.dice_pt_analyses(pt2)}\n"
"----\n"
f"结算金额:{session.gold}\n"
f'领先:{user_data[session.win].group_accounts[group_id].nickname}\n'
f'下一回合:{next_name}'
)
await bot.send_group_msg(group_id = group_id, message = MessageSegment.image(linecard_to_png(msg, width = 700)))
if session.round > 10:
await self.end(bot)
def game_tips(self, msg):
"""
发起游戏:掷色子
"""
return ("哗啦哗啦~,骰子准备完毕\n"
f'挑战金额:{self.gold}/次\n'
f'{msg}')
def session_tips(self):
tip1 = "本场对决为【掷色子】\n"
tip2 = "开数!"
return self.acceptmessage(tip1, tip2);
def end_tips(self):
return (
f'玩家 1\n'
f'组合:{" ".join(str(x) for x in self.dice_array1)}\n'
f'玩家 2\n'
f'组合:{" ".join(str(x) for x in self.dice_array2)}')
class Poker(Game):
"""
扑克对战
"""
name = "Poker"
max_bet_gold:int = max_bet_gold *5
def __init__(self, **kwargs):
super().__init__()
gold = kwargs["gold"]
deck = self.random_poker(2)
hand = deck[0:3].copy()
self.gold:int = gold
del deck[0:3]
self.deck:list = deck + [[0,0],[0,0],[0,0],[0,0]]
self.ACT:int = 1
self.P1:dict = {"hand":hand,"HP":20,"ATK":0,"DEF":0,"SP":0}
self.P2:dict = {"hand":[],"HP":25,"ATK":0,"DEF":0,"SP":2}
self.session.gold = gold
@staticmethod
def random_poker(n:int = 1):
"""
生成随机牌库
"""
poker_deck = [[i,j] for i in range(1,5) for j in range(1,14)]
poker_deck = poker_deck*n
random.shuffle(poker_deck)
return poker_deck
class pokerACT():
suit = {0:"结束",1:"防御",2:"恢复",3:"技能",4:"攻击"}
point = {0:"0",1:"A",2:"2",3:"3",4:"4",5:"5",6:"6",7:"7",8:"8",9:"9",10:"10",11:"11",12:"12",13:"13"}
@classmethod
def action_ACE(cls, Active:dict, roll:int = 1) -> str:
'''
手牌全部作为技能牌(ACE技能)
Active:行动牌生效对象
'''
card_msg = "技能牌为"
skill_msg = "\n"
for card in Active["hand"]:
suit = card[0]
point = roll if card[1] == 1 else card[1]
card_msg += f'【{cls.suit[suit]} {cls.point[point]}】'
if suit == 1:
Active["DEF"] += point
skill_msg += f'♤防御力强化了 {point}\n'
elif suit == 2:
Active["HP"] += point
skill_msg += f'♡生命值增加了 {point}\n'
elif suit == 3:
Active["SP"] += point + point
skill_msg += f'♧技能点增加了 {point}\n'
elif suit == 4:
Active["ATK"] += point
skill_msg += f'♢发动了攻击 {point}\n'
else:
return "出现未知错误"
Active["SP"] -= point
Active["SP"] = 0 if Active["SP"] < 0 else Active["SP"]
return card_msg + skill_msg[:-1]
@classmethod
def action(cls, index:int, Active:dict) -> str:
'''
行动牌生效
index:手牌序号
Active:行动牌生效对象
'''
card = Active["hand"][index]
suit = card[0]
point = card[1]
if point == 1:
roll = random.randint(1,6)
msg = f'发动ACE技能!六面骰子判定为 {roll}\n'
msg += cls.action_ACE(Active, roll)
else:
if suit == 1:
Active["ATK"] += point
msg = f"♤发动了攻击{point}"
elif suit == 2:
Active["HP"] += point
msg = f"♡生命值增加了{point}"
elif suit == 3:
Active["SP"] += point
msg = f"♧技能点增加了{point}...\n"
roll = random.randint(1,20)
if Active["SP"] < roll:
msg += f'二十面骰判定为{roll}点,当前技能点{Active["SP"]}\n技能发动失败...'
else:
del Active["hand"][index]
msg += f'二十面骰判定为{roll}点,当前技能点{Active["SP"]}\n技能发动成功!\n'
msg += cls.action_ACE(Active)
elif suit == 4:
Active["ATK"] = point
msg = f"♢发动了攻击{point}"
else:
msg = "出现未知错误"
return msg
@classmethod
def skill(cls, card:list, Player:dict) -> str:
'''
技能牌生效
card:技能牌
Player:技能牌生效对象
'''
suit = card[0]
point = card[1]
msg = f'技能牌为【{cls.suit[suit]} {cls.point[point]}】\n'
if suit == 1:
Player["DEF"] += point
msg += f"♤发动了防御 {point}"
elif suit == 2:
Player["HP"] += point
msg += f"♡生命值增加了 {point}"
elif suit == 3:
Player["SP"] += point + point
msg += f"♧技能点增加了 {point}"
elif suit == 4:
Player["ATK"] += point
msg += f"♢发动了反击 {point}"
else:
msg += "启动结算程序"
Player["SP"] -= point
Player["SP"] = 0 if Player["SP"] < 0 else Player["SP"]
return msg
async def action(self, bot:Bot, user_id:int, *args):
"""
出牌
"""
session = self.session
if self.ACT == 0:
return
if msg := session.shot_check(user_id):
return None if msg == " " else msg
if not 1<= (index := args[0]) <= 3:
return "请发送【出牌 1/2/3】打出你的手牌。"
group_id = session.group_id
self.ACT = 0
session.nextround()
deck = self.deck
if user_id == session.player1_id:
Active = self.P1
Passive = self.P2
else:
Active = self.P2
Passive = self.P1
# 出牌判定
msg = self.pokerACT.action(index - 1,Active)
try:
await bot.send_group_msg(group_id = group_id, message = MessageSegment.at(user_id) + msg)
except:
try:
await bot.send_group_msg(group_id = group_id, message = MessageSegment.at(user_id) + MessageSegment.image(linecard_to_png(msg,font_size = 30)))
except:
pass
await asyncio.sleep(0.03*len(msg))
# 敌方技能判定
next_name = user_data[session.next].group_accounts[group_id].nickname
if Passive["SP"] > 0:
roll = random.randint(1,20)
if Passive["SP"] < roll:
msg = f'{next_name} 二十面骰判定为{roll}点,当前技能点{Passive["SP"]}\n技能发动失败...'
else:
msg = f'{next_name} 二十面骰判定为{roll}点,当前技能点{Passive["SP"]}\n技能发动成功!\n'
msg += self.pokerACT.skill(deck[0], Passive)
del deck[0]
try:
await bot.send_group_msg(group_id = group_id, message = msg)
except:
try:
await bot.send_group_msg(group_id = group_id, message = MessageSegment.image(linecard_to_png(msg,font_size = 30)))
except:
pass
# 回合结算
Active["HP"] += Active["DEF"] - Active["ATK"] if Active["DEF"] < Passive["ATK"] else 0
Passive["HP"] += Passive["DEF"] - Active["ATK"] if Passive["DEF"] < Active["ATK"] else 0
Active["ATK"] = 0
Passive["ATK"] = 0
# 防御力强化保留一回合
Passive["DEF"] = 0
# 下回合准备
hand = deck[0:3].copy()
Passive["hand"] = hand
del deck[0:3]
next_name = "游戏结束" if Active["HP"] < 1 or Passive["HP"] < 1 or Passive["HP"] >= 40 or [0,0] in hand else next_name
msg = (
f'玩家:{user_data[session.player1_id].group_accounts[group_id].nickname}\n'
"状态:\n"
f'HP {self.P1["HP"]} SP {self.P1["SP"]} DEF {self.P1["DEF"]}\n'
"----\n"
f'玩家:{user_data[session.player2_id].group_accounts[group_id].nickname}\n'
"状态:\n"
f'HP {self.P2["HP"]} SP {self.P2["SP"]} DEF {self.P2["DEF"]}\n'
"----\n"
f'当前回合:{next_name}\n'
"手牌:\n[center]" +
"".join([f'【{self.pokerACT.suit[suit]}{self.pokerACT.point[point]}】' for suit, point in Passive["hand"]])
)
await asyncio.sleep(0.5)
await bot.send_group_msg(group_id = group_id, message = MessageSegment.image(linecard_to_png(msg, width = 880)))
if next_name == "游戏结束":
Passive["HP"] = Passive["HP"] + 100 if Passive["HP"] >= 40 else Passive["HP"]
session.win = session.player1_id if self.P1["HP"] > self.P2["HP"] else session.player2_id
await self.end(bot)
else:
self.ACT = 1
def game_tips(self, msg):
"""
发起游戏:扑克对战
"""
return ("唰唰~,随机牌堆已生成\n"
f'挑战金额:{self.gold}\n'
f'{msg}')
def session_tips(self):
tip1 = "本场对决为【扑克对战】\n"
tip2 = "出牌!\n"
tip2 += MessageSegment.image(linecard_to_png((
"P1初始状态\n"
f'HP {self.P1["HP"]} SP {self.P1["SP"]} DEF {self.P1["DEF"]}\n'
"----\n"
"P2初始状态\n"
f'HP {self.P2["HP"]} SP {self.P2["SP"]} DEF {self.P2["DEF"]}\n'
"----\n"
"P1初始手牌\n[center]" +
"".join([f'【{self.pokerACT.suit[suit]}{self.pokerACT.point[point]}】' for suit, point in self.P1["hand"]])
), width = 880))
return self.acceptmessage(tip1, tip2);
def end_tips(self):
return ""
class LuckyNumber(Game):
"""
猜数字
"""
name = "LuckyNumber"
max_bet_gold:int = max_bet_gold
def __init__(self, **kwargs):
super().__init__()
gold = kwargs["gold"]
self.gold:int = gold
self.number:int = random.randint(1,100)
self.session.gold = gold
@staticmethod
def parse_arg(arg:str):
if arg.isdigit():
gold = int(arg)
else:
gold = int(bet_gold/10)
return {"gold":gold}
async def action(self, bot:Bot, user_id:int, *args):
"""
猜数字
"""
session = self.session
if msg := session.shot_check(user_id):
return None if msg == " " else msg
session.gold += self.gold
session.gold = min(session.gold, session.bet_limit)
session.nextround()
N = args[0]
TrueN = self.number
if N == TrueN:
session.win = user_id
await self.end(bot)
else:
if N > TrueN:
msg = f"{N}比这个数字大\n金额:{session.gold}"
else:
msg = f"{N}比这个数字小\n金额:{session.gold}"
await bot.send_group_msg(group_id = session.group_id, message = msg)
def game_tips(self, msg):