123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using Sfs2X.Entities.Data;
- public class BattleStartHandler : BAHandler
- {
- public BattleStartHandler(BARoom room):base(room){}
- public override void HandleRequest (BAServer server, ISFSObject msg, int sender, int timeStamp)
- {
- room.isGameStart = true;
- if (!room.IsHost ())
- return;
- int blueCount = 0;
- int redCount = 0;
- List<BAUser> list = room.GetUserList ();
- for(int i=0; i<list.Count; i++)
- {
- BAUser user = list [i];
- if(user.teamId == TeamUtil.Team.Blue.GetHashCode())
- {
- blueCount++;
- }
- else
- {
- redCount++;
- }
- }
- int aiCount = room.maxUsers - (blueCount + redCount);
- if(aiCount == 0)
- {
- return;
- }
- List<int> playerIdList = room.GetAvailabledPlayerIds ();
- int playerIdIndex = 0;
- int overflowIndex = 0;
- string[] nickArr = GetAINick (aiCount);
- ISFSArray arr = new SFSArray ();
- int halfMaxUsers = room.maxUsers / 2;
- for(int i=blueCount; i<halfMaxUsers; i++)
- {
- int playerId = 0;
- if (playerIdIndex >= playerIdList.Count) {
- playerId = room.maxUsers + overflowIndex;
- overflowIndex++;
- } else {
- playerId = playerIdList [playerIdIndex];
- playerIdIndex++;
- }
- ISFSObject obj = new SFSObject ();
- obj.PutUtfString (UserSFSObjectLabel.ID, "-"+playerId.ToString());
- obj.PutUtfString (UserSFSObjectLabel.NICK, nickArr[arr.Size()]);
- obj.PutInt (UserSFSObjectLabel.TEAM_ID, TeamUtil.Team.Blue.GetHashCode());
- obj.PutInt (UserSFSObjectLabel.PLAYER_ID, playerId);
- arr.AddSFSObject (obj);
- }
- for(int i=redCount; i<halfMaxUsers; i++)
- {
- int playerId = 0;
- if (playerIdIndex >= playerIdList.Count) {
- playerId = room.maxUsers + overflowIndex;
- overflowIndex++;
- } else {
- playerId = playerIdList [playerIdIndex];
- playerIdIndex++;
- }
- ISFSObject obj = new SFSObject ();
- obj.PutUtfString (UserSFSObjectLabel.ID, "-"+playerId.ToString());
- obj.PutUtfString (UserSFSObjectLabel.NICK, nickArr[arr.Size()]);
- obj.PutInt (UserSFSObjectLabel.TEAM_ID, TeamUtil.Team.Red.GetHashCode());
- obj.PutInt (UserSFSObjectLabel.PLAYER_ID, playerId);
- arr.AddSFSObject (obj);
- }
- ISFSObject data = new SFSObject ();
- data.PutSFSArray ("a", arr);
- Message pjMsg = new Message(Command.PlayerJoin, data);
- server.Send(pjMsg);
- }
- private string[] GetAINick(int count)
- {
- 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 };
- while(list.Count > count)
- {
- int index = Random.Range (0, list.Count);
- list.RemoveAt (index);
- }
- string[] arr = new string[count];
- for(int i=0; i<arr.Length; i++)
- {
- arr [i] = Language.GetStr ("NPCNick", "nick" + list [i]);
- }
- return arr;
- }
- }
|