วิธีการใช้ Data Store¶
Data Store ใช้ NoSQL Cloud Database เพื่อทำการซิงโครไนซ์และเก็บข้อมูลสำหรับการใช้งานบนคลient เช่น ส่วนประกอบของเกมและข้อมูลการตั้งค่า การใช้บริการนี้เป็นทางออกที่ดีสำหรับเกมที่อิงจากคลient หรือถ้าต้องการการอัปเดตแบบเรียลไทม์ระหว่างคลient
Data Store มีคุณสมบัติดังต่อไปนี้:
- ข้อมูลจะถูกจัดเก็บในที่เก็บแยกต่างหากสำหรับแต่ละเกม。
- ข้อมูลทั้งหมดถูกเข้ารหัสและส่งมอบอย่างปลอดภัย。
- แตกต่างจากฐานข้อมูล SQL ฐานข้อมูล NoSQL Cloud ไม่มีตารางและแถว และข้อมูลจะถูกจัดเก็บในเอกสารที่ประกอบด้วยคอลเลกชัน。
- จัดเก็บและบันทึกข้อมูลเป็นคู่ key-value。
- ปรับให้เหมาะสมสำหรับการจัดเก็บคอลเลกชันที่ประกอบด้วยเอกสารหลายฉบับ。
- สำหรับข้อควรระวังในการออกแบบ key-value ดูที่คู่มือ Operation > Game Data Store จาก Hive Developers。
เพิ่มข้อมูล¶
คุณสามารถเพิ่มข้อมูลลงใน Data Store ในรูปแบบคู่ของคีย์-ค่า หรือหลายคู่ในรูปแบบแผนที่ในครั้งเดียว หลังจากการร้องขอ จะมีการส่งกลับค่า true หรือ false
Warning
คีย์-ค่าจำเป็นต้องถูกตั้งค่าในสตริง JSON แบบหนึ่งมิติที่รวมถึงสตริงธรรมดาหรือแบ็คสแลช()
ต่อไปนี้คือตัวอย่างโค้ดเพื่อเพิ่มข้อมูลของคู่คีย์-ค่า。
API Reference: DataStore .set
#include "HiveDataStore.h"
FString Key = TEXT("your data key");
FString Value = TEXT("your data value");
FHiveDataStore::Set(Key, Value, FHiveDataStoreOnSetDelegate::CreateLambda([this](const FHiveResultAPI& Result) {
        if (Result.IsSuccess()) {
                // API call successful 
        } else {
                // Call failed. See error code below
        }
}));
API Reference: DataStore ::set
#include <HIVE_SDK_Plugin/HIVE_CPP.h>    
    using namespace std;    
    using namespace hive;    
    string key = "your data key";    
    string value = "your data value";    
    DataStore::set(key, value, [=](ResultAPI const & result) {    
         if (result.isSuccess()) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
});
API Reference: DataStore.set
import com.hive.DataStore    
    import com.hive.ResultAPI    
    val key = "your data key"    
    val value = "your data value"    
    DataStore.set(key, value, object : DataStore.DataStoreSetListener {    
         override fun onDataStoreSet(result: ResultAPI) {    
             if (result.isSuccess) {    
                 // call successful    
             } else {    
                 // Call failed. See error code below    
             }    
         }    
})
API Reference: DataStore.INSTANCE .set
API Reference: DataStoreInterface.set
API Reference: HIVEDataStore set
ต่อไปนี้คือตัวอย่างโค้ดในการเพิ่มข้อมูลในรูปแบบแผนที่.
API Reference: DataStore .set
#include "HiveDataStore.h"
TMap<FString, FString> Data;
Data.Emplace(TEXT("key1"), TEXT("value1"));
Data.Emplace(TEXT("key2"), TEXT("value2"));
Data.Emplace(TEXT("key3"), TEXT("value3"));
FHiveDataStore::Set(Data, FHiveDataStoreOnSetDelegate::CreateLambda([this](const FHiveResultAPI& Result) {
        if (Result.IsSuccess()) {
                // call successfull
        } else {
                // Call failed. See error code below
        }
}));
API Reference: DataStore ::set
#include <HIVE_SDK_Plugin/HIVE_CPP.h>    
    using namespace std;    
    using namespace hive;    
    map<string, string> map = {    
         {"key1", "value1"},    
         {"key2", "value2"},    
         {"key3", "value3"}    
    };    
    DataStore::set(map, [=](ResultAPI const & result) {    
         if (result.isSuccess()) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
});
API Reference: DataStore.set
import com.hive.DataStore    
    import com.hive.ResultAPI    
    val map = mapOf(    
         "key1" to "value1",    
         "key2" to "value2",    
         "key3" to "value3",    
    )    
    DataStore.set(map, object : DataStore.DataStoreSetListener {    
         override fun onDataStoreSet(result: ResultAPI) {    
             if (result.isSuccess) {    
                 // call successful    
             } else {    
                 // Call failed. See error code below    
             }    
         }    
})
API Reference: DataStore.INSTANCE .set
import com.hive.DataStore;    
    import com.hive.ResultAPI;    
    Map<String, String> map = new HashMap<>();    
    map.put("key1", "value1");    
    map.put("key2", "value2");    
    map.put("key3", "value3");    
    DataStore.INSTANCE.set(map, result -> {    
         if (result.isSuccess()) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
});
API Reference: DataStoreInterface.set
API Reference: HIVEDataStore set
#import <HIVEService/HIVEService-Swift.h>    
    NSDictionary<NSString *, NSString *> *map = @{    
         @"key1" : @"value1",    
         @"key2" : @"value2",    
         @"key3" : @"value3"    
    };    
    [HIVEDataStore set: map handler: ^(HIVEResultAPI *result) {    
         if ([result isSuccess]) {    
             // เรียกใช้งานสำเร็จ    
         } else {    
             // การเรียกใช้งานล้มเหลว ดูรหัสข้อผิดพลาดด้านล่าง    
         }    
}];
รับข้อมูล¶
มีสองวิธีในการรับข้อมูลของคุณคือแบบหนึ่งหรือทั้งหมดในครั้งเดียว และคุณยังสามารถรับข้อมูลผู้ใช้ทั้งหมดที่ตรงกับคีย์ที่คุณร้องขอ วิธีการสามอย่างในการรับข้อมูลมีดังนี้:
- รับข้อมูลของฉันหนึ่งรายการ- รับข้อมูลทั้งหมดของฉัน
- รับข้อมูลทั้งหมดโดยใช้คีย์
 
