예제 #1
0
파일: core.py 프로젝트: fycheung/misc
 def pro_manager(uid, sid, optId, para):
     '''协议管理'''
     try:
         if optId == 98001: #进入房间
             return PvpUI.EnterRoom(optId, uid, sid, **para)
         elif optId == 98002: #离开房间
             return PvpUI.LeaveRoom(optId, uid, sid, **para)
         elif optId == 98003: #离开竞技场
             return PvpUI.LeaveArena(optId, uid, sid, **para)
         elif optId == 98004: #广播战斗信息
             return PvpUI.MultiCastOpt(optId, uid, sid, **para)
         elif optId == 98005: #战斗结束
             return PvpUI.ArenaEnd(optId, uid, sid, **para)
         elif optId == 98007: #获取本周所得荣誉
             return PvpUI.GetWeekHonour(optId, uid, sid, **para)
         
         elif optId == 98997: #重载自定义模块
             stat = Gcore.reload_userdefined_mod()
             if not stat: #重载自定义模块发生异常
                 return common.error(optId, -98997001)
             else: #成功重载自定义模块
                 return common.out(optId)
         elif optId == 98996: #开启竞技场
             Gcore.arena_start()
             return common.out(optId)
         elif optId == 98995: #关闭竞技场
             Gcore.arena_end()
             return common.out(optId)
         else: #未定义的协议号
             return common.error(98998, -98998001)
     except Exception, e: #运行程序时,出现异常。
         print e
         return common.error(98999, -98999001)
예제 #2
0
    def pro_manager(uid, sid, optId, para):
        '''协议管理'''
        try:
            if optId == 98001:  #进入房间
                return PvpUI.EnterRoom(optId, uid, sid, **para)
            elif optId == 98002:  #离开房间
                return PvpUI.LeaveRoom(optId, uid, sid, **para)
            elif optId == 98003:  #离开竞技场
                return PvpUI.LeaveArena(optId, uid, sid, **para)
            elif optId == 98004:  #广播战斗信息
                return PvpUI.MultiCastOpt(optId, uid, sid, **para)
            elif optId == 98005:  #战斗结束
                return PvpUI.ArenaEnd(optId, uid, sid, **para)
            elif optId == 98007:  #获取本周所得荣誉
                return PvpUI.GetWeekHonour(optId, uid, sid, **para)

            elif optId == 98997:  #重载自定义模块
                stat = Gcore.reload_userdefined_mod()
                if not stat:  #重载自定义模块发生异常
                    return common.error(optId, -98997001)
                else:  #成功重载自定义模块
                    return common.out(optId)
            elif optId == 98996:  #开启竞技场
                Gcore.arena_start()
                return common.out(optId)
            elif optId == 98995:  #关闭竞技场
                Gcore.arena_end()
                return common.out(optId)
            else:  #未定义的协议号
                return common.error(98998, -98998001)
        except Exception, e:  #运行程序时,出现异常。
            print e
            return common.error(98999, -98999001)
예제 #3
0
def MultiCastOpt(optId, UserId, ServerId, body, **para):
    '''广播战斗信息'''
    #参数检查
    if isinstance(body, basestring):  #如果是字符串,先尝试转成字典。
        try:
            body = json.loads(body)
        except Exception:
            return common.error(optId, -(optId * 1000 + 1))  #不是jason字符串,失败。
    if isinstance(body, dict):
        body['ServerTime'] = time.time()  #加上服务器端时间戳
        body['UserId'] = '%s.%s' % (ServerId, UserId)  #加上UserId,ServerId
    else:
        return common.error(optId, -(optId * 1000 + 2))  #参数类型错误

    FightObj = mod.is_in_arena(UserId, ServerId)  # 获取战斗对象
    # 注意:由于返回的是弱引用,使用的时候应该加括号:FightObj()
    if not FightObj:
        return common.error(optId, -(optId * 1000 + 3))  #不在战斗中,无法广播战斗信息。

    #战斗过程验证放在此处

    print '战斗是否结束', FightObj().is_fight_over()
    print '战斗是否超时', FightObj().is_time_out()
    print '战果是', FightObj().fight_over()
    print '当前损兵', FightObj().soldier_lose()
    print '1组与二组的级差', FightObj().user_level_diff()
    print '士兵和武将的平均等级', FightObj().army_level_avg()
    #开始广播
    body = json.dumps({"body": body})  #广播之前,转成json。
    stat = FightObj().zmq_muticast(body)
    if stat < 0:
        return common.error(optId, -(optId * 1000 - stat + 3))
        # -1 发送内容不正确;-2 zmq发布失败
    return common.out(optId)  #广播成功
