char_gen.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. '''
  2. char_gen.py
  3. The basic character generation module. Allows accounts to create new characters
  4. with basic selection for name, sex, and race.
  5. '''
  6. import mudsys, mud, socket, char, hooks
  7. def check_char_name(arg):
  8. '''checks to make sure the character name is valid. Names are valid if they
  9. are greater than 2 characters, less than 13, and comprise only alpha
  10. characters.'''
  11. return len(arg) >= 3 and len(arg) <= 12 and arg.isalpha()
  12. def cg_name_handler(sock, arg):
  13. if not check_char_name(arg):
  14. sock.send("{cIllegal name, please pick another.{n\r\n")
  15. elif mudsys.player_exists(arg):
  16. sock.send("{cA player with that name already exists.{n\r\n")
  17. elif mudsys.player_creating(arg):
  18. sock.send("{cA player is already being created with that name.{n\r\n")
  19. elif arg.lower().startswith("guest"):
  20. sock.send("{cCharacter names cannot begin with 'guest'.{n\r\n")
  21. else:
  22. name = arg[0].upper() + arg[1:]
  23. ch = mudsys.create_player(name)
  24. if ch == None:
  25. sock.send("{cIllegal name, please pick another.{n\r\n")
  26. else:
  27. mudsys.attach_char_socket(ch, sock)
  28. ch.rdesc = ch.name + " is here."
  29. sock.pop_ih()
  30. def cg_sex_handler(sock, arg):
  31. try:
  32. result = {
  33. 'M' : 'male',
  34. 'F' : 'female',
  35. }[arg[0].upper()]
  36. sock.ch.sex = result
  37. sock.pop_ih()
  38. except:
  39. sock.send("{cInvalid sex, try again.{n\r\n")
  40. def cg_race_handler(sock, arg):
  41. if not mud.is_race(arg, True):
  42. sock.send("{cInvalid race selection, try again.{n\r\n")
  43. else:
  44. sock.ch.race = arg.lower()
  45. sock.pop_ih()
  46. def cg_finish_handler(sock, arg):
  47. # pop our input handler for finishing character generation
  48. sock.pop_ih()
  49. # log that the character created
  50. mud.log_string("New player: " + sock.ch.name + " has entered the game.")
  51. # register and save him to disk and to an account
  52. mudsys.do_register(sock.ch)
  53. # make him exist in the game for functions to look him up
  54. mudsys.try_enter_game(sock.ch)
  55. # run the init_player hook
  56. hooks.run("init_player", hooks.build_info("ch", (sock.ch,)))
  57. # attach him to his account and save the account
  58. sock.account.add_char(sock.ch)
  59. mudsys.do_save(sock.account)
  60. mudsys.do_save(sock.ch)
  61. # clear their screen
  62. sock.ch.act("clear")
  63. # send them the motd
  64. sock.ch.page(mud.get_motd())
  65. # make him look at the room
  66. sock.ch.act("look")
  67. # run our enter hook
  68. hooks.run("enter", hooks.build_info("ch rm", (sock.ch, sock.ch.room)))
  69. def cg_name_prompt(sock):
  70. sock.send_raw("What is your character's name? ")
  71. def cg_sex_prompt(sock):
  72. sock.send_raw("What is your sex (M/F)? ")
  73. def cg_race_prompt(sock):
  74. sock.send("Available races are: ")
  75. sock.send(mud.list_races(True))
  76. sock.send_raw("\r\nPlease enter your choice: ")
  77. def cg_finish_prompt(sock):
  78. sock.send_raw("{c*** Press enter to finish character generation:{n ")
  79. ################################################################################
  80. # character generation hooks
  81. ################################################################################
  82. def char_gen_hook(info):
  83. '''Put a socket into the character generation menu when character generation
  84. hooks are called.
  85. '''
  86. sock, = hooks.parse_info(info)
  87. sock.push_ih(mudsys.handle_cmd_input, mudsys.show_prompt, "playing")
  88. sock.push_ih(cg_finish_handler, cg_finish_prompt)
  89. sock.push_ih(cg_race_handler, cg_race_prompt)
  90. sock.push_ih(cg_sex_handler, cg_sex_prompt)
  91. sock.push_ih(cg_name_handler, cg_name_prompt)
  92. def guest_gen_hook(info):
  93. sock, = hooks.parse_info(info)
  94. sock.push_ih(mudsys.handle_cmd_input, mudsys.show_prompt, "playing")
  95. # make the guest character
  96. ch = mudsys.create_player("Guest")
  97. # oops...
  98. if ch == None:
  99. sock.send("Sorry, there were issues creating a guest account.")
  100. sock.close()
  101. mudsys.attach_char_socket(ch, sock)
  102. ch.rdesc = "a guest player is here, exploring the world."
  103. ch.name = ch.name + str(ch.uid)
  104. ch.race = "human"
  105. # log that the character created
  106. mud.log_string("Guest character created (id %d)." % ch.uid)
  107. # make him exist in the game for functions to look him up
  108. mudsys.try_enter_game(ch)
  109. # run the init_player hook
  110. hooks.run("init_player", hooks.build_info("ch", (ch,)))
  111. # clear our screen
  112. ch.act("clear")
  113. # send them the motd
  114. ch.page(mud.get_motd())
  115. # make him look at the room
  116. ch.act("look")
  117. # run our enter hook
  118. hooks.run("enter", hooks.build_info("ch rm", (ch, ch.room)))
  119. ################################################################################
  120. # loading and unloading the module
  121. ################################################################################
  122. hooks.add("create_character", char_gen_hook)
  123. hooks.add("create_guest", guest_gen_hook)
  124. def __unload__():
  125. '''removes the hooks for character generation'''
  126. hooks.remove("create_character", char_gen_hook)
  127. hooks.remove("create_guest", guest_gen_hook)