123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using Sfs2X.Entities.Data;
- public class FlagRoom : BARoom
- {
- public const int MAX_FLAG_CAPTURE = 3;
- private int lastResetFlagTime = -1;
- private bool isFirstTimeFlagSet = false;
- public FlagRoom(BAServer server) : base(server)
- {
- // AttempAddStation (0, TeamUtil.Team.Blue.GetHashCode ());
- // AttempAddStation (1, TeamUtil.Team.Red.GetHashCode());
- }
- private void ResetFlags()
- {
- flagDict.Add (TeamUtil.Team.Blue.GetHashCode(), new BAFlag(TeamUtil.Team.Blue.GetHashCode(), 0, 0));
- flagDict.Add (TeamUtil.Team.Red.GetHashCode(), new BAFlag(TeamUtil.Team.Red.GetHashCode(), 0, 0));
- }
- private void SendFlagAdded()
- {
- ISFSObject data = new SFSObject ();
- data.PutSFSArray ("l", GetFlagDataArray());
- server.Send (new Message(Command.AddFlag, data));
- }
- private ISFSArray GetFlagDataArray()
- {
- SFSArray arr = new SFSArray();
- foreach(KeyValuePair<int, BAFlag> kvp in flagDict)
- {
- BAFlag flag = kvp.Value;
- ISFSObject obj = new SFSObject ();
- obj.PutInt ("t", flag.teamId);
- obj.PutInt ("x", flag.x);
- obj.PutInt ("y", flag.y);
- obj.PutInt ("c", flag.craftId);
- obj.PutBool ("b", flag.inBase);
- arr.AddSFSObject (obj);
- }
- return arr;
- }
- public override bool AttempGetFlag (int userId, int craftId)
- {
- BAUser user = GetUser (userId);
- if(user != null)
- {
- int teamId = TeamUtil.GetOpponentTeam (user.teamId).GetHashCode();
- BAFlag flag = null;
- flagDict.TryGetValue (teamId, out flag);
- if(flag != null && flag.craftId == -1)
- {
- flag.craftId = craftId;
- flag.x = flag.y = 0;
- flag.inBase = false;
- return true;
- }
- }
- return false;
- }
- public override bool AttempPutFlag (int userId, int craftId, int flagTeamId, int x, int y)
- {
- BAUser user = GetUser (userId);
- BAFlag flag = null;
- flagDict.TryGetValue (flagTeamId, out flag);
- if(flag == null)
- {
- return false;
- }
- if (flagTeamId == user.teamId) {
- flag.craftId = -1;
- flag.x = flag.y = 0;
- flag.inBase = true;
- return true;
- }
- else if(flagTeamId != user.teamId && craftId == flag.craftId)
- {
- flag.craftId = -1;
- flag.x = x;
- flag.y = y;
- flag.inBase = false;
- return true;
- }
- return false;
- }
- public override bool AttempCaptureFlag (int userId, int craftId)
- {
- BAUser user = GetUser (userId);
- if (user == null)
- return false;
- BAFlag myFlag = null;
- BAFlag oppFlag = null;
- flagDict.TryGetValue (user.teamId, out myFlag);
- flagDict.TryGetValue (TeamUtil.GetOpponentTeam(user.teamId).GetHashCode(), out oppFlag);
- if(myFlag == null || oppFlag == null)
- return false;
- if(myFlag.craftId == -1 && myFlag.inBase && oppFlag.craftId == craftId)
- {
- flagDict.Clear ();
- if(user.teamId == TeamUtil.Team.Blue.GetHashCode())
- {
- blueScore++;
- if(blueScore >= MAX_FLAG_CAPTURE)
- {
- SendBattleInfo(GetScoreData(TeamUtil.Team.Blue.GetHashCode()));
- }
- }
- else
- {
- redScore++;
- if(redScore >= MAX_FLAG_CAPTURE)
- {
- SendBattleInfo (GetScoreData(TeamUtil.Team.Red.GetHashCode()));
- }
- }
- lastResetFlagTime = GetGameTime () + scoreTaskInterval;
- return true;
- }
- return false;
- }
- public SFSObject GetScoreData(int winTeam)
- {
- if(isGameOver)
- return null;
- int freeTime = GetFreeTime();
- int time = roundTime-GetGameTime();
- if(time<=0)
- {
- if(blueScore>redScore)
- {
- winTeam = TeamUtil.Team.Blue.GetHashCode();
- }
- else if(blueScore<redScore)
- {
- winTeam = TeamUtil.Team.Red.GetHashCode();
- }
- else
- {
- winTeam = TeamUtil.Team.Yellow.GetHashCode();
- }
- }
- SFSObject data = new SFSObject();
- data.PutInt("t", time);
- data.PutInt("f", freeTime);
- data.PutInt("b", blueScore);
- data.PutInt("r", redScore);
- if(!isFirstTimeFlagSet)
- {
- if(GetFreeTime() == 0)
- {
- isFirstTimeFlagSet = true;
- ResetFlags();
- SendFlagAdded();
- }
- }
- else if(lastResetFlagTime != -1 && GetGameTime() > lastResetFlagTime)
- {
- lastResetFlagTime = -1;
- ResetFlags();
- SendFlagAdded();
- }
- else if(flagDict.Count > 0)
- {
- ISFSArray l = new SFSArray();
- PutFlagData(flagDict[TeamUtil.Team.Blue.GetHashCode()], l);
- PutFlagData(flagDict[TeamUtil.Team.Red.GetHashCode()], l);
- data.PutSFSArray("fl", l);
- }
- if(winTeam > 0)
- {
- DealGameOver(data, winTeam);
- }
- return data;
- }
- private void PutFlagData(BAFlag flag, ISFSArray arr)
- {
- if(flag != null)
- {
- ISFSObject 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);
- }
- }
- public override void Update ()
- {
- base.Update ();
- if(scoreTaskRunning)
- {
- if (GetTime () - lastSendScoreTime >= scoreTaskInterval) {
- lastSendScoreTime = GetTime ();
- SendBattleInfo (GetScoreData (-1));
- }
- }
- }
- }
|