Crossfire Server, Trunk
CFGuilds.py
Go to the documentation of this file.
1 # CFGuilds.py - classes for crossfire guilds
2 #
3 # Copyright (C) 2004 Todd Mitchell
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19 # The author can be reached via e-mail at temitchell@sourceforge.net
20 
21 import Crossfire
22 
23 from time import localtime, strftime, time
24 import os
25 from CFDataFile import CFDataFile, CFData
26 
28  GuildList = os.path.join(Crossfire.DataDirectory(),Crossfire.MapDirectory(),'templates','guild','GuildList')
29  try:
30  guildfile = open(GuildList,'r')
31  guildlisting = guildfile.read().split('\n')
32  guildfile.close()
33  guildlisting.pop()
34  except:
35  Crossfire.Log(Crossfire.LogError,'No GuildList file. Please check %s' %GuildList)
36  if (guildlisting):
37  Crossfire.Log(Crossfire.LogDebug, '%s' %guildlisting)
38  for guild in guildlisting:
39  if not CFGuildHouses().info(guild):
40  if CFGuildHouses().add_guild(guild):
41  Crossfire.Log(Crossfire.LogInfo,'New Guild: %s' %guild)
42 
43 def SearchGuilds(player):
44  guildlist = CFGuildHouses().list_guilds()
45  for guild in guildlist:
46  memberlist = CFGuild(guild).list_members()
47  for member in memberlist:
48  if member == player:
49  return guild
50  return 0
51 
53  '''Inter-Guild management class - loads guild from GuildList'''
54 
55  def __init__(self):
56  guildhousesheader= ['Founded_Date', 'Points', 'Status', 'Quest_points']
57  self.status=['active', 'suspended', 'probation', 'inactive']
58  self.guildhouselist = CFData('GuildHouses_log', guildhousesheader)
59  self.pointscap = 2000000000
60  self.qpcap = 2000000000
61 
62  def info(self, name):
63  record = self.guildhouselist.get_record(name)
64  if record:
65  return record
66  else:
67  return 0
68 
69  def list_guilds(self):
70  return self.guildhouselist.get_keys()
71 
72  def add_guild(self, name):
73  record={'#' : name
74  ,'Founded_Date' : 'never'
75  ,'Points' : 0
76  ,'Status' : 'inactive'
77  ,'Quest_points': 0
78  }
79  try:
80  self.guildhouselist.put_record(record)
81  return 1
82  except:
83  return 0
84 
85  def establish(self, name):
86  record = self.info(name)
87  date = strftime("%a, %d %b %Y", localtime(time()))
88  if record:
89  record['Founded_Date'] = date
90  record['Status'] = 'active'
91  self.guildhouselist.put_record(record)
92  return 1
93  else:
94  return 0
95 
96  def change_status(self, name, status):
97  record = self.info(name)
98  if record:
99  if self.status.count(status) != 0:
100  record['Status'] = status
101  self.guildhouselist.put_record(record)
102  return 1
103  else:
104  return 0
105  else:
106  return 0
107 
108  def add_questpoints(self, name, number):
109  record = self.info(name)
110  if record:
111  temp = int(record['Quest_points'])
112  if temp + number >= self.qpcap:
113  temp = self.qpcap
114  else:
115  temp = temp + number
116  record['Quest_points'] = temp
117  self.guildhouselist.put_record(record)
118  return 1
119  else:
120  return 0
121 
122  def update_points(self, name, number):
123  record = self.info(name)
124  if record:
125  temp = int(record['Points'])
126  if temp + number >= self.pointscap:
127  temp = self.pointscap
128  else:
129  temp = temp + number
130  record['Points'] = temp
131  self.guildhouselist.put_record(record)
132  return 1
133  else:
134  return 0
135 
136 class CFGuild:
137 
138  def __init__(self, guildname):
139  self.guildname = guildname
140  guildheader = ['Join_date', 'Rank', 'Status', 'Dues', 'Demerits', 'Quest_points']
141  self.ranks=['Initiate', 'Novice', 'Guildman', 'Journeyman', 'Master', 'GuildMaster']
142  self.status=['suspended', 'probation', 'good']
143  self.duescap = 1000000 #arbitrary limit on dues (if you change it remember it currently is an int)
144  self.demeritcap = 100 #arbitrary limit on demerits
145  self.qpcap = 100
146  guild_name = '%s_log' %self.guildname
147  self.guildlist = CFData(guild_name, guildheader)
148 
149  def add_member(self, name, rank):
150  date = strftime("%a, %d %b %Y", localtime(time()))
151  record={'#' : name
152  ,'Join_date' : date
153  ,'Rank' : rank
154  ,'Status' : 'good'
155  ,'Dues' : 0
156  ,'Demerits' : 0
157  ,'Quest_points': 0
158  }
159  try:
160  self.guildlist.put_record(record)
161  return 1
162  except:
163  return 0
164 
165  def remove_member(self, name):
166  try:
167  self.guildlist.remove_record(name)
168  return 1
169  except:
170  return 0
171 
172  def info(self, name):
173  record = self.guildlist.get_record(name)
174  if record:
175  return record
176  else:
177  return 0
178 
179  def list_members(self):
180  return self.guildlist.get_keys()
181 
182  def count_members(self):
183  number = len(self.guildlist.get_keys())
184  return number
185 
186  def add_demerits(self, name, number):
187  record = self.info(name)
188  if record:
189  temp = int(record['Demerits'])
190  if temp + number >= self.demeritcap:
191  temp = self.demeritcap
192  else:
193  temp = temp + number
194  record['Demerits'] = temp
195  self.guildlist.put_record(record)
196  return 1
197  else:
198  return 0
199 
200  def remove_demerits(self, name, number):
201  record = self.info(name)
202  if record:
203  temp = int(record['Demerits'])
204  if temp - number <=0:
205  temp = 0
206  else:
207  temp = temp - number
208  record['Demerits'] = temp
209  self.guildlist.put_record(record)
210  return 1
211  else:
212  return 0
213 
214  def add_questpoints(self, name, number):
215  record = self.info(name)
216  if record:
217  temp = int(record['Quest_points'])
218  if temp + number >= self.qpcap:
219  temp = self.qpcap
220  else:
221  temp = temp + number
222  record['Quest_points'] = temp
223  self.guildlist.put_record(record)
224  return 1
225  else:
226  return 0
227 
228  def pay_dues(self, name, number):
229  record = self.info(name)
230  if record:
231  temp = int(record['Dues'])
232  if temp + number >= self.duescap:
233  temp = self.duescap
234  else:
235  temp = temp + number
236  record['Dues'] = temp
237  self.guildlist.put_record(record)
238  # add points to the guild record
239  CFGuildHouses().update_points(self.guildname,number)
240  return 1
241  else:
242  return 0
243 
244  def clear_dues(self, name, number):
245  record = self.info(name)
246  if record:
247  record['Dues'] = 0
248  self.guildlist.put_record(record)
249  return 1
250  else:
251  return 0
252 
253  def change_status(self, name, status):
254  record = self.info(name)
255  if record:
256  if self.status.count(status) != 0:
257  record['Status'] = status
258  self.guildlist.put_record(record)
259  return 1
260  else:
261  return 0
262  else:
263  return 0
264 
265  def promote_member(self, name):
266  record = self.info(name)
267  if record:
268  currentrank = record['Rank']
269  if currentrank != 'GuildMaster':
270  ranknum = self.ranks.index(currentrank)
271  newrank = ranknum+1
272  record['Rank'] = self.ranks[newrank]
273  self.guildlist.put_record(record)
274  return 1
275  else:
276  return 0
277  else:
278  return 0
279 
280  def demote_member(self, name):
281  record = self.info(name)
282  if record:
283  currentrank = record['Rank']
284  if currentrank != 'Initiate':
285  ranknum = self.ranks.index(currentrank)
286  Crossfire.Log(Crossfire.LogDebug, "ranknum = %d"%ranknum)
287  newrank = ranknum-1
288  record['Rank'] = self.ranks[newrank]
289  self.guildlist.put_record(record)
290  return 1
291  else:
292  return 0
293  else:
294  return 0
CFGuilds.CFGuildHouses.qpcap
qpcap
Definition: CFGuilds.py:60
CFGuilds.CFGuild.list_members
def list_members(self)
Definition: CFGuilds.py:179
CFDataFile.CFData
Definition: CFDataFile.py:94
CFGuilds.SearchGuilds
def SearchGuilds(player)
Definition: CFGuilds.py:43
CFGuilds.CFGuild.clear_dues
def clear_dues(self, name, number)
Definition: CFGuilds.py:244
CFBank.open
def open()
Definition: CFBank.py:70
CFGuilds.CFGuild.status
status
Definition: CFGuilds.py:142
CFGuilds.CFGuild.ranks
ranks
Definition: CFGuilds.py:141
CFGuilds.CFGuild.promote_member
def promote_member(self, name)
Definition: CFGuilds.py:265
CFGuilds.CFGuild.add_member
def add_member(self, name, rank)
Definition: CFGuilds.py:149
CFGuilds.CFGuild.count_members
def count_members(self)
Definition: CFGuilds.py:182
CFGuilds.CFGuild.remove_demerits
def remove_demerits(self, name, number)
Definition: CFGuilds.py:200
CFGuilds.CFGuildHouses.guildhouselist
guildhouselist
Definition: CFGuilds.py:58
CFGuilds.CFGuild.change_status
def change_status(self, name, status)
Definition: CFGuilds.py:253
CFGuilds.CFGuildHouses.change_status
def change_status(self, name, status)
Definition: CFGuilds.py:96
CFGuilds.CFGuildHouses.__init__
def __init__(self)
Definition: CFGuilds.py:55
CFGuilds.CFGuildHouses.info
def info(self, name)
Definition: CFGuilds.py:62
CFGuilds.CFGuildHouses.add_guild
def add_guild(self, name)
Definition: CFGuilds.py:72
CFGuilds.CFGuildHouses.pointscap
pointscap
Definition: CFGuilds.py:59
CFGuilds.CFGuild.guildname
guildname
Definition: CFGuilds.py:139
CFGuilds.CFGuildHouses.update_points
def update_points(self, name, number)
Definition: CFGuilds.py:122
CFGuilds.CFGuild.remove_member
def remove_member(self, name)
Definition: CFGuilds.py:165
CFGuilds.CFGuild.demeritcap
demeritcap
Definition: CFGuilds.py:144
disinfect.count
int count
Definition: disinfect.py:7
CFGuilds.CFGuildHouses.establish
def establish(self, name)
Definition: CFGuilds.py:85
CFGuilds.CFGuild.pay_dues
def pay_dues(self, name, number)
Definition: CFGuilds.py:228
CFGuilds.CFGuild
Definition: CFGuilds.py:136
CFGuilds.CFGuild.add_questpoints
def add_questpoints(self, name, number)
Definition: CFGuilds.py:214
CFGuilds.CFGuildHouses
Definition: CFGuilds.py:52
CFGuilds.GuildUpdate
def GuildUpdate()
Definition: CFGuilds.py:27
CFGuilds.CFGuild.demote_member
def demote_member(self, name)
Definition: CFGuilds.py:280
CFGuilds.CFGuild.info
def info(self, name)
Definition: CFGuilds.py:172
npc_dialog.index
int index
Definition: npc_dialog.py:102
CFGuilds.CFGuild.qpcap
qpcap
Definition: CFGuilds.py:145
CFGuilds.CFGuild.duescap
duescap
Definition: CFGuilds.py:143
CFGuilds.CFGuildHouses.list_guilds
def list_guilds(self)
Definition: CFGuilds.py:69
make_face_from_files.int
int
Definition: make_face_from_files.py:26
CFGuilds.CFGuild.__init__
def __init__(self, guildname)
Definition: CFGuilds.py:138
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2671
CFGuilds.CFGuildHouses.status
status
Definition: CFGuilds.py:57
CFGuilds.CFGuild.add_demerits
def add_demerits(self, name, number)
Definition: CFGuilds.py:186
CFGuilds.CFGuild.guildlist
guildlist
Definition: CFGuilds.py:147
CFGuilds.CFGuildHouses.add_questpoints
def add_questpoints(self, name, number)
Definition: CFGuilds.py:108