ตรวจสอบตัวอย่างโค้ดต่อไปนี้สำหรับแต่ละวิธี
ต่อไปนี้คือตัวอย่างโค้ดเพื่อดึงข้อมูลหนึ่งจากฉัน
เอกสาร API: DataStore .get
API Reference: DataStore ::get
API Reference: DataStore.get
API Reference: DataStore.INSTANCE .get
API Reference: DataStoreInterface.get
API Reference: HIVEDataStore get
ต่อไปนี้คือตัวอย่างโค้ดเพื่อดึงข้อมูลทั้งหมดของฉัน.
API Reference: DataStore .getMyData
#include "HiveDataStore.h"
FHiveDataStore::GetMyData(FHiveDataStoreOnMyDataDelegate::CreateLambda([this](const FHiveResultAPI& Result, const DataStoreData& MyData) {
        if (Result.IsSuccess()) {
                // API  call successfull
                // myData: the added key-value map  
        } else {
                // Call failed. See error code below
        }
}));
API Reference: DataStore .getMyData
#include <HIVE_SDK_Plugin/HIVE_CPP.h>    
    using namespace std;    
    using namespace hive;    
    DataStore.getMyData([=](ResultAPI const & result, map<string, string> const & myData) {    
         if (result.isSuccess()) {    
             // call successful    
             // myData: the added key-value map    
         } else {    
             // Call failed. See error code below    
         }    
});
API Reference: DataStore.getMyData
import com.hive.DataStore    
    import com.hive.ResultAPI    
    DataStore.getMyData(object : DataStore.DataStoreMyDataListener {    
         override fun onDataStoreMyData(result: ResultAPI, myData: Map<String, String>) {    
             if (result.isSuccess) {    
                 // call successful    
                 // myData: the added key-value map    
             } else {    
                 // Call failed. See error code below    
             }    
         }    
})
API Reference: DataStore.INSTANCE .getMyData
API Reference: DataStoreInterface .getMyData
API Reference: HIVEDataStore getMyData
ตัวอย่างโค้ดด้านล่างนี้ใช้เพื่อดึงข้อมูลทั้งหมดโดยใช้คีย์
API Reference: DataStore .getUsersData
using hive;    
    string key = "your data key";    
    DataStore.getUsersData(key, (ResultAPI result, string key, Dictionary<long, string> usersData) => {    
         if (result.isSuccess()) {    
             // call successful    
             // key: Entered key    
             // usersData: Map in PlayerId-value format. Be careful about the long key type.    
         } else {    
             // Call failed. See error code below    
         }    
});
#include "HiveDataStore.h"
FString Key = TEXT("your data key");
FHiveDataStore::GetUsersData(Key, FHiveDataStoreOnUsersDataDelegate::CreateLambda([this](const FHiveResultAPI& Result, const FString& Key, const DataStoreUsersData& UsersData) {
        if (Result.IsSuccess()) {
                // call successful    
      // key: Entered key    
      // usersData: Map in PlayerId-value format. Be careful about the long key type.의
        } else {
                // Call failed. See error code below
        }
}));
API Reference: DataStore ::getUsersData
#include <HIVE_SDK_Plugin/HIVE_CPP.h>    
    using namespace std;    
    using namespace hive;    
    string key = "your data key";    
    DataStore::getUsersData(key, [=](ResultAPI const & result, string key, map const & usersData) {    
         if (result.isSuccess()) {    
             // call successful    
             // key: Entered key    
             // usersData: Map in PlayerId-value format. Be careful that the key is of long long type.    
         } else {    
             // Call failed. See error code below    
         }    
});
เอกสาร API: DataStore.getUsersData
import com.hive.DataStore    
    import com.hive.ResultAPI    
    val key = "your data key"    
    DataStore.getUsersData(key, object : DataStore.DataStoreUsersDataListener {    
         override fun onDataStoreUsersData(result: ResultAPI, key: String?, usersData: Map<Long, String>) {    
             if (result.isSuccess) {    
                 // การเรียกสำเร็จ    
                 // key: คีย์ที่ป้อน    
                 // usersData: แผนที่ในรูปแบบ PlayerId-value โปรดระวังเกี่ยวกับประเภท long.    
             } else {    
                 // การเรียกล้มเหลว ดูรหัสข้อผิดพลาดด้านล่าง    
             }    
         }    
})
API Reference: DataStore.INSTANCE .getUsersData
import com.hive.DataStore;    
    import com.hive.ResultAPI;    
    String key = "your data key";    
    DataStore.INSTANCE.getUsersData(key, (result, key1, usersData) -> {    
         if (result.isSuccess()) {    
             // call successful    
             // key: Entered key    
             // usersData: Map in PlayerId-value format. Be careful about the long type.    
         } else {    
             // Call failed. See error code below    
         }    
});
API Reference: DataStoreInterface.getUsersData
import HIVEService    
    let key = "your data key"    
    DataStoreInterface.getUsersData(key) { result, key, usersData in    
        if result.isSuccess() {    
        // call successful    
        // key: Entered key    
        // usersData: Map in PlayerId-value format. Note that the key is of type Int64.    
        } else {    
        // Call failed. See error code below    
        }    
}
API Reference: HIVEDataStore getUsersData
#import <HIVEService/HIVEService-Swift.h>    
    NSString* key = @"your data key";    
    [HIVEDataStore getUsersData: key handler: ^(HIVEResultAPI *result, NSString *key, NSDictionary<NSNumber *, NSString *> *usersData) {    
         if ([result isSuccess]) {    
             // success    
             // key: Entered key    
             // usersData: Map in PlayerId-value format. Note that the key is of type NSNumber *.    
         } else {    
             // Call failed. See error code below    
         }    
}];
รหัสข้อผิดพลาด¶
| รหัสข้อผิดพลาด | ข้อความ | คำอธิบาย | 
|---|---|---|
| RESPONSE_FAIL | DataStoreNotExistKey | ไม่มีคีย์ในเซิร์ฟเวอร์ | 
| RESPONSE_FAIL | DataStoreNotExistColumn | ไม่มี Column Family (ตาราง) ในเซิร์ฟเวอร์ | 
| RESPONSE_FAIL | DataStoreGameIsBeingInspected | DataStore ของเกมอยู่ระหว่างการบำรุงรักษา | 
| DEVELOPER_ERROR | DataStoreNotExistPublicKey | ไม่มีคีย์สาธารณะ ต้องตรวจสอบการตั้งค่าใน Hive Console. | 
| NEED_INITIALIZE | DataStoreNotInitialized | ยังไม่ได้ตั้งค่า SDK (AuthV4.setup) | 
| INVALID_SESSION | DataStoreNeedSignIn | ยังไม่ได้ลงชื่อเข้าใช้ ต้องลงชื่อเข้าใช้. | 
| DEVELOPER_ERROR | DataStoreDisabled | ตั้งค่า Data Store เป็น ไม่ใช้. ต้องตรวจสอบการตั้งค่าใน Hive Console. | 
| RESPONSE_FAIL | DataStoreResponseError | เชื่อมต่อกับเซิร์ฟเวอร์สำเร็จ แต่มีข้อผิดพลาดกลับมา ต้องตรวจสอบข้อความข้อผิดพลาด. | 
| INVALID_PARAM | DataStoreInvalidParam | ล้มเหลวในการเรียกใช้ API set()เนื่องจากพารามิเตอร์ไม่ถูกต้อง |