#!/usr/bin/python
|
# -*- coding: GBK -*-
|
#---------------------------------------------------------------------
|
#
|
#---------------------------------------------------------------------
|
##@package SubjectLib
|
# @todo: ´ðÌâ¶ÁÌâ¹ÜÀíÆ÷
|
#
|
# @author: panwei
|
# @date 2010-04-20
|
# @version 1.1
|
#
|
# @note:
|
#Ä£¿éÏêϸ˵Ã÷
|
#ΪÁ˱ÜÃâͬһ¸öÍæ¼ÒÈ¡µ½ÏàͬÌâÄ¿µÄÇé¿ö:
|
#Ìâ¿â·ÖΪ: ÔʼÌâ¿âºÍ¶¯Ì¬Ìâ¿â, ÔʼÌâ¿â¶ÁÈ¡ÌâÄ¿,
|
#
|
#1. ¶¯Ì¬Ìâ¿â ¸ù¾ÝÔʼÌâ¿âÉú³ÉËæ»úÌâ¿â
|
#2. Ìâ¿â´Ó¶¯Ì¬Ìâ¿âÀïÃæÈ¡
|
#3. ÿȡһÌâ, ´Ó¶¯Ì¬Ìâ¿âÖÐɾ³ýÕâÒ»Ìâ
|
#4. Èç¹û¶¯Ì¬Ìâ¿âÈ¡ÍêÁË, ÔòÖØÐÂÉú³É¶¯Ì¬Ìâ¿â
|
#---------------------------------------------------------------------
|
# @change: "2011-01-20 17:30" panwei ¸ñʽ»¯½Å±¾
|
#---------------------------------------------------------------------
|
"""Version = 2011-01-20 17:30"""
|
#---------------------------------------------------------------------
|
import random
|
import os
|
import LogUI
|
import ChConfig
|
import GameWorld
|
import copy
|
import QuestManager
|
#---------------------------------------------------------------------
|
#ËùÓеÄÌâ¿â,keyΪÌâÄ¿ÀàÐÍ,µ¥¶ÀÌâ¿âΪ[] -> ÔʼÌâ¿â
|
__AllSubject = {}
|
#¶¯Ì¬Ìâ¿â
|
__DelicacySuject = {}
|
#---------------------------------------------------------------------
|
## Ëæ»ú»ñµÃÒ»µÀÌâÄ¿
|
# @param key ×Ö·û´®
|
# @return chinSuject Ò»¸öÌâÄ¿
|
# @remarks º¯ÊýÏêϸ˵Ã÷.
|
def GetSubject(key):
|
global __DelicacySuject
|
|
if len(__AllSubject) == 0:
|
QuestManager.ReadSubjectLib()
|
|
key = key.lower()
|
|
#µÚÒ»´Î¼ÓÔØÌâÄ¿,³õʼ»¯
|
if len(__DelicacySuject) == 0:
|
__DelicacySuject = copy.deepcopy(__AllSubject)
|
|
#»ñµÃÌâ¿â
|
sujectList = __DelicacySuject.get(key)
|
#ÅׯúÒì³£
|
if sujectList == None:
|
GameWorld.Log("###Òì³£»ñµÃÌâÄ¿´íÎó,ÎÞÕâÀàÌâÄ¿key = %s"%(key))
|
return
|
|
#ÎÞÌâÄ¿ÁË,ÖØÐ¼ÓÔØÔʼÌâ¿â
|
if len(sujectList) == 0:
|
__DelicacySuject = copy.deepcopy(__AllSubject)
|
|
#ÖØÐ¼ÓÔØÌâ¿â
|
sujectList = __DelicacySuject.get(key)
|
#Ëæ»úÑ¡ÔñÒ»µÀÌâÄ¿(·µ»ØÖµÎªÀàclass ChinSubject µÄʵÀý)
|
chinSuject = random.choice(sujectList)
|
#ɾ³ýÕâ¸öÌâÄ¿
|
sujectList.remove(chinSuject)
|
return chinSuject
|
|
#---------------------------------------------------------------------
|
## µ¥¶ÀÒ»µÀÌâ
|
#
|
# PyClassÀàµÄÏêϸ˵Ã÷.
|
class ChinSubject:
|
ID = 0
|
Subject = ""
|
Answer = 0
|
Option = []
|
line = ""
|
## ³õʼ»¯º¯Êý
|
# @param line ×Ö·û´®
|
# @return None
|
# @remarks º¯ÊýÏêϸ˵Ã÷.
|
def __init__(self , line):
|
self.Clear()
|
self.line = line
|
return
|
|
## ³õʼ»¯
|
# @param
|
# @return None
|
# @remarks º¯ÊýÏêϸ˵Ã÷.
|
def Clear(self):
|
self.ID = 0
|
self.Subject = ""
|
self.Answer = 0
|
self.Option = []
|
self.line = ""
|
return
|
|
## ´Ó×Ö·û´®Àï¶ÁÈ¡
|
# @param
|
# @return None
|
# @remarks º¯ÊýÏêϸ˵Ã÷.
|
def ReadFromStr(self):
|
lineList = self.line.split('\t')
|
self.ID = int(lineList[0])
|
self.Subject = lineList[1]
|
self.Answer = lineList[2]
|
for i in range(3, len(lineList)):
|
curStr = lineList[i].strip()
|
if curStr == '':
|
continue
|
self.Option.append(curStr)
|
|
return
|
|
## ÌâÄ¿ID
|
# @param
|
# @return ÌâÄ¿ID
|
# @remarks º¯ÊýÏêϸ˵Ã÷.
|
def GetID(self):
|
return self.ID
|
|
## 퉀|
# @param
|
# @return 퉀|
# @remarks º¯ÊýÏêϸ˵Ã÷.
|
def GetSubject(self):
|
return self.Subject
|
|
## ´ð°¸
|
# @param
|
# @return ´ð°¸
|
# @remarks º¯ÊýÏêϸ˵Ã÷.
|
def GetAnswer(self):
|
return self.Answer
|
|
## Ñ¡Ïî
|
# @param
|
# @return Ñ¡Ïî
|
# @remarks º¯ÊýÏêϸ˵Ã÷.
|
def GetOption(self):
|
return self.Option
|
#---------------------------------------------------------------------
|
## ¶ÁÈ¡Êý¾Ý
|
# @param ÎÞ
|
# @return None
|
# @remarks º¯ÊýÏêϸ˵Ã÷.
|
def ReadData():
|
global __AllSubject
|
|
mapID = GameWorld.GetMap().GetMapID()
|
#²»ÐèÒªÖØ¶ÁµÄµØÍ¼
|
if mapID not in ChConfig.Def_Subject_Map:
|
return
|
|
curPath = ChConfig.GetAppPath() + "QUESTDATAGb\\SubjectLib"
|
|
if not os.path.isdir(curPath):
|
GameWorld.Log("·¾¶ = %s , ÎÞ·¨²éÕÒµ½´ðÌâÌâ¿â!"%(curPath))
|
return
|
|
files = os.listdir(curPath)
|
for file in files:
|
ext = os.path.splitext(file)
|
postfix = ext[1]
|
if postfix != ".txt":
|
continue
|
|
fileName = ext[0]
|
|
file = open(os.path.join(curPath, file),'r')
|
|
for line in file.readlines():
|
#È¥³ýͷβÎÞÓõÄ×Ö·û
|
line = line.strip()
|
|
if line == '':
|
continue
|
|
curSubject = ChinSubject(line)
|
curSubject.ReadFromStr()
|
|
if __AllSubject.has_key(fileName):
|
__AllSubject.get(fileName).append(curSubject)
|
|
else:
|
__AllSubject.update({fileName:[curSubject]})
|
|
return
|
|
## ½âÎöÌâÄ¿Êý¾Ý
|
# @param curData ÌâÄ¿Êý¾Ý
|
# @param fileName ÎļþÃû
|
# @return None
|
# @remarks º¯ÊýÏêϸ˵Ã÷.
|
def ReadSubjectData(curData, fileName):
|
curData = curData.split('\r\n')
|
for line in curData:
|
#È¥³ýͷβÎÞÓõÄ×Ö·û
|
line = line.strip()
|
|
if line == '':
|
continue
|
|
curSubject = ChinSubject(line)
|
curSubject.ReadFromStr()
|
|
if __AllSubject.has_key(fileName):
|
__AllSubject.get(fileName).append(curSubject)
|
|
else:
|
__AllSubject.update({fileName:[curSubject]})
|
return
|
|
#---------------------------------------------------------------------
|
|
|