public static void ContractsReceived(ClientStructure client, ShareProgressContractsMsgData data) { LunaLog.Debug("Contract data received:"); foreach (var item in data.Contracts) { LunaLog.Debug(item.ContractGuid.ToString()); } //send the contract update to all other clients MessageQueuer.RelayMessage <ShareProgressSrvMsg>(client, data); ScenarioDataUpdater.WriteContractDataToFile(data); }
/// <summary> /// We received a technology message so update the scenario file accordingly /// </summary> public static void WriteContractDataToFile(ShareProgressContractsMsgData techMsg) { Task.Run(() => { lock (Semaphore.GetOrAdd("ContractSystem", new object())) { if (!ScenarioStoreSystem.CurrentScenariosInXmlFormat.TryGetValue("ContractSystem", out var xmlData)) { return; } var updatedText = UpdateScenarioWithContractData(xmlData, techMsg.Contracts); ScenarioStoreSystem.CurrentScenariosInXmlFormat.TryUpdate("ContractSystem", updatedText, xmlData); } }); }
/// <summary> /// We received a contract message so update the scenario file accordingly /// </summary> public static void WriteContractDataToFile(ShareProgressContractsMsgData contractsMsg) { Task.Run(() => { lock (Semaphore.GetOrAdd("ContractSystem", new object())) { if (!ScenarioStoreSystem.CurrentScenarios.TryGetValue("ContractSystem", out var scenario)) { return; } var scenariosParentNode = scenario.GetNode("CONTRACTS")?.Value; if (scenariosParentNode == null) { return; } var existingContracts = scenariosParentNode.GetNodes("CONTRACT").Select(c => c.Value).ToArray(); if (existingContracts.Any()) { foreach (var contract in contractsMsg.Contracts.Select(v => new ConfigNode(Encoding.UTF8.GetString(v.Data, 0, v.NumBytes)) { Name = "CONTRACT" })) { var specificContractNode = existingContracts.FirstOrDefault(n => n.GetValue("guid").Value == contract.GetValue("guid").Value); if (specificContractNode != null) { scenariosParentNode.ReplaceNode(specificContractNode, contract); } else { scenariosParentNode.AddNode(contract); } } } } }); }