using UnityEngine; using System.Collections; using System.Collections.Generic; using Sfs2X.Entities.Data; public class ChallengeService : GameRoomService { public const int SEND_SCORE_INTERVAL = 5*1000; protected Dictionary crystalBaseMap; public ChallengeService (ResponseDelegate callBack):base(callBack) { } override protected void InitGame() { List list = new List(); list.Add(0); list.Add(1); list.Add(2); int index = Random.Range(0, list.Count); int blueCrystalIndex = list[index]; list.RemoveAt(index); index = Random.Range(0, list.Count); int redCrystalIndex = list[index]; AttempAddStation(blueCrystalIndex, TeamUtil.Team.Blue.GetHashCode()); AttempAddStation(redCrystalIndex, TeamUtil.Team.Red.GetHashCode()); } public override void RemoveStation (int stationId) { int numBlue = 0; int numRed = 0; int needRemoveKey = -1; foreach(KeyValuePair kvp in stationDict) { ServiceStation station = kvp.Value; if(station.id == stationId) { needRemoveKey = station.crystalId; } else if(station.teamId == TeamUtil.Team.Blue.GetHashCode()) { numBlue++; } else if(station.teamId == TeamUtil.Team.Red.GetHashCode()) { numRed++; } } if(needRemoveKey != -1) { stationDict.Remove(needRemoveKey); } if(numBlue == 0) { SendBattleInfo(GetScoreData(TeamUtil.Team.Red.GetHashCode()), null, null); } else if(numRed == 0) { SendBattleInfo(GetScoreData(TeamUtil.Team.Blue.GetHashCode()), null, null); } } public SFSObject GetScoreData(int winTeam) { SFSObject data = new SFSObject(); if(isGameOver) { return data; } int score = 0; if(GetFreeTime() == 0) score = SEND_SCORE_INTERVAL/1000; foreach(KeyValuePair kvp in stationDict) { ServiceStation station = kvp.Value; if(station.teamId == TeamUtil.Team.Blue.GetHashCode()) { blueScore += score; } else if(station.teamId == TeamUtil.Team.Red.GetHashCode()) { redScore += score; } } data.PutInt("b", blueScore); data.PutInt("r", redScore); int time = roundTime - GetGameTime(); if(winTeam > 0) { data.PutInt("t", time); DealGameOver(data, winTeam); } else if(time <= 0) { data.PutInt("t", 0); winTeam = blueScore >= redScore ? TeamUtil.Team.Blue.GetHashCode() : TeamUtil.Team.Red.GetHashCode(); DealGameOver(data, winTeam); } else { data.PutInt("t", time); } int freeTime = GetFreeTime(); data.PutInt("f", freeTime); return data; } override protected void DoTask() { SendBattleInfo(GetScoreData(-1), GetPlayerChangeData(), GetAiTakerChangeData()); } override protected void SavePlayerScore(SFSObject data, int winTeam) { SFSArray stats = new SFSArray(); int roundMin = roundTime/60/1000; if(roundMin <= 0) { roundMin = 1; } List list = new List(); list.Add(myPlayer); for(int i=0; i roundMin) time = roundMin; if(player.teamId == winTeam) { rankScore += 300*time/roundMin; expScore += 300*time/roundMin; coinGet = 100*time/roundMin; win = 1; } else { rankScore -= 300*time/roundMin; expScore += 100*time/roundMin; coinGet = 80*time/roundMin; lose = 1; } statsInfo.PutInt("t", time); statsInfo.PutInt("r", rankScore); statsInfo.PutInt("e", expScore); statsInfo.PutInt("c", coinGet); stats.AddSFSObject(statsInfo); } data.PutSFSArray("s", stats); } }