123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- 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<int, ServicePlayer> aiPlayerDict;
- public List<ServicePlayer> aiPlayerList;
- public Dictionary<int, ServiceStation> stationDict; //key is crystalBase id
- public Dictionary<int, ServiceFlag> flagDict;
- public Dictionary<int, ServiceDoor> doorDict;
- public Dictionary<int, ServiceItemPos> 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<string, LocalRequestHandler> handlerDict;
- private ResponseDelegate responseCallBack;
- public GameRoomService (ResponseDelegate responseCallBack)
- {
- this.responseCallBack = responseCallBack;
- aiPlayerDict = new Dictionary<int, ServicePlayer>();
- aiPlayerList = new List<ServicePlayer>();
- stationDict = new Dictionary<int, ServiceStation>();
- flagDict = new Dictionary<int, ServiceFlag>();
- doorDict = new Dictionary<int, ServiceDoor>();
- itemPosDict = new Dictionary<int, ServiceItemPos>();
- mapPlayer = new ServicePlayer(-1, "");
- blueScore = 0;
- redScore = 0;
- startTime = (int)(GameTime.time*1000f);
- lastTaskTime = GameTime.time;
- //add request handlers
- handlerDict = new Dictionary<string, LocalRequestHandler>();
- // 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<int, ServiceDoor> 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<int, ServiceFlag> 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<aiPlayerList.Count; i++)
- {
- ServicePlayer player = aiPlayerList[i];
- if(player.aiTakerId != myPlayer.userId)
- {
- player.aiTakerId = myPlayer.userId;
- SFSObject obj = new SFSObject();
- obj.PutInt("u", player.userId);
- obj.PutInt("t", player.aiTakerId);
- arr.AddSFSObject(obj);
- }
- }
- SFSObject res = new SFSObject();
- if(arr.Size() > 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<aiPlayerList.Count; i++)
- {
- ServicePlayer player = aiPlayerList[i];
- if(player.teamId == TeamUtil.Team.Blue.GetHashCode())
- blueCount++;
- else if(player.teamId == TeamUtil.Team.Red.GetHashCode())
- redCount++;
- }
- int maxTeamUserCount = maxUsers/2;
- SFSArray addArr = new SFSArray();
- while(blueCount < maxTeamUserCount)
- {
- SFSObject data = new SFSObject();
- ServicePlayer player = NewAIPlayer(TeamUtil.Team.Blue.GetHashCode());
- data.PutInt("u", player.userId);
- data.PutInt("t", player.teamId);
- data.PutUtfString("n", player.nickName);
- addArr.AddSFSObject(data);
- blueCount++;
- }
- while(redCount < maxTeamUserCount)
- {
- SFSObject data = new SFSObject();
- ServicePlayer player = NewAIPlayer(TeamUtil.Team.Red.GetHashCode());
- data.PutInt("u", player.userId);
- data.PutInt("t", player.teamId);
- data.PutUtfString("n", player.nickName);
- addArr.AddSFSObject(data);
- redCount++;
- }
- if(addArr.Size() > 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();
- }
- }
- }
- }
|