Example #1
0
def game_hu_pai(ro, data):
    """
    胡牌
    :param ro:
    :param data:
    胡牌的type,  1表示德国, 2表示正常胡牌, 3表示七对,  4表示十三烂,5表示七星十三烂
    德国就是没用精牌就胡了;德中德就是胡牌的时候,四个人手里都没有精牌;
    13烂就是没对子,没顺子,没刻子
    13烂,如果有东南西北中发白七张,就是七星十三烂
    :return:
    """
    obj = Parser(data)
    type = obj.read_ubyte()
    str1 = obj.read_string()
    tt = str1.split(",")
    print u"game_hu_pai:", ro.user.id, type, str1
    print u"game_hu_pai:", tt
    print u"game_hu_pai:", "\t".join(["".join(map(PAI_NAME, m)) for m in tt])
    if not check_room_state(ro):
        return
    d = defer.maybeDeferred(partial(ro.user.room.maj.hu_pai), ro, ro.user.tableid, type, str1)
    if d:
        d.addCallback(hupai_ok)
        d.addErrback(hupai_err)
    yield
Example #2
0
def game_peng_pai(ro, data):
    """
    碰牌
    :param ro:
    :param data:
    :return:
    """
    obj = Parser(data)
    pai = chr(obj.read_ubyte())
    print u"game_peng_pai:", PAI_NAME(pai)
    if not check_room_state(ro):
        return

    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())

    def peng_pai_err(*args):
        print "peng_pai_err"

    d = defer.maybeDeferred(partial(ro.user.room.maj.peng_pai), ro, ro.user.tableid, pai)
    if d:
        d.addCallback(peng_pai_ok)
        d.addErrback(peng_pai_err)
    yield
Example #3
0
def game_gang_pai(ro, data):
    """
    杠牌
    :param ro:
    :param data:
    :return:
    """

    def gang_pai_ok(result):
        print "gang_pai_ok"
        logging.getLogger("game").info(u"杠牌OK:%s[%s]" % (ro.user.id, PAI_NAME(pai)))
        res = Buffer()
        res.write_ushort(1065)
        res.write_byte(ro.user.tableid)
        res.write_byte(ord(pai))
        room_cast_msg(ro.user.room, res.getValue())

    def gang_pai_err(*args):
        print "gang_pai_err"
        # logging.getLogger('game').info(u'杠牌ERR:%s[%s]'%(ro.user.id, pai))

    obj = Parser(data)
    pai = chr(obj.read_ubyte())
    print u"game_gang_pai:", PAI_NAME(pai)
    if not check_room_state(ro):
        return
    logging.getLogger("game").info(u"杠牌:%s[%s]" % (ro.user.id, PAI_NAME(pai)))
    d = defer.maybeDeferred(partial(ro.user.room.maj.gang_pai), ro, ro.user.tableid, pai)
    if d:
        print "game_gang_pai 000", d
        d.addCallback(gang_pai_ok)
        print "game_gang_pai 111"
        d.addErrback(gang_pai_err)
    yield
    print "game_gang_pai 222"
Example #4
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())
Example #5
0
def game_ting(ro, data):
    """
    听牌
    :param ro:
    :param data:
    :return:
    """
    obj = Parser(data)
    ss = obj.read_string()
    print u"game_ting.", ro.user.id, ss
    if not check_room_state(ro):
        return
    ro.user.room.maj.ting_pai(ro.user.tableid, ss)
    yield
Example #6
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())
Example #7
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)
Example #8
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))
Example #9
0
def game_chi_pai(ro, data):
    """
    吃牌
    :param ro:
    :param data:
    :return:
    """
    obj = Parser(data)
    p1 = chr(obj.read_ubyte())
    p2 = chr(obj.read_ubyte())
    p3 = chr(obj.read_ubyte())
    print u"game_chi_pai: p1,p2,p3 = ", PAI_NAME(p1), PAI_NAME(p2), PAI_NAME(p3)
    if not check_room_state(ro):
        return
    logging.getLogger("game").info(u"玩家吃牌:%s[%s]" % (ro.user.id, [PAI_NAME(p1), PAI_NAME(p2), PAI_NAME(p3)]))
    # 方法一:在game_logic里面异步执行以回调方式接受后续数据包发送的逻辑(chipai_ok里面);
    # ro.user.room.maj.chi_pai(ro, ro.user.tableid, p1, p2, p3)#
    # 方法二:异步Deferred回调机制执行后续逻辑;
    d = defer.maybeDeferred(partial(ro.user.room.maj.chi_pai), ro, ro.user.tableid, p1, p2, p3)
    if d:
        d.addCallback(chipai_ok)
        d.addErrback(chipai_err)
    yield
Example #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)
Example #11
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())