예제 #4
0
파일: PvpUI.py 프로젝트: fycheung/misc
def MultiCastOpt(optId, UserId, ServerId, body, **para):
    '''广播战斗信息'''
    #参数检查
    if isinstance(body, basestring): #如果是字符串,先尝试转成字典。
        try:
            body = json.loads(body)
        except Exception:
            return common.error(optId, -(optId * 1000 + 1)) #不是jason字符串,失败。
    if isinstance(body, dict):
        body['ServerTime'] = time.time() #加上服务器端时间戳
        body['UserId'] = '%s.%s' % (ServerId, UserId) #加上UserId,ServerId
    else:
        return common.error(optId, -(optId * 1000 + 2)) #参数类型错误

    FightObj = mod.is_in_arena(UserId, ServerId) # 获取战斗对象
    # 注意:由于返回的是弱引用,使用的时候应该加括号:FightObj()
    if not FightObj:
        return common.error(optId, -(optId * 1000 + 3)) #不在战斗中,无法广播战斗信息。
    
    #战斗过程验证放在此处
    
    
    print '战斗是否结束', FightObj().is_fight_over()
    print '战斗是否超时', FightObj().is_time_out()
    print '战果是', FightObj().fight_over()
    print '当前损兵', FightObj().soldier_lose()
    print '1组与二组的级差', FightObj().user_level_diff()
    print '士兵和武将的平均等级', FightObj().army_level_avg()
    #开始广播
    body = json.dumps({"body":body}) #广播之前,转成json。
    stat = FightObj().zmq_muticast(body)
    if stat < 0:
        return common.error(optId, -(optId * 1000 - stat + 3))
        # -1 发送内容不正确;-2 zmq发布失败
    return common.out(optId) #广播成功
예제 #5
0
def ArenaEnd(optId, UserId, ServerId, **para):
    '''战斗结束'''
    FightObj = mod.is_in_arena(UserId, ServerId)  # 获取战斗对象
    # 注意:由于返回的是弱引用,使用的时候应该加括号:FightObj()
    if not FightObj:
        return common.error(optId, -(optId * 1000 + 1))  #不在战斗中,无法结束战斗。

    fight_result = FightObj().fight_over()
    if fight_result == False:  #没有达到战斗结束的条件
        return common.error(optId, -(optId * 1000 + 2))

    #处理战斗结果
    # + 发送消息给分服
    RabbitMod.rm.send(optId, ServerId,
                      copy.deepcopy(fight_result[(UserId, ServerId)]))
    # + 更新荣誉表
    HonourMod.addHonour(UserId, ServerId,
                        fight_result[(UserId, ServerId)]['gainHonour'],
                        time.time())

    #从竞技场中退出
    stat = mod.leave_arena(UserId, ServerId)
    if stat:  #返回战斗结果给玩家
        return common.out(
            optId, body={'FightResult': fight_result[(UserId, ServerId)]})  #成功
    return common.error(optId, -(optId * 1000 + 3))  #不在竞技场战斗中
예제 #6
0
파일: PvpUI.py 프로젝트: fycheung/misc
def ArenaEnd(optId, UserId, ServerId, **para):
    '''战斗结束'''
    FightObj = mod.is_in_arena(UserId, ServerId) # 获取战斗对象
    # 注意:由于返回的是弱引用,使用的时候应该加括号:FightObj()
    if not FightObj:
        return common.error(optId, -(optId * 1000 + 1)) #不在战斗中,无法结束战斗。
    
    fight_result = FightObj().fight_over()
    if fight_result == False: #没有达到战斗结束的条件
        return common.error(optId, -(optId * 1000 + 2))
    
    #处理战斗结果
    # + 发送消息给分服
    RabbitMod.rm.send(optId, ServerId, 
                      copy.deepcopy(fight_result[(UserId, ServerId)]))
    # + 更新荣誉表
    HonourMod.addHonour(UserId, ServerId, 
                        fight_result[(UserId, ServerId)]['gainHonour'], 
                        time.time())
    
    #从竞技场中退出 
    stat = mod.leave_arena(UserId, ServerId)
    if stat: #返回战斗结果给玩家
        return common.out(optId, 
            body={'FightResult':fight_result[(UserId, ServerId)]}) #成功
    return common.error(optId, -(optId * 1000 + 3)) #不在竞技场战斗中
