def becomehold(self,para): '''协议号1: 被设置为藩国 @todo 收集一下资源,推送给前端被设为藩国 ''' uid, hid, hsid, htime = int(para['GiverId']), int(para['HolderId']), int(para['HolderServerId']), int(para['EndTime']) update_dic = {} update_dic['HolderId'] = hid update_dic['HolderServerId'] = hsid update_dic['HoldEndTime'] = htime self.db.update('tb_user', update_dic, 'UserId="%s"'%uid) #纳贡记录 arr = {"UserId":uid, "HolderId":hid, "HolderServerId":hsid, "Jcoin":0, "Gcoin":0, "LastGiveTime":htime} self.db.insert('tb_hold_log', arr) #反抗进度 arr = {"UserId":uid, "HolderId":hid, "HolderServerId":hsid, "ProcessTotal":0, "RevengeCount":0, "LastRevengeDate":datetime.date.today()} self.db.insert('tb_hold_revenge', arr) #如果有藩国的话全部释放掉 # + 玩家的藩国 holds = Gcore.getMod('Building_hold',uid).getHold() for h in holds: param = {"typ":2, "uid":h["UserId"], "sid":h.get("ServerId", Gcore.getServerId())} Gcore.getUI('Building_hold',uid).SlaveOperand(param)
def checkOpt(uid, optId, para): '''将协议号定位于功能''' #global runtime_logger #global exception_logger print ' --> ps proManager.checkOpt', uid, optId if optId not in proManager.mappingDict: return Gcore.error(optId, -11111111) #协议号未定义,或程序未更新 try: startTime = time.time() mapInfo = proManager.mappingDict.get(optId) calledMethod = getattr(Gcore.getUI(mapInfo[0], uid), mapInfo[1]) response = calledMethod(para) #if Gcore.TEST and Gcore.IsServer: #调试计时 if True and optId not in [ 91005, 99001, 99002, 99003, 99004, 99005, 99006 ]: #扫荡不计 因为一次gevent.sleep(3.5) try: runtime = time.time() - startTime #print '-------before log runtime---------' db = Gcore.getNewDB() row = { 'UserId': uid, 'OptId': optId, 'CallMethod': '%s.%s' % (mapInfo[0], mapInfo[1]), 'Param': Gcore.common.json_encode(para), 'Response': Gcore.common.json_encode(response), 'Runtime': runtime, 'RecordTime': Gcore.common.datetime(), } db.insert('temp_runtime_log', row, isdelay=True) db.close() #print '-------end log runtime---------' #runtime_logger.info("%s\t%s\t%s\t%s\t%s\t%s\t%s" % map(str, [uid, optId, str((mapInfo[0],mapInfo[1])), # Gcore.common.json_encode(para), # Gcore.common.json_encode(response), # runtime, # Gcore.common.datetime()])) except: pass return response except Exception, e: print '==Exception==', e mapInfo = proManager.mappingDict.get(optId) #if Gcore.TEST and Gcore.IsServer: if Gcore.IsServer: import traceback strExcept = traceback.format_exc() print >>sys.stderr, 'Time:' + time.strftime('%Y-%m-%d %H:%M:%S') + '\n' \ + 'UserId:' + str(uid) + '\n' \ + 'OptId:' + str(optId) + '\n' \ + 'Para:' + str(para) + '\n' \ + strExcept sys.stderr.flush() #strExcept = str(mapInfo[0])+'.'+str(mapInfo[1])+' >> '+str(e)+'\n' #strExcept = strExcept+('\n'.join(traceback.format_exc().splitlines()[-3:]))#最后三行traceback body = {'Exception': strExcept} #print '-------before log excetion---------' db = Gcore.getNewDB() # row = { # 'UserId':uid, # 'OptId':optId, # 'CallMethod':'%s.%s'%(mapInfo[0],mapInfo[1]), # 'Param':Gcore.common.json_encode(para), # 'Exception':strExcept, # 'RecordTime':Gcore.common.datetime(), # } # db.insert('temp_exception_log', row, isdelay=True) # print 'db.sql',db.sql #OptId,Exception 为唯一 RecordTime = Gcore.common.datetime() CallMethod = '%s.%s' % (mapInfo[0], mapInfo[1]) Param = Gcore.common.json_encode(para) strExcept = db.escape_string(strExcept) sql = "INSERT INTO temp_exception_log (OptId,Exception,RecordTime,CallMethod,UserId,Param)" sql += " VALUES ('%s','%s','%s','%s','%s','%s') ON DUPLICATE KEY UPDATE UserId='%s',ApearTimes=ApearTimes+1,RecordTime='%s'" \ %(optId,strExcept,RecordTime,CallMethod,uid,Param,uid,RecordTime) db.execute(sql, None, True) #print '-------end log excetion---------' #exception_logger.exception("%s\t%s\t%s\t%s\t%s\%s\t%s\t%s" % map(str, [optId,strExcept,RecordTime,CallMethod,uid,Param,uid,RecordTime])) else: body = {} # if Gcore.TEST or True: # time.sleep(1) # raise return Gcore.error(optId, -22222222, body) #程序运行错误
gstr = ' CASE UserId' for row in rows3: jstr += ' WHEN %d THEN %d' % (row["UserId"], incdic.get(row["UserId"], {}).get(1, 0) + row["Jcoin"]) gstr += ' WHEN %d THEN %d' % (row["UserId"], incdic.get(row["UserId"], {}).get(2, 0) + row["Gcoin"]) jstr += ' END ' gstr += ' END ' sql = 'UPDATE tb_hold_log SET Jcoin=%s, Gcoin=%s, LastGiveTime="%s" WHERE %s' \ % (jstr, gstr, curtime, where) pyDB.execute(sql) #自动解除关系 for row in rows: if row["HoldEndTime"] > curtime: continue if row["HolderServerId"] == config.SERVER_ID: #本服 ui = Gcore.getUI('Building_hold', row["HolderId"]) ui.SlaveOperand({'typ':2, 'uid':row["UserId"], 'sid':config.SERVER_ID}) else: #非本服,发消息 try: msg = {'HolderId':row["HolderId"], "GiverId":row["UserId"], "GiverServerId":config.SERVER_ID} msg['optId'] = 3 msg = str(msg) message = amqp.Message(msg) message.properties['delivery_mod'] = 2 s2e = int(row["HolderServerId"])%exchange_num if not s2e: s2e = exchange_num chan = ensure_conn(send_conn) chan.basic_publish(message, exchange='sggameexchange%d'%s2e, routing_key=str(row["HolderServerId"]))
1, 0)) gstr += ' WHEN %d THEN %d' % (UserId, incdic.get(UserId, {}).get( 2, 0)) jstr += ' END ' gstr += ' END ' sql = 'UPDATE tb_hold_log SET Jcoin = Jcoin+%s, Gcoin = Gcoin+%s, LastGiveTime="%s" WHERE %s' \ % (jstr, gstr, curtime, where) db.execute(sql) print '进贡日志:', sql #自动解除关系 (前台自己到时间,请求接口,自动更新资源情况) for row in rows: if row["HoldEndTime"] > curtime + 600: #提前10分钟释放 continue if row["HolderServerId"] == Gcore.getServerId(): #本服 ui = Gcore.getUI('Building_hold', row["HolderId"]) ui.SlaveOperand({ 'typ': 2, 'uid': row["UserId"], 'sid': Gcore.getServerId() }) else: #非本服,发消息 msg = { 'HolderId': row["HolderId"], "GiverId": row["UserId"], "GiverServerId": Gcore.getServerId() } Gcore.sendmq(3, int(row["HolderServerId"]), msg) cnt += 1 runtime = time.time() - curtime
def autofree(self,para): '''协议号3: 释放奴隶''' uid, suid, ssid = map(int, [para["HolderId"], para['GiverId'], para['GiverServerId']]) param = {"typ":2, "uid":suid, "sid":ssid} Gcore.getUI('Building_hold',uid).SlaveOperand(param)
def checkOpt(uid,optId,para): '''将协议号定位于功能''' #global runtime_logger #global exception_logger print ' --> ps proManager.checkOpt',uid,optId if optId not in proManager.mappingDict: return Gcore.error(optId,-11111111) #协议号未定义,或程序未更新 try: startTime = time.time() mapInfo = proManager.mappingDict.get(optId) calledMethod = getattr(Gcore.getUI(mapInfo[0],uid),mapInfo[1]) response = calledMethod(para) #if Gcore.TEST and Gcore.IsServer: #调试计时 if True and optId not in [91005,99001,99002,99003,99004,99005,99006]: #扫荡不计 因为一次gevent.sleep(3.5) try: runtime = time.time() - startTime #print '-------before log runtime---------' db = Gcore.getNewDB() row = { 'UserId':uid, 'OptId':optId, 'CallMethod':'%s.%s'%(mapInfo[0],mapInfo[1]), 'Param':Gcore.common.json_encode(para), 'Response':Gcore.common.json_encode(response), 'Runtime':runtime, 'RecordTime':Gcore.common.datetime(), } db.insert('temp_runtime_log', row, isdelay=True) db.close() #print '-------end log runtime---------' #runtime_logger.info("%s\t%s\t%s\t%s\t%s\t%s\t%s" % map(str, [uid, optId, str((mapInfo[0],mapInfo[1])), # Gcore.common.json_encode(para), # Gcore.common.json_encode(response), # runtime, # Gcore.common.datetime()])) except: pass return response except Exception,e: print '==Exception==',e mapInfo = proManager.mappingDict.get(optId) #if Gcore.TEST and Gcore.IsServer: if Gcore.IsServer: import traceback strExcept = traceback.format_exc() print >>sys.stderr, 'Time:' + time.strftime('%Y-%m-%d %H:%M:%S') + '\n' \ + 'UserId:' + str(uid) + '\n' \ + 'OptId:' + str(optId) + '\n' \ + 'Para:' + str(para) + '\n' \ + strExcept sys.stderr.flush() #strExcept = str(mapInfo[0])+'.'+str(mapInfo[1])+' >> '+str(e)+'\n' #strExcept = strExcept+('\n'.join(traceback.format_exc().splitlines()[-3:]))#最后三行traceback body = {'Exception':strExcept} #print '-------before log excetion---------' db = Gcore.getNewDB() # row = { # 'UserId':uid, # 'OptId':optId, # 'CallMethod':'%s.%s'%(mapInfo[0],mapInfo[1]), # 'Param':Gcore.common.json_encode(para), # 'Exception':strExcept, # 'RecordTime':Gcore.common.datetime(), # } # db.insert('temp_exception_log', row, isdelay=True) # print 'db.sql',db.sql #OptId,Exception 为唯一 RecordTime = Gcore.common.datetime() CallMethod = '%s.%s'%(mapInfo[0],mapInfo[1]) Param = Gcore.common.json_encode(para) strExcept = db.escape_string(strExcept) sql = "INSERT INTO temp_exception_log (OptId,Exception,RecordTime,CallMethod,UserId,Param)" sql += " VALUES ('%s','%s','%s','%s','%s','%s') ON DUPLICATE KEY UPDATE UserId='%s',ApearTimes=ApearTimes+1,RecordTime='%s'" \ %(optId,strExcept,RecordTime,CallMethod,uid,Param,uid,RecordTime) db.execute(sql,None,True) #print '-------end log excetion---------' #exception_logger.exception("%s\t%s\t%s\t%s\t%s\%s\t%s\t%s" % map(str, [optId,strExcept,RecordTime,CallMethod,uid,Param,uid,RecordTime])) else: body = {} # if Gcore.TEST or True: # time.sleep(1) # raise return Gcore.error(optId,-22222222,body) #程序运行错误