def update_room(uid, good_word=None, bad_word=None):
    delete_room()
    exist_room = Room.query.filter_by(owner_id=uid).first()

    if exist_room is None:
        send_message(uid, '请先创建房间')
        return

    if good_word is None and bad_word is None:
        words_path = os.path.join(os.path.dirname(__file__), '..', '..',
                                  'words.txt')

        with open(words_path, 'r') as f:
            lines = f.readlines()
            line_sample = sample(lines, 1)[0].strip()
            good_word, bad_word = line_sample.split(' ')

    num = exist_room.num
    room_id = exist_room.room_id
    white = exist_room.white
    bad_num = 0
    if num < 7:
        bad_num = 1
    elif num >= 7:
        bad_num = 2

    if white != -1:
        bad_number = sample(range(num), bad_num + 1)
        white_number = bad_number[-1] + 1
        bad_number = bad_number[:-1]
    else:
        white_number = -1
        bad_number = sample(range(num), bad_num)

    bad_number = [str(n + 1) for n in bad_number]
    str_bad_number = ','.join(bad_number)

    now = int(time.time())
    exist_room.update = now
    exist_room.good_word = good_word
    exist_room.bad_word = bad_word
    exist_room.bad_number = str_bad_number
    exist_room.white = white_number
    db.session.add(exist_room)
    db.session.commit()

    message = wrap_update_message(room_id, bad_num, num, bad_word, good_word,
                                  bad_number, white_number)
    send_message(uid, message)
Example #2
0
def parse_change(message):
    content = message['text']
    uid = message['from']['id']

    ss = content.split(' ')
    if len(ss) > 1:
        if len(ss) != 3:
            send_message(uid, '命令格式错误,如果需要自定义词语,请输入两个词')
            return
        else:
            good_word = ss[1]
            bad_word = ss[2]
            update_room(uid, good_word, bad_word)
    else:
        update_room(uid)
Example #3
0
def parse_message(message):
    content = message['text']
    uid = message['from']['id']

    if not content.startswith('/'):
        send_message(uid, '现在只能识别命令')
        return

    ss = content.split(' ')
    if ss[0] == '/new':
        parse_new(message)
    elif ss[0] == '/enter':
        parse_enter(message)
    elif ss[0] == '/change':
        parse_change(message)
Example #4
0
def parse_enter(message):
    content = message['text']
    uid = message['from']['id']

    ss = content.split(' ')
    if len(ss) != 2:
        send_message(uid, '命令格式错误,需要指定房间号')
        return

    room_id = ss[1]

    try:
        room_id = int(room_id)
    except ValueError:
        send_message(uid, '命令格式错误,房间号只能为数字')
        return
    enter_room(room_id, uid)
def init_room(num, uid, user_name, good_word=None, bad_word=None, white=False):
    delete_room()
    if num < 4:
        send_message(uid, '不能少于4个人')
        return
    elif num > 13:
        send_message(uid, '不能多于13个人')
        return

    exist_room = Room.query.filter_by(owner_id=uid).all()

    if len(exist_room) != 0:
        for room in exist_room:
            db.session.delete(room)
            db.session.commit()

    if good_word is None and bad_word is None:
        words_path = os.path.join(os.path.dirname(__file__), '..', '..',
                                  'words.txt')

        with open(words_path, 'r') as f:
            lines = f.readlines()
            line_sample = sample(lines, 1)[0].strip()
            good_word, bad_word = line_sample.split(' ')

    records = Room.query.all()

    exist_room_ids = [record.room_id for record in records]
    rows = int(os.getenv('ROWS'))
    all_room_ids = range(rows)
    ok_room_ids = list(set(all_room_ids) - set(exist_room_ids))
    room_id = choice(ok_room_ids)

    bad_num = 0
    if num < 7:
        bad_num = 1
    elif num >= 7:
        bad_num = 2

    if white:
        bad_number = sample(range(num), bad_num + 1)
        white_number = bad_number[-1] + 1
        bad_number = bad_number[:-1]
    else:
        white_number = -1
        bad_number = sample(range(num), bad_num)

    bad_number = [str(n + 1) for n in bad_number]
    str_bad_number = ','.join(bad_number)

    new_room = Room(room_id, num, good_word, bad_word, str_bad_number,
                    user_name, uid, white_number)
    db.session.add(new_room)
    db.session.commit()

    message = wrap_new_message(room_id, bad_num, num, bad_word, good_word,
                               bad_number, white_number)
    send_message(uid, message)
