private void _serverSellItem(String info) { String[] splitInfo = info.Split(";"); int infoIndex = 0; int inventoryIndex = int.Parse(splitInfo[infoIndex]); infoIndex++; TeamMapAI teamMapAI = _gameWorld.GetTeamMapAIs()[int.Parse(splitInfo[infoIndex])]; infoIndex++; Agent agent = teamMapAI.GetUnit(splitInfo[infoIndex]); infoIndex++; if (agent != null && IsInstanceValid(agent)) { Inventory inventory = agent.GetInventory(); if (inventory != null && IsInstanceValid(inventory)) { if (inventory.GetItems()[inventoryIndex] != null && !inventory.IsItemIndexInUsed(inventoryIndex)) { ItemResource itemResource = inventory.GetItem(inventoryIndex); // Debit back the amount teamMapAI.ChargeAmount(-itemResource.Price); inventory.RemoveItem(inventoryIndex); } // Sync the client no matter if is passing (if not passing, it may indicate cliet is out of sync with server inventory so need to sync) if (GetTree().NetworkPeer != null && GetTree().IsNetworkServer()) { SyncInventory(-1, agent); } } } }
private void _serverPurchaseItem(String info) { String[] splitInfo = info.Split(";"); int infoIndex = 0; ItemResource itemResource = _inventoryDatabase.GetItemByID(splitInfo[infoIndex]); infoIndex++; TeamMapAI teamMapAI = _gameWorld.GetTeamMapAIs()[int.Parse(splitInfo[infoIndex])]; infoIndex++; Agent agent = teamMapAI.GetUnit(splitInfo[infoIndex]); infoIndex++; if (agent != null && IsInstanceValid(agent)) { Inventory inventory = agent.GetInventory(); // No need to sync cost as cost will sync by team AI system if (inventory != null && IsInstanceValid(inventory) && itemResource != null && IsInstanceValid(itemResource)) { if (inventory.GetAvailableCapacity() > 0 && teamMapAI.ChargeAmount(itemResource.Price)) { inventory.AddItem(itemResource); } // Sync the client no matter if is passing the lastcheck // If not passing, as long as the agent still exit it may indicate cliet is out of sync with server inventory so need to sync if (GetTree().NetworkPeer != null && GetTree().IsNetworkServer()) { SyncInventory(-1, agent); } } } }