Handle structured server errors in fail events

This commit is contained in:
Tyler Hallada 2021-02-28 18:51:59 -05:00
parent 4b6c1570ac
commit b591391197
2 changed files with 158 additions and 61 deletions

View File

@ -14,6 +14,7 @@ ObjectReference property soldItemRef auto
Form property soldItemBase auto
int property soldItemQuantity auto
int property soldAmount auto
bool property pendingTransaction auto
event OnInit()
debug.Trace("BRMerchChestScript OnInit")
@ -57,6 +58,11 @@ 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 pendingTransaction
debug.Notification("Previous transaction still in progress! Cancelling new transaction.")
; TODO: undo transaction
; PlayerRef.RemoveItem(baseItem, itemCount, self)
else
if baseItem == Gold001 as Form
soldAmount = itemCount
MaybeCreateTransaction()
@ -67,11 +73,16 @@ Event OnItemRemoved(Form baseItem, int itemCount, ObjectReference itemRef, Objec
MaybeCreateTransaction()
endif
endif
endif
endEvent
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 pendingTransaction
; self.RemoveItem(baseItem, itemCount, PlayerRef)
; TODO: undo transaction
else
if baseItem == Gold001 as Form
boughtAmount = itemCount
MaybeCreateTransaction()
@ -82,31 +93,26 @@ Event OnItemAdded(Form baseItem, int itemCount, ObjectReference itemRef, ObjectR
MaybeCreateTransaction()
endif
endif
endif
endEvent
function MaybeCreateTransaction()
if boughtAmount > 0 && boughtItemBase && boughtItemQuantity > 0
debug.Trace("BRMerchChestScript MaybeCreateTransaction creating buy transaction")
BRQuestScript BRScript = BRQuest as BRQuestScript
pendingTransaction = true
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
pendingTransaction = true
bool result = BRTransaction.CreateFromVendorSale(BRScript.ApiUrl, BRScript.ApiKey, BRScript.ActiveShopId, true, soldItemQuantity, soldAmount, soldItemBase, self)
if !result
Debug.MessageBox("Failed to sell merchandise.\n\n" + BRScript.BugReportCopy)
endif
soldAmount = 0
soldItemBase = None
soldItemRef = None
soldItemQuantity = 0
else
debug.Trace("BRMerchChestScript MaybeCreateTransaction sale not yet finalized")
endif
@ -132,23 +138,59 @@ event OnCreateMerchandiseSuccess(bool created)
endif
endEvent
event OnCreateMerchandiseFail(string error)
Debug.Trace("BRMerchChestScript OnCreateMerchandiseFail error: " + error)
event OnCreateMerchandiseFail(bool isServerError, int status, string title, string detail, string otherError)
BRQuestScript BRScript = BRQuest as BRQuestScript
Debug.MessageBox("Failed to save shop merchandise.\n\n" + error + "\n\n" + BRScript.BugReportCopy)
if isServerError
Debug.Trace("BRMerchChestScript OnCreateMerchandiseFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to save shop merchandise.\n\nServer " + status + " " + title + ": " + detail + "\n\n" + BRScript.BugReportCopy)
else
Debug.Trace("BRMerchChestScript OnCreateMerchandiseFail other error: " + otherError)
Debug.MessageBox("Failed to save shop merchandise.\n\n" + otherError + "\n\n" + BRScript.BugReportCopy)
endif
endEvent
event OnCreateTransactionSuccess(int id, int quantity, int amount)
debug.Trace("BRMerchChestScript OnCreateTransactionSuccess id: " + id + " quantity: " + quantity + " amount: " + amount)
ObjectReference merchRef = self.GetLinkedRef(BRLinkItemRef)
RefreshMerchandise()
pendingTransaction = false
boughtAmount = 0
boughtItemBase = None
boughtItemRef = None
boughtItemQuantity = 0
soldAmount = 0
soldItemBase = None
soldItemRef = None
soldItemQuantity = 0
endEvent
; 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)
Event OnCreateTransactionFail(bool isServerError, int status, string title, string detail, string otherError)
BRQuestScript BRScript = BRQuest as BRQuestScript
Debug.MessageBox("Failed to buy merchandise.\n\n" + error + "\n\n" + BRScript.BugReportCopy)
if isServerError
Debug.Trace("BRMerchChestScript OnCreateTransactionFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to buy or sell merchandise.\n\nServer " + status + " " + title + ": " + detail + "\n\n" + BRScript.BugReportCopy)
else
Debug.Trace("BRMerchChestScript OnCreateTransactionFail other error: " + otherError)
Debug.MessageBox("Failed to buy or sell merchandise.\n\n" + otherError + "\n\n" + BRScript.BugReportCopy)
endif
; TODO Undo transaction
; if boughtItemBase
; PlayerRef.RemoveItem(boughtItemBase, boughtItemQuantity, self)
; PlayerRef.AddItem(Gold001, boughtAmount)
; elseif soldItemBase
; self.RemoveItem(soldItemBase, soldItemQuantity, PlayerRef)
; self.AddItem(Gold001, soldAmount)
; endif
pendingTransaction = false
boughtAmount = 0
boughtItemBase = None
boughtItemRef = None
boughtItemQuantity = 0
soldAmount = 0
soldItemBase = None
soldItemRef = None
soldItemQuantity = 0
RefreshMerchandise()
endEvent
@ -168,10 +210,15 @@ event OnLoadMerchandiseSuccess(bool result)
endWhile
endEvent
event OnLoadMerchandiseFail(string error)
Debug.Trace("BRMerchChestScript OnLoadMerchandiseFail error: " + error)
event OnLoadMerchandiseFail(bool isServerError, int status, string title, string detail, string otherError)
BRQuestScript BRScript = BRQuest as BRQuestScript
Debug.MessageBox("Failed to load or clear shop merchandise.\n\n" + error + "\n\n" + BRScript.BugReportCopy)
if isServerError
Debug.Trace("BRMerchChestScript OnLoadMerchandiseFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to load or clear shop merchandise.\n\nServer " + status + " " + title + ": " + detail + "\n\n" + BRScript.BugReportCopy)
else
Debug.Trace("BRMerchChestScript OnLoadMerchandiseFail other error: " + otherError)
Debug.MessageBox("Failed to load or clear shop merchandise.\n\n" + otherError + "\n\n" + BRScript.BugReportCopy)
endif
endEvent
event OnLoadShelfPageSuccess(bool result)
@ -185,10 +232,15 @@ event OnLoadShelfPageSuccess(bool result)
endWhile
endEvent
event OnLoadShelfPageFail(string error)
Debug.Trace("BRMerchChestScript OnLoadShelfPageFail error: " + error)
event OnLoadShelfPageFail(bool isServerError, int status, string title, string detail, string otherError)
BRQuestScript BRScript = BRQuest as BRQuestScript
Debug.MessageBox("Failed to load or clear page of shelf merchandise.\n\n" + error + "\n\n" + BRScript.BugReportCopy)
if isServerError
Debug.Trace("BRMerchChestScript OnLoadShelfPageFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to load or clear page of shelf merchandise.\n\nServer " + status + " " + title + ": " + detail + "\n\n" + BRScript.BugReportCopy)
else
Debug.Trace("BRMerchChestScript OnLoadShelfPageFail other error: " + otherError)
Debug.MessageBox("Failed to load or clear page of shelf merchandise.\n\n" + otherError + "\n\n" + BRScript.BugReportCopy)
endif
endEvent
event OnRefreshShopGoldSuccess(int gold)
@ -200,8 +252,13 @@ event OnRefreshShopGoldSuccess(int gold)
endif
endEvent
event OnRefreshShopGoldFail(string error)
Debug.Trace("BRMerchChestScript OnRefreshShopGoldFail error: " + error)
event OnRefreshShopGoldFail(bool isServerError, int status, string title, string detail, string otherError)
BRQuestScript BRScript = BRQuest as BRQuestScript
Debug.MessageBox("Failed to refresh shop gold.\n\n" + error + "\n\n" + BRScript.BugReportCopy)
if isServerError
Debug.Trace("BRMerchChestScript OnRefreshShopGoldFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to refresh shop gold.\n\nServer " + status + " " + title + ": " + detail + "\n\n" + BRScript.BugReportCopy)
else
Debug.Trace("BRMerchChestScript OnRefreshShopGoldFail other error: " + otherError)
Debug.MessageBox("Failed to refresh shop gold.\n\n" + otherError + "\n\n" + BRScript.BugReportCopy)
endif
endEvent

View File

@ -94,9 +94,14 @@ event OnStatusCheckSuccess(bool result)
endif
endEvent
event OnStatusCheckFail(string error)
Debug.Trace("BRQuestScript OnStatusCheckFail error: " + error)
Debug.MessageBox("Failed to initialize Bazaar Realm client.\n\n" + error + "\n\nThe API server might be down: " + ApiUrl)
event OnStatusCheckFail(bool isServerError, int status, string title, string detail, string otherError)
if isServerError
Debug.Trace("BRMerchChestScript OnStatusCheckFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to initialize Bazaar Realm client.\n\nServer " + status + " " + title + ": " + detail + "\n\nThe API server might be down: " + ApiUrl)
else
Debug.Trace("BRMerchChestScript OnStatusCheckFail other error: " + otherError)
Debug.MessageBox("Failed to initialize Bazaar Realm client.\n\n" + otherError + "\n\nThe API server might be down: " + ApiUrl)
endif
StartModFailed = true
endEvent
@ -111,9 +116,14 @@ event OnCreateOwnerSuccess(int id)
endif
endEvent
event OnCreateOwnerFail(string error)
Debug.Trace("BRQuestScript OnCreateOwnerFail error: " + error)
Debug.MessageBox("Failed to create new owner in the API.\n\n" + error + "\n\n" + BugReportCopy)
event OnCreateOwnerFail(bool isServerError, int status, string title, string detail, string otherError)
if isServerError
Debug.Trace("BRMerchChestScript OnCreateOwnerFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to create a new owner in the API.\n\nServer " + status + " " + title + ": " + detail + "\n\n" + BugReportCopy)
else
Debug.Trace("BRMerchChestScript OnCreateOwnerFail other error: " + otherError)
Debug.MessageBox("Failed to create a new owner in the API.\n\n" + otherError + "\n\n" + BugReportCopy)
endif
StartModFailed = true
endEvent
@ -129,9 +139,14 @@ event OnCreateShopSuccess(int id, string name, string description, int gold, str
Debug.Notification("Initialized Bazaar Realm client")
endEvent
event OnCreateShopFail(string error)
Debug.Trace("BRQuestScript OnCreateShopFail error: " + error)
Debug.MessageBox("Failed to initialize Bazaar Realm client.\n\n" + error + "\n\n" + BugReportCopy)
event OnCreateShopFail(bool isServerError, int status, string title, string detail, string otherError)
if isServerError
Debug.Trace("BRMerchChestScript OnCreateShopFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to create a new shop in the API.\n\nServer " + status + " " + title + ": " + detail + "\n\n" + BugReportCopy)
else
Debug.Trace("BRMerchChestScript OnCreateShopFail other error: " + otherError)
Debug.MessageBox("Failed to create a new shop in the API.\n\n" + otherError + "\n\n" + BugReportCopy)
endif
StartModFailed = true
endEvent
@ -152,9 +167,14 @@ event OnCreateInteriorRefListSuccess(int id)
Debug.MessageBox("Successfully saved shop.")
endEvent
event OnCreateInteriorRefListFail(string error)
Debug.Trace("BRQuestScript OnCreateInteriorRefListFail error: " + error)
Debug.MessageBox("Failed to save shop.\n\n" + error + "\n\n" + BugReportCopy)
event OnCreateInteriorRefListFail(bool isServerError, int status, string title, string detail, string otherError)
if isServerError
Debug.Trace("BRMerchChestScript OnCreateInteriorRefListFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to save shop.\n\nServer " + status + " " + title + ": " + detail + "\n\n" + BugReportCopy)
else
Debug.Trace("BRMerchChestScript OnCreateInteriorRefListFail other error: " + otherError)
Debug.MessageBox("Failed to save shop.\n\n" + otherError + "\n\n" + BugReportCopy)
endif
endEvent
bool function LoadInteriorRefs()
@ -198,9 +218,14 @@ event OnLoadInteriorRefListSuccess(bool result, ObjectReference[] shelves)
endif
endEvent
event OnLoadInteriorRefListFail(string error)
Debug.Trace("BRQuestScript OnLoadInteriorRefListFail error: " + error)
Debug.MessageBox("Failed to load shop.\n\n" + error + "\n\n" + BugReportCopy)
event OnLoadInteriorRefListFail(bool isServerError, int status, string title, string detail, string otherError)
if isServerError
Debug.Trace("BRMerchChestScript OnLoadInteriorRefListFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to load shop.\n\nServer " + status + " " + title + ": " + detail + "\n\n" + BugReportCopy)
else
Debug.Trace("BRMerchChestScript OnLoadInteriorRefListFail other error: " + otherError)
Debug.MessageBox("Failed load shop.\n\n" + otherError + "\n\n" + BugReportCopy)
endif
endEvent
; currently unused, was testing out UILib
@ -246,9 +271,14 @@ event OnUpdateShopSuccess(int id, string name, string description, int gold, str
UpdateShopComplete = true
endEvent
event OnUpdateShopFail(string error)
Debug.Trace("BRQuestScript OnUpdateShopFail error: " + error)
Debug.MessageBox("Failed to update shop.\n\n" + error + "\n\n" + BugReportCopy)
event OnUpdateShopFail(bool isServerError, int status, string title, string detail, string otherError)
if isServerError
Debug.Trace("BRMerchChestScript OnUpdateShopFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to update shop.\n\nServer " + status + " " + title + ": " + detail + "\n\n" + BugReportCopy)
else
Debug.Trace("BRMerchChestScript OnUpdateShopFail other error: " + otherError)
Debug.MessageBox("Failed to update shop.\n\n" + otherError + "\n\n" + BugReportCopy)
endif
UpdateShopComplete = true
endEvent
@ -284,9 +314,14 @@ event OnGetShopSuccess(int id, string name, string description, int gold, string
GetShopComplete = true
endEvent
event OnGetShopFail(string error)
Debug.Trace("BRQuestScript OnGetShopFail error: " + error)
Debug.MessageBox("Failed to get shop.\n\n" + error + "\n\n" + BugReportCopy)
event OnGetShopFail(bool isServerError, int status, string title, string detail, string otherError)
if isServerError
Debug.Trace("BRMerchChestScript OnGetShopFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to get shop.\n\nServer " + status + " " + title + ": " + detail + "\n\n" + BugReportCopy)
else
Debug.Trace("BRMerchChestScript OnGetShopFail other error: " + otherError)
Debug.MessageBox("Failed to get shop.\n\n" + otherError + "\n\n" + BugReportCopy)
endif
GetShopComplete = true
endEvent
@ -334,8 +369,13 @@ event OnListShopsSuccess(int[] ids, string[] names, string[] descriptions, int[]
endif
endEvent
event OnListShopsFail(string error)
Debug.Trace("BRQuestScript OnListShopsFail error: " + error)
Debug.MessageBox("Failed to list shops.\n\n" + error + "\n\n" + BugReportCopy)
event OnListShopsFail(bool isServerError, int status, string title, string detail, string otherError)
if isServerError
Debug.Trace("BRMerchChestScript OnListShopsFail server error: " + status + " " + title + ": " + detail)
Debug.MessageBox("Failed to list shops.\n\nServer " + status + " " + title + ": " + detail + "\n\n" + BugReportCopy)
else
Debug.Trace("BRMerchChestScript OnListShopsFail other error: " + otherError)
Debug.MessageBox("Failed to list shops.\n\n" + otherError + "\n\n" + BugReportCopy)
endif
ListShopsComplete = true
endEvent