public bool AcceptProposedContract(string id, out BaseContract foundedContract) { foundedContract = null; foreach (var pc in m_ProposedContracts) { if (pc.Value.id == id) { foundedContract = pc.Value; break; } } if (foundedContract != null) { if (false == HasActiveContract(foundedContract.category)) { BaseContract rc; if (m_ProposedContracts.TryRemove(foundedContract.category, out rc)) { bool acceptedStatus = foundedContract.Accept(); if (AddOrReplaceActiveContract(foundedContract)) { m_MmoMessage.ContractsUpdate(GetInfo()); m_MmoMessage.ContractAccepted(foundedContract); if (acceptedStatus) { foundedContract.OnAccepted(); } return(true); } } } } return(false); }
private bool AddOrReplaceCompletedContract(BaseContract contract) { //first remove all completed contracts this category ConcurrentBag <string> categoryContracts = new ConcurrentBag <string>(); foreach (var pcc in m_CompletedContracts) { if (pcc.Value.category == contract.category) { categoryContracts.Add(pcc.Value.id); } } foreach (var cid in categoryContracts) { BaseContract oldContract; m_CompletedContracts.TryRemove(cid, out oldContract); } //then add completed contract -we have only single completed contract of category in completed contracts bool removedSuccessfully = true; if (m_CompletedContracts.ContainsKey(contract.id)) { BaseContract oldContract; removedSuccessfully = m_CompletedContracts.TryRemove(contract.id, out oldContract); } if (removedSuccessfully) { return(m_CompletedContracts.TryAdd(contract.id, contract)); } return(false); }
public bool DeclineContract(string id, out BaseContract declinedContract) { BaseContract foundedContract = null; foreach (var pc in m_ProposedContracts) { if (pc.Value.id == id) { foundedContract = pc.Value; break; } } if (foundedContract != null) { foundedContract.Decline(); if (HasActiveContract(foundedContract.category)) { MoveActiveContractToTrash(foundedContract.category); } BaseContract rc; if (m_ProposedContracts.TryRemove(foundedContract.category, out rc)) { if (AddOrReplaceActiveContract(foundedContract)) { m_MmoMessage.ContractDeclined(foundedContract); m_MmoMessage.ContractsUpdate(GetInfo()); declinedContract = foundedContract; return(true); } } } else { foreach (var pac in m_ActiveContracts) { if (pac.Key == id) { pac.Value.Decline(); m_MmoMessage.ContractDeclined(pac.Value); m_MmoMessage.ContractsUpdate(GetInfo()); declinedContract = pac.Value; return(true); } } } declinedContract = null; return(false); }
private bool AddOrReplaceActiveContract(BaseContract contract) { bool removedSuccessfully = true; if (m_ActiveContracts.ContainsKey(contract.id)) { BaseContract oldContract; removedSuccessfully = m_ActiveContracts.TryRemove(contract.id, out oldContract); } if (removedSuccessfully) { return(m_ActiveContracts.TryAdd(contract.id, contract)); } return(false); }
//private bool AcceptContract(BaseContract contract ) { // if(contract.state == ContractState.accepted) { // bool success = AddOrReplaceActiveContract(contract); // if(success) { // m_MmoMessage.ContractAccepted(contract); // } // return success; // } // return false; //} public bool CompleteContract(string contractId, out BaseContract completedContract) { var contract = GetActiveContract(contractId); if (contract != null) { if (contract.state == ContractState.ready) { BaseContract removedActiveContract; if (m_ActiveContracts.TryRemove(contractId, out removedActiveContract)) { contract.Complete(); if (AddOrReplaceCompletedContract(contract)) { m_MmoMessage.ContractCompleted(contract); completedContract = contract; return(true); } } } } completedContract = null; return(false); }
public bool ProposeContract(BaseContract contract) { bool removedOld = true; if (m_ProposedContracts.ContainsKey(contract.category)) { BaseContract removedContract; if (false == m_ProposedContracts.TryRemove(contract.category, out removedContract)) { removedOld = false; } } if (removedOld) { contract.Propose(); if (m_ProposedContracts.TryAdd(contract.category, contract)) { m_MmoMessage.ContractsUpdate(GetInfo()); return(true); } } return(false); }