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 Form property soldItemBase auto
int property soldItemQuantity auto int property soldItemQuantity auto
int property soldAmount auto int property soldAmount auto
bool property pendingTransaction auto
event OnInit() event OnInit()
debug.Trace("BRMerchChestScript OnInit") debug.Trace("BRMerchChestScript OnInit")
@ -57,6 +58,11 @@ endEvent
Event OnItemRemoved(Form baseItem, int itemCount, ObjectReference itemRef, ObjectReference destContainer) Event OnItemRemoved(Form baseItem, int itemCount, ObjectReference itemRef, ObjectReference destContainer)
if destContainer == PlayerRef if destContainer == PlayerRef
debug.Trace("BRMerchChestScript item moved to player: " + itemRef + " " + baseItem + " " + itemCount) 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 if baseItem == Gold001 as Form
soldAmount = itemCount soldAmount = itemCount
MaybeCreateTransaction() MaybeCreateTransaction()
@ -67,11 +73,16 @@ Event OnItemRemoved(Form baseItem, int itemCount, ObjectReference itemRef, Objec
MaybeCreateTransaction() MaybeCreateTransaction()
endif endif
endif endif
endif
endEvent endEvent
Event OnItemAdded(Form baseItem, int itemCount, ObjectReference itemRef, ObjectReference sourceContainer) Event OnItemAdded(Form baseItem, int itemCount, ObjectReference itemRef, ObjectReference sourceContainer)
if sourceContainer == PlayerRef if sourceContainer == PlayerRef
debug.Trace("BRMerchChestScript item moved to container from player: " + itemRef + " " + baseItem + " " + itemCount) 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 if baseItem == Gold001 as Form
boughtAmount = itemCount boughtAmount = itemCount
MaybeCreateTransaction() MaybeCreateTransaction()
@ -82,31 +93,26 @@ Event OnItemAdded(Form baseItem, int itemCount, ObjectReference itemRef, ObjectR
MaybeCreateTransaction() MaybeCreateTransaction()
endif endif
endif endif
endif
endEvent endEvent
function MaybeCreateTransaction() function MaybeCreateTransaction()
if boughtAmount > 0 && boughtItemBase && boughtItemQuantity > 0 if boughtAmount > 0 && boughtItemBase && boughtItemQuantity > 0
debug.Trace("BRMerchChestScript MaybeCreateTransaction creating buy transaction") debug.Trace("BRMerchChestScript MaybeCreateTransaction creating buy transaction")
BRQuestScript BRScript = BRQuest as BRQuestScript BRQuestScript BRScript = BRQuest as BRQuestScript
pendingTransaction = true
bool result = BRTransaction.CreateFromVendorSale(BRScript.ApiUrl, BRScript.ApiKey, BRScript.ActiveShopId, false, boughtItemQuantity, boughtAmount, boughtItemBase, self) bool result = BRTransaction.CreateFromVendorSale(BRScript.ApiUrl, BRScript.ApiKey, BRScript.ActiveShopId, false, boughtItemQuantity, boughtAmount, boughtItemBase, self)
if !result if !result
Debug.MessageBox("Failed to buy merchandise.\n\n" + BRScript.BugReportCopy) Debug.MessageBox("Failed to buy merchandise.\n\n" + BRScript.BugReportCopy)
endif endif
boughtAmount = 0
boughtItemBase = None
boughtItemRef = None
boughtItemQuantity = 0
elseif soldAmount > 0 && soldItemBase && soldItemQuantity > 0 elseif soldAmount > 0 && soldItemBase && soldItemQuantity > 0
debug.Trace("BRMerchChestScript MaybeCreateTransaction creating sell transaction") debug.Trace("BRMerchChestScript MaybeCreateTransaction creating sell transaction")
BRQuestScript BRScript = BRQuest as BRQuestScript BRQuestScript BRScript = BRQuest as BRQuestScript
pendingTransaction = true
bool result = BRTransaction.CreateFromVendorSale(BRScript.ApiUrl, BRScript.ApiKey, BRScript.ActiveShopId, true, soldItemQuantity, soldAmount, soldItemBase, self) bool result = BRTransaction.CreateFromVendorSale(BRScript.ApiUrl, BRScript.ApiKey, BRScript.ActiveShopId, true, soldItemQuantity, soldAmount, soldItemBase, self)
if !result if !result
Debug.MessageBox("Failed to sell merchandise.\n\n" + BRScript.BugReportCopy) Debug.MessageBox("Failed to sell merchandise.\n\n" + BRScript.BugReportCopy)
endif endif
soldAmount = 0
soldItemBase = None
soldItemRef = None
soldItemQuantity = 0
else else
debug.Trace("BRMerchChestScript MaybeCreateTransaction sale not yet finalized") debug.Trace("BRMerchChestScript MaybeCreateTransaction sale not yet finalized")
endif endif
@ -132,23 +138,59 @@ event OnCreateMerchandiseSuccess(bool created)
endif endif
endEvent endEvent
event OnCreateMerchandiseFail(string error) event OnCreateMerchandiseFail(bool isServerError, int status, string title, string detail, string otherError)
Debug.Trace("BRMerchChestScript OnCreateMerchandiseFail error: " + error)
BRQuestScript BRScript = BRQuest as BRQuestScript 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 endEvent
event OnCreateTransactionSuccess(int id, int quantity, int amount) event OnCreateTransactionSuccess(int id, int quantity, int amount)
debug.Trace("BRMerchChestScript OnCreateTransactionSuccess id: " + id + " quantity: " + quantity + " amount: " + amount) debug.Trace("BRMerchChestScript OnCreateTransactionSuccess id: " + id + " quantity: " + quantity + " amount: " + amount)
ObjectReference merchRef = self.GetLinkedRef(BRLinkItemRef) ObjectReference merchRef = self.GetLinkedRef(BRLinkItemRef)
RefreshMerchandise() RefreshMerchandise()
pendingTransaction = false
boughtAmount = 0
boughtItemBase = None
boughtItemRef = None
boughtItemQuantity = 0
soldAmount = 0
soldItemBase = None
soldItemRef = None
soldItemQuantity = 0
endEvent endEvent
; TODO: gracefully handle expected error cases (e.g. someone else buys all of item before this player can buy them) ; TODO: gracefully handle expected error cases (e.g. someone else buys all of item before this player can buy them)
Event OnCreateTransactionFail(string error) Event OnCreateTransactionFail(bool isServerError, int status, string title, string detail, string otherError)
Debug.Trace("BRMerchChestScript OnCreateTransactionFail error: " + error)
BRQuestScript BRScript = BRQuest as BRQuestScript 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() RefreshMerchandise()
endEvent endEvent
@ -168,10 +210,15 @@ event OnLoadMerchandiseSuccess(bool result)
endWhile endWhile
endEvent endEvent
event OnLoadMerchandiseFail(string error) event OnLoadMerchandiseFail(bool isServerError, int status, string title, string detail, string otherError)
Debug.Trace("BRMerchChestScript OnLoadMerchandiseFail error: " + error)
BRQuestScript BRScript = BRQuest as BRQuestScript 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 endEvent
event OnLoadShelfPageSuccess(bool result) event OnLoadShelfPageSuccess(bool result)
@ -185,10 +232,15 @@ event OnLoadShelfPageSuccess(bool result)
endWhile endWhile
endEvent endEvent
event OnLoadShelfPageFail(string error) event OnLoadShelfPageFail(bool isServerError, int status, string title, string detail, string otherError)
Debug.Trace("BRMerchChestScript OnLoadShelfPageFail error: " + error)
BRQuestScript BRScript = BRQuest as BRQuestScript 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 endEvent
event OnRefreshShopGoldSuccess(int gold) event OnRefreshShopGoldSuccess(int gold)
@ -200,8 +252,13 @@ event OnRefreshShopGoldSuccess(int gold)
endif endif
endEvent endEvent
event OnRefreshShopGoldFail(string error) event OnRefreshShopGoldFail(bool isServerError, int status, string title, string detail, string otherError)
Debug.Trace("BRMerchChestScript OnRefreshShopGoldFail error: " + error)
BRQuestScript BRScript = BRQuest as BRQuestScript 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 endEvent

View File

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