123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using Sfs2X.Entities.Data;
- public class BARoom
- {
- public const int MAP_USER_ID = -1000;
- // public const int STATION_ID_START = 100;
- // public const int DOOR_ID_START = 200;
- // public const int FLAG_ID_START = 300;
- public const int STATION_ID_START = 100;
- public const int DOOR_ID_START = 200;
- public const int FLAG_ID_START = 300;
- protected int m_MaxUsers = 6;
- public int roundTime = 5*60*1000;
- protected int freeTime = 1*30*1000;
- protected float startTime = -1f;
- public int aiTaker = -1;
- public int blueScore;
- public int redScore;
- protected int scoreTaskInterval = 5000;
- protected int lastSendScoreTime = 0;
- protected bool scoreTaskRunning = true;
- public bool isGameStart = false;
- public bool isGameOver = false;
- public bool isRecord = true;
- public MessageManager.MessageCallBackDelegate handlerCallBack;
- protected BAServer server;
- protected Dictionary<string, BAHandler> handlerDict;
- protected List<BAUser> userList;
- protected Dictionary<int, BAUser> userDict;
- public Dictionary<int, BAStation> stationDict; //key is crystalBase id
- public Dictionary<int, BAFlag> flagDict;
- public Dictionary<int, BADoor> doorDict;
- public Dictionary<int, BAItemPos> itemPosDict;
- public BARoom(BAServer server)
- {
- this.server = server;
- userList = new List<BAUser>();
- userDict = new Dictionary<int, BAUser>();
- stationDict = new Dictionary<int, BAStation> ();
- itemPosDict = new Dictionary<int, BAItemPos> ();
- doorDict = new Dictionary<int, BADoor> ();
- flagDict = new Dictionary<int, BAFlag> ();
- handlerDict = new Dictionary<string, BAHandler>();
- InitHandler ();
- startTime = GameTime.realtimeSinceStartup;
- }
- protected virtual void InitHandler()
- {
- handlerDict.Add (Command.Ready, new ReadyHandler(this));
- handlerDict.Add (Command.PlayerJoin, new PlayerJoinHandler(this));
- handlerDict.Add (Command.PlayerLeft, new PlayerLeftHandler(this));
- handlerDict.Add (Command.BattleStart, new BattleStartHandler(this));
- handlerDict.Add (Command.BattleInfo, new BattleInfoHandler(this));
- handlerDict.Add (Command.Dead, new DeadHandler (this));
- handlerDict.Add (Command.Station, new StationHandler(this));
- handlerDict.Add (Command.AddMapItem, new AddItemHandler(this));
- handlerDict.Add (Command.GetMapItem, new GetItemHandler(this));
- handlerDict.Add (Command.AddFlag, new AddFlagHandler(this));
- handlerDict.Add (Command.GetFlag, new GetFlagHandler(this));
- handlerDict.Add (Command.PutFlag, new PutFlagHandler(this));
- handlerDict.Add (Command.CaptureFlag, new CaptureFlagHandler(this));
- handlerDict.Add (Command.SimpleSync, new SyncHandler(this));
- handlerDict.Add (Command.AITaker, new AITakerHandler(this));
- handlerDict.Add (Command.AITakerRequest, new AITakeRequestrHandler(this));
- }
- public void AddUser(ISFSArray arr)
- {
- for(int i=0; i<arr.Size(); i++)
- {
- ISFSObject obj = arr.GetSFSObject (i);
- int id = StringUtil.ToInt(obj.GetUtfString (UserSFSObjectLabel.ID));
- string nick = obj.GetUtfString (UserSFSObjectLabel.NICK);
- int teamId = obj.GetInt (UserSFSObjectLabel.TEAM_ID);
- int playerId = obj.GetInt (UserSFSObjectLabel.PLAYER_ID);
- RemoveAIUserByPlayerId (playerId);
- BAUser user = GetUser (id);
- if (user == null) {
- user = new BAUser (id, nick);
- userList.Add(user);
- userDict.Add(id, user);
- } else {
- user.lost = false;
- }
- user.teamId = teamId;
- user.playerId = playerId;
- }
- }
- public void UserLeft(int id)
- {
- BAUser user = GetUser(id);
- if (user != null)
- user.lost = true;
- }
- public void RemoveUser(int id)
- {
- BAUser user = null;
- userDict.TryGetValue (id, out user);
- userDict.Remove (id);
- userList.Remove (user);
- }
- public void RemoveAIUserByPlayerId(int playerId)
- {
- for(int i=0; i<userList.Count; i++)
- {
- BAUser user = userList [i];
- if(user.playerId == playerId && user.id < 0)
- {
- RemoveUser (user.id);
- return;
- }
- }
- }
- public BAUser GetUser(int id)
- {
- BAUser user = null;
- userDict.TryGetValue (id, out user);
- return user;
- }
- public List<BAUser> GetUserList()
- {
- return userList;
- }
- public bool IsMyUserId(int userId)
- {
- return userId == Session.myUserId;
- }
- public bool IsHost()
- {
- return Session.myUserId == aiTaker;
- }
- public int maxUsers
- {
- get{
- return m_MaxUsers;
- }
- }
- public int GetTime()
- {
- return Mathf.FloorToInt((GameTime.realtimeSinceStartup-startTime)*1000f);
- }
- public int GetFreeTime()
- {
- int time = GetTime ();
- if(time <= freeTime)
- {
- return freeTime - time;
- }
- return 0;
- }
- public int GetGameTime()
- {
- int time = GetTime ();
- if(time <= freeTime)
- {
- return 0;
- }
- return time - freeTime;
- }
- public virtual ISFSArray GetRoomBuildingData()
- {
- ISFSArray arr = new SFSArray ();
- foreach(KeyValuePair<int, BAStation> kvp in stationDict)
- {
- BAStation station = kvp.Value;
- ISFSObject obj = new SFSObject ();
- obj.PutInt ("i", station.id);
- obj.PutInt ("t", station.teamId);
- obj.PutInt ("c", station.crystalId);
- obj.PutInt ("s", MapObjectUtil.TypeId.Station.GetHashCode());
- arr.AddSFSObject (obj);
- }
- foreach(KeyValuePair<int, BADoor> kvp in doorDict)
- {
- BADoor door = kvp.Value;
- ISFSObject obj = new SFSObject ();
- obj.PutInt ("u", MAP_USER_ID);
- obj.PutInt ("i", door.id);
- obj.PutInt ("t", door.teamId);
- obj.PutInt ("c", door.index);
- obj.PutInt ("s", MapObjectUtil.TypeId.Door.GetHashCode());
- arr.AddSFSObject (obj);
- }
- foreach(KeyValuePair<int, BAItemPos> kvp in itemPosDict)
- {
- BAItemPos itemPos = kvp.Value;
- ISFSObject obj = new SFSObject ();
- obj.PutInt ("i", itemPos.id);
- obj.PutInt ("t", itemPos.itemId);
- obj.PutInt ("s", MapObjectUtil.TypeId.MapItemPos.GetHashCode());
- arr.AddSFSObject (obj);
- }
- return arr;
- }
- public virtual ISFSArray GetRoomUserData()
- {
- ISFSArray arr = new SFSArray ();
- for(int i=0; i<userList.Count; i++)
- {
- BAUser user = userList [i];
- ISFSObject obj = new SFSObject ();
- obj.PutUtfString (UserSFSObjectLabel.ID, user.id.ToString());
- obj.PutUtfString (UserSFSObjectLabel.NICK, user.nick);
- obj.PutInt (UserSFSObjectLabel.TEAM_ID, user.teamId);
- obj.PutInt (UserSFSObjectLabel.PLAYER_ID, user.playerId);
- arr.AddSFSObject (obj);
- }
- return arr;
- }
- public int GetAvailabledPlayerId()
- {
- int aiId = -1;
- for(int i=0; i<maxUsers; i++)
- {
- bool found = false;
- for(int j=0; j<userList.Count; j++)
- {
- BAUser user = userList [j];
- if(user.id < 0 && aiId < 0)
- {
- aiId = user.playerId;
- }
- if(user.playerId == j)
- {
- found = true;
- break;
- }
- }
- if (found)
- continue;
-
- return i;
- }
- return aiId;
- }
- public List<int> GetAvailabledPlayerIds()
- {
- List<int> list = new List<int> ();
- for(int i=0; i<maxUsers; i++)
- {
- bool found = false;
- for(int j=0; j<userList.Count; j++)
- {
- BAUser user = userList [j];
- if(user.playerId == i)
- {
- found = true;
- break;
- }
- }
- if (found)
- continue;
- list.Add(i);
- }
- return list;
- }
- public BAStation AttempAddStation(int crystalIndex, int teamId)
- {
- if(stationDict.ContainsKey(crystalIndex))
- {
- return null;
- }
- BAStation station = new BAStation(STATION_ID_START+crystalIndex, crystalIndex, teamId);
- stationDict.Add(crystalIndex, station);
- return station;
- }
- public virtual void RemoveStation(int id)
- {
-
- }
- public void AddDoor(int index, int teamId)
- {
- if(doorDict.ContainsKey(index))
- {
- return;
- }
- BADoor door = new BADoor(DOOR_ID_START+index, index, teamId);
- doorDict.Add(index, door);
- }
- public void RemoveDoor(int doorId)
- {
- foreach(KeyValuePair<int, BADoor> kvp in doorDict)
- {
- BADoor door = kvp.Value;
- if(door.id == doorId)
- {
- doorDict.Remove(kvp.Key);
- return;
- }
- }
- }
- public int AttempGetItem(int posId)
- {
- if(itemPosDict.ContainsKey(posId))
- {
- BAItemPos itemPos = itemPosDict[posId];
- int itemId = itemPos.itemId;
- itemPos.itemId = -1;
- return itemId;
- }
- return -1;
- }
- public virtual bool AttempGetFlag(int userId, int craftId)
- {
- return false;
- }
- public virtual bool AttempPutFlag(int userId, int craftId, int flagTeamId, int x, int y)
- {
- return false;
- }
- public virtual bool AttempCaptureFlag(int userId, int craftId)
- {
- return false;
- }
- protected void SendBattleInfo(ISFSObject scoreData)
- {
- ISFSObject data = new SFSObject();
- if(scoreData != null)
- data.PutSFSObject("s", scoreData);
- Message msg = new Message (Command.BattleInfo, data);
- server.Send(msg);
- }
- protected virtual void StopScoreTask()
- {
- scoreTaskRunning = false;
- }
- protected void DealGameOver(SFSObject data, int winTeam)
- {
- StopScoreTask();
- isGameOver = true;
- data.PutInt("w", winTeam);
- }
- protected virtual void CheckGameStart()
- {
- if(!isGameStart && IsHost())
- {
- if(GetFreeTime() < 10*1000)
- {
- isGameStart = true;
- Message msg = new Message (Command.BattleStart, new SFSObject ());
- server.Send (msg);
- }
- }
- }
- public virtual void Update(){
- CheckGameStart ();
- CheckAITaker ();
- }
- public void HandleMassage(ISFSObject msg, int sender, int timeStamp)
- {
- string cmd = msg.GetUtfString(Message.LABEL_COMMAND);
- BAHandler handler = null;
- handlerDict.TryGetValue(cmd, out handler);
- if (handler != null) {
- handler.HandleRequest (server, msg, sender, timeStamp);
- } else {
- server.RoomHandleResponse (msg);
- // Debuger.LogError ("No such handler["+cmd+"]");
- }
- }
- private float aiTakerMsgWaitTime;
- public void AITakerMessageGot()
- {
- aiTakerMsgWaitTime = GameTime.time;
- aiTakerRequestId = 0;
- timeStamp = 0;
- requestTime = 0;
- }
- private bool IsAITakerTimeOut()
- {
- return GameTime.time - aiTakerMsgWaitTime >= 5f;
- }
- private int aiTakerRequestId;
- private int timeStamp;
- private float requestTime;
- public void AITakerRequest(int id, int timeStamp)
- {
- if (aiTakerRequestId == 0 || timeStamp < this.timeStamp || (requestTime > 0 && GameTime.time - requestTime > 5f))
- {
- aiTakerRequestId = id;
- this.timeStamp = timeStamp;
- requestTime = GameTime.time;
- }
- }
- public bool AITakerRequestHoldComplete()
- {
- if(IsMyUserId(aiTakerRequestId))
- return (GameTime.time - requestTime) > 2f;
- return false;
- }
- protected virtual void CheckAITaker()
- {
- if (isGameOver)
- return;
- if (IsAITakerTimeOut ()) {
- ISFSObject data = new SFSObject ();
- Message msg = new Message (Command.AITakerRequest, data);
- server.Send (msg);
- aiTakerMsgWaitTime = GameTime.time;
- }
- if(AITakerRequestHoldComplete())
- {
- ISFSObject data = new SFSObject ();
- Message msg = new Message (Command.AITaker, data);
- server.Send (msg);
- AITakerMessageGot ();
- }
- }
- public virtual void Dispose()
- {
-
- }
- }
|