public static void Recreate(SessionClient newClient) { Single.Disconnect(); Single = newClient; }
/// <summary> /// Начинаем процесс, первые запросы информации /// </summary> /// <param name="connect"></param> public void Start(SessionClient connect) { Loger.Log("Client GameAttackHost Start 1"); var tolient = connect.AttackOnlineHost(new AttackHostToSrv() { State = 2 }); if (!string.IsNullOrEmpty(connect.ErrorMessage)) { ErrorBreak(connect.ErrorMessage); return; } AttackerLogin = tolient.StartInitiatorPlayer; InitiatorPlaceServerId = tolient.InitiatorPlaceServerId; HostPlaceServerId = tolient.HostPlaceServerId; Loger.Log("Client GameAttackHost Start 2 " + tolient.HostPlaceServerId); LongEventHandler.QueueLongEvent(delegate { try { Loger.Log("Client GameAttackHost Start 3"); var hostPlace = UpdateWorldController.GetWOByServerId(HostPlaceServerId) as MapParent; var cloneMap = hostPlace.Map; var toSrvMap = new AttackHostToSrv() { State = 4, TerrainDefNameCell = new List <IntVec3S>(), TerrainDefName = new List <string>(), Thing = new List <ThingTrade>(), ThingCell = new List <IntVec3S>() }; toSrvMap.MapSize = new IntVec3S(cloneMap.Size); CellRect cellRect = CellRect.WholeMap(cloneMap); cellRect.ClipInsideMap(cloneMap); //почва Loger.Log("Client GameAttackHost Start 4"); foreach (IntVec3 current in cellRect) { var terr = cloneMap.terrainGrid.TerrainAt(current); toSrvMap.TerrainDefNameCell.Add(new IntVec3S(current)); toSrvMap.TerrainDefName.Add(terr.defName); } //скалы, преграды и строения без пешек и без растений не деревьев, вывести кол-во Loger.Log("Client GameAttackHost Start 5"); foreach (IntVec3 current in cellRect) { foreach (Thing thc in Find.CurrentMap.thingGrid.ThingsAt(current).ToList <Thing>()) { if (thc is Pawn) { continue; } //из растений оставляем только деревья if (thc.def.category == ThingCategory.Plant && !thc.def.plant.IsTree) { continue; } if (thc.Position != current) { continue; } var tt = ThingTrade.CreateTrade(thc, thc.stackCount, false); toSrvMap.ThingCell.Add(new IntVec3S(current)); toSrvMap.Thing.Add(tt); } } Loger.Log("Client GameAttackHost Start 6"); SessionClientController.Command((connect0) => { Loger.Log("Client GameAttackHost Start 7"); connect0.AttackOnlineHost(toSrvMap); if (!string.IsNullOrEmpty(connect0.ErrorMessage)) { ErrorBreak(connect0.ErrorMessage); return; } //Ждем и получаем пешки атакующего и добавляем их Loger.Log("Client GameAttackHost Start 8"); List <ThingEntry> pawnsA = null; var s1Time = DateTime.UtcNow; while (true) { var toClient5 = connect0.AttackOnlineHost(new AttackHostToSrv() { State = 5 }); if (!string.IsNullOrEmpty(connect0.ErrorMessage)) { ErrorBreak(connect0.ErrorMessage); return; } pawnsA = toClient5.Pawns; if (pawnsA != null && pawnsA.Count > 0) { break; } if ((DateTime.UtcNow - s1Time).TotalSeconds > 20) { ErrorBreak("Timeout"); return; } } if (!string.IsNullOrEmpty(connect0.ErrorMessage)) { ErrorBreak(connect0.ErrorMessage); return; } Loger.Log("Client GameAttackHost Start 9"); //Потом добавляем список к отправке SendedPawnsId = new HashSet <int>(); SendedState = new Dictionary <int, int>(); ToUpdateStateId = new List <int>(); ToUpdateState = new List <Thing>(); ToSendDeleteId = new HashSet <int>(); ToSendAddId = new HashSet <int>(); ToSendThingAdd = new HashSet <Thing>(); AttackingPawns = new List <Pawn>(); AttackingPawnDic = new Dictionary <int, int>(); AttackingPawnJobDic = new Dictionary <int, AttackPawnCommand>(); AttackUpdateTick = 0; GameMap = cloneMap; UIEventNewJobDisable = true; var cellPawns = GameUtils.SpawnCaravanPirate(cloneMap, pawnsA, (th, te) => { var p = th as Pawn; if (p == null) { return; } AttackingPawns.Add(p); AttackingPawnDic.Add(p.thingIDNumber, te.OriginalID); //задаем команду стоять и не двигаться (но стрелять если кто в радиусе) Loger.Log("Client GameAttackHost Start 9 StartJob Wait_Combat "); p.playerSettings.hostilityResponse = HostilityResponseMode.Ignore; p.jobs.StartJob(new Job(JobDefOf.Wait_Combat) { playerForced = true, expiryInterval = int.MaxValue, checkOverrideOnExpire = false, } , JobCondition.InterruptForced); /* * if (p.Label == "Douglas, Клерк") * { * //todo для теста не забыть удалить! * var pp = p; * var th = new Thread(() => * { * Thread.Sleep(5000); * while (true) * { * Thread.Sleep(1000); * try * { * var jj = pp.jobs.curJob; * Loger.Log("Host ThreadTestJob " + pp.Label + " job=" + (jj == null ? "null" : jj.def.defName.ToString())); * } * catch * { } * } * }); * th.IsBackground = true; * th.Start(); * } */ }); UIEventNewJobDisable = false; Loger.Log("Client GameAttackHost Start 10"); CameraJumper.TryJump(cellPawns, cloneMap); TimerObj = SessionClientController.Timers.Add(200, AttackUpdate); //включаем обработку событий урона и уничтожения объектов GameAttackTrigger_Patch.ActiveAttackHost.Add(GameMap, this); Loger.Log("Client GameAttackHost Start 11"); }); } catch (Exception ext) { Loger.Log("GameAttackHost Start() Exception " + ext.ToString()); } }, "...", false, null); }
/// <summary> /// Отбрасывание старого подключения, создание нового и аутэнтификация. Перед вызовом убедиться, что поток таймера остановлен /// </summary> /// <returns></returns> public static bool Reconnect() { if (string.IsNullOrEmpty(Data?.KeyReconnect)) { Loger.Log("Client Reconnect fail: no KeyReconnect "); return(false); } if (string.IsNullOrEmpty(My?.Login)) { Loger.Log("Client Reconnect fail: no Login "); return(false); } if (string.IsNullOrEmpty(ConnectAddr)) { Loger.Log("Client Reconnect fail: no ConnectAddr "); return(false); } //Connect { var addr = ConnectAddr; int port = 0; if (addr.Contains(":") && int.TryParse(addr.Substring(addr.LastIndexOf(":") + 1), out port)) { addr = addr.Substring(0, addr.LastIndexOf(":")); } var logMsg = "Reconnect to server. Addr: " + addr + ". Port: " + (port == 0 ? SessionClient.DefaultPort : port).ToString(); Loger.Log("Client " + logMsg); Log.Warning(logMsg); var connect = new SessionClient(); if (!connect.Connect(addr, port)) { logMsg = "Reconnect net fail: " + connect.ErrorMessage; Loger.Log("Client " + logMsg); Log.Warning(logMsg); return(false); } SessionClient.Recreate(connect); // } logMsg = "Reconnect login: "******"Client " + logMsg); Log.Warning(logMsg); //var connect = SessionClient.Get; if (!connect.Reconnect(My.Login, Data.KeyReconnect)) { logMsg = "Reconnect login fail: " + connect.ErrorMessage; Loger.Log("Client " + logMsg); Log.Warning(logMsg); //Find.WindowStack.Add(new Dialog_Input("OCity_SessionCC_LoginFailTitle".Translate(), connect.ErrorMessage, true)); return(false); } else { logMsg = "Reconnect OK"; Loger.Log("Client " + logMsg); Log.Warning(logMsg); return(true); } }