-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
2041 lines (1683 loc) · 82.1 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
import os
import csv
import random
from plugins import *
from common.log import logger
from bridge.context import ContextType, Context
from bridge.reply import Reply, ReplyType
from channel.chat_message import ChatMessage
import plugins
import time
from typing import Optional
from .player import Player
from .fishing_system import FishingSystem
import datetime
from .shop import Shop
from .item import Item
from .equipment import Equipment
import json
from .monopoly import MonopolySystem
@plugins.register(
name="Game",
desc="一个简单的文字游戏系统",
version="0.2.2",
author="assistant",
desire_priority=0
)
class Game(Plugin):
# 将 STANDARD_FIELDS 定义为类变量
STANDARD_FIELDS = [
'user_id', 'nickname', 'gold', 'level', 'last_checkin',
'inventory', 'hp', 'max_hp', 'attack', 'defense', 'exp',
'last_fishing', 'rod_durability', 'equipped_weapon', 'equipped_armor',
'last_item_use', 'spouse', 'marriage_proposal', 'last_attack',
'position'
]
# 添加开关机状态和进程锁相关变量
PROCESS_LOCK_FILE = "game_process.lock"
game_status = True # 游戏系统状态
scheduled_tasks = {} # 定时任务字典
# 添加新的类变量
REMINDER_COST = 50 # 每条提醒消息的费用
REMINDER_DURATION = 24 * 60 * 60 # 提醒持续时间(24小时)
def __init__(self):
super().__init__()
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
# 添加进程锁和状态恢复逻辑
try:
self.data_dir = os.path.join(os.path.dirname(__file__), "data")
os.makedirs(self.data_dir, exist_ok=True)
# 初始化进程锁文件路径
self.process_lock_file = os.path.join(self.data_dir, self.PROCESS_LOCK_FILE)
# 恢复游戏状态和定时任务
self._restore_game_state()
# 确保数据目录""" """存在
self.player_file = os.path.join(self.data_dir, "players.csv")
self.shop_file = os.path.join(self.data_dir, "shop_items.csv")
# 初始化物品系统
self.item_system = Item(self.data_dir)
self.item_system.init_default_items()
# 初始化商店数据文件
if not os.path.exists(self.shop_file):
with open(self.shop_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['name', 'price'])
# 写入默认商品
default_items = [
['木剑', '100'],
['铁剑', '300'],
['布甲', '150'],
['铁甲', '400'],
['面包', '20'],
['药水', '50'],
['道生羽的节操', '1'],
['木制鱼竿', '200'],
['铁制鱼竿', '500'],
['金制鱼竿', '1000']
]
writer.writerows(default_items)
# 初始化玩家数据文件
if not os.path.exists(self.player_file):
with open(self.player_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(self.STANDARD_FIELDS)
# 初始化钓鱼系统
self.fishing_system = FishingSystem(self.data_dir)
self.shop = Shop(self)
# 初始化装备系统
self.equipment_system = Equipment(self)
# 初始化提醒系统
self.reminders = {} # 格式: {user_id: {'content': str, 'expire_time': int}}
self._load_reminders() # 从文件加载提醒
# 初始化配置文件
config_file = os.path.join(self.data_dir, "config.json")
if not os.path.exists(config_file):
default_config = {
"admins": ["xxx"] # 默认管理员列表
}
with open(config_file, 'w', encoding='utf-8') as f:
json.dump(default_config, f, ensure_ascii=False, indent=2)
# 初始化大富翁系统
self.monopoly = MonopolySystem(self.data_dir)
except Exception as e:
logger.error(f"初始化游戏系统出错: {e}")
raise
def _migrate_data_files(self):
"""数据文件迁移和兼容性检查"""
# 标准字段列表
standard_player_fields = [
'user_id', 'nickname', 'gold', 'level', 'last_checkin',
'inventory', 'hp', 'max_hp', 'attack', 'defense', 'exp',
'last_fishing', 'rod_durability', 'equipped_weapon', 'equipped_armor',
'last_item_use', 'spouse', 'marriage_proposal', 'last_attack'
]
# 默认值设置
default_values = {
'gold': '0',
'level': '1',
'hp': '100',
'max_hp': '100',
'attack': '10',
'defense': '5',
'exp': '0',
'inventory': '[]',
'rod_durability': '{}',
'equipped_weapon': '',
'equipped_armor': '',
'last_item_use': '0',
'spouse': '',
'marriage_proposal': '',
'last_attack': '0'
}
if os.path.exists(self.player_file):
try:
# 读取所有现有数据
all_players = {}
with open(self.player_file, 'r', encoding='utf-8') as f:
reader = csv.DictdReader(f)
for row in reader:
# 跳过空行或无效数据
if not row.get('user_id') and not row.get('nickname'):
continue
# 使用user_id作为主键,如果没有user_id则使用nickname
key = row.get('user_id') or row.get('nickname')
if not key:
continue
# 如果已存在玩家记录,合并数据
if key in all_players:
# 保留非空值
for field in standard_player_fields:
if row.get(field):
all_players[key][field] = row[field]
else:
# 创建新记录
player_data = default_values.copy()
for field in standard_player_fields:
if row.get(field):
player_data[field] = row[field]
all_players[key] = player_data
# 确保user_id和nickname字段
if row.get('user_id'):
player_data['user_id'] = row['user_id']
if row.get('nickname'):
player_data['nickname'] = row['nickname']
# 写入整理后的数据
with open(self.player_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=standard_player_fields)
writer.writeheader()
for player_data in all_players.values():
# 确保所有��要字段都存在
for field in standard_player_fields:
if field not in player_data:
player_data[field] = default_values.get(field, '')
writer.writerow(player_data)
except Exception as e:
logger.error(f"数据迁移出错: {e}")
# 创建备份
backup_file = f"{self.player_file}.bak"
if os.path.exists(self.player_file):
import shutil
shutil.copy2(self.player_file, backup_file)
def _load_reminders(self):
"""从文件加载提醒数据"""
reminder_file = os.path.join(self.data_dir, "reminders.json")
if os.path.exists(reminder_file):
try:
with open(reminder_file, 'r', encoding='utf-8') as f:
self.reminders = json.load(f)
# 清理过期提醒
current_time = int(time.time())
self.reminders = {
k: v for k, v in self.reminders.items()
if v['expire_time'] > current_time
}
except Exception as e:
logger.error(f"加载提醒数据出错: {e}")
self.reminders = {}
def _save_reminders(self):
"""保存提醒数据到文件"""
reminder_file = os.path.join(self.data_dir, "reminders.json")
try:
with open(reminder_file, 'w', encoding='utf-8') as f:
json.dump(self.reminders, f, ensure_ascii=False, indent=2)
except Exception as e:
logger.error(f"保存提醒数据出错: {e}")
def set_reminder(self, user_id, content):
"""设置提醒"""
player = self.get_player(user_id)
if not player:
return "您还没有注册游戏"
if len(content.split()) < 2:
return "请使用正确的格式:提醒 内容"
reminder_content = ' '.join(content.split()[1:])
# 去除感叹号和加号
reminder_content = reminder_content.replace('!', '').replace('!', '').replace('+', '')
if len(reminder_content) > 50: # 限制提醒长度
return "提醒内容不能超过50个字符"
# 检查金币是否足够
if int(player.gold) < self.REMINDER_COST:
return f"设置提醒需要{self.REMINDER_COST}金币,金币不足"
# 扣除金币
new_gold = int(player.gold) - self.REMINDER_COST
self._update_player_data(user_id, {'gold': str(new_gold)})
# 保存提醒
self.reminders[user_id] = {
'content': reminder_content,
'expire_time': int(time.time()) + self.REMINDER_DURATION
}
self._save_reminders()
return f"提醒设置成功!消息将在24小时内显示在每条游戏回复后面\n花费: {self.REMINDER_COST}金币"
def get_active_reminders(self):
"""获取所有有效的提醒"""
current_time = int(time.time())
active_reminders = []
for user_id, reminder in self.reminders.items():
if reminder['expire_time'] > current_time:
player = self.get_player(user_id)
if player:
active_reminders.append(f"[{player.nickname}]: {reminder['content']}")
return "\n".join(active_reminders) if active_reminders else ""
def on_handle_context(self, e_context: EventContext):
if e_context['context'].type != ContextType.TEXT:
return
# 在处理任何命令前,先检查定时任务
self._check_scheduled_tasks()
content = e_context['context'].content.strip()
msg: ChatMessage = e_context['context']['msg']
# 获取用户ID作为主要标识符
current_id = msg.actual_user_id if msg.is_group else msg.from_user_id
# 修改这里:使用 sender 作为昵称
nickname = msg.actual_user_nickname if msg.is_group else msg.from_user_nickname
if not current_id:
return "无法获取您的ID,请确保ID已设置"
if not self.game_status and content not in ['注册', '开机', '关机', '定时', '查看定时', '取消定时', '清空定时']:
return "游戏系统当前已关闭"
logger.debug(f"当前用户信息 - current_id: {current_id}")
# 修改这里:更新 lambda 函数定义,使其接受两个参数
cmd_handlers = {
"注册": lambda i, n: self.register_player(i, n),
"状态": lambda i, n: self.get_player_status(i),
"个人状态": lambda i, n: self.get_player_status(i),
"签到": lambda i, n: self.daily_checkin(i),
"商店": lambda i, n: self.shop.show_shop(content),
"购买": lambda i, n: self.shop.buy_item(i, content),
"背包": lambda i, n: self.show_inventory(i),
"装备": lambda i, n: self.equip_from_inventory(i, content),
"游戏菜单": lambda i, n: self.game_help(),
"赠送": lambda i, n: self.give_item(i, content, msg),
"钓鱼": lambda i, n: self.fishing(i),
"图鉴": lambda i, n: self.show_fish_collection(i, content),
"出售": lambda i, n: self.shop.sell_item(i, content),
"批量出售": lambda i, n: self.shop.sell_item(i, content),
"外出": lambda i, n: self.go_out(i),
"使用": lambda i, n: self.use_item(i, content),
"排行榜": lambda i, n: self.show_leaderboard(i, content),
"求婚": lambda i, n: self.propose_marriage(i, content, msg),
"同意求婚": lambda i, n: self.accept_marriage(i),
"拒绝求婚": lambda i, n: self.reject_marriage(i),
"离婚": lambda i, n: self.divorce(i),
"攻击": lambda i, n: self.attack_player(i, content, msg),
"开机": lambda i, n: self.toggle_game_system(i, 'start'),
"关机": lambda i, n: self.toggle_game_system(i, 'stop'),
"定时": lambda i, n: self.schedule_game_system(i, content),
"查看定时": lambda i, n: self.show_scheduled_tasks(i),
"取消定时": lambda i, n: self.cancel_scheduled_task(i, content),
"清空定时": lambda i, n: self.clear_scheduled_tasks(i),
"提醒": lambda i, n: self.set_reminder(i, content),
"删除提醒": lambda i, n: self.delete_reminder(i),
"购买地块": lambda i, n: self.buy_property(i),
"升级地块": lambda i, n: self.upgrade_property(i),
"我的地产": lambda i, n: self.show_properties(i),
"地图": lambda i, n: self.show_map(i),
}
cmd = content.split()[0]
if cmd in cmd_handlers:
reply = cmd_handlers[cmd](current_id, nickname)
# 添加活动提醒
reminders = self.get_active_reminders()
if reminders:
reply += f"\n\n📢 当前提醒:\n{reminders}"
reply += "\n📢 如何使用提醒:\n设置提醒: 提醒 内容"
e_context['reply'] = Reply(ReplyType.TEXT, reply)
e_context.action = EventAction.BREAK_PASS
else:
e_context.action = EventAction.CONTINUE
def game_help(self):
import time
return """
🎮 游戏指令大全 🎮
基础指令
————————————
📝 注册 - 注册新玩家
📊 状态 - 查看当前状态
📅 签到 - 每日签到领取金币
物品相关
————————————
🏪 商店 - 查看商店物品
💰 购买 [物品名] - 购买物品
🎒 背包 - 查看背包物品
⚔️ 装备 [物品名] - 装备物品
🎁 赠送 [@用户] [物品名] [数量] - 赠送物品
💊 使用 [物品名] - 使用消耗品
交易相关
————————————
💸 出售 [物品名] [数量] - 出售物品(原价60%)
📦 批量出售 [类型] - 批量出售背包物品
冒险相关
————————————
🎣 钓鱼 - 进行钓鱼获取金币
📖 图鉴 - 查看鱼类图鉴
🌄 外出 - 外出探险冒险
👊 攻击 [@用户] - 攻击其他玩家
🗺️ 地图 - 查看游戏地图
地产相关
————————————
🏠 我的地产 - 查看玩家地产
🏘️ 购买地块 - 购买地块
🏘️ 升级地块 - 升级地块
社交系统
————————————
💕 求婚 [@用户] - 向玩家求婚
💑 同意求婚 - 同意求婚请求
💔 拒绝求婚 - 拒绝求婚请求
⚡️ 离婚 - 解除婚姻关系
其他功能
————————————
🏆 排行榜 [类型] - 查看排行榜
🔔 提醒 [内容] - 设置提醒
🗑️ 删除提醒 - 删除提醒
管理员功能
————————————
🔧 开机 - 开启游戏系统
🔧 关机 - 关闭游戏系统
⏰ 定时 [开机/关机] [时间] [每天] - 设置定时任务
📋 查看定时 - 查看定时任务
❌ 取消定时 [开机/关机] [时间] - 取消定时任务
🗑️ 清空定时 - 清空所有定时任务
系统时间: {}
""".format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
def register_player(self, user_id, nickname=None):
"""注册新玩家
Args:
user_id: 玩家ID
nickname: 玩家昵称,如果未提供则使用user_id
"""
if not user_id:
return "无法获取您的ID,请确保ID已设置"
# 检查是否已注册
if self.get_player(user_id):
return "您已经注册过了"
try:
# 如果没有提供昵称,使用user_id作为默认昵称
if not nickname:
nickname = str(user_id)
# 创建新玩家
player = Player.create_new(user_id, nickname)
player.player_file = self.player_file
player.standard_fields = self.STANDARD_FIELDS
# 保存玩家数据
with open(self.player_file, 'a', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=self.STANDARD_FIELDS)
writer.writerow(player.to_dict())
return f"注册成功!"
except Exception as e:
logger.error(f"注册玩家出错: {e}")
return "注册失败,请稍后再试"
def get_player(self, user_id) -> Optional[Player]:
"""获取玩家数据"""
try:
player = Player.get_player(user_id, self.player_file)
if player:
# 设置必要的文件信息
player.player_file = self.player_file
player.standard_fields = self.STANDARD_FIELDS
return player
except Exception as e:
logger.error(f"获取玩家数据出错: {e}")
raise
def fishing(self, user_id):
"""钓鱼"""
player = self.get_player(user_id)
if not player:
return "您还没注册,请先注册"
# 检查是否有鱼竿
inventory = player.inventory
rod = None
for item in inventory:
if item in ['木制鱼竿', '铁制鱼竿', '金制鱼竿']:
rod = item
break
if not rod:
return "您需要先购买一个鱼竿才能钓鱼"
# 检查冷却时间
now = datetime.datetime.now()
last_fishing_str = player.last_fishing
if last_fishing_str:
last_fishing = datetime.datetime.strptime(last_fishing_str, '%Y-%m-%d %H:%M:%S')
cooldown = datetime.timedelta(minutes=3) # 3分钟冷却时间
if now - last_fishing < cooldown:
remaining = cooldown - (now - last_fishing)
return f"钓鱼冷却中,还需等待 {remaining.seconds} 秒"
# 调用钓鱼系统
result = self.fishing_system.go_fishing(player, rod)
# 更新玩家数据
updates = {
'last_fishing': now.strftime('%Y-%m-%d %H:%M:%S')
}
# 处理耐久度
rod_durability = player.rod_durability
new_durability = max(0, rod_durability.get(rod, 100) - result['durability_cost'])
rod_durability[rod] = new_durability
updates['rod_durability'] = json.dumps(rod_durability)
# 如果钓到鱼
if result['success']:
new_inventory = inventory + [result['fish']['name']]
updates['inventory'] = json.dumps(new_inventory)
# 添加金币奖励
new_gold = int(player.gold) + result['coins_reward']
updates['gold'] = str(new_gold)
message = result['message'] # 使用钓鱼系返回的完整消息
else:
message = result['message']
# 处理鱼竿损坏
if new_durability <= 0:
inventory.remove(rod)
updates['inventory'] = json.dumps(inventory)
durability_warning = f"\n💔 {rod}已损坏,已从背包移除"
elif new_durability < 30:
durability_warning = f"\n⚠️警告:{rod}耐久度不足30%"
else:
durability_warning = ""
self._update_player_data(user_id, updates)
return f"{message}{durability_warning}"
def show_fish_collection(self, user_id, content=""):
"""显示鱼类图鉴"""
player = self.get_player(user_id)
if not player:
return "您还没有注册,请先注册 "
# 解析命令参数
parts = content.split()
page = 1
search_term = ""
if len(parts) > 1:
if parts[1].isdigit():
page = int(parts[1])
else:
search_term = parts[1]
return self.fishing_system.show_collection(player, page, search_term)
# 外出打怪
def go_out(self, user_id):
"""外出探险或漫步"""
player = self.get_player(user_id)
if not player:
return "您还没有注册游戏"
# 检查玩家状态
if int(player.hp) <= 0:
return "您的生命值不足,请先使用药品恢复"
# 检查冷却时间
current_time = int(time.time())
last_attack_time = int(player.last_attack)
cooldown = 60
if current_time - last_attack_time < cooldown:
remaining = cooldown - (current_time - last_attack_time)
return f"您刚刚进行过活动,请等待 {remaining} 秒后再次外出"
# 掷骰子
steps = self.monopoly.roll_dice()
# 获取当前位置
current_position = int(player.position) if hasattr(player, 'position') else 0
new_position = (current_position + steps) % self.monopoly.map_data["total_blocks"]
# 获取地块信息
block = self.monopoly.get_block_info(new_position)
# 更新玩家位置
self._update_player_data(user_id, {
'position': str(new_position),
'last_attack': str(current_time)
})
result = [
f"🎲 掷出 {steps} 点",
f"来到了 {block['name']}"
]
if block['type'] == '起点':
bonus = 200
new_gold = int(player.gold) + bonus
self._update_player_data(user_id, {'gold': str(new_gold)})
result.append(f"经过起点获得 {bonus} 金币")
elif block['type'] == '森林':
# 触发战斗
battle_result = self._battle(user_id, self._generate_monster(player))
result.append(battle_result)
elif block['type'] == '机遇':
event = self.monopoly.trigger_random_event()
if 'effect' in event:
for key, value in event['effect'].items():
if key == 'gold':
new_gold = int(player.gold) + value
self._update_player_data(user_id, {'gold': str(new_gold)})
# 添加金币变化提示
if value > 0:
result.append(f"💰 获得 {value} 金币")
else:
result.append(f"💸 失去 {abs(value)} 金币")
result.append(f"🎲 触发事件: {event['name']}")
result.append(event['description'])
elif block['type'] in ['空地', '直辖市', '省会', '地级市', '县城', '乡村']:
property_info = self.monopoly.get_property_owner(new_position)
if property_info is None or 'owner' not in property_info:
# 可以购买
price = self.monopoly.calculate_property_price(new_position)
result.append(f"这块地还没有主人")
result.append(f"区域类型: {block['region']}")
result.append(f"需要 {price} 金币购买")
result.append("发送'购买地块'即可购买")
print(f"[DEBUG] 玩家 {user_id} 访问了未拥有的地块,位置: {new_position}, 价格: {price}")
else:
# 需要付租金
owner = property_info['owner']
if user_id != owner: # 不是自己的地产才需要付租金
owner_player = self.get_player(owner)
if owner_player:
rent = self.monopoly.calculate_rent(new_position)
if int(player.gold) >= rent:
# 扣除玩家金币
new_player_gold = int(player.gold) - rent
self._update_player_data(user_id, {'gold': str(new_player_gold)})
# 增加房主金币
owner_new_gold = int(owner_player.gold) + rent
self._update_player_data(owner, {'gold': str(owner_new_gold)})
result.append(f"这是 {owner_player.nickname} 的地盘")
result.append(f"区域类型: {block['region']}")
result.append(f"支付租金 {rent} 金币")
result.append(f"当前金币: {new_player_gold}")
print(f"[INFO] 玩家 {user_id} 支付了 {rent} 金币租金给 {owner_player.nickname},剩余金币: {new_player_gold}")
else:
result.append(f"你的金币不足以支付 {rent} 金币的租金!")
print(f"[WARNING] 玩家 {user_id} 的金币不足以支付租金,当前金币: {player.gold}, 需要租金: {rent}")
else:
result.append("地产所有者信息异常,请联系管理员")
print(f"[ERROR] 无法获取地产所有者 {owner} 的信息,位置: {new_position}")
else:
result.append("这是你的地盘")
result.append(f"区域类型: {block['region']}")
if property_info.get('level', 0) < 3:
result.append("可以发送'升级地块'进行升级")
print(f"[INFO] 玩家 {user_id} 访问了自己的地盘,位置: {new_position}")
return "\n".join(result)
def _generate_monster(self, player):
"""根据玩家等级生成怪物"""
player_level = int(player.level)
level_factor = 1 + (player_level - 1) * 0.2
monsters = [
{
'name': '森林史莱姆',
'hp': int(60 * level_factor),
'attack': int(10 * level_factor),
'defense': int(6 * level_factor),
'exp': int(20 * level_factor),
'gold': int(30 * level_factor)
},
# ... 其他森林怪物
]
monster = random.choice(monsters)
if random.random() < 0.15: # 15%概率变异
monster['name'] = f"变异{monster['name']}"
monster['hp'] = int(monster['hp'] * 1.5)
monster['attack'] = int(monster['attack'] * 1.3)
monster['defense'] = int(monster['defense'] * 1.2)
monster['exp'] = int(monster['exp'] * 1.5)
monster['gold'] = int(monster['gold'] * 1.5)
return monster
def _battle(self, user_id, monster):
"""战斗系统"""
player = self.get_player(user_id)
# 获取玩家基础属性
player_base_hp = int(player.hp)
player_base_attack = int(player.attack)
player_base_defense = int(player.defense)
# 获取装备加成
weapon_bonus = self.equipment_system.get_weapon_bonus(player)
armor_reduction = self.equipment_system.get_armor_reduction(player)
# 获取护甲提供的生命值加成
hp_bonus = 0
if player.equipped_armor:
items_info = self.item_system.get_all_items()
if player.equipped_armor in items_info:
armor_info = items_info[player.equipped_armor]
hp_bonus = int(armor_info.get('hp', 0))
# 计算总属性
player_total_hp = player_base_hp + hp_bonus
player_total_attack = player_base_attack + weapon_bonus
player_total_defense = player_base_defense + int(armor_reduction * player_base_defense)
monster_hp = monster['hp']
monster_max_hp = monster['hp']
monster_defense = monster['defense']
battle_log = [f"⚔️ 遭遇了 {monster['name']}"]
battle_log.append(f"\n你的属性:")
battle_log.append(f"❤️ 生命值: {player_total_hp} (基础{player_base_hp} / 装备{hp_bonus})")
battle_log.append(f"⚔️ 攻击力: {player_total_attack} (基础{player_base_attack} / 装备{weapon_bonus})")
battle_log.append(f"🛡️ 防御力: {player_total_defense} (基础{player_base_defense} / 装备{int(armor_reduction * player_base_defense)})")
battle_log.append(f"\n怪物属性:")
battle_log.append(f"❤️ 生命值: {monster['hp']}")
battle_log.append(f"⚔️ 攻击力: {monster['attack']}")
battle_log.append(f"🛡️ 防御力: {monster['defense']}")
# 怪物是否狂暴状态
is_berserk = False
round_num = 1
important_events = []
# 使用总生命值进行战斗
player_hp = player_total_hp
while player_hp > 0 and monster_hp > 0:
# 玩家攻击
damage = max(1, player_total_attack - monster_defense)
final_damage = int(damage * random.uniform(0.8, 1.2))
monster_hp -= final_damage
if round_num <= 5:
battle_log.append(f"\n第{round_num}回合")
battle_log.append(f"你对{monster['name']}造成 {final_damage} 点伤害")
# 检查怪物是否进入狂暴状态
if not is_berserk and monster_hp < monster_max_hp * 0.3 and random.random() < 0.4:
is_berserk = True
monster['attack'] = int(monster['attack'] * 1.5)
if round_num <= 5:
battle_log.append(f"💢 {monster['name']}进入狂暴状态!")
else:
important_events.append(f"第{round_num}回合: {monster['name']}进入狂暴状态!")
# 怪物反击
if monster_hp > 0:
damage_multiplier = random.uniform(0.8, 1.2)
base_damage = max(1, monster['attack'] - player_total_defense)
monster_damage = int(base_damage * damage_multiplier)
player_hp -= monster_damage
# 狂暴状态下吸血
if is_berserk:
life_steal = int(monster_damage * 0.3)
monster_hp = min(monster_max_hp, monster_hp + life_steal)
if round_num <= 5:
battle_log.append(f"{monster['name']}对你造成 {monster_damage} 点伤害,并吸取了 {life_steal} 点生命值")
else:
if round_num <= 5:
battle_log.append(f"{monster['name']}对你造成 {monster_damage} 点伤害")
round_num += 1
if round_num > 5:
battle_log.append(f"\n战斗持续了{round_num}回合")
if important_events:
battle_log.append("重要事件:")
battle_log.extend(important_events)
if player_hp > 0:
# 根据怪物等级增加经验值
player_level = int(player.level)
monster_level = int(monster['exp'] / 15) # 根据基础经验值估算怪物等级
level_diff = monster_level - player_level
exp_multiplier = 1.0
if level_diff > 0:
exp_multiplier = 1 + (level_diff * 0.2) # 每高一级增加20%经验
elif level_diff < 0:
exp_multiplier = max(0.2, 1 + (level_diff * 0.1)) # 每低一级减少10%经验,最低20%
exp_gain = int(monster['exp'] * exp_multiplier)
gold_gain = monster['gold']
new_exp = int(float(player.exp)) + exp_gain
new_gold = int(player.gold) + gold_gain
level_up = False
exp_needed = 100 * (1 + (int(player.level) - 1) * 0.5)
if new_exp >= exp_needed:
new_level = int(player.level) + 1
new_exp -= exp_needed
level_up = True
# 使用固定增长值
hp_increase = 50 # 每级+50血量
attack_increase = 15 # 每级+15攻击
defense_increase = 10 # 每级+10防御
new_max_hp = int(player.max_hp) + hp_increase
new_attack = int(player.attack) + attack_increase
new_defense = int(player.defense) + defense_increase
self._update_player_data(user_id, {
'level': str(new_level),
'max_hp': str(new_max_hp),
'attack': str(new_attack),
'defense': str(new_defense)
})
self._update_player_data(user_id, {
'hp': str(player_hp),
'exp': str(new_exp),
'gold': str(new_gold)
})
battle_log.append(f"\n🎉 战斗胜利")
if exp_multiplier != 1.0:
battle_log.append(f"经验值倍率: x{exp_multiplier:.1f}")
battle_log.append(f"获得 {exp_gain} 经验值")
battle_log.append(f"获得 {gold_gain} 金币")
if level_up:
battle_log.append(f"\n🆙 升级啦!当前等级 {new_level}")
battle_log.append("属性提升:")
battle_log.append(f"❤️ 生命上限 +{hp_increase}")
battle_log.append(f"⚔️ 攻击力 +{attack_increase}")
battle_log.append(f"🛡️ 防御力 +{defense_increase}")
else:
self._update_player_data(user_id, {'hp': '0'})
battle_log.append(f"\n💀 战斗失败")
battle_log.append("你被打倒了,需要使用药品恢复生命值")
return "\n".join(battle_log)
def use_item(self, user_id, content):
"""使用物品功能"""
try:
# 解析命令,格式为 "使用 物品名" 或 "使用 物品名 数量"
parts = content.split()
if len(parts) < 2:
return "使用格式错误!请使用: 使用 物品名 [数量]"
item_name = parts[1]
amount = 1 # 默认使用1个
if len(parts) > 2:
amount = int(parts[2])
if amount <= 0:
return "使用数量必须大于0"
except (IndexError, ValueError):
return "使用格式错误!请使用: 使用 物品名 [数量]"
# 检查玩家是否存在
player = self.get_player(user_id)
if not player:
return "您还没注册,请先注册 "
# 获取物品信息
items = self.get_shop_items()
if item_name not in items:
return "没有这个物品"
# 检查背包中是否有足够的物品
inventory = player.inventory # 直接使用列表,不需要json.loads
item_count = inventory.count(item_name)
if item_count < amount:
return f"背包中只有 {item_count} 个 {item_name}"
# 获取物品类型和效果
item = items[item_name]
# 判断物品类型
if item.get('type') != 'consumable':
return "该物品不能直接使用"
# 计算恢复效果
current_hp = int(player.hp)
max_hp = int(player.max_hp)
heal_amount = int(item.get('hp', 0)) * amount
# 计算新的生命值
new_hp = min(current_hp + heal_amount, max_hp)
# 从背包中移除物品
for _ in range(amount):
inventory.remove(item_name)
# 添加物品使用冷却时间
current_time = int(time.time())
try:
last_use = player.last_item_use
except AttributeError:
# 如果属性不存在,则默认为0
last_use = 0
if current_time - int(last_use) < 5: # 5秒冷却时间
return f"物品使用太频繁,请等待{5 - (current_time - int(last_use))}秒"
# 更新玩家数据时添加使用时间
updates = {
'inventory': json.dumps(inventory),
'hp': str(new_hp),
'last_item_use': str(current_time)
}
# 如果玩家数据中没有last_item_use字段,确保它被添加到标准字段中
if hasattr(player, 'standard_fields') and player.standard_fields and 'last_item_use' not in player.standard_fields:
player.standard_fields.append('last_item_use')
player.update_data(updates)
return f"使用 {amount} 个 {item_name},恢复 {new_hp - current_hp} 点生命值!\n当前生命值: {new_hp}/{max_hp}"
def get_player_status(self, user_id):
"""获取玩家状态"""
player = self.get_player(user_id)
if not player:
return "您还没注册,请先注册 "
# 获取物品信息
items_info = self.item_system.get_all_items()
# 使用Player类的get_player_status方法
return player.get_player_status(items_info)
def daily_checkin(self, user_id):
"""每日签到"""
try:
logger.info(f"用户 {user_id} 尝试进行每日签到")
player = self.get_player(user_id)
if not player:
logger.warning(f"用户 {user_id} 未注册,无法签到")
return "您还没注册,请先注册 "
import datetime
today = datetime.datetime.now().strftime('%Y-%m-%d')
logger.info(f"当前日期: {today}")
# 检查签到状态
if player.last_checkin == today:
logger.info(f"用户 {user_id} 今天已经签到过了")
return "您今天已经签到过了"
# 计算奖励
reward = 500 # 签到奖励50金币
exp_reward = 50 # 签到奖励10经验
logger.info(f"用户 {user_id} 签到奖励: {reward}金币, {exp_reward}经验")
# 更新数据
updates = {
'gold': player.gold + reward,
'exp': player.exp + exp_reward,
'last_checkin': today
}
self._update_player_data(user_id, updates)
logger.info(f"用户 {user_id} 数据更新成功: {updates}")
return f"签到成功 获得{reward}金币,经验{exp_reward},当前金币: {player.gold + reward}"
except Exception as e:
logger.error(f"用户 {user_id} 签到出错: {e}")
return f"签到失败: {str(e)}"
def get_shop_items(self) -> dict:
"""获取商店物品列表"""
return self.item_system.get_shop_items()
def give_item(self, user_id, content, msg: ChatMessage):
# 解析命令参数
parts = content.split()
if len(parts) < 4:
return "格式错误!请使用: 赠送 @用户 物品名 数量"
# 获取被赠送者ID
if not msg.is_group:
return "只能在群聊中使用赠送功能"
target_id = None
# 解析@后面的用户名