/// <summary> /// Attempts to add an item to the inventory, note if you haven't been given express permission from your latest request this will fail. /// Also it's worth noting that the return value doesn't mean the server will definitely add the item, if any error occurs it will not exist. /// </summary> /// <returns><c>true</c>, if the transaction goes through, <c>false</c> otherwise.</returns> /// <param name="ticket">The ticket given by the server which authorises your transaction.</param> public bool AddItemToServer(ItemTicket ticket) { // Check the item exists and whether the transaction has been authorised if (ticket && ticket.IsValid() && ticket == m_itemAddResponse) { // Unity silliness again if (Network.isServer) { ServerAddItem(ticket); } else { networkView.RPC("ServerAddItem", RPCMode.Server, ticket.uniqueID, ticket.itemID, ticket.itemIndex); } // Reset the response to ensure the security of future transactions ResetResponse(false); // Transaction processed successfully return(true); } // If this point has been reached then a problem has occurred Debug.LogError(name + ".NetworkInventory.AddItemToServer() transaction failed."); ResetResponse(false); return(false); }
/// <summary> /// Attempts to remove the item from the server, if the ticket doesn't exist on the servers side due to it timing out and such then /// it will not be removed. /// </summary> /// <returns><c>true</c>, if item from server was removed, <c>false</c> otherwise.</returns> /// <param name="ticket">Ticket.</param> public bool RemoveItemFromServer(ItemTicket ticket) { // Ensure the ticket is both valid to prevent wasting the servers time if (ticket && ticket.IsValid() && ticket == m_itemRequestResponse) { if (Network.isServer) { PropagateRemovalAtIndex(ticket); } else { networkView.RPC("PropagateRemovalAtIndex", RPCMode.Server, ticket.uniqueID, ticket.itemID, ticket.itemIndex); } // Reset the response to maintain security of the inventory ResetResponse(false); // Transaction completed successfully return(true); } // If this point has been reached then a problem has occurred Debug.LogError(name + ": NetworkInventory.RemoveItemFromServer() transaction failed."); ResetResponse(false); return(false); }
// Used to ensure the validity of a ticket just before handing it in public void RequestTicketValidityCheck(ItemTicket ticket) { if (ticket && ticket.IsValid()) { // Reset the response, don't call the function otherwise it will break AddItemToServer() and RemoveItemFromServer() m_hasServerResponded = false; m_ticketValidityResponse = false; // All that needs to be done is just contact the server to find out if the ticket still exists in the list if (Network.isServer) { TicketValidityCheck(ticket, m_blankMessage); } else { networkView.RPC("TicketValidityCheck", RPCMode.Server, ticket.uniqueID, ticket.itemID, ticket.itemIndex); } } else { ResetResponse(true); } }
void PropagateRemovalAtIndex(int uniqueID, int itemID, int itemIndex) { // Reconstruct the ticket ItemTicket ticket = new ItemTicket(uniqueID, itemID, itemIndex); PropagateRemovalAtIndex(ticket); }
public void tradeItem(Bag otherBag, ItemTicket otherItemTicket) { print("Trade items with other bag."); if (otherBag.addItem(owenr.getItem(bagPos))) { owenr.removeItem(bagPos); owenr.addItem(otherBag.getItem(otherItemTicket.bagPos)); otherBag.removeItem(otherItemTicket.bagPos); } }
// The server can just pass the ticket reference through to improve performance void RespondToAddRequest(ItemTicket ticket) { m_hasServerResponded = true; m_itemAddResponse = ticket; // Ensure the ticket expires, the server doesn't need to do it again if (Network.isClient) { StartCoroutine(ExpireItemTicket(ticket, m_requestTimeOutSeconds)); } }
// Reserves an item using the passed ticket void ReserveItem(int index, ItemTicket ticket) { if (IsValidIndex(index)) { // Ensure the previous ticket gets reset so it's invalid m_requestTickets[index].Reset(); // Keep a copy on the server and flag it as requested m_requestTickets[index] = ticket; m_isItemRequested[index] = true; } // Start the expiration countdown StartCoroutine(ExpireItemTicket(ticket, m_requestTimeOutSeconds)); }
public void TestCombinedTicket() { var sb = new StringBuilder(); List <ILineItems> orderTicket = new List <ILineItems>(); orderTicket.Add(ItemTicket.GetWeaponTicket("Short Sword")); orderTicket.Add(ItemTicket.GetMaterialIngot("Cold Iron")); orderTicket.Add(ItemTicket.GetSpecialAbilityTicket("Icy Burst")); foreach (var item in orderTicket) { sb.AppendLine(item.ToString() + Environment.NewLine); } Approvals.Verify(sb.ToString()); }
// Used to perform a synchronised removal void PropagateRemovalAtIndex(ItemTicket ticket) { //Debug.Log(ticket.ToString()); if (ticket.IsValid()) { // The correct index is guaranteed for the clients so only determine it for the server int index = Network.isServer ? DetermineTicketIndex(ticket) : ticket.itemIndex; //Debug.Log("Index: " + index); // Check if it is valid if (IsValidIndex(index)) { // Remove or null the item based on the passed parameter if (m_nullRemovedItems) { m_inventory[index] = null; m_isItemRequested[index] = false; } else { //Debug.Log("Removing at: " + index); m_inventory.RemoveAt(index); m_isItemRequested.RemoveAt(index); // Reset the ticket so the expiration coroutine knows it has been removed m_requestTickets.RemoveAt(index); } // Propagate the change to the clients if (Network.isServer) { // Give clients the correct index ticket.itemIndex = index; // Propagate the removal Debug.Log("Sending removal notification to others."); networkView.RPC("PropagateRemovalAtIndex", RPCMode.Others, ticket.uniqueID, ticket.itemID, ticket.itemIndex); } } else { Debug.LogError("Attempt to remove an item from " + name + " with an invalid or expired ticket."); } } }
void ServerAddItem(ItemTicket ticket) { if (ticket.IsValid()) { // Attempt to find the first null value, if a position hasn't been specified if (m_nullRemovedItems && !IsValidIndex(ticket.itemIndex)) { ticket.itemIndex = FindFirstNull(); } // Finally propagate the addition PropagateItemAtIndex(ticket.itemIndex, ticket.itemID); } else { Debug.LogError("Attempt to add item with invalid or expired ticket in " + name + ".NetworkInventory"); } }
// An ItemTicket version to increase efficiency for the host void TicketValidityCheck(ItemTicket ticket, NetworkMessageInfo message) { // We know that DetermineTicketIndex will either return a correct index or an invalid index if it doesn't work int index = DetermineTicketIndex(ticket); // Using the validity of the index we can tell if the ticket is available and then check if it's still valid bool response = IsValidIndex(index) && m_requestTickets[index].IsValid(); // Silly Unity workaround if (message.Equals(m_blankMessage)) { RespondToTicketValidityCheck(response); } else { networkView.RPC("RespondToTicketValidityCheck", message.sender, response); } }
// An ItemTicket version is provided to increase performance as items time out. void RequestCancel(ItemTicket ticket) { if (ticket.IsValid()) { // If the index is invalid we know that the ticket is an add request if (!IsValidIndex(ticket.itemIndex)) { --addRequests; } else { // The index of the cancellation int index = DetermineTicketIndex(ticket); // Attempt to cancel the ticket if (IsValidIndex(index)) { // Reset the ticket m_requestTickets[index].Reset(); m_isItemRequested[index] = false; // Check to see if the desired index was a null item, this means that it was an add requests if (!m_inventory[index]) { --addRequests; } } else { Debug.LogError("An attempt was made to cancel a request which doesn't exist in " + name + ".NetworkInventory."); } } } else { Debug.LogError("Attempt to cancel invalid ticket in " + name + ".NetworkInventory"); } }
// Causes the server to cancel a request so that others can request the item public void RequestServerCancel(ItemTicket ticket) { Debug.Log("Cancelling ticket: " + ticket); // Ensure we are not wasting time by checking if the ticket is valid if (ticket && ticket.IsValid()) { if (Network.isServer) { RequestCancel(ticket); } else { networkView.RPC("RequestCancel", RPCMode.Server, ticket.uniqueID, ticket.itemID, ticket.itemIndex); } } // Reset the response since we know they've received it ResetResponse(false); }
void RequestItem(int itemID, int preferredIndex, NetworkMessageInfo message) { if (Network.isServer) { ItemTicket ticket = new ItemTicket(); int index = DetermineDesiredIndex(itemID, preferredIndex, RequestCheck.Unrequested); if (IsValidIndex(index)) { // Create the ticket ticket.uniqueID = ticketNumber++; ticket.itemID = itemID; ticket.itemIndex = index; // Ensure the item is successfully reserved ReserveItem(index, ticket); } // Else send back an invalid ticket //Debug.Log("Index: " + index + ", itemID: " + itemID + ", preferredIndex: " + preferredIndex + "."); // This is the only way I've found to check if the message is blank, an alternative method would be preferable if (message.Equals(m_blankMessage)) { RespondToItemRequest(ticket); } else { networkView.RPC("RespondToItemRequest", message.sender, ticket.uniqueID, ticket.itemID, ticket.itemIndex); } } else { ResetResponse(true); Debug.LogError("A client attempted to call RequestItem in NetworkInventory."); } }
// Searches for the corresponding index of the m_requestedTickets list for the given ticket int DetermineTicketIndex(ItemTicket ticket) { // Ideally the itemIndex will contain the perfect value if (IsDesiredIndex(ticket.itemIndex, ticket.itemID, RequestCheck.Requested) && m_requestTickets[ticket.itemIndex].Equals(ticket)) { return(ticket.itemIndex); } else { // Manually search for the ticket for (int i = 0; i < m_requestTickets.Count; ++i) { if (m_requestTickets[i].Equals(ticket)) { return(i); } } } Debug.LogError("Couldn't determine index of " + ticket + " in " + name + ".NetworkInventory"); return(-1); }
// This function should be called using Invoke() after the desired period of time IEnumerator ExpireItemTicket(ItemTicket toExpire, float timeToWait) { //Debug.Log("Waiting to expire ticket: " + toExpire + " in " + timeToWait); // Wait for the desired amount of time yield return(new WaitForSeconds(timeToWait)); // Tickets which have been redeemed will be reset to the standard ticket if (toExpire.IsValid()) { // Only the server needs to manage whether the item is flagged as requested or not if (Network.isServer) { // Obtain the index int index = DetermineTicketIndex(toExpire); // Flag the item as unrequested if (IsValidIndex(index)) { // Reset the request m_isItemRequested[index] = false; } // Must be an add request else { --addRequests; } } // Reset the ticket to ensure it's invalid toExpire.Reset(); Debug.Log("Expiring ItemTicket: " + toExpire.uniqueID + " / " + toExpire.itemID + " / " + toExpire.itemIndex); } }
// Custom equivalence function public override bool Equals(object o) { ItemTicket ticket = (ItemTicket)o; return((this.uniqueID == ticket.uniqueID) && (this.itemID == ticket.itemID) && (this.itemIndex == ticket.itemIndex)); }
public void TestGetMithralIngot() { var ingot = ItemTicket.GetMaterialIngot("Mithral"); Approvals.Verify(ingot.ToString()); }
public void TestRequestSpecialAbility() { var specialAbility = ItemTicket.GetSpecialAbilityTicket("Flaming"); Approvals.Verify(LineItemDisplayUtilites.BasicSpecialAbilityDisplay(specialAbility)); }
void RequestAdd(int itemID, int index, bool adminMode, NetworkMessageInfo info) { if (Network.isServer) { // Create the default response ItemTicket ticket = new ItemTicket(-1, itemID, index); // We now need to check if the itemID is valid if (itemID >= 0) { bool isValidIndex = IsValidIndex(index); // An item can only be requested to be replaced if the item hasn't been requested or we're running in admin mode if (!isValidIndex || !m_isItemRequested[index] || adminMode) { if (isValidIndex) { // Check to see if the item at the index is null, if so we know it is an add request. if (!m_inventory[index]) { if (!IsInventoryFull()) { ticket.uniqueID = ticketNumber++; ++addRequests; ReserveItem(index, ticket); } } // We know it is a replace request so it doesn't matter if the inventory is full, we also know that the item // is either unrequested or we have access to overwrite it because of the intial conditions. else { ticket.uniqueID = ticketNumber++; ReserveItem(index, ticket); } } // If the index is invalid it should be added on to the end which is an add request. else if (!IsInventoryFull()) { ticket.uniqueID = ticketNumber++; ++addRequests; ReserveItem(index, ticket); } } } // Silly workaround for RPC sending limitation if (info.Equals(m_blankMessage)) { RespondToAddRequest(ticket); } else { networkView.RPC("RespondToAddRequest", info.sender, ticket.uniqueID, ticket.itemID, ticket.itemIndex); } } // A client called the function else { ResetResponse(true); Debug.LogError("A client attempted to call RequestAdd in NetworkInventory"); } }
public void OnDrop(PointerEventData eventData) { PointerEventData pointerEvent = new PointerEventData(EventSystem.current); List <RaycastResult> resultList = new List <RaycastResult>(); pointerEvent.position = Input.mousePosition; Bag oldBag = owenr; EventSystem.current.RaycastAll(pointerEvent, resultList); for (int i = 0; i < resultList.Count; i++) { //check on what(ui) the icon fall. if ((resultList[i].gameObject.tag != "Icon" && resultList[i].gameObject.tag != "Bag") || resultList[i].gameObject == this.gameObject) { resultList.RemoveAt(i); i--; } } if (resultList.Count == 2) { //if resultList.count==2 then the icon fall on another icon and bag ui. if (resultList[0].gameObject.GetComponent <ItemTicket>().checkBags(owenr)) { //if the items from the same bag then switch items slots in the bag. print("(icons)the items from the same bag."); ItemTicket otherItem = resultList[0].gameObject.GetComponent <ItemTicket>(); owenr.switchPlaces(bagPos, otherItem.itemBagPos); } else { //if the items not from the same bag then move this item to the other bag. print("(icons)the items from diffrent bags."); ItemTicket otherItem = resultList[0].gameObject.GetComponent <ItemTicket>(); otherItem.tradeItem(owenr, this); } } else if (resultList.Count == 1) { //if resultList.count==1 then the icon fall on bag ui. if (resultList[0].gameObject.GetComponent <BagUi>().checkBags(owenr)) { print("(icons)from the this bag."); //if its someone else bag. resultList[0].gameObject.GetComponent <BagUi>().addToBag(this.owenr.getItem(bagPos)); this.owenr.removeItem(bagPos); } else { print("(icons)the items NOT! from the this bag."); BagUi otherBag = resultList[0].gameObject.GetComponent <BagUi>(); otherBag.addToBag(this.owenr.getItem(bagPos)); this.owenr.removeItem(bagPos); } } else { //if the icon fall on nothing(resultList.count==0). this.transform.position = startPos; } Bag.refreshIcons(); }
public void TestGetWeaponItem() { var weapon = ItemTicket.GetWeaponTicket("Dagger"); Approvals.Verify(LineItemDisplayUtilites.BasicDisplay(weapon)); }