Coder Social home page Coder Social logo

layagcs's Introduction

LayaGCS ( Laya Game Chain SDK)

LayaGCS是一个基于ETH的链互SDK,内置了钱包功能,智能合约调用,web3实例,以及一些基础界面,可以非常方便的让H5游戏接入。

LayaGCS运行需要Laya Air环境(请参阅https://www.layabox.com/)

Updates

加入了签名算法库,用于签名Laya.Cloud前端的前后包关联

    LayaGCS.ethSign.verifySignature
    LayaGCS.ethSgin.sign

Installation

npm install layagcs

Hello LayaGCS

初始化

    var LayaGCS = require('layagcs');

    //初始化Laya Air环境
    var SCREEN_WIDTH = 1136;
	var SCREEN_HEIGHT = 640;
    Laya.init(SCREEN_WIDTH, SCREEN_HEIGHT,Laya.WebGL);
     
    //初始化LayaGCS
    LayaGCS.initlize({
        laya_stage_node:laya.stage,     //Laya Air根节点
        network:0                       //ETH区块链网络(0位测试网络ropstenTestNet , 1为正式网络MainNet)
        auto_load_last_account:false    //自动读取上次登入的账户
    })

使用例子

调用智能合约

    var web3 = LayaGCS.web3;
	var wei = 1000000000000000000; // 10^18
	var gooAbi = require('./gooabi');

	var gooContract = web3.eth.contract(gooAbi).at('0x57b116da40f21f91aec57329ecb763d29c1b2355');

	if(LayaGCS.get_current_account() == undefined){
		LayaGCS.show_login_ui(Laya.stage);
		return;
	}

	/*
		测试读取合约数据
	*/	
	gooContract.getGameInfo(function (err, result) {
		var time = result[0].toNumber() * 1000;
		var totalPot = result[1].toNumber() / wei;
		var totalProduction = result[2].toNumber();
		var nextSnapshot = result[3].toNumber();
		var goo = result[4].toNumber();
		var ether = result[5].toNumber() / wei;
		var gooProduction = result[6].toNumber();
		var units = result[7];
		var upgrades = result[8];

		console.log('时间',time,"总pot",totalPot,"总生产",totalProduction,'下次快照',nextSnapshot,'Goo',goo,'Ether',ether.toFixed(8),'gooProduction',gooProduction)
		
	});

在调用写操作智能合约时,请务必保证LayaGCS.web3.eth.defaultAccount不为空

Documentation

以下为LayaGCS的一些基础API,文档不断更新,详情关注 Laya.one 官网

LayaGCS.initlize()

初始化SDK以及设置LayaGCS以哪种ETH网络工作,同时会少量加载一些LayaGCS的资源,此处必须传入 LayaAir的根节点

    LayaGCS.initlize({
        laya_stage_node:laya.stage,     //Laya Air根节点
        network:0                       //ETH区块链网络(0位测试网络ropstenTestNet , 1为正式网络MainNet)
        auto_load_last_account:false    //自动读取上次登入的账户
    })

初始化成功后,游戏会出现LayaGCS的浮层游标,如图

LayaGCS.get_current_account

得到当前ETH账户地址

LayaGCS.set_inited_callback

设定加载完成回调

LayaGCS.show_login_ui

弹出账号生成导入界面,在LayaGCS没有defualt_account时,会弹出导入界面,不然则弹出账户主页面

if(LayaGCS.get_current_account() == undefined){
    LayaGCS.show_login_ui(Laya.stage);
    return;
}

LayaGCS.web3

这是一个完整的web3实例。LayaGCS的web3有一些改动。

由于LayaOne提供了一个全节点,所以游戏前端无需同步区块数据

为了更好的游戏体验,LayaGCS.web3不再提供同步方法,例如

var web3 = LayaGCS.web3
//原来同步的写法
var balance = web3.eth.getBalance(LayaGCS.get_default_account()); //同步的写法,LayaGCS不再支持
//异步调用方法co
co(function*(){
    var balance = yield function(done){
        web3.eth.getBalance(LayaGCS.get_default_account(),done)
    }

    console.log('账户余额为',balalnce)
})
//同样也支持async/await
//await web3.eth.getBlance() ...

LayaGCS.web3会拦截所有的method为eth_sendTransaction的异步方法,并自动为用户弹出交易确认界面

还是以etherGoohttps://ethergoo.io/game/ 这个游戏举例,我们做一个白色按钮调用其合约【购买基础单位】

    /*
		测试调用合约函数 - 购买基础单位
    */
    function test_call_contract(){
        gooContract.buyBasicUnit(1,1,function(err,res){
            if(err){
                console.log('购买失败',err);
            }
	    })
    }
	

当用户点击确认交易后,LayaGCS会将本次Transaction签名后,以sendRawTransaction发送至eth节点,然后由节点广播

相关合约您可以查阅0x57b116da40f21f91aec57329ecb763d29c1b2355

GCS 运行截图

其他结构

LayaGCS是整个Laya.one环节中的一部分

LayaOne由Laya.cloud, laya.chain, laya.air, laya.maker等多个大模块组成

使用LAYA GSC开发的游戏结构

Laya IDE

Laya IDE集成了Truffle.js,可以非常方便的创建和部署智能合约,并提供了相应的智能合约模板

LayaIDE界面

创建游戏(合约)

编译合约

校验签名

部署合约

关于Laya.One

IDE的发布以及更多模块请关注https://laya.one以及https://layabox.com

layagcs's People

Contributors

xhacker5000 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.