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 handlerDict; protected List userList; protected Dictionary userDict; public Dictionary stationDict; //key is crystalBase id public Dictionary flagDict; public Dictionary doorDict; public Dictionary itemPosDict; public BARoom(BAServer server) { this.server = server; userList = new List(); userDict = new Dictionary(); stationDict = new Dictionary (); itemPosDict = new Dictionary (); doorDict = new Dictionary (); flagDict = new Dictionary (); handlerDict = new Dictionary(); 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 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 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 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 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 GetAvailabledPlayerIds() { List list = new List (); for(int i=0; i 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() { } }