message.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //-----------------------------------------------------------------------------
  23. // Functions that process commands sent from the server.
  24. // Game event descriptions, which may or may not include text messages, can be
  25. // sent using the message* functions in core/scripts/server/message.cs. Those
  26. // functions do commandToClient with the tag ServerMessage, which invokes the
  27. // function below.
  28. // For ServerMessage messages, the client can install callbacks that will be
  29. // run, according to the "type" of the message.
  30. function clientCmdServerMessage(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
  31. {
  32. // Get the message type; terminates at any whitespace.
  33. %tag = getWord(%msgType, 0);
  34. // First see if there is a callback installed that doesn't have a type;
  35. // if so, that callback is always executed when a message arrives.
  36. for (%i = 0; (%func = $MSGCB["", %i]) !$= ""; %i++) {
  37. call(%func, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10);
  38. }
  39. // Next look for a callback for this particular type of ServerMessage.
  40. if (%tag !$= "") {
  41. for (%i = 0; (%func = $MSGCB[%tag, %i]) !$= ""; %i++) {
  42. call(%func, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10);
  43. }
  44. }
  45. }
  46. // Called by the client to install a callback for a particular type of
  47. // ServerMessage.
  48. function addMessageCallback(%msgType, %func)
  49. {
  50. for (%i = 0; (%afunc = $MSGCB[%msgType, %i]) !$= ""; %i++) {
  51. // If it already exists as a callback for this type,
  52. // nothing to do.
  53. if (%afunc $= %func) {
  54. return;
  55. }
  56. }
  57. // Set it up.
  58. $MSGCB[%msgType, %i] = %func;
  59. }
  60. // The following is the callback that will be executed for every ServerMessage,
  61. // because we're going to install it without a specified type. Any type-
  62. // specific callbacks will be executed afterward.
  63. // This just invokes onServerMessage, which can be overridden by the game
  64. function onServerMessage(%a, %b, %c, %d, %e, %f, %g, %h, %i)
  65. {
  66. echo("onServerMessage: ");
  67. if(%a !$= "") echo(" +- a: " @ %a);
  68. if(%b !$= "") echo(" +- b: " @ %b);
  69. if(%c !$= "") echo(" +- c: " @ %c);
  70. if(%d !$= "") echo(" +- d: " @ %d);
  71. if(%e !$= "") echo(" +- e: " @ %e);
  72. if(%f !$= "") echo(" +- f: " @ %f);
  73. if(%g !$= "") echo(" +- g: " @ %g);
  74. if(%h !$= "") echo(" +- h: " @ %h);
  75. if(%i !$= "") echo(" +- i: " @ %i);
  76. }
  77. function defaultMessageCallback(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
  78. {
  79. onServerMessage(detag(%msgString));
  80. }
  81. // Register that default message handler now.
  82. addMessageCallback("", defaultMessageCallback);