/tags/sgs-0.9.1/example/hack/src/com/sun/sgs/example/hack/server/CreatorMessageHandler.java

https://github.com/Cyberqat/Red-Dwarf · Java · 125 lines · 64 code · 21 blank · 40 comment · 1 complexity · 43cec7b406d01845618cceff86fddd29 MD5 · raw file

  1. /*
  2. * Copyright 2007 Sun Microsystems, Inc. All rights reserved
  3. */
  4. package com.sun.sgs.example.hack.server;
  5. import com.sun.sgs.app.AppContext;
  6. import com.sun.sgs.app.ManagedReference;
  7. import com.sun.sgs.example.hack.share.CharacterStats;
  8. import java.io.Serializable;
  9. import java.nio.ByteBuffer;
  10. /**
  11. * This <code>MessageHandler</code> is used by <code>Creator</code> to define
  12. * and handle all messages sent from the client.
  13. */
  14. public class CreatorMessageHandler implements MessageHandler, Serializable {
  15. private static final long serialVersionUID = 1;
  16. // the current character stats
  17. private CharacterStats currentStats;
  18. private int characterId;
  19. /**
  20. * Creates a new <code>CreatorMessageHandler</code>.
  21. */
  22. public CreatorMessageHandler() {
  23. }
  24. /**
  25. * Called when the given <code>Player</code> has a message to handle.
  26. *
  27. * @param player the <code>Player</code> who received the message
  28. * @param data the message to handle
  29. */
  30. public void handleMessage(Player player, byte [] message) {
  31. ByteBuffer data = ByteBuffer.allocate(message.length);
  32. data.put(message);
  33. data.rewind();
  34. // the command identifier is always stored in the first byte
  35. int command = (int)(data.get());
  36. // FIXME: we should use an enum to define the messages
  37. //try {
  38. switch (command) {
  39. case 1:
  40. // get the id and create the stats
  41. characterId = data.getInt();
  42. currentStats = getNewStats(characterId);
  43. player.sendCharacterStats(-1, currentStats);
  44. break;
  45. case 2:
  46. // get the name...
  47. byte [] bytes = new byte[data.remaining()];
  48. data.get(bytes);
  49. String characterName = new String(bytes);
  50. // ...create the character...
  51. PlayerCharacter pc = setupCharacter(player, characterName);
  52. player.getCharacterManager().addCharacter(pc);
  53. // ...and go to the lobby
  54. moveToLobby(player);
  55. break;
  56. case 3:
  57. // go to the lobby
  58. moveToLobby(player);
  59. break;
  60. }
  61. /*} catch (Exception e) {
  62. // FIXME: here what we want to do is either log the error, or
  63. // send back a generic error response
  64. }*/
  65. }
  66. /**
  67. *
  68. */
  69. private CharacterStats getNewStats(int id) {
  70. // FIXME: this should change based on the character class, but for
  71. // now it's purely random
  72. int hp = NSidedDie.roll20Sided() + NSidedDie.roll20Sided();
  73. return new CharacterStats("", NSidedDie.roll20Sided(),
  74. NSidedDie.roll20Sided(),
  75. NSidedDie.roll20Sided(),
  76. NSidedDie.roll20Sided(),
  77. NSidedDie.roll20Sided(),
  78. NSidedDie.roll20Sided(), hp, hp);
  79. }
  80. /**
  81. *
  82. */
  83. private PlayerCharacter setupCharacter(Player player, String name) {
  84. CharacterStats stats =
  85. new CharacterStats(name, currentStats.getStrength(),
  86. currentStats.getIntelligence(),
  87. currentStats.getDexterity(),
  88. currentStats.getWisdom(),
  89. currentStats.getConstitution(),
  90. currentStats.getCharisma(),
  91. currentStats.getHitPoints(),
  92. currentStats.getMaxHitPoints());
  93. return new PlayerCharacter(player, characterId, stats);
  94. }
  95. /**
  96. *
  97. */
  98. private void moveToLobby(Player player) {
  99. Lobby lobby = AppContext.getDataManager().
  100. getBinding(Lobby.IDENTIFIER, Lobby.class);
  101. AppContext.getTaskManager().
  102. scheduleTask(new MoveGameTask(player, lobby));
  103. }
  104. }