#!/usr/bin/python  
 | 
# -*- coding: GBK -*-  
 | 
#---------------------------------------------------------------------  
 | 
#  
 | 
#---------------------------------------------------------------------  
 | 
##@package ReadCaptchaImage  
 | 
# @todo: ¼ÓÔØÍ¼ÐÎÑéÖ¤Âë  
 | 
#  
 | 
# @author: panwei  
 | 
# @date 2011-05-31 21:30  
 | 
# @version 1.5  
 | 
#  
 | 
# @note:   
 | 
# @change: "2011-06-05 01:20" Alee ÐÞ¸ÄΪ¶àÎļþ¶ÁÈ¡£¬²»´æÄÚ´æÖÐ  
 | 
# @change: "2011-06-07 11:10" Alee ·µ»ØÖµÍ³Ò»  
 | 
# @change: "2011-06-17 11:00" Alee Ìí¼ÓÖØ¶Á¹¦ÄÜ  
 | 
# @change: "2011-06-23 13:40" Alee Ëæ»úÉú³ÉÑéÖ¤ÂëÖØÔØ  
 | 
# @change: "2011-07-11 18:00" Alee ÐÞ¸´È«¾Ö²ÎÊý´«²ÎÎÞ·¨±ä¸üµÄÎÊÌâ  
 | 
#---------------------------------------------------------------------  
 | 
"""Version = 2011-07-11 18:00"""  
 | 
#---------------------------------------------------------------------  
 | 
import ChConfig  
 | 
import GameWorld  
 | 
import os  
 | 
import random  
 | 
import ShareDefine  
 | 
import ReadChConfig  
 | 
#---------------------------------------------------------------------  
 | 
#È«¾Ö±äÁ¿  
 | 
g_captchaImageTexDict = {}  
 | 
g_captchaImageJpegDict = {}  
 | 
  
 | 
#Ëæ»úÑéÖ¤Â뿪¹Ø  
 | 
Def_Random_Captcha_Open = 1  
 | 
#---------------------------------------------------------------------  
 | 
#È«¾Ö³£Á¿  
 | 
Def_FilePath = ChConfig.GetAppPath() + "\\" + "CaptchaImage\\"  
 | 
  
 | 
#---------------------------------------------------------------------  
 | 
##»ñÈ¡ÑéÖ¤Âë  
 | 
# @param ÎÞ²ÎÊý  
 | 
# @return ÑéÖ¤Âë´ð°¸, ÑéÖ¤ÂëÄÚÈÝ  
 | 
# @remarks   
 | 
def GetCaptchaImage():  
 | 
    global g_captchaImageTexDict  
 | 
    global g_captchaImageJpegDict  
 | 
      
 | 
    #¼ì²éËæ»úÑéÖ¤Â뿪¹Ø  
 | 
    if ReadChConfig.GetEvalChConfig("Random_Captcha_Open") == Def_Random_Captcha_Open:  
 | 
        captchaName, captchaStream, g_captchaImageJpegDict = \  
 | 
        GetCaptchaImageByExt(ShareDefine.Def_Notify_WorldKey_ReloadJpegCaptcha,   
 | 
                             'jpegCaptchaPath.txt', g_captchaImageJpegDict)  
 | 
          
 | 
        #³É¹¦·µ»Ø  
 | 
        if captchaName != 0:  
 | 
            return captchaName, captchaStream  
 | 
      
 | 
      
 | 
    #Ëæ»úʧ°ÜÔò¶ÁÈ¡¹Ì¶¨tex  
 | 
    captchaName, captchaStream, g_captchaImageTexDict = \  
 | 
    GetCaptchaImageByExt(ShareDefine.Def_Notify_WorldKey_ReloadTexCaptcha,   
 | 
                         'texCaptchaPath.txt', g_captchaImageTexDict)  
 | 
  
 | 
    return captchaName, captchaStream  
 | 
  
 | 
##¸ù¾ÝÖ¸¶¨µÄÎļþ»ñÈ¡ÑéÖ¤Âë  
 | 
# @param reloadKey ÖضÁ×Öµä  
 | 
# @param fileName Ö¸¶¨Îļþ  
 | 
# @param gameDict ´æ´¢ÑéÖ¤Âë·¾¶  
 | 
# @return ÑéÖ¤Âë´ð°¸, ÑéÖ¤ÂëÄÚÈÝ  
 | 
