BattleStartHandler.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Sfs2X.Entities.Data;
  5. public class BattleStartHandler : BAHandler
  6. {
  7. public BattleStartHandler(BARoom room):base(room){}
  8. public override void HandleRequest (BAServer server, ISFSObject msg, int sender, int timeStamp)
  9. {
  10. room.isGameStart = true;
  11. if (!room.IsHost ())
  12. return;
  13. int blueCount = 0;
  14. int redCount = 0;
  15. List<BAUser> list = room.GetUserList ();
  16. for(int i=0; i<list.Count; i++)
  17. {
  18. BAUser user = list [i];
  19. if(user.teamId == TeamUtil.Team.Blue.GetHashCode())
  20. {
  21. blueCount++;
  22. }
  23. else
  24. {
  25. redCount++;
  26. }
  27. }
  28. int aiCount = room.maxUsers - (blueCount + redCount);
  29. if(aiCount == 0)
  30. {
  31. return;
  32. }
  33. List<int> playerIdList = room.GetAvailabledPlayerIds ();
  34. int playerIdIndex = 0;
  35. int overflowIndex = 0;
  36. string[] nickArr = GetAINick (aiCount);
  37. ISFSArray arr = new SFSArray ();
  38. int halfMaxUsers = room.maxUsers / 2;
  39. for(int i=blueCount; i<halfMaxUsers; i++)
  40. {
  41. int playerId = 0;
  42. if (playerIdIndex >= playerIdList.Count) {
  43. playerId = room.maxUsers + overflowIndex;
  44. overflowIndex++;
  45. } else {
  46. playerId = playerIdList [playerIdIndex];
  47. playerIdIndex++;
  48. }
  49. ISFSObject obj = new SFSObject ();
  50. obj.PutUtfString (UserSFSObjectLabel.ID, "-"+playerId.ToString());
  51. obj.PutUtfString (UserSFSObjectLabel.NICK, nickArr[arr.Size()]);
  52. obj.PutInt (UserSFSObjectLabel.TEAM_ID, TeamUtil.Team.Blue.GetHashCode());
  53. obj.PutInt (UserSFSObjectLabel.PLAYER_ID, playerId);
  54. arr.AddSFSObject (obj);
  55. }
  56. for(int i=redCount; i<halfMaxUsers; i++)
  57. {
  58. int playerId = 0;
  59. if (playerIdIndex >= playerIdList.Count) {
  60. playerId = room.maxUsers + overflowIndex;
  61. overflowIndex++;
  62. } else {
  63. playerId = playerIdList [playerIdIndex];
  64. playerIdIndex++;
  65. }
  66. ISFSObject obj = new SFSObject ();
  67. obj.PutUtfString (UserSFSObjectLabel.ID, "-"+playerId.ToString());
  68. obj.PutUtfString (UserSFSObjectLabel.NICK, nickArr[arr.Size()]);
  69. obj.PutInt (UserSFSObjectLabel.TEAM_ID, TeamUtil.Team.Red.GetHashCode());
  70. obj.PutInt (UserSFSObjectLabel.PLAYER_ID, playerId);
  71. arr.AddSFSObject (obj);
  72. }
  73. ISFSObject data = new SFSObject ();
  74. data.PutSFSArray ("a", arr);
  75. Message pjMsg = new Message(Command.PlayerJoin, data);
  76. server.Send(pjMsg);
  77. }
  78. private string[] GetAINick(int count)
  79. {
  80. List<int> list = new List<int>{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
  81. while(list.Count > count)
  82. {
  83. int index = Random.Range (0, list.Count);
  84. list.RemoveAt (index);
  85. }
  86. string[] arr = new string[count];
  87. for(int i=0; i<arr.Length; i++)
  88. {
  89. arr [i] = Language.GetStr ("NPCNick", "nick" + list [i]);
  90. }
  91. return arr;
  92. }
  93. }