-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
593 lines (582 loc) · 17.8 KB
/
bot.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
# coding: utf-8
from wxpy import *
import sqlite3
import random
import time
import asyncio
conn = sqlite3.connect('mafia.db')
conn.close()
robot = Robot('wx.cookie')
groupName = '天黑请闭眼'
game = {
'create' : '新游戏',
'join' : '加入',
'quit' : '退出',
'start' : '开始游戏',
'end' : '结束游戏',
'choose' : '票'
}
@robot.register()
def mafia(data):
msg = data.text
msgFrom = str(data.chat)
name = str(data.member)[9:-1]
# 群消息处理
if(msgFrom.find(groupName) != -1):
if(msg == game['create']):
return newGame()
if(msg == game['end']):
return endGame()
if(msg == game['join']):
userid = data.raw['acturename']
return joinGame(name, userid)
if(msg == game['quit']):
return quitGame(name)
if(msg == game['start']):
return startGame()
# 私聊消息处理
if gaming():
players = getSpecialAll()
for player in players:
if(msgFrom.find(player) != -1):
if(msg == game['choose']):
def iinit():
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('create table if not exists user (id int primary key, name varchar(20))')
db.close()
conn.commit()
conn.close()
print('读取用户配置,如不存在则自动创建...')
except:
print('数据库连接失败')
def newGame():
if not gaming():
if not waiting():
try:
conn = sqlite3.connect('mafia.db')
print('有人发起了新游戏')
db = conn.cursor()
db.execute('create table player (id int, user_id varchar(50), name text primary key, type int, status int, choose int, vote int, votable int)')
db.close()
conn.commit()
conn.close()
return '游戏创建成功,正在等待玩家加入...\n发送"加入"即可加入游戏。'
except:
return '未知错误,请联系管理员。'
else:
return '当前有正在进行中的游戏,请结束游戏后在开新局。'
else:
return '当前有正在进行中的游戏,请结束游戏后在开新局。'
def joinGame(name, userid):
if not gaming():
if waiting():
print('有人想加入游戏,游戏人数:'+ getWait())
try:
print('有人想加入游戏,游戏人数:'+ getWait())
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('insert into player (id, user_id, name, votable) values (?, ?, ?, 0)',(getId(), userid, name,))
db.execute('insert into player (id, user_id, name, votable) values (2, 556464, wq, 0)')
db.execute('insert into player (id, user_id, name, votable) values (3, 56464, w321q, 0)')
db.execute('insert into player (id, user_id, name, votable) values (4, 5464, wq23, 0)')
db.execute('insert into player (id, user_id, name, votable) values (5, 564614, w5q, 0)')
db.execute('insert into player (id, user_id, name, votable) values (6, 516464, w5641q, 0)')
db.execute('insert into player (id, user_id, name, votable) values (7, 562464, wq32, 0)')
db.execute('insert into player (id, user_id, name, votable) values (8, 564164, w1q, 0)')
db.close()
conn.commit()
conn.close()
return name + ' 加入了游戏。\n' + getWait()
except:
return '你已经加入游戏了。'
else:
return '操作失败,没有等待中的游戏。'
else:
return '操作失败,没有游戏。'
def quitGame(name):
if not gaming():
if waiting():
try:
print('有人退出游戏')
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select * from player where name = ?', (name,))
if not db.fetchall():
db.close()
conn.commit()
conn.close()
print('退出的玩家不在游戏中')
return '退出游戏失败,你不在游戏中。'
else:
db.execute('delete from player where name = ?',(name,))
db.close()
conn.commit()
conn.close()
return name + ' 退出了游戏。\n' + getWait(1)
except:
return '未知错误,请联系管理员。'
else:
return '操作失败,没有等待中的游戏。'
else:
return '操作失败,游戏已经开始了。'
def endGame():
if not gaming():
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('drop table player')
db.close()
conn.commit()
conn.close()
return '游戏结束成功,现在可以发起新的游戏。'
except:
return '未知错误。'
else:
return '操作失败,游戏未开始。'
def startGame():
if not gaming():
if waiting():
player = getWait()
killer = getKiller(player)
actor = []
n = 0
while n < killer:
n = n+1
actor.append(1)
actor.append(2)
m = 0
pm = player-killer*2
while m < pm
m = m+1
actor.append(0)
random.shuffle(actor)
# 分配角色
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
num = player
# 创建游戏进程
db.execute('create table game (days int, time int, voting int)')
db.execute('insert into game (days) values (1)')
for actType in actor:
db.execute('update player set type=?,status=? where id=?', (actType, 1,num,))
# 发送身份
sendActById(num, act)
num = num-1
db.close()
conn.commit()
conn.close()
sendGroup(groupName, '游戏即将在10秒内开始,已将各位的身份私聊发送出去,请确认好自己的身份。')
time.sleep(10)
sendGroup(groupName, '天黑了,所有人请闭眼。\n杀手出来杀人,私聊法官票选目标。')
return '天黑了。'
except:
return '未知错误,请联系管理员。'
else:
return '操作失败,没有等待中的游戏。'
else:
return '操作失败,没有游戏。'
# 获取游戏数据
def getId():
try:
print('尝试获取玩家数...')
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select max(id) from player')
print('数据库操作成功')
value = db.fetchall()
db.close()
conn.close()
Id = value[0][0]
if not Id:
print('没有玩家')
Id = 0
print('已有玩家:'+ str(Id))
return '2'
except:
return '1'
def gaming():
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select * from game')
value = db.fetchall()
db.close()
conn.close()
return True
except:
return False
def waiting():
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select * from player')
value = db.fetchall()
db.close()
conn.close()
return True
except:
return False
def getWait(plus=0):
num = int(getId())-1
if(num < 7):
return '还需要 ' + str(7-num+plus) + ' 人即可开始游戏。'
else:
return '人数已达到要求。\n你可以继续等待或发送“开始游戏”来开始。'
def getKiller(num=8):
last = num%4
killer = (num-last)/4
return killer
def getPlayerAll():
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select name from player')
plist = db.fetchall()
db.close()
conn.close()
return plist
except:
return [0]
def getSpecialAll():
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select name from player where type != 0')
plist = db.fetchall()
db.close()
conn.close()
return plist
except:
return [0]
def getKillerAll():
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select name from player where type = 2')
plist = db.fetchall()
db.close()
conn.close()
return plist
except:
return [0]
def getGoodAll():
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select name from player where type = 1')
plist = db.fetchall()
db.close()
conn.close()
return plist
except:
return [0]
def getNameById(userid):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select name from player where id = ?',(userid,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return res
except:
return '获取玩家名称失败。'
def getIdbyName(name):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select id from player where name = ?',(name,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return res
except:
return '获取玩家id失败。'
def getChoiceById(userid):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select choose from player where id = ?',(userid,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return res
except:
return '获取玩家投票目标失败。'
def getChoiceByName(name):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select choose from player where name = ?',(name,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return res
except:
return '获取玩家投票目标失败。'
def getVoteById(userid):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select vote from player where id = ?',(userid,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return res
except:
return '获取玩家票数失败。'
def getVoteByName(name):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select vote from player where name = ?',(name,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return res
except:
return '获取玩家票数失败。'
def getTypeById(ref):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select type from player where id = ?',(ref,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return res
except:
return '获取玩家身份失败。'
def getTypeByName(ref):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select type from player where name = ?',(ref,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return res
except:
return '获取玩家身份失败。'
def getStatusById(ref):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select status from player where id = ?',(ref,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return res
except:
return '获取玩家生存状态失败。'
def getStatusByName(ref):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select status from player where name = ?',(ref,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return res
except:
return '获取玩家生存状态失败。'
def getUserIdById(ref):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select user_id from player where id = ?',(ref,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return res
except:
return '获取玩家user_id失败。'
def getUserIdByName(ref):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select user_id from player where id = ?',(ref,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return res
except:
return '获取玩家user_id失败。'
def getVotableById(ref):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select votable from player where id = ?',(ref,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
if (res == 0):
return False
else:
return True
except:
return False
def getVotableByName(ref):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select user_id from player where name = ?',(ref,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
if (res == 0):
return False
else:
return True
except:
return False
def getGameDays(ref):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select days from game',(ref,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return int(res)
except:
return '获取游戏天数失败。'
def getGameTime():
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select time from game',(ref,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return int(res)
except:
return '获取白天黑夜失败。'
# 游戏过程
def setVotableById(ref):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('update player set votable = 1 where id = ?',(ref,))
db.close()
conn.commit()
conn.close()
return True
except:
return False
def setVotableByName(ref):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('update player set votable = 1 where name = ?',(ref,))
db.close()
conn.commit()
conn.close()
return True
except:
return False
def nextDay():
try:
days = getGameDays()+1
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('update game set days = days + 1',(ref,))
res = db.fetchall()
res = res[0][0]
db.close()
conn.commit()
conn.close()
return '第二天到了。'
except:
return '错误,进入下一天失败。'
def nextTime():
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
now = getGameTime()
if(now < 4):
db.execute('update game set time = time + 1',(ref,))
db.close()
conn.commit()
conn.close()
if (now+1 == 2):
return '黄昏来临,杀手即将出现!'
if (now+1 == 4):
return '天快亮了,请速速投票!'
else:
db.execute('update game set time = 1',(ref,))
db.close()
conn.commit()
conn.close()
except:
return '错误,进入下一时段失败。'
# 消息处理
def sendActById(num, act):
actor = ''
if (act == 0):
actor = '平民'
if (act == 1):
actor = ''
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('select * from player where id = ?', (num,))
name = db.fetchall()[0]['name']
db.close()
conn.close()
friend = robot.friends().search(name)[0]
friend.send('你的身份是: ' + actor)
def sendGroup(group, text):
group = robot.groups().search(group)[0]
group.send(text)
def vote(name, who):
vote = 'choose=' + str(who)
where = 'where name =' + name
setValue(vote, where, 'player')
return '你投票了 '+str(who)+' 号:'
def setValue(exe, where, table):
try:
conn = sqlite3.connect('mafia.db')
db = conn.cursor()
db.execute('update ? set ? where ?',(table, exe, where,))
db.close()
conn.commit()
conn.close()
return '修改字段值成功'
except:
return '获取字段值失败。'
robot.start()