예제 #7
0
파일: PvpMod.py 프로젝트: fycheung/misc
 def fight_end(self, fight_result): #内部用
     '''战后处理'''
     # 1,将战果广播给客户端
     # 2,将战果分发给各分服
     # 3,更新总服的荣誉表
     from core import Gcore
     for user in fight_result:
         Gcore.multicast(user, common.out(98005, fight_result[user]))
         RabbitMod.rm.send(98005, user[1], fight_result[user])
         HonourMod.addHonour(user[0], user[1], 
                             fight_result[user]['gainHonour'], time.time())
     self.zmq_sock_close() #释放端口
예제 #8
0
파일: PvpMod.py 프로젝트: hw233/python_misc
 def fight_end(self, fight_result):  #内部用
     '''战后处理'''
     # 1,将战果广播给客户端
     # 2,将战果分发给各分服
     # 3,更新总服的荣誉表
     from core import Gcore
     for user in fight_result:
         Gcore.multicast(user, common.out(98005, fight_result[user]))
         RabbitMod.rm.send(98005, user[1], fight_result[user])
         HonourMod.addHonour(user[0], user[1],
                             fight_result[user]['gainHonour'], time.time())
     self.zmq_sock_close()  #释放端口
예제 #9
0
파일: PvpMod.py 프로젝트: hw233/python_misc
class ArenaObj(object):
    '''竞技场对象'''
    __zmq_ctx = zmq.Context()  #初始化zmq

    def __init__(self, typ, armyinfo, userinfo):
        self._typ = typ  #类型
        self._armyinfo = armyinfo  #部队信息
        self._userinfo = userinfo  #用户信息
        self._duration = self.get_time_by_type(typ)  #战斗时长
        self._id = str(uuid.uuid1())  #战斗唯一标识符
        self.leavers = []  #离开战斗的人
        self.army_avg_lv = self.army_level_avg()
        print '平均等级', self.army_avg_lv
        self.__zmq_sock = self.__class__.__zmq_ctx.socket(
            zmq.PUB)  #生成zmq socket
        while 1:
            try:
                self.freeport = getfreeport()  #获取一个随机未被占用的端口
                self.__zmq_sock.bind(
                    "tcp://%s:%s" %
                    (config.ZMQ_SERVER_HOST, self.freeport))  #绑定
            except Exception, e:
                print e
            else:
                print 'zmq服务器启动成功@PORT:%s' % self.freeport
                break  #退出循环
        self._starttime = int(time.time())  #战斗对象生成时间

        #组播给客户端:战斗初始信息。
        body = {}
        body["armyInfo"] = armyinfo
        body['userInfo'] = {}
        for uid, sid in userinfo:
            body['userInfo'][uid] = {}
            body['userInfo'][uid].update(userinfo[(uid, sid)])
            body['userInfo'][uid]['ServerId'] = sid
        body['startTime'] = self._starttime
        body['duration'] = self._duration
        body['mapId'] = self._typ  #战斗类型 与 地图类型相同
        body['channelId'] = 1
        body['zmqIP'] = config.ZMQ_SERVER_HOST
        body['zmqPort'] = self.freeport
        body['armyNum'] = len(self._armyinfo)  #参战的军队数量
        content = common.out(98006, body)  #战斗匹配成功的协议号98006
        print '开始组播战斗开始信息'
        self.sock_multicast(0, content)
예제 #10
0
def EnterRoom(optId, UserId, ServerId, ArenaType, **para):
    '''进入Pvp房间'''
    #获取用户等级
    UserInfo = rm.getUserInfo(UserId, ServerId)
    #if not UserInfo:
    #    return common.error(optId, -(optId * 1000 + 1)) #获取用户信息失败。
    #UserLevel = UserInfo['UserLevel']
    #if UserLevel < config.ARENA_MIN_LEVEL:
    #    return common.error(optId, -(optId * 1000 + 2)) #级别不够(至少五级)

    #上阵武将
    GeneralOnEmbbatle = rm.getGeneralOnEmbattle(UserId, ServerId)
    #if not GeneralOnEmbbatle:
    #    return common.error(optId, -(optId * 1000 + 3)) #尚未布阵

    #获取科技等级
    SoldierTechs = rm.getSoldierTech(UserId, ServerId)
    #if not SoldierTechs:
    #    return common.error(optId, -(optId * 1000 + 4)) #获取科技等级失败

    #获取好友信息
    Friends = rm.getFriendInfo(UserId, ServerId)

    if mod.is_in_arena(UserId, ServerId):
        return common.error(optId, -(optId * 1000 + 5))  #已在战斗中

    if not ArenaType in [1, 2, 3]:
        return common.error(optId, -(optId * 1000 + 6))  #竞技场类型错误
    #加入到房间
    Infos = {
        'Generals': GeneralOnEmbbatle,
        'User': UserInfo,
        'Techs': SoldierTechs
    }
    et = time.time()
    stat = mod.enter_room(UserId, ServerId, 20, et, Friends, ArenaType,
                          Infos)  ##等级先写死成20。
    if stat == -1:
        return common.error(optId, -(optId * 1000 + 7))  #竞技场尚未开始
    if stat == -2:
        return common.error(optId, -(optId * 1000 + 8))  #进入房间失败。
    return common.out(optId)  #成功进入房间
