Exemplo n.º 1
0
    def User_disconnected(*args, **kwargs):
        if hasattr(ro.user, "room"):
            room = ro.user.room
            logging.getLogger("room").info("room_disconnected: <%s>room[%s]table[%s]" % (ro.user.id, roomid, tableid))
            if hasattr(ro.user.room, "maj"):
                ro.user.room.maj.game_exit(ro, ro.user.tableid)
                res = Buffer()
                res.write_ushort(1099)
                res.write_string("exit")
                yield room_cast_msg(room, res.getValue())
                for i in range(4):
                    user = room_get_user(room, i)
                    if not user:
                        continue
                    if hasattr(user.room, "maj"):
                        del user.room.maj
                room_exit_usr(ro.user, ro.user.room.id, ro.user.tableid)

            res = Buffer()
            res.write_ushort(1053)
            res.write_string(ro.user.id)
            res.write_string(ro.user.nick)
            res.write_byte(tableid)
            res.write_short(int(roomid))
            ddat = res.getValue()
            # yield room_cast_msg(room,res.getValue())
            pageId = getattr(ro.user, "pageId", None)
            ss = page_get_users(pageId)
            for kk in ss:
                print "\t->", ss[kk].user.id
                yield ss[kk].sendData(ddat)
            page_remove_user(ro, pageId)
Exemplo n.º 2
0
 def peng_pai_ok(result):
     logging.getLogger("game").info(u"碰牌:%s[%s]" % (ro.user.id, PAI_NAME(pai)))
     res = Buffer()
     res.write_ushort(1064)
     res.write_byte(ro.user.tableid)
     res.write_byte(ord(pai))
     room_cast_msg(ro.user.room, res.getValue())
Exemplo n.º 3
0
def zhua_pai_ok(result):
    ro, pai = result
    logging.getLogger("game").info(u"抓牌OK:%s[%s]" % (ro.user.id, pai))
    res = Buffer()
    res.write_ushort(1054)
    res.write_byte(ord(pai))
    res.write_byte(ro.user.tableid)
    # ro.sendData(res.getValue())
    room_cast_msg(ro.user.room, res.getValue())
Exemplo n.º 4
0
def game_pai_pass(ro, data):
    """
    pass牌
    :param ro:
    :param data:
    :return:
    """
    print u"game_pai_pass.", ro.user.id
    if not check_room_state(ro):
        return
    ro.user.room.maj.pass_pai(ro.user.tableid)
    res = Buffer()
    res.write_ushort(1068)
    res.write_byte(ro.user.tableid)
    yield room_cast_msg(ro.user.room, res.getValue())
Exemplo n.º 5
0
def game_saizi_3(ro, data):
    """
    3-玩家摇骰子3
    :param ro:
    :param data:
    :return:
    """
    print u"game_saizi_3.", ro.user.id
    if not check_room_state(ro):
        return
    res = Buffer()
    res.write_ushort(1103)
    res.write_byte(ro.user.tableid)
    res.write_byte(random.choice(range(1, 7)))
    res.write_byte(random.choice(range(1, 7)))
    yield room_cast_msg(ro.user.room, res.getValue())
Exemplo n.º 6
0
def hupai_ok(result):
    print "hupai_ok", result
    ro, str1, pai, int1, int2, int3, int4, flag = result
    logging.getLogger("game").info(u"胡牌:%s\n\t%s" % (ro.user.id, result))
    for i in range(4):
        usr = room_get_user(ro.user.room, i)
        usr.ready = False
    res = Buffer()
    res.write_ushort(1089)
    res.write_byte(ro.user.tableid)
    res.write_string(str1)
    res.write_byte(ord(pai))
    res.write_short(int1)
    res.write_short(int2)
    res.write_short(int3)
    res.write_short(int4)
    res.write_string(flag)
    room_cast_msg(ro.user.room, res.getValue())
Exemplo n.º 7
0
def game_drop_one(ro, data):
    """
    出牌;
    :param ro:
    :param data:
    :return:
    """
    obj = Parser(data)
    pai = chr(obj.read_ubyte())
    print u"game_drop_one:", PAI_NAME(pai), ro.user.id, ro.user.tableid
    if not check_room_state(ro):
        return
    ff = ro.user.room.maj.drop_one(ro.user.tableid, pai)
    if ff:
        logging.getLogger("game").info(u"%s出牌OK:[%s]" % (ro.user.id, pai))
        res = Buffer()
        res.write_ushort(1059)
        res.write_byte(ro.user.tableid)
        res.write_byte(ord(pai))
        res.write_byte(0)
        yield room_cast_msg(ro.user.room, res.getValue())
        # '''
        #     update all players state
        # '''
        # print 'game_drop_one ok.'
    else:
        print "game_drop_one 出牌ERR.", ro.user.id
        res = Buffer()
        res.write_ushort(1058)
        res.write_short(-1)
        yield ro.sendData(res.getValue())
Exemplo n.º 8
0
def chipai_ok(result):
    try:
        ro, p1, p2, p3 = result
        print "chipai_ok.", ro.user.id, p1, p2, p3
        logging.getLogger("game").info(u"吃牌OK:%s[%s]" % (ro.user.id, [p1, p2, p3]))
        res = Buffer()
        res.write_ushort(1063)
        res.write_byte(ro.user.tableid)
        res.write_byte(ord(p1))
        res.write_byte(ord(p2))
        res.write_byte(ord(p3))
        room_cast_msg(ro.user.room, res.getValue())
    except:
        print "chipai_ok_but_err."
