//-----------------------コンストラクタ----------------------- //-----------------------privateメソッド----------------------- /// <summary> /// 各ターン最初に受信するフィールド情報をGameTableクラスに反映 /// </summary> /// <param name="json">サバから受信したJSONデータ</param> private static GameTable FieldInfoToMyGameTable(string json, int myTeamID, int opTeamID) { //受信したJSONデータを変換用クラスに変換 var gt = new _GameTable(); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json))) { var serializer = new DataContractJsonSerializer(typeof(_GameTable)); gt = (_GameTable)serializer.ReadObject(stream); } //End_Using //return用変数 var result = new GameTable(); //各種変数を更新 result.FieldWidth = gt.Width; result.FieldHeight = gt.Height; result.AgentNum = gt.Teams[0].Agents.Length; result.ElapsedTurn = gt.Turn + 1; //盤面を作成 result.Field = new Tile[gt.Height][]; for (int y = 0; y < gt.Height; ++y) { result.Field[y] = new Tile[gt.Width]; for (int x = 0; x < gt.Width; ++x) { result.Field[y][x] = new Tile(gt.Points[y][x], gt.Tiled[y][x]); } //End_For } //End_For //自チームの状態を作成 result.MyTeam = new Team(); for (int i = 0; i < gt.Teams.Length; ++i) { if (gt.Teams[i].TeamID == myTeamID) { //ポイントを更新 result.MyTeam.TeamID = gt.Teams[i].TeamID; result.MyTeam.TilePoint = gt.Teams[i].TilePoint; result.MyTeam.AreaPoint = gt.Teams[i].AreaPoint; //エージェントを更新 result.MyTeam.Agents = new Agent[gt.Teams[i].Agents.Length]; for (int j = 0; j < gt.Teams[i].Agents.Length; ++j) { result.MyTeam.Agents[j] = new Agent(gt.Teams[i].Agents[j].AgentID, gt.Teams[i].Agents[j].X, gt.Teams[i].Agents[j].Y); } //End_For } //End_If } //End_For //相手チームの状態を作成 result.OpTeam = new Team(); for (int i = 0; i < gt.Teams.Length; ++i) { if (gt.Teams[i].TeamID != myTeamID) { //ポイントを更新 result.OpTeam.TeamID = gt.Teams[i].TeamID; result.OpTeam.TilePoint = gt.Teams[i].TilePoint; result.OpTeam.AreaPoint = gt.Teams[i].AreaPoint; //エージェントを更新 result.OpTeam.Agents = new Agent[gt.Teams[i].Agents.Length]; for (int j = 0; j < gt.Teams[i].Agents.Length; ++j) { result.OpTeam.Agents[j] = new Agent(gt.Teams[i].Agents[j].AgentID, gt.Teams[i].Agents[j].X, gt.Teams[i].Agents[j].Y); } //End_For } //End_If } //End_For //更新完了 return(result); } //End_Method
} //End_Method /// <summary> /// 現在のフィールド情報をプロコン公式のJSON形式に変換 /// </summary> /// <returns></returns> public static string FieldInfoToJson() { //プロコン公式に変換 var ngo = new _GameTable(); ngo.Width = GameMaster.Table.FieldWidth; ngo.Height = GameMaster.Table.FieldHeight; ngo.Points = new int[GameMaster.Table.FieldHeight][]; for (int i = 0; i < GameMaster.Table.FieldHeight; ++i) { ngo.Points[i] = new int[GameMaster.Table.FieldWidth]; for (int j = 0; j < GameMaster.Table.FieldWidth; ++j) { ngo.Points[i][j] = GameMaster.Table.Field[i][j].Point; } //End_For } //End_For ngo.StartedAtUnixTime = GameServer.StartedAtUnixTime; ngo.Turn = GameMaster.Table.ElapsedTurn; ngo.Tiled = new int[GameMaster.Table.FieldHeight][]; for (int i = 0; i < GameMaster.Table.FieldHeight; ++i) { ngo.Tiled[i] = new int[GameMaster.Table.FieldWidth]; for (int j = 0; j < GameMaster.Table.FieldWidth; ++j) { ngo.Tiled[i][j] = GameMaster.Table.Field[i][j].TeamID; } //End_For } //End_For var teams = new List <Team>() { GameMaster.Table.MyTeam, GameMaster.Table.OpTeam }; ngo.Teams = new _Team[2]; for (int i = 0; i < 2; ++i) { ngo.Teams[i] = new _Team(); ngo.Teams[i].TeamID = teams[i].TeamID; ngo.Teams[i].TilePoint = teams[i].TilePoint; ngo.Teams[i].AreaPoint = teams[i].AreaPoint; ngo.Teams[i].Agents = new _Agent[GameMaster.Table.AgentNum]; for (int j = 0; j < GameMaster.Table.AgentNum; ++j) { ngo.Teams[i].Agents[j] = new _Agent(); ngo.Teams[i].Agents[j].AgentID = teams[i].Agents[j].AgentID; ngo.Teams[i].Agents[j].X = teams[i].Agents[j].X + 1; ngo.Teams[i].Agents[j].Y = teams[i].Agents[j].Y + 1; } //End_For } //End_For var actionList = new List <_ReceiveAction>(); foreach (var l in JsonInOut.TurnActions) { foreach (var a in l) { actionList.Add(a); } //End_For } //End_For ngo.Actions = actionList.ToArray(); //JSON化 string json; using (var ms = new MemoryStream()) { var serializer = new DataContractJsonSerializer(typeof(_GameTable)); serializer.WriteObject(ms, ngo); json = Encoding.UTF8.GetString(ms.ToArray()); } //End_Using return(json); } //End_Method