123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using Sfs2X.Entities.Data;
- public class DefenseService : GameRoomService
- {
- public const int SEND_ITEM_INTERVAL = 20*1000;
- private int lastAddItemTime = 0;
- public DefenseService (ResponseDelegate callBack):base(callBack)
- {
- }
-
- override protected void InitGame()
- {
- base.InitGame();
- lastAddItemTime = GetTime() - SEND_ITEM_INTERVAL;
-
- AttempAddStation(0, TeamUtil.Team.Blue.GetHashCode());
- AttempAddStation(1, TeamUtil.Team.Red.GetHashCode());
- AddDoor(0, TeamUtil.Team.Blue.GetHashCode());
- AddDoor(1, TeamUtil.Team.Blue.GetHashCode());
- AddDoor(2, TeamUtil.Team.Red.GetHashCode());
- AddDoor(3, TeamUtil.Team.Red.GetHashCode());
- }
- public override void RemoveStation (int stationId)
- {
- foreach(KeyValuePair<int, ServiceStation> kvp in stationDict)
- {
- ServiceStation station = kvp.Value;
- if(station.id == stationId)
- {
- stationDict.Remove(kvp.Key);
- KeyStationDestroyed(station.teamId);
- return;
- }
- }
- }
- private void SendItem()
- {
- SFSArray items = new SFSArray();
- foreach(KeyValuePair<int, ServiceItemPos> kvp in itemPosDict)
- {
- ServiceItemPos itemPos = kvp.Value;
- if(itemPos.itemId == -1)
- {
- itemPos.itemId = MapItem.BIGGER;
- SFSObject itemObj = new SFSObject();
- itemObj.PutInt("i", itemPos.id);
- itemObj.PutInt("t", itemPos.itemId);
- items.AddSFSObject(itemObj);
- }
- }
- if(items.Size() > 0)
- {
- SFSObject data = new SFSObject();
- data.PutSFSArray("l", items);
- Send(Command.AddMapItem.ToString(), data);
- }
- }
- private void KeyStationDestroyed(int teamId)
- {
- TeamUtil.Team team = TeamUtil.GetTeam(teamId);
- TeamUtil.Team winTeam = TeamUtil.GetOpponentTeam(team);
- SendBattleInfo(GetScoreData(winTeam.GetHashCode()), GetPlayerChangeData(), GetAiTakerChangeData());
- }
- public SFSObject GetScoreData(int winTeam)
- {
- if(isGameOver)
- {
- return null;
- }
- SFSObject data = new SFSObject();
- int freeTime = GetFreeTime();
- int time = roundTime - GetGameTime();
- if(time <= 0)
- {
- winTeam = FindWinTeam();
- }
- data.PutInt("t", time);
- data.PutInt("f", freeTime);
- if(winTeam > 0)
- {
- DealGameOver(data, winTeam);
- }
- return data;
- }
- private int FindWinTeam()
- {
- float blueDoorCount = 0;
- float redDoorCount = 0;
- foreach(KeyValuePair<int, ServiceDoor> kvp in doorDict)
- {
- if(kvp.Value.teamId == TeamUtil.Team.Blue.GetHashCode())
- {
- blueDoorCount++;
- }
- else if(kvp.Value.teamId == TeamUtil.Team.Red.GetHashCode())
- {
- redDoorCount++;
- }
- }
-
- if(blueDoorCount > redDoorCount)
- return TeamUtil.Team.Blue.GetHashCode();
- else if(blueDoorCount < redDoorCount)
- return TeamUtil.Team.Red.GetHashCode();
- return TeamUtil.Team.Blue.GetHashCode();
- }
- protected override void DoTask ()
- {
- SendBattleInfo(GetScoreData(-1), GetPlayerChangeData(), GetAiTakerChangeData());
- if(GetFreeTime() == 0)
- {
- if(GetTime() - lastAddItemTime >= SEND_ITEM_INTERVAL)
- {
- lastAddItemTime += SEND_ITEM_INTERVAL;
- SendItem();
- }
- }
- }
-
-
- override protected void SavePlayerScore(SFSObject data, int winTeam)
- {
- SFSArray stats = new SFSArray();
- List<ServicePlayer> list = new List<ServicePlayer>();
- list.Add(myPlayer);
-
- for(int i=0; i<list.Count; i++)
- {
- ServicePlayer player = list[i];
-
- SFSObject statsInfo = new SFSObject();
- statsInfo.PutInt("u", player.userId);
- statsInfo.PutInt("k", player.kill);
- statsInfo.PutInt("a", player.assists);
- statsInfo.PutInt("d", player.death);
-
- int rankScore = player.kill * 10 + player.assists * 2;
- int expScore = rankScore;
- int win = 0;
- int lose = 0;
- int coinGet = 0;
-
- if(player.teamId == winTeam)
- win = 1;
- else
- lose = 1;
-
- statsInfo.PutInt("r", rankScore);
- statsInfo.PutInt("e", expScore);
- statsInfo.PutInt("c", coinGet);
- stats.AddSFSObject(statsInfo);
- }
-
- data.PutSFSArray("s", stats);
- }
- }
|