1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/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
 
#---------------------------------------------------------------------