LocalConnection.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using UnityEngine;
  2. using Sfs2X.Entities.Data;
  3. public class LocalConnection : IConnection
  4. {
  5. MessageManager.MessageCallBackDelegate callBack;
  6. private int _myUserId = 1;
  7. public int myUserId
  8. {
  9. set
  10. {
  11. }
  12. get
  13. {
  14. return _myUserId;
  15. }
  16. }
  17. public bool holding
  18. {
  19. get{
  20. return false;
  21. }
  22. }
  23. public LocalConnection ()
  24. {
  25. }
  26. public void ProcessEvents()
  27. {
  28. }
  29. public void Connect()
  30. {
  31. }
  32. public bool IsConnected
  33. {
  34. get{
  35. return true;
  36. }
  37. }
  38. public void SetResponseMessageCallBack(MessageManager.MessageCallBackDelegate callBack)
  39. {
  40. this.callBack = callBack;
  41. }
  42. public void SendMessage(Message msg)
  43. {
  44. if(callBack != null)
  45. {
  46. if(msg.cmd == Command.EnterBase)
  47. DealEnterBase(msg);
  48. else if(msg.cmd == Command.SearchRoom)
  49. DealSearchBattle(msg);
  50. else
  51. DealBattleMessage(msg);
  52. }
  53. }
  54. private void DealEnterBase(Message msg)
  55. {
  56. string mapName = MapData.MapID.Base.ToString ();
  57. ISFSArray arr = new SFSArray ();
  58. ISFSObject myObj = new SFSObject ();
  59. myObj.PutInt ("u", myUserId);
  60. myObj.PutInt ("t", TeamUtil.Team.Blue.GetHashCode());
  61. myObj.PutUtfString ("i", "1");
  62. myObj.PutUtfString ("n", WWW.EscapeURL(Session.GetInstance().myUserData.nick));
  63. arr.AddSFSObject(myObj);
  64. ISFSObject oppObj = new SFSObject ();
  65. oppObj.PutInt ("u", -1);
  66. oppObj.PutInt ("t", TeamUtil.Team.Red.GetHashCode());
  67. oppObj.PutUtfString ("i", "-1");
  68. oppObj.PutUtfString ("n", RandomNPCNick());
  69. arr.AddSFSObject (oppObj);
  70. ISFSObject data = new SFSObject();
  71. data.PutUtfString("m", mapName);
  72. data.PutSFSArray("a", arr);
  73. Message callBackMsg = new Message(Command.SearchRoom, data);
  74. callBack(callBackMsg.GetMessagePackage());
  75. }
  76. private void DealSearchBattle(Message msg)
  77. {
  78. string mapName = MapData.GetRandomMap ().ToString ();
  79. ISFSArray arr = new SFSArray ();
  80. ISFSObject myObj = new SFSObject ();
  81. myObj.PutInt ("u", myUserId);
  82. myObj.PutInt ("t", TeamUtil.Team.Blue.GetHashCode());
  83. myObj.PutUtfString ("i", Session.GetInstance().myUserData.id.ToString());
  84. myObj.PutUtfString ("n", WWW.EscapeURL(Session.GetInstance().myUserData.nick));
  85. arr.AddSFSObject(myObj);
  86. // ISFSObject oppObj = new SFSObject ();
  87. // oppObj.PutInt ("u", -1);
  88. // oppObj.PutInt ("t", TeamUtil.Team.Red.GetHashCode());
  89. // oppObj.PutUtfString ("n", RandomNPCNick());
  90. // arr.AddSFSObject (oppObj);
  91. ISFSObject data = new SFSObject();
  92. data.PutUtfString("m", mapName);
  93. data.PutSFSArray("a", arr);
  94. Message callBackMsg = new Message(Command.SearchRoom, data);
  95. callBack(callBackMsg.GetMessagePackage());
  96. }
  97. private string RandomNPCNick()
  98. {
  99. int index = UnityEngine.Random.Range (0, 20);
  100. return Language.GetStr ("NPCNick", "nick"+index);
  101. }
  102. private void DealBattleMessage(Message msg)
  103. {
  104. msg.data.PutUtfString (Message.LABEL_SENDER, Session.myUserId.ToString());
  105. msg.data.PutInt (Message.LABEL_TIMESTAMP, 0);
  106. callBack(msg.GetMessagePackage());
  107. }
  108. private void ResponseMesssage(string cmd, ISFSObject data)
  109. {
  110. Message callBackMsg = new Message(cmd, data);
  111. callBack(callBackMsg.GetMessagePackage());
  112. }
  113. public void Disconnect()
  114. {
  115. }
  116. }