def answer_game_start(msg): ''' 3号消息 客户端设备号,0,3,'' ''' dev_num = msg.send bs = msg.MSG2BYTE() if gv.game_page == 1: # 首位有人坐 if gv.site_list[0] == dev_num: player_num = 0 for s in gv.site_list: if s != 0: player_num = player_num + 1 if player_num >= 4: m_ans = com.GAMEMSG(0, dev_num, 0, bs) m_all = com.GAMEMSG(0, 0, -2, bs) com.send(m_ans) com.send(m_all) gv.player_num = player_num set_game_page(2) return m_ans = com.GAMEMSG(0, dev_num, -1, bs) com.send(m_ans)
def answer_site_set(msg): ''' 2号消息 客户端设备号,0,2,座位号 ''' dev_num = msg.send bs = msg.MSG2BYTE() site_num = msg.msg[0] if gv.game_page == 1 and site_num < 8 and gv.name_dict[dev_num] != '': # 非空座 if gv.site_list[site_num] != 0: m_ans = com.GAMEMSG(0, dev_num, -1, bs) com.send(m_ans) # 空座 else: if dev_num in gv.site_list: gv.site_list[gv.site_list.index(dev_num)] = 0 gv.site_list[site_num] = dev_num print('site_list', gv.site_list) m_ans = com.GAMEMSG(0, dev_num, 0, bs) m_all = com.GAMEMSG(0, 0, -2, bs) com.send(m_ans) com.send(m_all) else: m_ans = com.GAMEMSG(0, dev_num, -1, bs) com.send(m_ans)
def answer_game_start(msg): ''' 4号消息 客户端设备号,0,消息号,0,'' -4号消息 0,客户端设备号,消息号,1,答复 0 可以开始 1 人数不足 2 没有权限 -5号消息 0,0,消息号,0,'' ''' dev_num = msg.send m_ok = com.GAMEMSG(0, dev_num, -4, 1, bytes([0])) m_not_enough = com.GAMEMSG(0, dev_num, -4, 1, bytes([1])) m_no_power = com.GAMEMSG(0, dev_num, -4, 1, bytes([2])) m_all = com.GAMEMSG(0, 0, -5, 0, bytes([])) site_dict = gv.site_dict if 0 in site_dict.keys(): if site_dict[0] == dev_num: player_num = site_dict.__len__() if player_num >= 4: com.send(m_ok) com.send(m_all) # 游戏开始 gv.game_mode = 2 # 生成有序座位表 site_order_list = [] for k in site_dict.keys(): site_order_list.append(k) site_order_list.sort() gv.site_order_list = site_order_list # 生成牌库,洗牌,发牌 build_deck() # 查找第一发现人 else: com.send(m_not_enough) else: com.send(m_no_power) else: com.send(m_no_power)
def require_game_start(): ''' 4号消息 自己设备号,0,消息号,0,'' ''' game_mode = gv.game_mode if game_mode == 0: # 一些图形操作 print('你不能开始游戏,请先起个昵称,并找个座位坐下') elif game_mode == 1: site_num = gv.my_site_num if site_num == -1: # 一些图形操作 print('你不能开始游戏,请找个座位坐下') elif site_num != 0: # 一些图形操作 print('你不能开始游戏,只有0号位玩家可以宣布开始游戏') else: m = com.GAMEMSG(gv.dev_num, 0, 4, 0, bytes([])) com.send(m) else: # 一些图形操作 print('不能重复开始游戏')
def require_site_set(site_num): ''' 2号消息 自己设备号,0,2,座位号 ''' if gv.game_page == 1: if gv.name == '': # 一些图形操作 print('起名后才能选座') else: m = com.GAMEMSG(gv.dev_num, 0, 2, bytes([site_num])) com.send(m) if gv.site_num == -1: # 一些图形操作 print('锁定座位中') else: # 一些图形操作 print('尝试换位中')
def deal_keyboard(): '''处理键盘输入的各种命令''' word = gv.word if word: if word[0] == '$': if word == '$exit': # 关闭通讯模块 com.close() return 1 else: # 命令分解 cmd_list = word.split() gv.word = '' # 修改昵称 if cmd_list[0] == '$name': gamectrl.require_name_set(cmd_list[1]) # 修改座位 elif cmd_list[0] == '$sit': gamectrl.require_site_set(int(cmd_list[1]), 0) elif cmd_list[0] == '$leave': gamectrl.require_site_set(int(cmd_list[1]), 1) # 开始游戏 elif cmd_list[0] == '$start': gamectrl.require_game_start() # 打牌 elif cmd_list[0] == '$play': gamectrl.require_play_card(int(cmd_list[1])) # 测试 elif cmd_list[0] == '$test': # 测试 多信息密集发送 是否能做到 接收端 自动 分割 if cmd_list[1] == 'msgs': msg = com.GAMEMSG(gv.dev_num, 0, 0, 1, bytes([99])) com.send(msg) com.send(msg) com.send(msg) # 方便测试,自动输入加入房间后的各类指令 elif cmd_list[1] == '0': gamectrl.require_name_set('a') gamectrl.require_site_set(0, 0) elif cmd_list[1] == '1': gamectrl.require_name_set('b') gamectrl.require_site_set(1, 0) elif cmd_list[1] == '2': gamectrl.require_name_set('c') gamectrl.require_site_set(2, 0) elif cmd_list[1] == '3': gamectrl.require_name_set('d') gamectrl.require_site_set(3, 0) return 0
def answer_site_set(msg): ''' 2号消息 需要批复 客户端设备号,0,消息号,2,[座位号 动作] 1 离开 0 坐下 -2号消息 0,客户端设备号,消息号,1,答复 0 可以 1 不可以 -3号消息 0,0,消息号,3,[设备号 座位号 动作] ''' dev_num = msg.send bs = msg.msg site_num = bs[0] site_act = bs[1] m_ok = com.GAMEMSG(0, dev_num, -2, 1, bytes([0])) m_no = com.GAMEMSG(0, dev_num, -2, 1, bytes([1])) m_all = com.GAMEMSG(0, 0, -3, 3, bytes([dev_num, site_num, site_act])) site_dict = gv.site_dict if site_act: if site_num in site_dict.keys(): if site_dict[site_num] == dev_num: com.send(m_ok) com.send(m_all) del gv.site_dict[site_num] else: com.send(m_no) else: com.send(m_no) else: if site_num in site_dict.keys(): com.send(m_no) else: gv.site_dict[site_num] = dev_num com.send(m_ok) com.send(m_all)
def set_game_page(page): if (page == 2): # 游戏开始 gv.game_page = 2 # 重新生成紧密排列的座位表 new_site_list = [0, 0, 0, 0, 0, 0, 0, 0] cnt = 0 for s in gv.site_list: if s != 0: new_site_list[cnt] = s cnt = cnt + 1 gv.site_list = new_site_list # 生成牌库,洗牌,发牌 ''' 普通人 0 不在场证明 1 第一发现人 2 共犯 3 目击者 4 谣言 5 情报交换 6 交易 7 犯人 8 侦探 9 神犬 10 ''' card_deck = [] player_num = gv.player_num card_num = player_num * 4 for i in range(card_num): card_deck.append(0) # 洗牌 # 发牌 for i in range(player_num): gv.player_card_list[i] = [] cnt = 0 for c in card_deck: gv.player_card_list[cnt].append(c) cnt = cnt + 1 if cnt >= player_num: cnt = 0 for i in range(player_num): dev_num = new_site_list[i] bs = bytes([dev_num] + gv.player_card_list[i]) m = com.GAMEMSG(0, dev_num, -3, bs) com.send(m)
def build_deck(): ''' 创建牌组,洗牌,并分配初始手牌 普通人 0 不在场证明 1 第一发现人 2 共犯 3 目击者 4 谣言 5 情报交换 6 交易 7 犯人 8 侦探 9 神犬 10 -12号消息 0,客户端设备号,消息号,1 + 2*卡牌个数,[自己/其他人设备号,[卡牌id,卡牌type],...] ''' player_num = gv.site_dict.__len__() card_num = player_num * 4 # <card_id, card_type> deck = dict() # 全部是普通人 for i in range(card_num): deck[i] = 0 # 洗牌 # 发牌 site_order_list = gv.site_order_list for site_num in site_order_list: gv.player_card_dict[site_num] = dict() for i in range(card_num): site_num = site_order_list[i % player_num] gv.player_card_dict[site_num][i] = deck[i] site_dict = gv.site_dict for site_num in site_order_list: card_list = gv.player_card_dict[site_num] dev_num = site_dict[site_num] bs = bytes([]) for card_id in card_list.keys(): card_type = card_list[card_id] bs = bs + bytes([card_id, card_type]) msg = com.GAMEMSG(0, dev_num, -12, 2 * 4 + 1, bytes([dev_num]) + bs) com.send(msg)
def answer_name_set(msg): ''' 1号消息 客户端设备号,0,1,名字 ''' bs = msg.MSG2BYTE() dev_num = msg.send if gv.game_page == 1: name = msg.msg.decode() m_all = com.GAMEMSG(0, 0, -2, bs) m_ans = com.GAMEMSG(0, dev_num, 0, bs) gv.name_dict[dev_num] = name com.send(m_ans) com.send(m_all) else: m_ans = com.GAMEMSG(0, dev_num, -1, bs) com.send(m_ans)
def answer_login(msg): ''' 10号消息 客户端设备号,0,10 ''' div_flag = bytes([253]) dev_num = msg.send bs = bytes(gv.dev.dict.keys()) + div_flag + \ bytes(gv.name_dict.keys()) + div_flag for v in gv.name_dict.values(): bs = bs + v.encode() + div_flag bs = bs + bytes(gv.site_list) + div_flag m = com.GAMEMSG(0, dev_num, -4, bs) com.send(m)
def require_site_set(site_num, site_act): ''' 2号消息 需要批复 自己设备号,0,消息号,2,[座位号 动作] 1 离开 0 坐下 ''' m = com.GAMEMSG(gv.dev_num, 0, 2, 2, bytes([site_num, site_act])) game_mode = gv.game_mode if game_mode == 0: # 一些图形操作 print('你现在还不能选座,请先起个昵称') elif game_mode == 1: if site_act: if gv.my_site_num != site_num: # 一些图形操作 print('你不能离开此座位,因为坐的人不是你') else: com.send(m) # 放入请求消息缓存 gv.msg_require.append(m) else: if gv.my_site_num != -1: # 一些图形操作 print('你已经坐下了,请先离开原座位再换座') else: com.send(m) # 放入请求消息缓存 gv.msg_require.append(m) else: # 一些图形操作 print('当前不能选座')
def require_name_set(name): ''' 1号消息 自己设备号,0,消息号,名字长度,名字 ''' game_mode = gv.game_mode if game_mode == 0: len0 = name.__len__() m = com.GAMEMSG(gv.dev_num, 0, 1, len0, name.encode()) com.send(m) # 一些图形操作 print('命名成功') gv.game_mode = 1 gv.my_name = name else: # 一些图形操作 print('你已经起过名字了')
def require_name_set(name): ''' 1号消息 自己设备号,0,1,名字 ''' if gv.game_page == 1: if gv.name == '': # 一些图形操作 print('命名请求中') m = com.GAMEMSG(gv.dev_num, 0, 1, name.encode()) com.send(m) else: # 一些图形操作 print('你已经起过名字了')
def require_game_start(): ''' 3号消息 自己设备号,0,3,'' ''' if gv.game_page == 1: if gv.site_num == 0: # 一些图形操作 print('正在请求开始游戏') m = com.GAMEMSG(gv.dev_num, 0, 3, bytes([])) com.send(m) else: # 一些图形操作 print('你不能开始游戏,只有0号位玩家可以宣布开始游戏')
def answer_name_set(msg): ''' 1号消息 客户端设备号,0,消息号,名字长度,名字 -1号消息 0,0,消息号,1 + 名字长度,[设备号 名字] ''' dev_num = msg.send name = msg.msg.decode() len0 = msg.len m_all = com.GAMEMSG(0, 0, -1, len0 + 1, bytes([dev_num]) + name.encode()) name_dict = gv.name_dict # 同一设备号的重命名将顶掉之前的昵称,但为了方便,目前在require函数中禁止重命名 if dev_num in name_dict.keys(): del gv.name_dict[dev_num] gv.name_dict[dev_num] = name com.send(m_all)
def deal_msg(): '''处理消息队列中的消息,丢给不同的模块的函数处理''' msg_list = gv.msg_list gv.msg_list = [] for m in msg_list: mm = com.GAMEMSG(0, 0, 0, bytes([])) mm.BYTE2MSG(m.msg) dev_num = mm.send t = m.type # 答复类消息 if t == 0 or t == -1: if dev_num == gv.dev_num: tt = mm.type if tt == 1: gamectrl.answer_name_set(t + 1, mm) elif tt == 2: gamectrl.answer_site_set(t + 1, mm) elif tt == 3: gamectrl.answer_game_start(t + 1) # 转达-通知类消息 elif t == -2: if dev_num != gv.dev_num: t = mm.type if t == 1: gamectrl.told_name_set(mm) elif t == 2: gamectrl.told_site_set(mm) elif t == 3: gamectrl.told_game_start() # 告知-通知类消息 elif t == -3: gamectrl.told_card_list(m) elif t == -4: gamectrl.told_game_room(m)
def require_login(): com.connect() m = com.GAMEMSG(gv.dev_num, 0, 10, bytes([])) com.send(m)