当前位置:网站首页>Using cloud DB to build apps quick start - quick games

Using cloud DB to build apps quick start - quick games

2022-06-11 15:47:00 Gauss squirrel Club

Fast game data will be stored on the cloud side , Data is not cached locally . When performing data management operations , You will directly operate cloud side data . Play fast SDK It will guarantee the communication and communication security between your application and cloud database .

Use Cloud DB Build fast games , The following preparations need to be completed :

Enable service

Use Cloud DB Before service , You need to enable the service first .

  1. Sign in AppGallery Connect Website , choice “ My project ”.
  2. Select the item in the item list page , Click the application that needs to enable cloud database service under the project .
  3. Select... From the navigation tree “ structure > Cloud database ”.
  4. single click “ Open now ”, Open Cloud database service .
  5. ( Optional ) If you have not selected a data processing location , You need to set the data processing location first , Please refer to Set the data processing location .

  6. After the service is initialized successfully , That is, the cloud database service is enabled successfully .

Add and export object types

You need to be based on AppGallery Connect Console creates object types , Please follow the steps to create the object types involved in the example , And export... For fast game development json Format and js Format object type file . It is not allowed to modify the exported json Format and js Format file , Otherwise, the data synchronization function will be abnormal .

  1. Sign in AppGallery Connect Website , choice “ My project ”.
  2. Select the item in the item list page , Click the application that needs to create the object type under the project .
  3. Select... From the navigation tree “ structure > Cloud database ”.
  4. single click “ newly added ”, Enter the create object type page .

  5. Enter an object type named “BookInfo” after , single click “ next step ”.
  6. single click

    , After adding the following fields , single click “ next step ”.

    surface 1  Field definition table

    Field name

    type

    Primary key

    Non empty

    encryption

    The default value is

    id

    Integer

    bookName

    String

    author

    String

    price

    Double

    publisher

    String

    publishTime

    Date

    shadowFlag

    Boolean

    true

  7. single click , Set the index name to “bookName”, The index field is “bookName” after , single click “ next step ”.
  8. Set the permissions of each role according to the following requirements , single click “ next step ”.

    surface 2  Permission configuration table

    role

    query

    upsert

    delete

    All the people

    Authenticated user

    Data creator

    Administrators

  9. single click “ determine ”.

    After creation, return to the object type list , You can view the object types that have been created .

  10. single click “ export ”.

  11. export “json Format ” and “js Format ” file , The exported file is used to add to the local development environment in subsequent steps .
    • export json Format file
      1. choice “json Format ”.
      2. single click “ export ”.
    • export js Format file
      1. choice “js Format ”.
      2. choice js file type , choice “quickGame”.
      3. single click “ export ”.

 

New storage area

You can be based on AppGallery Connect The console creates a data store on the cloud side , Please follow the steps to create a storage area named “QuickStartDemo” The storage area of .

  1. Sign in AppGallery Connect Website , choice “ My project ”.
  2. Select the item in the item list page , Click the application that needs to create a storage area under the project .
  3. Select... From the navigation tree “ structure > Cloud database ”.
  4. choice “ Storage area ” Tab .
  5. single click “ newly added ”, Enter the create storage page .

     

  6. The input store name is “QuickStartDemo”.
  7. single click “ determine ”.

    After creation, return to the storage area list , You can view the created storage .

Configure the development environment

  1. open cocos creator, An example of its directory is as follows .

  2. Configure application information .
    1. Sign in AppGallery Connect Website , choice “ My project ”.
    2. Select the item in the item list page , Click apply... Under project .
    3. choice “ routine ” Tab , Download profile “agconnect-services.json”, And copy it to your fast game project assets/Script Under the table of contents , It is recommended to create a separate directory to store .
    4. stay function Add... To the file agconnect-services.json File reference .
      import * as context from './agconnect-services.json';

  3. Integrate Play fast SDK. Download the fast game of fast cloud database service SDK, And will SDK js The script is imported into... In the project assets/Script Under the table of contents , It is recommended to create a separate directory to store . And put all SDK js Script settings “ Import as plug-in ”, The specific steps are: :
    1. Click Select... In Explorer js Script .
    2. Check... In the property inspector " Import as plug-in ".
    3. Click... In the upper right corner of the property inspector “ application ” Button .

Add object type file

When developing applications , You can directly put AppGallery Connect Exported from the console json Format and js Add the format file to the local development environment , And pass AGConnectCloudDB Class createObjectType() Method to define and create object types . When you are developing local applications , There is no need to create the object type again .

  1. Will already be in AppGallery Connect All exported on the console json Format and js Add the format file to the local development environment .
  2. initialization Cloud DB, adopt AGConnectCloudDB Class createObjectType() Method to define and create object types , For details, see initialization .

initialization

After adding the object type file , You can use cloud database for application development . When you develop an application , You need to perform initialization first , initialization AGConnectCloudDB、 establish Cloud DB zone And object types .

  1. adopt initialize() Method initialization AGConnectCloudDB.
    agconnect.cloudDB.AGConnectCloudDB.initialize(context);

  2. adopt getInstance() Method to get AGConnectCloudDB example , And use createObjectType() Create object type .
    const schema = require('./BookInfo.json');
    const agcCloudDB =  agconnect.cloudDB.AGConnectCloudDB.getInstance();
    agcCloudDB.createObjectType(schema);

  3. open Cloud DB zone.
    const config = new agconnect.cloudDB.CloudDBZoneConfig('QuickStartDemo');
    const cloudDBZone = await agcCloudDB.openCloudDBZone(config);

Write data

This section mainly introduces how to write data in the application , As shown below , Use executeUpsert() Realize data writing .

async function executeUpsert (book) {
    try {
        const cloudDBZoneResult = await cloudDBZone.executeUpsert(book);
        console.log('upsert' + cloudDBZoneResult + 'record' );
    } catch (e) {
        conso.log(e);
    }
}

View the data

Data query and sorting

adopt executeQuery() Realize asynchronous query of data .

async function executeQuery() {
    try {
        const query = agconnect.cloudDB.CloudDBZoneQuery.where(BookInfo);
        const snapshot = await cloudDBZone.executeQuery(query);
        const resultArray = snapshot.getSnapshotObjects();
        console.log(resultArray);
    } catch(e) {
        console.log(e);
    }
}

Through query and limit() Method combination , Realize the function of limiting the number of query data display ; And orderByAsc() Methods or orderByAsc() Method combination to realize the sorting function of data .

async function executeQueryWithOrder (object)  {
    const query = agconnect.cloudDB.CloudDBZoneQuery.where(BookInfo);
    if (object.name.length > 0) {
        query.equalTo('bookName', object.name);
    }
    if (parseFloat(object.minPrice) > 0) {
        query.greaterThanOrEqualTo('price', parseFloat(object.minPrice));
    }
    if (parseFloat(object.maxPrice) > 0 && parseFloat(object.maxPrice) > parseFloat(object.minPrice)) {
        query.lessThanOrEqualTo('price', parseFloat(object.maxPrice));
    }
    if (parseInt(object.bookCount) > 0) {
        query.limit(parseInt(object.bookCount));
    }
    query.orderByAsc('id');
    try {
        const snapshot = await cloudDBZone.executeQuery(query);
        console.log(snapshot.getSnapshotObjects());
        return snapshot.getSnapshotObjects();
    } catch (e) {
        console.log(e);
    }
}

That's all for this article , Thank you for reading .

原网站

版权声明
本文为[Gauss squirrel Club]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111535104797.html