utils.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. '''
  2. utils.py
  3. Various utility functions used by other Python modules.
  4. '''
  5. import mud
  6. ################################################################################
  7. # utility functions
  8. ################################################################################
  9. def parse_keywords(kw):
  10. '''turns a comma-separated list of strings to a list of keywords'''
  11. list = kw.lower().split(",")
  12. for i in range(len(list)):
  13. list[i] = list[i].strip()
  14. return list
  15. def is_one_keyword(kw, word, abbrev_ok = False):
  16. '''returns whether or not the single word is a keyword in the list'''
  17. for one_kw in kw:
  18. if word == one_kw:
  19. return True
  20. elif abbrev_ok and len(one_kw)>len(word) and one_kw[0:len(word)]==word:
  21. return True
  22. return False
  23. def is_keyword(kw, word, abbrev_ok = False):
  24. '''returns whether or not the word (or list of words) is a keyword'''
  25. kw = parse_keywords(kw)
  26. word = parse_keywords(word)
  27. for one_word in word:
  28. if is_one_keyword(kw, one_word, abbrev_ok):
  29. return True
  30. return False
  31. def has_proto(ch, proto):
  32. '''returns whether or not the character has on his or her person an object
  33. that inherits from the given prototype'''
  34. for obj in ch.inv + ch.eq:
  35. if obj.isinstance(proto):
  36. return True
  37. return False
  38. def find_all_chars(looker, list, name, proto = None, must_see = True):
  39. '''returns a list of all the chars that match the supplied constraints'''
  40. found = []
  41. for ch in list:
  42. if must_see and not looker.cansee(ch):
  43. continue
  44. elif name != None and is_keyword(ch.keywords, name, True):
  45. found.append(ch)
  46. elif proto != None and ch.isinstance(proto):
  47. found.append(ch)
  48. return found
  49. def find_char(looker, list, num, name, proto = None, must_see = True):
  50. '''returns the numth char to match the supplied constraints'''
  51. count = 0
  52. for ch in list:
  53. if must_see and not looker.cansee(ch):
  54. continue
  55. elif name != None and is_keyword(ch.keywords, name, True):
  56. count = count + 1
  57. elif proto != None and ch.isinstance(proto):
  58. count = count + 1
  59. if count == num:
  60. return ch
  61. return None
  62. def find_all_objs(looker, list, name, proto = None, must_see = True):
  63. '''returns a list of all the objects that match the supplied constraints'''
  64. found = []
  65. for obj in list:
  66. if must_see and not looker.cansee(obj):
  67. continue
  68. elif name != None and is_keyword(obj.keywords, name, True):
  69. found.append(obj)
  70. elif proto != None and obj.isinstance(proto):
  71. found.append(obj)
  72. return found
  73. def find_obj(looker, list, num, name, proto = None, must_see = True):
  74. '''returns the numth object to match the supplied constraints'''
  75. count = 0
  76. for obj in list:
  77. if must_see and not looker.cansee(obj):
  78. continue
  79. elif name != None and is_keyword(obj.keywords, name, True):
  80. count = count + 1
  81. elif proto != None and obj.isinstance(proto):
  82. count = count + 1
  83. if count == num:
  84. return obj
  85. return None
  86. def get_count(str):
  87. '''separates a name and a count, and returns the two'''
  88. parts = str.lower().split(".", 1)
  89. # did we get two, or one?
  90. if len(parts) == 1 and parts[0] == "all":
  91. return "all", ""
  92. elif len(parts) == 1:
  93. return 1, str
  94. if parts[0] == "all":
  95. return "all", parts[1]
  96. try:
  97. return int(parts[0]), parts[1]
  98. except:
  99. return 1, str
  100. def build_show_list(ch, list, s_func, m_func = None, joiner = "\r\n",
  101. and_end=False):
  102. '''builds a list of things to show a character. s_func is the description if
  103. there is only a single item of the type. m_func is the description if
  104. there are multiple occurences of the thing in the list'''
  105. # the outbound info
  106. buf = [ ]
  107. # maps descriptions to counts
  108. counts = { }
  109. # build up our counts
  110. for thing in list:
  111. if counts.has_key(s_func(thing)):
  112. counts[s_func(thing)] = counts[s_func(thing)] + 1
  113. else:
  114. counts[s_func(thing)] = 1
  115. # print out our counts
  116. for thing in list:
  117. # only display > 0 counts. Otherwise it has been displayed
  118. if counts.has_key(s_func(thing)):
  119. count = counts.pop(s_func(thing))
  120. # display our item(s)
  121. if count == 1:
  122. buf.append(s_func(thing))
  123. elif m_func == None or m_func(thing) == "":
  124. buf.append("(" + str(count) + ") " + s_func(thing))
  125. else:
  126. buf.append(m_func(thing) % count)
  127. else: pass
  128. # do we have to put "and" at the end?
  129. if and_end and len(buf) > 1:
  130. last = buf.pop()
  131. return joiner.join(buf) + " and " + last
  132. return joiner.join(buf)
  133. def show_list(ch, list, s_func, m_func = None):
  134. '''shows a list of things to the character. s_func is the description if
  135. there is only a single item of the type. m_func is the description if
  136. there are multiple occurences of the thing in the list'''
  137. ch.send(build_show_list(ch, list, s_func, m_func, "\r\n"))
  138. def olc_display_table(sock, list, num_cols, disp = lambda x: x):
  139. '''used by OLC functions to display a list of options in a table form.
  140. Also displays each option\'s position number and colorizes everything.'''
  141. print_room = (80 - 10*num_cols)/num_cols;
  142. fmt = " {c%%2d{y) {g%%-%ds%%s" % print_room
  143. i = 0
  144. # display each cell
  145. for item in list:
  146. endtag = " "
  147. if i % num_cols == (num_cols - 1):
  148. endtag = "\r\n"
  149. sock.send_raw(fmt % (i, disp(item), endtag))
  150. i += 1
  151. # do we need to end this with a newline?
  152. if i % num_cols != 0:
  153. sock.send_raw("\r\n")
  154. def aan(word):
  155. '''return "a" or "an", depending on the word.'''
  156. if len(word) == 0 or not word[0].lower() in "aeiou":
  157. return "a " + word
  158. return "an " + word
  159. def chk_conscious(ch, cmd):
  160. if ch.pos in ["sleeping", "unconscious"]:
  161. ch.send("You cannot do that while " + ch.pos + "!")
  162. return False