Example #6
0
def parse_new(message):
    content = message['text']
    uid = message['from']['id']

    ss = content.split(' ')
    if len(ss) < 2:
        send_message(uid, '命令格式错误,至少需要指定参与人数')
        return

    uid = message['from']['id']
    user_name = message['from']['username']

    try:
        num = int(ss[1])
    except ValueError:
        send_message(uid, '命令格式错误,人数只能是数字')
        return

    if len(ss) > 2:
        if len(ss) == 3:
            try:
                white = int(ss[2])
                if white == 1:
                    init_room(num, uid, user_name, white=True)
                else:
                    send_message(uid, '命令格式不正确')
                    return
            except ValueError:
                send_message(uid, '命令格式不正确')
                return

        if len(ss) == 4:
            good_word = ss[2]
            bad_word = ss[3]
            return init_room(num, uid, user_name, good_word, bad_word)
        if len(ss) == 5:
            try:
                good_word = ss[2]
                bad_word = ss[3]
                white = int(ss[4])
                if white == 1:
                    init_room(num, uid, user_name, good_word, bad_word, True)
                else:
                    send_message(uid, '命令格式不正确')
                    return
            except ValueError:
                send_message(uid, '命令格式不正确')
                return
        else:
            send_message(uid, '你输这么多参数干嘛?')
            return

    else:
        init_room(num, uid, user_name)
def enter_room(room_id, uid):
    delete_room()
    exist_room = Room.query.filter_by(room_id=room_id).first()
    if exist_room is None:
        send_message(uid, '房间不存在,请法官重新建房。')
        return

    room_owner = exist_room.owner_name
    owner_id = exist_room.owner_id
    num = exist_room.num
    bad_number = exist_room.bad_number
    white_number = exist_room.white
    bad_number = bad_number.split(',')
    bad_num = len(bad_number)
    good_word = exist_room.good_word
    bad_word = exist_room.bad_word

    if uid == owner_id:
        send_message(uid, '法官凑什么热闹。')
        return

    has_come = Member.query.filter_by(room_id=room_id, uid=uid).first()
    if has_come is not None:
        your_number = has_come.index
        if white_number != -1 and your_number == white_number:
            word = ''
        else:
            if str(your_number) in bad_number:
                word = bad_word
            else:
                word = good_word
        message = wrap_enter_message(room_id, room_owner, word, has_come.index,
                                     bad_num, num, white_number)
        send_message(uid, message)
        return

    member = Member(room_id, uid)
    db.session.add(member)
    db.session.commit()

    room_members = Member.query.filter_by(room_id=room_id).all()
    room_members = sorted(room_members, key=lambda x: x.enter_time)

    index = -1
    for i, m in enumerate(room_members):
        if m.uid == uid:
            index = i
    if index >= num:
        db.session.delete(room_members[index])
        db.session.commit()
        send_message(uid, '房间人数已满')
        return

    your_number = index + 1
    if white_number != -1 and your_number == white_number:
        word = ''
    else:
        if str(your_number) in bad_number:
            word = bad_word
        else:
            word = good_word
    member.index = your_number
    db.session.add(member)
    db.session.commit()

    message = wrap_enter_message(room_id, room_owner, word, your_number,
                                 bad_num, num, white_number)
    send_message(uid, message)