def whether_to_discard(self): room = rmg.get(self.roomid) if room == None: return False,None last_rule = room.last_rule if last_rule == None: return True,None if self.islandlord(): return True,last_rule if room.last_discard_player != None: if room.last_discard_player.islandlord(): return True,last_rule elif self.left_to_landlord(): if room.last_discard_player != self: #take over card chain if len(self._cards_list) -last_rule.count <= 2 : return True,last_rule elif last_rule.rule_type == pattern.single and (last_rule.origin_value < 11): last_rule = Rule([10],True) return True,last_rule else: return False,last_rule elif self.right_to_landlord(): if room.last_discard_player != self: if len(self._cards_list) -last_rule.count <= 2 : return True,last_rule elif last_rule.rule_type == pattern.single and (last_rule.origin_value < 14): return True,last_rule else: return False,last_rule return True,last_rule
def get_rule_nearly(self,last_rule): if self._cards_list == None: return room = rmg.get(self.roomid) rule = None if last_rule == None: index = len(self._cards_list)-1 card = 0 while index >= 0: card = self._cards_list[index] index -= 1 discards = cmg.try_get_pattern(card,self._cards_flat) if discards != None: rule = Rule(discards,True) if self.left_to_landlord(): if rule.rule_type == pattern.single: if room.landlord().get_counts() == 1: if card > 14: break if card > 10: break else: if rule.count != room.landlord().get_counts() or rule.count == self.get_counts(): break else: break if None == rule: index = len(self._cards_list)-1 card = 0 while index >= 0: card = self._cards_list[index] index -= 1 discards = cmg.try_get_pattern(card,self._cards_flat) if discards != None: rule = Rule(discards,True) break if None == rule : card = self._cards_list[len(self._cards_list)-1] got_count = self._cards_flat.get(card) or 0 discards = [card] * got_count rule = Rule(discards,True) else: index = len(self._cards_list)-1 card = 0 while index >= 0: card = self._cards_list[index] if card > last_rule.origin_value : discards = self.get_cards_nearly(index,last_rule) if discards != None: rule = Rule(discards,True) break index -= 1 if rule == None and last_rule.rule_type != pattern.bomb: discards = self.try_get_bomb() if discards != None: rule = Rule(discards,True) return rule
def show_last_discards(self): room = rmg.get(self.roomid) if room != None and room.last_rule != None: if room.last_rule.rule_type == pattern.bomb : msg = "%s discard a bomb ! %s "%(room.last_discard_player.nickname,cmg.tostr(room.last_rule.cards)) msg = color_html(msg,Color.orange) else: msg = "%s discard %s"%(room.last_discard_player.nickname,cmg.tostr(room.last_rule.cards)) if room.last_discard_player.is_warnning(): msg = "%s (left:%s)"%(msg,room.last_discard_player.get_counts()) self.sendmessage(s2c.message,0,msg)
def on_room_waiting(player,questid,answer): answer = toint(answer) if answer == 0: player.leave_room() player.askquestion(1) else: msg = "invalid input!" player.sendmessage(s2c.message,0,msg) room = rmg.get(player.roomid) if room != None: room.check_players(player.id)
def logout(websocket): print("logout") playerid = websockets_to_id.get(websocket) if playerid != None: del websockets_to_id[websocket] player = clients.get(playerid) if player != None: player.online = 0 player.save_to_db() room = rmg.get(player.roomid) if room != None: room.on_player_disconnect(player)
def on_room_join(player,questid,answer): numbers = toint(answer) if numbers != None : if numbers == 0: player.askquestion(1) else: room = rmg.get(numbers) print("on_room_join",numbers) if room != None: rmg.add_player(room,player) else: msg = "room not exsits,try again!" return player.askquestion_with_msg(msg,questid)
def showcards_soon(self): discard,last_rule = self.whether_to_discard() room = rmg.get(self.roomid) if discard : if room.is_new_chain(): rule = self.get_rule_nearly(None) room.discards(self,rule) else: rule = self.get_rule_nearly(last_rule) if rule != None: room.discards(self,rule) else: room.try_discards(self,None) else: room.try_discards(self,None)
def on_game_continue(player,questid,answer): answer = toint(answer) if answer == 2: player.leave_room() player.askquestion(1) else: player.ready = True room =rmg.get(player.roomid) if rmg.check_room_conditions(player) : if not room.isfull(): room.check_players() else: room.start_game() else: player.leave_room() player.askquestion(1)
def handle(self,data): data = data.strip() room = rmg.get(self.roomid) if room.cur_pos != self.room_pos: print("error handle,not your turn") return if len(data) > 0 and len(data[0]) > 0 and data[0] == '0': room.try_discards(self,None) return discards = [] b_count = self._cards_flat.get(JOKER_S) or 0 b_count += (self._cards_flat.get(JOKER_B) or 0) for word in data: card = cmg.str2card(word) if card != None: if card > Two and b_count > 0: count = self._cards_flat.get(card) or 0 if count <= 0: b_count -= 1 if card == JOKER_S: discards.append(JOKER_B) else: discards.append(JOKER_S) else: discards.append(card) else: discards.append(card) # valid = cmg.validate(discards) rule = Rule(discards,True) valid = rule.rule_type != None if valid: valid = self.validate(rule) if valid : room.try_discards(self,rule) else: self.showcards(color_html("not exsit cards,try again!",Color.red),s2c.handle) else: self.showcards(color_html("forbidden,try again!",Color.red),s2c.handle)
def login(websocket, cmd): username = cmd.get("username") secretid = cmd.get("secretid") or 0 playerid = None points = 0 freepoints = 0 ret = 0 col = get_player_info(username) if col != None: playerid, _, saved_secretid, points, freepoints = col if saved_secretid != secretid: ret = 2 else: ret = 0 else: confirm_secretid = cmd.get("confirm_secretid") if confirm_secretid == secretid: ret = 0 else: ret = 1 msg = {} msg["id"] = s2c.login.value msg["ret"] = ret msg["secretid"] = secretid msg["username"] = username if ret == 0: player = None if playerid != None: player = clients.get(playerid) if player == None: #new login player = create_player(secretid, username, playerid, points, freepoints) player.websocket = websocket clients[player.id] = player websockets_to_id[websocket] = player.id msg["playerid"] = player.id msg["points"] = player.points msg = json.dumps(msg) print(">", msg) messageQueue.put(websocket.send(msg)) player.askquestion(1) else: #reconnect msg["playerid"] = player.id msg["points"] = player.points msg = json.dumps(msg) print(">", msg) messageQueue.put(websocket.send(msg)) player.websocket = websocket websockets_to_id[websocket] = player.id room = rmg.get(player.roomid) if room != None: room.on_player_reconnected(player) else: player.askquestion(1) else: msg = json.dumps(msg) print(">", msg) messageQueue.put(websocket.send(msg))
def on_client_chat(player, data): room = rmg.get(player.roomid) if room != None: room.on_player_chat(player, data) else: player.sendmessage(s2c.chat, 0, data)
def right_to_landlord(self): room = rmg.get(self.roomid) return room.get_next_pos_by(room.landlord_pos) == self.room_pos
def left_to_landlord(self): room = rmg.get(self.roomid) return room.landlord_pos == room.get_next_pos_by(self.room_pos)
def islandlord(self): room = rmg.get(self.roomid) return room.landlord_pos == self.room_pos
def onlandlord_choose(player,questid,answer): answer = toint(answer) room = rmg.get(player.roomid) room.roll_landlord(answer == 1)