Using Coupons and Saving Items
1. Write a logic for the method of using coupons and saving data
Add content to the coupon method in BackndCoupon.cs written in Pre-arrangements.
BackndCoupon.cs
Before editing
public void CouponUse(string couponNumber) {
// Step 3. Using Coupons and Saving Items
}
After editing
public void CouponUse(string couponNumber) {
var bro = Backnd.Coupon.UseCoupon(couponNumber);
if(bro.IsSuccess() == false) {
Debug.LogError("An error occurred while using the coupon. : " + bro);
return;
}
Debug.Log("Coupon successfully used. : " + bro);
if(BackndGameData.userData == null) {
BackndGameData.Instance.PlayerTableGet();
}
if(BackndGameData.userData == null) {
BackndGameData.Instance.PlayerTableInsert();
}
if(BackndGameData.userData == null) {
Debug.LogError("userData does not exist.");
return;
}
foreach(BACKND.LitJson.JsonData item in bro.GetFlattenJSON()["itemObject"]) {
if(item["item"].ContainsKey("itemType")) {
int itemId = int.Parse(item["item"]["itemId"].ToString());
string itemType = item["item"]["itemType"].ToString();
string itemName = item["item"]["itemName"].ToString();
int itemCount = int.Parse(item["itemCount"].ToString());
if(BackndGameData.userData.inventory.ContainsKey(itemName)) {
BackndGameData.userData.inventory[itemName] += itemCount;
} else {
BackndGameData.userData.inventory.Add(itemName, itemCount);
}
} else {
Debug.LogError("Unsupported item.");
}
}
BackndGameData.Instance.PlayerTableUpdate();
}
2. Copy the coupon code from BACKND Console
Go to BACKND Console, and in BACKND Base > Coupon Management
, click the created coupon to copy the code of an unused coupon.
3. Add method call to BackndManager.cs
BackndManager, which is automatically called when the game is executed, must be used to call this method.
Add it so that the method may be called after BACKND initialization and BACKND login.
Paste the coupon code copied in Step 2 as the parameter value for CouponUse.
Example: "Coupon Code" -> "f9ffeed2c882bc1418"
BackndManager.cs
Before editing
async void Test() {
await Task.Run(() => {
BackndLogin.Instance.CustomSignIn("user1", "1234");
// Adds logic for using coupons
Debug.Log("Test complete.");
});
}
After editing
async void Test() {
await Task.Run(() => {
BackndLogin.Instance.CustomSignIn("user1", "1234");
// [Change required] Change the coupon code to the value created in BACKND Console > Coupon Management > Test Coupon.
BackndCoupon.Instance.CouponUse("Coupon Code"); // Example: "Coupon Code" -> "f9ffeed2c882bc1418"
Debug.Log("Test complete.");
});
}
4. Test in Unity
After editing the script, execute Unity debugging and check the console log of Unity.
The method has been successfully called when the log displays 'Successfully modified game information data. : statusCode : 204'.
When errors, such as statusCode : 400, 404, and 409, occur instead of the above log, you can check which errors caused the failure in UseCoupon error case.
5. Check the coupon use status in the console
When a coupon is successfully used, the use status of the coupon is changed to Used.
6. Check whether the data has been changed in Game Information Management
In this example, the game information data changes because the provided item's data is saved after using the coupon.
Go to BACKND Base > Game Information Management
and click the corresponding table to check that the data has been changed.