微信小游戏实战--cocos creator实现wordle游戏(七)

这篇接着实现玩家战绩统计功能,使用了微信云数据库(主要是图方便和便宜)。微信云数据库本质上来说算是MongoDB数据库,是一种非关系型数据库。打开微信开发者工具,登陆以后点击云开发:

完整代码

微信云数据库

在“云开发”首页会显示“云函数”,“云数据库”等相关云资源的使用情况。右上角有一个“环境ID”,开发时会用到。点击“数据库”:

在“数据库”页面就可以进行数据库的创建等操作了:

这里简单介绍一下云数据库的几个基本概念:

1、集合:可以理解为就是一张“表”,对应关系型数据库中的Table。

2、记录:可以理解为“行”,对应关系型数据库中的Row。

3、字段:这个和关系型数据库中的Field一样。

在“集合名称”那里点击“+”创建一个“score”集合用于存放玩家的战绩信息,字段可以不用先定义只需要在添加记录的时候把需要的字段传进去就行了(这一点和sqlserver、mysql等关系型数据库不一样)。通过调用微信云数据库API添加的记录会默认生成“_id”,“_openid”两个字段,这两个字段我们不需要维护,也不允许维护。

玩家战绩设计

根据游戏特点,除了基本的“总局数”,“胜利局数”,“当前连胜”,“最大连胜”以外再额外添加了每一步猜中的局数。新建userScore.ts,代码如下:

export class userScore {
    public static _id = '1';
    public static _openid = '1';
    public static userName = '';//用户名称
    public static avatarUrl = '';//用户头像Url
    public static totalcount = 0; //总局数
    public static wincount = 0;//胜利局数
    public static step1 = 0;//第一步猜中局数
    public static step2 = 0;//第二步猜中局数
    public static step3 = 0;//第三步猜中局数
    public static step4 = 0;//第四步猜中局数
    public static step5 = 0;//第五步猜中局数
    public static step6 = 0;//第六步猜中局数
    public static cstraitwin = 0;//当前连胜
    public static mstraightwin = 0; //最大连胜

    public static setScore (data:any){
        if(!data){
            return;
        }
        
        if (typeof(data._id) !== 'undefined') {
            console.log('setScore data._id:' + data._id);
            this._id = data._id;
        }
        if (typeof(data._openid) !== 'undefined') {
            console.log('setScore data._openid:' + data._openid);
            this._openid = data._openid;
        }
        this.userName = data.userName;
        this.avatarUrl = data.avatarUrl;
        this.totalcount = data.totalcount;
        this.wincount = data.wincount;
        this.step1 = data.step1;
        this.step2 = data.step2;
        this.step3 = data.step3;
        this.step4 = data.step4;
        this.step5 = data.step5;
        this.step6 = data.step6;
        this.cstraitwin = data.cstraitwin;
        this.mstraightwin = data.mstraightwin;
    }

    public static toData(): any{
        var data = {
            userName : this.userName,
            avatarUrl : this.avatarUrl,            
            totalcount : this.totalcount,
            wincount : this.wincount,
            step1 : this.step1,
            step2 : this.step2,
            step3 : this.step3,
            step4 : this.step4,
            step5 : this.step5,
            step6 : this.step6,
            cstraitwin : this.cstraitwin,
            mstraightwin : this.mstraightwin            
        }
        return data;
    }
}

 因为在更新数据时,默认创建的“_id”,“_openid”两个字段不能操作。而更新数据需要用到“_id”字段,所以额外新增一个toData函数,生成需要更新的数据并把“_id”,“_openid”去掉。

微信云API调用封装

新建wxCloud.ts,代码如下:

import { _decorator} from 'cc';
import { userScore } from './userScore';
const { ccclass, property } = _decorator;

@ccclass('wxCloud')
export class wxCloud {//微信云api封装
    private static _instance = null;

    static get instance (){
        if (!this._instance) {
            this._instance = new wxCloud();
        }
        return this._instance;
    }

    constructor () {
        //微信云初始化
        wx.cloud.init({
            traceUser: true,
            env: 'cl
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大宝贱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值