2020-11-19 04:31:41 +00:00
|
|
|
scriptname BRMerchChestScript extends ObjectReference
|
2020-10-13 01:26:16 +00:00
|
|
|
|
2020-11-02 06:39:45 +00:00
|
|
|
Keyword property BRLinkItemRef auto
|
2020-11-19 04:31:41 +00:00
|
|
|
Actor property PlayerRef auto
|
|
|
|
Quest property BRQuest auto
|
|
|
|
MiscObject property Gold001 auto
|
|
|
|
FormList property BREmptyFormList auto
|
|
|
|
|
|
|
|
ObjectReference property boughtItemRef auto
|
|
|
|
Form property boughtItemBase auto
|
|
|
|
int property boughtItemQuantity auto
|
|
|
|
int property boughtAmount auto
|
|
|
|
ObjectReference property soldItemRef auto
|
|
|
|
Form property soldItemBase auto
|
|
|
|
int property soldItemQuantity auto
|
|
|
|
int property soldAmount auto
|
|
|
|
|
|
|
|
event OnInit()
|
|
|
|
debug.Trace("BRMerchChestScript OnInit")
|
|
|
|
AddInventoryEventFilter(BREmptyFormList)
|
|
|
|
endEvent
|
2020-10-13 01:26:16 +00:00
|
|
|
|
|
|
|
event OnMenuClose(string menuName)
|
2020-11-19 04:31:41 +00:00
|
|
|
if menuName == "BarterMenu"
|
|
|
|
debug.Trace("BRMerchChestScript barter menu closed, stop listending to add/remove events")
|
|
|
|
AddInventoryEventFilter(BREmptyFormList)
|
|
|
|
UnregisterForMenu("BarterMenu")
|
2021-02-10 04:54:24 +00:00
|
|
|
elseif menuName == "ContainerMenu"
|
|
|
|
debug.Trace("BRMerchChestScript container menu closed")
|
|
|
|
BRQuestScript BRScript = BRQuest as BRQuestScript
|
|
|
|
if BRScript.ActiveShopId == BRScript.ShopId
|
|
|
|
; TODO: the assumption that player is in shop cell may be incorrect
|
|
|
|
Cell shopCell = PlayerRef.GetParentCell()
|
|
|
|
|
|
|
|
bool result = BRMerchandiseList.Create(BRScript.ApiUrl, BRScript.ApiKey, BRScript.ShopId, shopCell, BRScript.ActiveShopShelves, self)
|
|
|
|
if !result
|
|
|
|
Debug.MessageBox("Failed to save shop merchandise.\n\n" + BRScript.BugReportCopy)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
UnregisterForMenu("ContainerMenu")
|
2020-11-19 04:31:41 +00:00
|
|
|
endif
|
|
|
|
endEvent
|
|
|
|
|
|
|
|
event OnMenuOpen(string menuName)
|
|
|
|
if menuName == "BarterMenu"
|
|
|
|
debug.Trace("BRMerchChestScript barter menu opened, start listening to add/remove events")
|
|
|
|
RemoveAllInventoryEventFilters()
|
2021-02-10 04:54:24 +00:00
|
|
|
elseif menuName == "ContainerMenu"
|
|
|
|
debug.Trace("BRMerchChestScript container menu opened")
|
2020-11-19 04:31:41 +00:00
|
|
|
endif
|
|
|
|
endEvent
|
|
|
|
|
|
|
|
Event OnItemRemoved(Form baseItem, int itemCount, ObjectReference itemRef, ObjectReference destContainer)
|
|
|
|
if destContainer == PlayerRef
|
|
|
|
debug.Trace("BRMerchChestScript item moved to player: " + itemRef + " " + baseItem + " " + itemCount)
|
|
|
|
if baseItem == Gold001 as Form
|
|
|
|
soldAmount = itemCount
|
|
|
|
MaybeCreateTransaction()
|
|
|
|
else
|
|
|
|
boughtItemRef = itemRef
|
|
|
|
boughtItemBase = baseItem
|
|
|
|
boughtItemQuantity = itemCount
|
|
|
|
MaybeCreateTransaction()
|
2020-10-13 01:26:16 +00:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endEvent
|
|
|
|
|
2020-11-19 04:31:41 +00:00
|
|
|
Event OnItemAdded(Form baseItem, int itemCount, ObjectReference itemRef, ObjectReference sourceContainer)
|
|
|
|
if sourceContainer == PlayerRef
|
|
|
|
debug.Trace("BRMerchChestScript item moved to container from player: " + itemRef + " " + baseItem + " " + itemCount)
|
|
|
|
if baseItem == Gold001 as Form
|
|
|
|
boughtAmount = itemCount
|
|
|
|
MaybeCreateTransaction()
|
|
|
|
else
|
|
|
|
soldItemRef = itemRef
|
|
|
|
soldItemBase = baseItem
|
|
|
|
soldItemQuantity = itemCount
|
|
|
|
MaybeCreateTransaction()
|
|
|
|
endif
|
2020-10-13 01:26:16 +00:00
|
|
|
endif
|
2020-10-16 03:42:57 +00:00
|
|
|
endEvent
|
|
|
|
|
2020-11-19 04:31:41 +00:00
|
|
|
function MaybeCreateTransaction()
|
|
|
|
if boughtAmount > 0 && boughtItemBase && boughtItemQuantity > 0
|
|
|
|
debug.Trace("BRMerchChestScript MaybeCreateTransaction creating buy transaction")
|
2020-10-16 03:42:57 +00:00
|
|
|
BRQuestScript BRScript = BRQuest as BRQuestScript
|
2020-11-19 04:31:41 +00:00
|
|
|
bool result = BRTransaction.CreateFromVendorSale(BRScript.ApiUrl, BRScript.ApiKey, BRScript.ActiveShopId, false, boughtItemQuantity, boughtAmount, boughtItemBase, self)
|
|
|
|
if !result
|
|
|
|
Debug.MessageBox("Failed to buy merchandise.\n\n" + BRScript.BugReportCopy)
|
|
|
|
endif
|
|
|
|
boughtAmount = 0
|
|
|
|
boughtItemBase = None
|
|
|
|
boughtItemRef = None
|
|
|
|
boughtItemQuantity = 0
|
|
|
|
elseif soldAmount > 0 && soldItemBase && soldItemQuantity > 0
|
|
|
|
debug.Trace("BRMerchChestScript MaybeCreateTransaction creating sell transaction")
|
|
|
|
BRQuestScript BRScript = BRQuest as BRQuestScript
|
2021-02-06 05:29:47 +00:00
|
|
|
bool result = BRTransaction.CreateFromVendorSale(BRScript.ApiUrl, BRScript.ApiKey, BRScript.ActiveShopId, true, soldItemQuantity, soldAmount, soldItemBase, self)
|
2020-11-19 04:31:41 +00:00
|
|
|
if !result
|
2021-02-06 05:29:47 +00:00
|
|
|
Debug.MessageBox("Failed to sell merchandise.\n\n" + BRScript.BugReportCopy)
|
2020-11-02 06:39:45 +00:00
|
|
|
endif
|
2020-11-19 04:31:41 +00:00
|
|
|
soldAmount = 0
|
|
|
|
soldItemBase = None
|
|
|
|
soldItemRef = None
|
|
|
|
soldItemQuantity = 0
|
2020-10-19 00:56:10 +00:00
|
|
|
else
|
2020-11-19 04:31:41 +00:00
|
|
|
debug.Trace("BRMerchChestScript MaybeCreateTransaction sale not yet finalized")
|
2020-10-16 03:42:57 +00:00
|
|
|
endif
|
2020-11-19 04:31:41 +00:00
|
|
|
endFunction
|
|
|
|
|
|
|
|
function RefreshMerchandise()
|
|
|
|
BRQuestScript BRScript = BRQuest as BRQuestScript
|
2021-01-24 01:11:47 +00:00
|
|
|
|
|
|
|
; TODO: the assumption that player is in shop cell may be incorrect
|
|
|
|
Cell shopCell = PlayerRef.GetParentCell()
|
|
|
|
if !BRMerchandiseList.Load(BRScript.ApiUrl, BRScript.ApiKey, BRScript.ActiveShopId, shopCell, BRScript.ActiveShopShelves, self)
|
|
|
|
Debug.MessageBox("Failed to load shop merchandise.\n\n" + BRScript.BugReportCopy)
|
|
|
|
endif
|
2020-11-19 04:31:41 +00:00
|
|
|
endFunction
|
|
|
|
|
|
|
|
event OnCreateTransactionSuccess(int id, int quantity, int amount)
|
|
|
|
debug.Trace("BRMerchChestScript OnCreateTransactionSuccess id: " + id + " quantity: " + quantity + " amount: " + amount)
|
|
|
|
ObjectReference merchRef = self.GetLinkedRef(BRLinkItemRef)
|
|
|
|
RefreshMerchandise()
|
2020-10-19 00:56:10 +00:00
|
|
|
endEvent
|
|
|
|
|
2020-11-19 04:31:41 +00:00
|
|
|
; TODO: gracefully handle expected error cases (e.g. someone else buys all of item before this player can buy them)
|
|
|
|
Event OnCreateTransactionFail(string error)
|
|
|
|
Debug.Trace("BRMerchChestScript OnCreateTransactionFail error: " + error)
|
2020-10-19 00:56:10 +00:00
|
|
|
BRQuestScript BRScript = BRQuest as BRQuestScript
|
2020-11-19 04:31:41 +00:00
|
|
|
Debug.MessageBox("Failed to buy merchandise.\n\n" + error + "\n\n" + BRScript.BugReportCopy)
|
|
|
|
RefreshMerchandise()
|
2020-11-29 04:45:55 +00:00
|
|
|
endEvent
|
|
|
|
|
|
|
|
event OnLoadMerchandiseSuccess(bool result)
|
|
|
|
Debug.Trace("BRMerchChestScript OnLoadMerchandiseSuccess result: " + result)
|
|
|
|
debug.MessageBox("Successfully loaded shop merchandise")
|
|
|
|
|
|
|
|
; TODO: the assumption that player is in shop cell may be incorrect
|
|
|
|
Cell shopCell = PlayerRef.GetParentCell()
|
|
|
|
while !BRMerchandiseList.ReplaceAll3D(shopCell)
|
|
|
|
Debug.Trace("BRMerchandiseList.Replace3D returned false, waiting and trying again")
|
|
|
|
Utility.Wait(0.05)
|
|
|
|
endWhile
|
|
|
|
endEvent
|
|
|
|
|
|
|
|
event OnLoadMerchandiseFail(string error)
|
|
|
|
Debug.Trace("BRMerchChestScript OnLoadMerchandiseFail error: " + error)
|
|
|
|
BRQuestScript BRScript = BRQuest as BRQuestScript
|
|
|
|
Debug.MessageBox("Failed to load or clear shop merchandise.\n\n" + error + "\n\n" + BRScript.BugReportCopy)
|
2021-01-24 01:11:47 +00:00
|
|
|
endEvent
|
|
|
|
|
|
|
|
event OnLoadShelfPageSuccess(bool result)
|
|
|
|
Debug.Trace("BRMerchChestScript OnLoadShelfPageSuccess result: " + result)
|
|
|
|
|
|
|
|
; TODO: the assumption that player is in shop cell may be incorrect
|
|
|
|
Cell shopCell = PlayerRef.GetParentCell()
|
|
|
|
while !BRMerchandiseList.ReplaceAll3D(shopCell) ; replace all 3D or only refs linked to this chest's shelf?
|
|
|
|
Debug.Trace("BRMerchandiseList.Replace3D returned false, waiting and trying again")
|
|
|
|
Utility.Wait(0.05)
|
|
|
|
endWhile
|
|
|
|
endEvent
|
|
|
|
|
|
|
|
event OnLoadShelfPageFail(string error)
|
|
|
|
Debug.Trace("BRMerchChestScript OnLoadShelfPageFail error: " + error)
|
|
|
|
BRQuestScript BRScript = BRQuest as BRQuestScript
|
|
|
|
Debug.MessageBox("Failed to load or clear page of shelf merchandise.\n\n" + error + "\n\n" + BRScript.BugReportCopy)
|
2020-10-13 01:26:16 +00:00
|
|
|
endEvent
|