예제 #11
0
파일: PvpUI.py 프로젝트: fycheung/misc
def EnterRoom(optId, UserId, ServerId, ArenaType, **para):
    '''进入Pvp房间'''
    #获取用户等级
    UserInfo = rm.getUserInfo(UserId, ServerId)
    #if not UserInfo:
    #    return common.error(optId, -(optId * 1000 + 1)) #获取用户信息失败。
    #UserLevel = UserInfo['UserLevel']
    #if UserLevel < config.ARENA_MIN_LEVEL:
    #    return common.error(optId, -(optId * 1000 + 2)) #级别不够(至少五级)
    
    #上阵武将
    GeneralOnEmbbatle = rm.getGeneralOnEmbattle(UserId, ServerId)
    #if not GeneralOnEmbbatle:
    #    return common.error(optId, -(optId * 1000 + 3)) #尚未布阵
    
    #获取科技等级
    SoldierTechs = rm.getSoldierTech(UserId, ServerId)
    #if not SoldierTechs:
    #    return common.error(optId, -(optId * 1000 + 4)) #获取科技等级失败

    #获取好友信息
    Friends = rm.getFriendInfo(UserId, ServerId)

    if mod.is_in_arena(UserId, ServerId):
        return common.error(optId, -(optId * 1000 + 5)) #已在战斗中
        
    if not ArenaType in [1, 2, 3]:
        return common.error(optId, -(optId * 1000 + 6)) #竞技场类型错误
    #加入到房间
    Infos = {'Generals':GeneralOnEmbbatle, 'User':UserInfo,
             'Techs':SoldierTechs}
    et = time.time()
    stat = mod.enter_room(UserId, ServerId, 20, et, Friends, ArenaType, Infos) ##等级先写死成20。
    if stat == -1:
        return common.error(optId, -(optId * 1000 + 7)) #竞技场尚未开始
    if stat == -2:
        return common.error(optId, -(optId * 1000 + 8)) #进入房间失败。
    return common.out(optId) #成功进入房间
예제 #12
0
파일: PvpUI.py 프로젝트: fycheung/misc
def LeaveRoom(optId, UserId, ServerId, **para):
    '''离开房间'''
    stat = mod.leave_room(UserId, ServerId)
    if stat:
        return common.out(optId) #成功离开房间
    return common.error(optId, -(optId * 1000 + 1)) #不在房间中
예제 #13
0
def LeaveArena(optId, UserId, ServerId, **para):
    '''离开竞技场'''
    stat = mod.leave_arena(UserId, ServerId)
    if stat:
        return common.out(optId)  #成功
    return common.error(optId, -(optId * 1000 + 1))  #不在竞技场战斗中
예제 #14
0
def LeaveRoom(optId, UserId, ServerId, **para):
    '''离开房间'''
    stat = mod.leave_room(UserId, ServerId)
    if stat:
        return common.out(optId)  #成功离开房间
    return common.error(optId, -(optId * 1000 + 1))  #不在房间中
예제 #15
0
def GetWeekHonour(optId, UserId, ServerId, **para):
    '''获取本周荣誉'''
    HonourNum = HonourMod.getWeekHonour(UserId, ServerId)
    return common.out(optId, {"HornourNum": HonourNum})
예제 #16
0
파일: PvpUI.py 프로젝트: fycheung/misc
def LeaveArena(optId, UserId, ServerId, **para):
    '''离开竞技场'''
    stat = mod.leave_arena(UserId, ServerId)
    if stat:
        return common.out(optId) #成功
    return common.error(optId, -(optId * 1000 + 1)) #不在竞技场战斗中
예제 #17
0
파일: PvpUI.py 프로젝트: fycheung/misc
def GetWeekHonour(optId, UserId, ServerId, **para):
    '''获取本周荣誉'''
    HonourNum = HonourMod.getWeekHonour(UserId, ServerId)
    return common.out(optId, {"HornourNum":HonourNum})