def GetCaptchaImageByExt(reloadKey, fileName, gameDict):  
 | 
    if GameWorld.GetGameWorld().GetGameWorldDictByKey(reloadKey):  
 | 
        #ÖØ¶Á  
 | 
        gameDict = {}  
 | 
        #¹Ø±Õ×Öµä  
 | 
        GameWorld.GetGameWorld().SetGameWorldDict(reloadKey, 0)  
 | 
     
 | 
    if len(gameDict) == 0:  
 | 
        #ÖØÐ¼ÓÔØÑéÖ¤Âë  
 | 
        gameDict = ReloadCaptchaImageDict(fileName, gameDict)  
 | 
      
 | 
    if len(gameDict) != 0:  
 | 
        #Ëæ»ú¸øÒ»¸ö  
 | 
        captchaInfo = random.choice(gameDict.items())  
 | 
          
 | 
        if len(captchaInfo) != 2:  
 | 
            GameWorld.ErrLog('Ëæ»ú³öÒ»¸ö´íÎóµÄͼÐÎÑéÖ¤Âë %s'%str(captchaInfo))  
 | 
            return 0, '', gameDict  
 | 
          
 | 
        fileName, fileStream = GetParseCaptchaInfo(captchaInfo)  
 | 
          
 | 
        return fileName, fileStream, gameDict  
 | 
      
 | 
    GameWorld.ErrLog('GetCaptchaImage Error, Def_FilePath = %s%s'%(Def_FilePath, fileName))  
 | 
    return 0, '', gameDict  
 | 
  
 | 
  
 | 
##ÖØÐ¼ÓÔØÑéÖ¤Âë  
 | 
# @param ÎÞ²ÎÊý  
 | 
# @return ×Öµä  
 | 
def ReloadCaptchaImageDict(fileName, gameDict):  
 | 
    filePath = Def_FilePath + fileName  
 | 
    if not os.path.isfile(filePath):  
 | 
        return  
 | 
      
 | 
    fileIns = open(filePath, 'r')  
 | 
      
 | 
    for imageStr in fileIns.readlines():  
 | 
        imageInfo = imageStr.split()  
 | 
          
 | 
        if len(imageInfo) != 2:  
 | 
            continue  
 | 
          
 | 
        gameDict.update({imageInfo[0].strip():imageInfo[1].strip()})  
 | 
      
 | 
    GameWorld.Log('ReloadCaptchaImage %s'%fileName)  
 | 
          
 | 
    return gameDict  
 | 
  
 | 
##»ñµÃÎļþÃûºÍ¶þ½øÖÆÎļþÁ÷  
 | 
# @param captchaInfo È¡µ½µÄ×Öµä¶Ô  
 | 
# @return ÎļþÃûºÍ¶þ½øÖÆÎļþÁ÷  
 | 
def GetParseCaptchaInfo(captchaInfo):  
 | 
    fileName = captchaInfo[0].split('_')[-1]  
 | 
      
 | 
    openfilePath = Def_FilePath + captchaInfo[1]  
 | 
    try:  
 | 
        openfile = open(openfilePath, 'rb')  
 | 
    except:  
 | 
        GameWorld.ErrLog('Ëæ»ú³öÒ»¸ö´íÎóµÄͼÐÎÑéÖ¤Âë·¾¶ %s'%str(openfilePath))  
 | 
        return 0, ''  
 | 
      
 | 
    return fileName, openfile.read()  
 | 
  
 | 
###ÖØÐÂÉèÖÃͼÐÎÑéÖ¤Âë  
 | 
## @param ÎÞ  
 | 
## @return ÎÞ  
 | 
#def ReSetCaptcha():  
 | 
#    #¿ª¹âÑéÖ¤  
 | 
#    if ReadChConfig.GetEvalChConfig("Random_Captcha_Open") != Def_Random_Captcha_Open:  
 | 
#        return  
 | 
#      
 | 
#    curHour = GameWorld.GetCurrentTime().hour  
 | 
#      
 | 
#    #ͼƬºÍÎļþÉú³ÉÖ»Ò»¸öµØÍ¼´¦Àí£¨¾©³Ç£©  
 | 
#    if GameWorld.GetMap().GetMapID() == ChConfig.Def_MapID_JingCheng \  
 | 
#    and GameWorld.GetGameWorld().GetCopyMapID() == 0:  
 | 
#  
 | 
#        if curHour == 3:  
 | 
#            #5µãÖØÐÂÉú³ÉͼƬ  
 | 
#            os.system("start %s"%(Def_FilePath + "VerificationCodePic\\Start.bat"))  
 | 
#              
 | 
#        elif curHour == 4:  
 | 
#            #6µãÖØÐÂÉú³ÉÎļþ¼Ç¼  
 | 
#            os.system("start %s"%(Def_FilePath + "jpeg.bat"))  
 | 
#              
 | 
#    if curHour == 5:  
 | 
#        #7µãÖØ¶ÁÎļþ¼ÓÔØÖÁÓÎÏ·ÖÐ  
 | 
#        GameWorld.GetGameWorld().SetGameWorldDict(ShareDefine.Def_Notify_WorldKey_ReloadJpegCaptcha, 1)  
 | 
#      
 | 
#    return  
 | 
  
 | 
  
 | 
  
 |