use-and-save
BACKND Guidelines > 6. Implementing Coupon Functions > Step 3. Using Coupons and Saving Items
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 BackendCoupon.cs written in Pre-arrangements.
BackendCoupon.cs
Before editing
public void CouponUse(string couponNumber) {
// Step 3. Using Coupons and Saving Items
}
After editing
public void CouponUse(string couponNumber) {
var bro = Backend.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(BackendGameData.userData == null) {
BackendGameData.Instance.GameDataGet();
}
if(BackendGameData.userData == null) {
BackendGameData.Instance.GameDataInsert();
}
if(BackendGameData.userData == null) {
Debug.LogError("userData does not exist.");
return;
}
foreach(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(BackendGameData.userData.inventory.ContainsKey(itemName)) {
BackendGameData.userData.inventory[itemName] += itemCount;
} else {
BackendGameData.userData.inventory.Add(itemName, itemCount);
}
} else {
Debug.LogError("Unsupported item.");
}
}
BackendGameData.Instance.GameDataUpdate();
}
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 BackendManager.cs
BackendManager, 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"
BackendManager.cs
Before editing
async void Test() {
await Task.Run(() => {
BackendLogin.Instance.CustomLogin("user1", "1234");
// Adds logic for using coupons
Debug.Log("Test complete.");
});
}
After editing
async void Test() {
await Task.Run(() => {
BackendLogin.Instance.CustomLogin("user1", "1234");
// [Change required] Change the coupon code to the value created in BACKND Console > Coupon Management > Test Coupon.
BackendCoupon.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.