Exemple #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)
Exemple #2
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())
Exemple #3
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())
Exemple #4
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())
Exemple #5
0
def hupai_err(result):
    print "hupai_err."
    print result
    import sys

    print >> sys.stderr
    ro = result.value
    res = Buffer()
    res.write_ushort(1088)
    res.write_short(-1)
    ro.sendData(res.getValue())
Exemple #6
0
def auth_login(ro, data):
    """
    用户登录
    :param ro:连接
    :param data:客户端数据包
    :return:
    """
    obj = Parser(data)
    u = obj.read_string()
    p = obj.read_string()
    print u"auth_login:"******"0")
    usr = yield getUser(u)
    if not usr or usr.pwd != p:
        print u"登录失败", usr, usr or u"无次用户"
        yield rc.disconnect()
        res = Buffer()
        res.write_ushort(1025)
        res.write_short(-100)
        res.write_string(u"登录失败")
        ro.sendData(res.getValue())
    else:
        ro2 = getSockByUsrId(u)
        if ro2:
            res = Buffer()
            res.write_ushort(1025)
            res.write_short(-102)
            res.write_string(u"账号被重新登录")
            ro2.sendData(res.getValue())
            ro2.transport.loseConnection()
        yield rc.sadd("lobby_usr", u)
        yield rc.disconnect()
        res = Buffer()
        res.write_ushort(1025)
        res.write_short(1)
        res.write_ushort(int(usr.avata))
        ro.sendData(res.getValue())
        User_connected(usr, ro)
        logging.getLogger("user").info(u"登录成功:" + u)
Exemple #7
0
def account_create(ro, data):
    """
    创建用户账号;
    """
    obj = Parser(data)
    u = obj.read_string()
    p = obj.read_string()
    nick = obj.read_string()
    mail = obj.read_string()
    sex = obj.read_ushort()
    avata = obj.read_ushort()
    print u"account_create:", u, p, mail, sex, avata
    rc = yield redis.Connection()
    print u"redis connected!"
    yield rc.select("0")
    usr = yield rc.get("usr_" + u)
    if usr:
        print u"用户已存在"
        yield rc.disconnect()
        res = Buffer()
        res.write_ushort(1024)
        res.write_short(-100)
        res.write_string(u"用户已存在")
        ro.sendData(res.getValue())
    else:
        m = User()
        m.id, m.pwd, m.nick, m.mail, m.sex, m.avata = u, p, nick, mail, sex, avata
        yield rc.set("usr_" + u, repr(m))
        yield rc.disconnect()
        res = Buffer()
        res.write_ushort(1024)
        res.write_short(1)
        res.write_string(u"ok")
        # dat=res.getValue()
        # for i in range(len(dat)):
        #     print dat[i]
        ro.sendData(res.getValue())
        logging.getLogger("user").info(u"用户账号注册成功:%s<%s>" % (nick, u))
Exemple #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."
Exemple #9
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())
Exemple #10
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())
Exemple #11
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())
Exemple #12
0
def game_exit(ro, data):
    """
    游戏结束;
    :param ro:
    :param data:
    :return:
    """
    print u"game_exit:", ro.user.tableid
    if not check_room_state(ro):
        print "game_exit error."
        return
    ro.user.room.maj.game_exit(ro, ro.user.tableid)
    del ro.user.room.maj.room
    del ro.user.room.maj
    logging.getLogger("game").info(u"游戏结束: %s" % (ro.user.id))
    res = Buffer()
    res.write_ushort(1099)
    res.write_string("exit")
    yield room_cast_msg(ro.user.room, res.getValue())
Exemple #13
0
def list_rooms(ro, data):
    """
    获取大厅房间列表;
    :param ro:
    :param data:
    :return:
    """
    obj = Parser(data)
    page = obj.read_short()
    pagesize = obj.read_short()
    print u"list_rooms:", page, pagesize
    jsonStr = room_list()
    pageId = getattr(ro.user, "pageId", None)
    page_remove_user(ro, pageId)
    page_join_user(ro, page)
    res = Buffer()
    res.write_ushort(1030)
    res.write_short(1)
    res.write_string(jsonStr)
    yield ro.sendData(res.getValue())
Exemple #14
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())
Exemple #15
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)
Exemple #16
0
def lobby_user_chat(ro, data):
    """
    大厅用户聊天
    :param ro:
    :param data:
    :return:
    """
    obj = Parser(data)
    peer = obj.read_string()
    msg = obj.read_string()
    print u"user_chat:", peer, msg
    rc = yield redis.Connection()
    yield rc.select("0")
    me = ro.user
    if not me:
        print u"msg发送失败", u"登录过期"
        yield rc.disconnect()
        res = Buffer()
        res.write_ushort(1040)
        res.write_short(-101)
        res.write_string(u"登录过期")
        ro.sendData(res.getValue())
        return
    if peer == "All":
        res = Buffer()
        res.write_ushort(1040)
        res.write_short(1)
        res.write_string(u"ok")
        ro.sendData(res.getValue())
        mems = yield rc.smembers("lobby_usr")
        for mm in mems:
            ro2 = getSockByUsrId(mm)
            if not ro2:
                continue
            res = Buffer()
            res.write_ushort(1042)
            res.write_string(me.id)
            res.write_string(me.nick)
            res.write_string(msg)
            ro2.sendData(res.getValue())
        yield rc.disconnect()
        print u"发送成功!"
    else:
        usr = yield getUser(peer)
        if not usr:
            print u"msg发送失败", usr, usr or u"无次用户"
            yield rc.disconnect()
            res = Buffer()
            res.write_ushort(1040)
            res.write_short(-100)
            res.write_string(u"无此用户")
            ro.sendData(res.getValue())
            return
        ro2 = getSockByUsrId(peer)
        if not ro2:
            print u"msg发送失败", u"用户不在线"
            yield rc.disconnect()
            res = Buffer()
            res.write_ushort(1040)
            res.write_short(-101)
            res.write_string(u"用户不在线")
            ro.sendData(res.getValue())
            return
        yield rc.disconnect()
        print u"发送成功!"
        res = Buffer()
        res.write_ushort(1040)
        res.write_short(1)
        res.write_string(u"ok")
        ro.sendData(res.getValue())
        res = Buffer()
        res.write_ushort(1042)
        res.write_string(me.id)
        res.write_string(me.nick)
        res.write_string(msg)
        ro2.sendData(res.getValue())