history.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. '''
  2. history.py
  3. a little database module for storing communication histories. Can store by
  4. arbitrary groupings e.g., for guild, global, zone, or personal communications.
  5. '''
  6. import mudsys
  7. ################################################################################
  8. # local variables
  9. ################################################################################
  10. # our table that maps communication type to a second table of groupings and
  11. # grouping functions
  12. comm_table = { }
  13. # what is the maximum length of our history logs
  14. MAX_HISTORY_LEN = 20
  15. ################################################################################
  16. # history functions
  17. ################################################################################
  18. def register_comm_history(type, group_func):
  19. '''register a new type of history, and add a grouping function as well.'''
  20. if not type in comm_table:
  21. comm_table[type] = (group_func, { })
  22. def get_history(ch, type):
  23. '''return the communication history for a character.'''
  24. group_func, table = comm_table[type]
  25. key = group_func(ch)
  26. if not key in table:
  27. return [ ]
  28. return table[key]
  29. def add_history(ch, type, mssg):
  30. group_func, table = comm_table[type]
  31. key = group_func(ch)
  32. if key != None:
  33. if not key in table:
  34. table[key] = [ ]
  35. table[key].append(mssg)
  36. # make sure we don't get too big
  37. while len(table[key]) > MAX_HISTORY_LEN:
  38. table[key].pop(0)
  39. ################################################################################
  40. # commands
  41. ################################################################################
  42. def cmd_history(ch, cmd, arg):
  43. '''Communication logs are stored as you receive communication. To review
  44. communication you have used, you can use the history command.'''
  45. arg = arg.lower()
  46. if arg == "":
  47. opts = comm_table.keys()
  48. opts.sort()
  49. ch.send("History logs available to you are:")
  50. ch.send(" " + ", ".join(opts))
  51. elif not arg in comm_table:
  52. ch.send("There is no history log for that type of communication.")
  53. else:
  54. group_func, table = comm_table[arg]
  55. key = group_func(ch)
  56. if not key in table:
  57. ch.send("Your history is empty.")
  58. else:
  59. ch.page("\r\n".join(table[key]) + "\r\n")
  60. ################################################################################
  61. # initialization
  62. ################################################################################
  63. mudsys.add_cmd("history", None, cmd_history, "player", False)