Exemplo n.º 9
0
def game_init(ro, data):
    """
    玩家准备好开始游戏;发送 庄家tableid 以及初始发牌;
    :param ro:
    :param data:
    :return:
    """
    if not hasattr(ro.user, "room"):
        print "check_ready: state_wrong.", ro.user.room, ro.user.tableid
        return
    print u"game_init:", ro.user.room.id, ro.user.tableid
    all_ready = True
    ro.user.ready = True
    for i in range(4):
        user = room_get_user(ro.user.room, i)
        if not user:
            all_ready = False
            continue
        if not hasattr(user, "ready") or not user.ready:
            all_ready = False
        elif i != ro.user.tableid:
            res = Buffer()
            res.write_ushort(1050)
            res.write_byte(i)
            yield ro.sendData(res.getValue())
    res = Buffer()
    res.write_ushort(1050)
    res.write_byte(ro.user.tableid)
    yield room_cast_msg(ro.user.room, res.getValue())
    if not all_ready:
        return
    logging.getLogger("game").info(u"游戏开始: check_all_ready.")
    ro.user.room.maj = Pai()
    ro.user.room.maj.init_game(room=ro.user.room, debug=False)

    rnd_saizi = [random.choice(range(1, 7)) for i in range(6)]

    # 精牌:正精/副精;上精/下精
    jin_arr = ro.user.room.maj.game_jin((rnd_saizi[-1] + rnd_saizi[-2]) * 2)

    for i in range(4):
        user = room_get_user(ro.user.room, i)
        pai = user.room.maj.pais[user.tableid]
        res = Buffer()
        res.write_ushort(1051)
        res.write_byte(ro.user.room.maj.zuang)
        res.write_string(pai)

        for i in range(6):
            res.write_byte(rnd_saizi[i])
        res.write_byte(ord(ro.user.room.maj.jin1))
        res.write_byte(ord(ro.user.room.maj.jin2))

        for i in range(len(jin_arr)):
            res.write_string(str(jin_arr[i]))
        # for i in range(len(jin_arr)):
        #    res.write_byte(ro.user.room.maj.ct[i])

        sock = getSockByUsrId(user.id)
        if sock:
            yield sock.sendData(res.getValue())
Exemplo n.º 10
0
def enter_room(ro, data):
    """
    加入游戏房间;
    :param ro:
    :param data:
    :return:
    """
    obj = Parser(data)
    roomid = str(obj.read_short())
    tableid = obj.read_ubyte()
    pwd = obj.read_string()
    print u"enter_room:", roomid, tableid, pwd
    if roomid == "-1":
        roomid, tableid, msg = room_find_waiting()
        print u"room_find_waiting>>", roomid, tableid, msg
        if msg:
            res = Buffer()
            res.write_ushort(1032)
            res.write_short(-100)
            res.write_string(msg)
            ro.sendData(res.getValue())
            print u"自动匹配失败"
            return
    room, pos, msg, num = room_join_usr(ro.user, roomid, tableid)
    if not room:
        res = Buffer()
        res.write_ushort(1032)
        res.write_short(-100)
        res.write_string(msg)
        ro.sendData(res.getValue())
        return
    res = Buffer()
    res.write_ushort(1032)
    res.write_short(1)
    res.write_ushort(int(roomid))
    res.write_byte(tableid)
    ro.sendData(res.getValue())
    logging.getLogger("user").info(u"房间[%s]座位[%d]玩家<%s>加入ok" % (roomid, tableid, ro.user.id))

    res = Buffer()
    res.write_ushort(1052)
    res.write_string(ro.user.id)
    res.write_string(ro.user.nick)
    res.write_ushort(ro.user.sex)
    res.write_ushort(int(ro.user.avata))
    res.write_byte(tableid)
    res.write_short(int(roomid))
    res.write_byte(num)
    ddat = res.getValue()
    # 广播新玩家加入房间的消息;
    yield room_cast_msg(room, ddat)
    pageId = getattr(ro.user, "pageId", None)
    ss = page_get_users(pageId)
    for kk in ss:
        print "\t->", kk
        yield ss[kk].sendData(ddat)
    page_remove_user(ro, pageId)
    for i in range(4):
        if i == tableid:
            continue
        user = room_get_user(room, i)
        if not user:
            continue
        res = Buffer()
        res.write_ushort(1052)
        res.write_string(user.id)
        res.write_string(user.nick)
        res.write_ushort(user.sex)
        res.write_ushort(int(user.avata))
        res.write_byte(i)
        ro.sendData(res.getValue())

    @registe_msg_handler(ro, "connectionLost")
    @defer.inlineCallbacks
    def User_disconnected(*args, **kwargs):
        if hasattr(ro.user, "room"):
            room = ro.user.room
            logging.getLogger("room").info("room_disconnected: <%s>room[%s]table[%s]" % (ro.user.id, roomid, tableid))
            if hasattr(ro.user.room, "maj"):
                ro.user.room.maj.game_exit(ro, ro.user.tableid)
                res = Buffer()
                res.write_ushort(1099)
                res.write_string("exit")
                yield room_cast_msg(room, res.getValue())
                for i in range(4):
                    user = room_get_user(room, i)
                    if not user:
                        continue
                    if hasattr(user.room, "maj"):
                        del user.room.maj
                room_exit_usr(ro.user, ro.user.room.id, ro.user.tableid)

            res = Buffer()
            res.write_ushort(1053)
            res.write_string(ro.user.id)
            res.write_string(ro.user.nick)
            res.write_byte(tableid)
            res.write_short(int(roomid))
            ddat = res.getValue()
            # yield room_cast_msg(room,res.getValue())
            pageId = getattr(ro.user, "pageId", None)
            ss = page_get_users(pageId)
            for kk in ss:
                print "\t->", ss[kk].user.id
                yield ss[kk].sendData(ddat)
            page_remove_user(ro, pageId)