using UnityEngine; using System.Collections; using System.Collections.Generic; using Sfs2X; using Sfs2X.Entities.Data; public class GameRoomService { public delegate void ResponseDelegate(string cmd, ISFSObject data); public int userId = 1; public int maxUsers = 6; public MapData.Mode mode; public int roundTime = 5*60*1000; public int freeTime = 1*15*1000; protected ServicePlayer myPlayer; public Dictionary aiPlayerDict; public List aiPlayerList; public Dictionary stationDict; //key is crystalBase id public Dictionary flagDict; public Dictionary doorDict; public Dictionary itemPosDict; protected int craftIdCount = 1; protected int aiPlayerIdCount = -2; public ServicePlayer mapPlayer; public int blueScore; public int redScore; protected int startTime; public bool isGameOver = false; protected float taskInterval = 5f; protected float lastTaskTime = 0; protected bool isTaskRunning = true; protected Dictionary handlerDict; private ResponseDelegate responseCallBack; public GameRoomService (ResponseDelegate responseCallBack) { this.responseCallBack = responseCallBack; aiPlayerDict = new Dictionary(); aiPlayerList = new List(); stationDict = new Dictionary(); flagDict = new Dictionary(); doorDict = new Dictionary(); itemPosDict = new Dictionary(); mapPlayer = new ServicePlayer(-1, ""); blueScore = 0; redScore = 0; startTime = (int)(GameTime.time*1000f); lastTaskTime = GameTime.time; //add request handlers handlerDict = new Dictionary(); // handlerDict.Add(Command.PlayerJoin, new PlayerJoinHandler()); // handlerDict.Add(Command.Ready, new ReadyHandler()); // handlerDict.Add(Command.CraftSelection, new CraftSelectionHandler()); handlerDict.Add(Command.Attack, new AttackHandler()); handlerDict.Add(Command.Radio, new RadioHandler()); handlerDict.Add(Command.Station, new OccupyStationHandler()); handlerDict.Add(Command.GetMapItem, new GetMapItemHandler()); // handlerDict.Add(Command.GetFlag, new GetFlagHandler()); // handlerDict.Add(Command.PutFlag, new PutFlagHandler()); // handlerDict.Add(Command.CaptureFlag, new CaptureFlagHandler()); handlerDict.Add(Command.SimpleSync, new SimpleSyncHandler()); InitGame(); } public ServicePlayer GetMyPlayer() { return myPlayer; } protected virtual void InitGame() { } public SFSObject GetMapPlayerObj() { SFSObject obj = new SFSObject(); obj.PutInt("u", mapPlayer.userId); obj.PutUtfString("n", mapPlayer.nickName); obj.PutInt("t", mapPlayer.teamId); obj.PutInt("a", mapPlayer.aiTakerId); return obj; } public ServiceStation AttempAddStation(int crystalIndex, int teamId) { if(stationDict.ContainsKey(crystalIndex)) { return null; } ServiceStation station = new ServiceStation(CreateCraftId(), crystalIndex, teamId); stationDict.Add(crystalIndex, station); return station; } public virtual void RemoveStation(int stationId) { } public int AttempGetItem(int id) { if(itemPosDict.ContainsKey(id)) { ServiceItemPos itemPos = itemPosDict[id]; int itemId = itemPos.itemId; itemPos.itemId = -1; return itemId; } return -1; } public virtual bool AttempGetFlag(int userId) { return false; } public virtual bool AttempPutFlag(int userId, int teamId, int x, int y) { return false; } public virtual bool AttempCaptureFlag(int userId) { return false; } public void AddDoor(int index, int teamId) { if(doorDict.ContainsKey(index)) { return; } ServiceDoor door = new ServiceDoor(CreateCraftId(), index, teamId); doorDict.Add(index, door); } public virtual void RemoveDoor(int doorId) { foreach(KeyValuePair kvp in doorDict) { ServiceDoor door = kvp.Value; if(door.id == doorId) { doorDict.Remove(door.index); return; } } } public SFSArray GetFlagDataArray() { SFSArray arr = new SFSArray(); foreach(KeyValuePair kvp in flagDict) { ServiceFlag flag = kvp.Value; SFSObject obj = new SFSObject(); obj.PutInt("t", flag.teamId); obj.PutInt("x", flag.x); obj.PutInt("y", flag.y); obj.PutInt("c", flag.craftId); arr.AddSFSObject(obj); } return arr; } public int GetTime() { return (int)(GameTime.time*1000) - startTime; } public int GetGameTime() { int time = GetTime(); if(time <= freeTime) { return 0; } return time-freeTime; } public int GetFreeTime() { int time = GetTime(); if(time <= freeTime) { return freeTime-time; } return 0; } public ServicePlayer NewPlayerJoin(string nick, int userId) { mapPlayer.aiTakerId = userId; ServicePlayer player = new ServicePlayer(userId, nick); player.teamId = TeamUtil.Team.Blue.GetHashCode(); myPlayer = player; return player; } public ServicePlayer NewAIPlayer(int teamId) { int userId = CreateAIPlayerId(); ServicePlayer player = new ServicePlayer(userId, "AI"+userId); player.teamId = teamId; player.joinTime = GetGameTime(); aiPlayerDict.Add(userId, player); aiPlayerList.Add(player); return player; } public ServicePlayer GetPlayer(int userId) { if(myPlayer.userId == userId) return myPlayer; if(aiPlayerDict.ContainsKey(userId)) return aiPlayerDict[userId]; return null; } public int CreateCraftId() { return craftIdCount++; } public int CreateAIPlayerId() { return aiPlayerIdCount--; } protected void SendBattleInfo(SFSObject scoreData, SFSObject playerData, SFSObject aiData) { SFSObject data = new SFSObject(); if(scoreData != null) data.PutSFSObject("s", scoreData); if(aiData != null) data.PutSFSObject("a", aiData); if(playerData != null) data.PutSFSObject("p", playerData); Send(Command.BattleInfo.ToString(), data); } protected SFSObject GetAiTakerChangeData() { SFSArray arr = new SFSArray(); for(int i=0; i 0) { res.PutSFSArray("a", arr); return res; } return null; } protected SFSObject GetPlayerChangeData() { if(aiPlayerList.Count >= maxUsers - 1) return null; SFSObject res = new SFSObject(); if(GetFreeTime() > 5) return null; int blueCount = 1; int redCount = 0; for(int i=0; i 0) { res.PutSFSArray("a", addArr); return res; } return null; } protected void DealGameOver(SFSObject data, int winTeam) { StopTask(); isGameOver = true; data.PutInt("w", winTeam); SavePlayerScore(data, winTeam); } protected virtual void SavePlayerScore(SFSObject data, int winTeam) { } public void DealMessage(Message msg) { if(handlerDict.ContainsKey(msg.cmd)) handlerDict[msg.cmd].HandleClientRequest(this, msg.data); else Debuger.LogError("can not find handler with command["+msg.cmd+"]"); } public void Send(string cmd, ISFSObject data) { responseCallBack(cmd, data); } protected virtual void DoTask() { } public void StopTask() { isTaskRunning = false; } public void run() { if(isTaskRunning) { float currentTime = GameTime.time; if(currentTime-lastTaskTime >= taskInterval) { lastTaskTime = currentTime; DoTask(); } } } }