游戏领域区块链应用探索

简介: 如何将区块链嫁接到游戏领域,我做了很多思考,经过分析总结,发现下面几项内容非常适合上链。

游戏领域区块链探索

本文节选自电子书《Netkiller Blockchain 手札》

Netkiller Blockchain 手札

本文作者最近在找工作,有意向致电 13113668890

Mr. Neo Chan, 陈景峯(BG7NYT)

中国广东省深圳市龙华新区民治街道溪山美地 518131 +86 13113668890 <netkiller@msn.com>

文档始创于2018-02-10

版权 © 2018 Netkiller(Neo Chan). All rights reserved.

版权声明

转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。

http://www.netkiller.cnhttp://netkiller.github.iohttp://netkiller.sourceforge.net

http://www.netkiller.cn

http://netkiller.github.io

http://netkiller.sourceforge.net

微信订阅号 netkiller-ebook (微信扫描二维码)QQ:13721218 请注明“读者”QQ群:128659835 请注明“读者”

微信订阅号 netkiller-ebook (微信扫描二维码)

QQ:13721218 请注明“读者”

QQ群:128659835 请注明“读者”

http://www.netkiller.cn

http://netkiller.github.io

http://netkiller.sourceforge.net

微信订阅号 netkiller-ebook (微信扫描二维码)

QQ:13721218 请注明“读者”

QQ群:128659835 请注明“读者”

$Data$

内容摘要

这一部关于区块链开发及运维的电子书。

为什么会写区块链电子书?因为2018年是区块链年。

这本电子书是否会出版(纸质图书)? 不会,因为互联网技术更迭太快,纸质书籍的内容无法实时更新,一本书动辄百元,很快就成为垃圾,你会发现目前市面的上区块链书籍至少是一年前写的,内容已经过时,很多例子无法正确运行。所以我不会出版,电子书的内容会追逐技术发展,及时跟进软件版本的升级,做到内容最新,至少是主流。

这本电子书与其他区块链书籍有什么不同?市面上大部分区块链书籍都是用2/3去讲区块链原理,只要不到 1/3 的干货,干货不够理论来凑,通篇将理论或是大谈特谈区块链行业,这些内容更多是头脑风暴,展望区块链,均无法落地实施。本书与那些书籍完全不同,不讲理论和原理,面向应用落地,注重例子,均是干货。

电子书更新频率?每天都会有新内容加入,更新频率最迟不会超过一周,更新内容请关注 https://github.com/netkiller/netkiller.github.io/commits/master

本文采用碎片化写作,原文会不定期更新,请尽量阅读原文。

http://www.netkiller.cn/blockchain/index.html

您的打赏是我的写作动力:http://www.netkiller.cn/blockchain/donations.html

-------------------------

33.4. 游戏领域区块链探索

如何将区块链嫁接到游戏领域,我做了很多思考,经过分析总结,发现下面几项内容非常适合上链。

上链内容

  • 积分代币 如果说区块链应用于游戏领域,可能99%的人首先会想到是代币,的确游戏领域实施区块链连,代币必不可少。但是区块链不等于代币。
  • 游戏装备
  • 人物属性
  • 关卡任务

下面我们要思考为什么需要将游戏数据放到区块链上,玩游戏的人都知道私服,私人架设游戏服务器,私服上玩游戏遇到最大的问题就是公平性。管理员可以随意调整服务器参数。

私服存在哪些问题呢?

  • 修改游戏装备属性
  • 修改生命与魔法值
  • 关卡参数
  • 人物属性
  • 随意封账号

这是我们在私服上遇到的最大问题,那么官方服务器就公平吗?不一定,对于弱势的玩家只能相信游戏公司的承诺。

有了区块链技术,我们能做什么呢?例如我们将用户装备数据等数据上链,这样保证了装备永远属于玩家

区块链能做什么?

  • “点” 奖励采用代币实现,可以实现流通,兑换,消费等等......
  • 爆出装备立即上链
  • 用户等级属性上链
  • 用户状态上链
  • 关卡数据上链

了凸显公平性,我们采用公链,查询用户数据可以使用接口,也可以直接到公链上查询。

下面详细讲解具体怎么实现。

33.4.1. 游戏代币

传统币 Point (点) 仅仅是一个数字,数字存在数据库中,例如

Username	| Point (Integer)
-----------------------
Neo		| 1000
Jam		| 500

因为仅仅是一个数字,管理员可以随意修改,黑客也可随意修改,例如

update member set point = 1000000000000 where username = 'Neo'	

瞬间就有 1000000000000 点。由于是在数据库中修改,没有日志,不知道谁操作的,可能是开发人员,可以是管理员,也可能是黑客。

如何消费“点呢”,例如消费 100 个点:

update member set point = point - 100 where username = 'Neo'

传统币“点”,只是一个数字做加法和减法运算,安全性主要依赖于开发团队的能(期望别出BUG),运维团队的能力(被别黑客攻击),以及DBA(数据库管理员)的节操。

审计全靠开发人员打印出的日志。

现在我们再看看数字货币,跟很多朋友聊天中发现,他们还没有理解什么是币,他们认为数字代币花掉就没了(消失了),然后通过挖矿不停的产生新币,这种理解完全错误。

数字币是这样运作的,首先发行时设置了币的总量例如 1000000,然后将所有代币存入发行者账号,例如 account 1

account			| coin
---------------------------------
account1     	| 1000000
account2     	| 0
account3     	| 0
account4     	| 0
account5     	| 0				

现在 account2 游戏在线1小时奖励 10 个币,系统从账号account1转账5个币给 account2

account			| coin
---------------------------------
account1     	| 999990
account2     	| 10
account3     	| 0
account4     	| 0
account5     	| 0				

以此类推,从 account1 转账给其他账号。

account			| coin
---------------------------------
account1     	| 999960
account2     	| 10
account3     	| 10
account4     	| 10
account5     	| 10			

现在 account3 消费 5个币买了装备。从 account3 转 5 个币到 account1

account			| coin
---------------------------------
account1     	| 999965
account2     	| 10
account3     	| 5
account4     	| 10
account5     	| 10			

现在你应该看懂了把,代币是流通的,总量是不变的。account1 账号负责币的发行,回收等等工作。

同时任何转账将产生区块,历史数据永久记录。

下面是一个高级代币合约,地址https://github.com/ibook/NetkillerAdvancedToken

pragma solidity ^0.4.20;

/******************************************/
/*       Netkiller ADVANCED TOKEN         */
/******************************************/
/* Author netkiller <netkiller@msn.com>   */
/* Home http://www.netkiller.cn           */
/* Version 2018-03-05                     */
/* Version 2018-03-06 - Add Global lock   */
/******************************************/

interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; }

contract NetkillerAdvancedToken {
    address public owner;
    // Public variables of the token
    string public name;
    string public symbol;
    uint8 public decimals = 2;
    // 18 decimals is the strongly suggested default, avoid changing it
    uint256 public totalSupply;
    
    uint256 public sellPrice;
    uint256 public buyPrice;

    // This creates an array with all balances
    mapping (address => uint256) public balanceOf;
    mapping (address => mapping (address => uint256)) public allowance;

    // This generates a public event on the blockchain that will notify clients
    event Transfer(address indexed from, address indexed to, uint256 value);

    // This notifies clients about the amount burnt
    event Burn(address indexed from, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
    
    mapping (address => bool) public frozenAccount;

    /* This generates a public event on the blockchain that will notify clients */
    event FrozenFunds(address target, bool frozen);

    bool lock = true;

    /**
     * Constrctor function
     *
     * Initializes contract with initial supply tokens to the creator of the contract
     */
    function NetkillerAdvancedToken(
        uint256 initialSupply,
        string tokenName,
        string tokenSymbol
    ) public {
        owner = msg.sender;
        totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount
        balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens
        name = tokenName;                                   // Set the name for display purposes
        symbol = tokenSymbol;                               // Set the symbol for display purposes
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    modifier isLock {
        require(!lock);
	_;
    }
    
    function setLock(bool _lock) onlyOwner {
        lock = _lock;
    }

    function transferOwnership(address newOwner) onlyOwner public {
        owner = newOwner;
    }
 
    /* Internal transfer, only can be called by this contract */
    function _transfer(address _from, address _to, uint _value) isLock internal {
        require (_to != 0x0);                               // Prevent transfer to 0x0 address. Use burn() instead
        require (balanceOf[_from] >= _value);               // Check if the sender has enough
        require (balanceOf[_to] + _value > balanceOf[_to]); // Check for overflows
        require(!frozenAccount[_from]);                     // Check if sender is frozen
        require(!frozenAccount[_to]);                       // Check if recipient is frozen
        balanceOf[_from] -= _value;                         // Subtract from the sender
        balanceOf[_to] += _value;                           // Add the same to the recipient
        Transfer(_from, _to, _value);
    }

    /**
     * Transfer tokens
     *
     * Send `_value` tokens to `_to` from your account
     *
     * @param _to The address of the recipient
     * @param _value the amount to send
     */
    function transfer(address _to, uint256 _value) public {
        _transfer(msg.sender, _to, _value);
    }

    /**
     * Transfer tokens from other address
     *
     * Send `_value` tokens to `_to` in behalf of `_from`
     *
     * @param _from The address of the sender
     * @param _to The address of the recipient
     * @param _value the amount to send
     */
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        require(_value <= allowance[_from][msg.sender]);     // Check allowance
        allowance[_from][msg.sender] -= _value;
        _transfer(_from, _to, _value);
        return true;
    }

    /**
     * Set allowance for other address
     *
     * Allows `_spender` to spend no more than `_value` tokens in your behalf
     *
     * @param _spender The address authorized to spend
     * @param _value the max amount they can spend
     */
    function approve(address _spender, uint256 _value) public
        returns (bool success) {
        allowance[msg.sender][_spender] = _value;
        Approval(msg.sender, _spender, _value);
        return true;
    }

    /**
     * Set allowance for other address and notify
     *
     * Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it
     *
     * @param _spender The address authorized to spend
     * @param _value the max amount they can spend
     * @param _extraData some extra information to send to the approved contract
     */
    function approveAndCall(address _spender, uint256 _value, bytes _extraData)
        public
        returns (bool success) {
        tokenRecipient spender = tokenRecipient(_spender);
        if (approve(_spender, _value)) {
            spender.receiveApproval(msg.sender, _value, this, _extraData);
            return true;
        }
    }

    /**
     * Destroy tokens
     *
     * Remove `_value` tokens from the system irreversibly
     *
     * @param _value the amount of money to burn
     */
    function burn(uint256 _value) onlyOwner public returns (bool success) {
        require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough
        balanceOf[msg.sender] -= _value;            // Subtract from the sender
        totalSupply -= _value;                      // Updates totalSupply
        Burn(msg.sender, _value);
        return true;
    }

    /**
     * Destroy tokens from other account
     *
     * Remove `_value` tokens from the system irreversibly on behalf of `_from`.
     *
     * @param _from the address of the sender
     * @param _value the amount of money to burn
     */
    function burnFrom(address _from, uint256 _value) onlyOwner public returns (bool success) {
        require(balanceOf[_from] >= _value);                // Check if the targeted balance is enough
        require(_value <= allowance[_from][msg.sender]);    // Check allowance
        balanceOf[_from] -= _value;                         // Subtract from the targeted balance
        allowance[_from][msg.sender] -= _value;             // Subtract from the sender's allowance
        totalSupply -= _value;                              // Update totalSupply
        Burn(_from, _value);
        return true;
    }

    /// @notice Create `mintedAmount` tokens and send it to `target`
    /// @param target Address to receive the tokens
    /// @param mintedAmount the amount of tokens it will receive
    function mintToken(address target, uint256 mintedAmount) onlyOwner public {
        balanceOf[target] += mintedAmount;
        totalSupply += mintedAmount;
        Transfer(0, this, mintedAmount);
        Transfer(this, target, mintedAmount);
    }

    /// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
    /// @param target Address to be frozen
    /// @param freeze either to freeze it or not
    function freezeAccount(address target, bool freeze) onlyOwner public {
        frozenAccount[target] = freeze;
        FrozenFunds(target, freeze);
    }

    /// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth
    /// @param newSellPrice Price the users can sell to the contract
    /// @param newBuyPrice Price users can buy from the contract
    function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public {
        sellPrice = newSellPrice;
        buyPrice = newBuyPrice;
    }

    /// @notice Buy tokens from contract by sending ether
    function buy() payable public {
        uint amount = msg.value / buyPrice;               // calculates the amount
        _transfer(this, msg.sender, amount);              // makes the transfers
    }

    /// @notice Sell `amount` tokens to contract
    /// @param amount amount of tokens to be sold
    function sell(uint256 amount) public {
        require(this.balance >= amount * sellPrice);      // checks if the contract has enough ether to buy
        _transfer(msg.sender, this, amount);              // makes the transfers
        msg.sender.transfer(amount * sellPrice);          // sends ether to the seller. It's important to do this last to avoid recursion attacks
    }
    
  function transfer(address _to, uint256 _value, bytes _data) public returns (bool) {
    require(_to != address(this));
    transfer(_to, _value);
    require(_to.call(_data));
    return true;
  }

  function transferFrom(address _from, address _to, uint256 _value, bytes _data) public returns (bool) {
    require(_to != address(this));

    transferFrom(_from, _to, _value);

    require(_to.call(_data));
    return true;
  }

  function approve(address _spender, uint256 _value, bytes _data) public returns (bool) {
    require(_spender != address(this));

    approve(_spender, _value);

    require(_spender.call(_data));

    return true;
  }
    
}

这个代币合约实现了,增发,减持,全局锁,账号冻结/解冻 等等功能。

33.4.2. 玩家属性与游戏装备

下面的合约实现了游戏玩家上链,上链信息有玩家属性,例如肤色,眼睛,头发,血统等等。身上的穿戴物品包括武器等等。

pragma solidity ^0.4.20;

contract Player {

    address public owner;
    
    string name;
    bool lock = false;	//合约锁
    uint number = 1;
    uint attr_number = 1;
    
    mapping (address  => string) guestbook; //客户留言本	

	struct Attribute {
        string key;		// 属性的名字
        string value;	// 属性值
        
    }
    mapping (uint  => Attribute) attribute;

    struct Wear {
        string name;        // 装备名
        string desciption;  // 信息
        string attribute;   // 属性,存储json 数据。例如生命+10,魔法+5,冰冻系...

    }
    mapping (uint  => Wear) wear;
    
    function Player(string _name) public {
        name = _name;
	}
	
	modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }
	
    // 名称
    function getName() public view returns(string){
        return name;
    }
    function setLock(bool _lock) onlyOwner public {
        lock = _lock;
    }
     // 增加人物属性,例如肤色,眼睛,头发等等
    function putAttribute(string _key, string _value) onlyOwner public{
        if(lock == false){
        		Attribute memory item = Attribute(_key, _value);
        		attribute[attr_number] = item;
        		attr_number = attr_number + 1;
        }
    }

	// 获得属性
    function getAttribute(uint _attr_number) public view returns(string, string) {
        require(_attr_number < attr_number);
        Attribute memory item = attribute[_attr_number];
        
		return (item.key, item.value);
	}
    
    // 增加装备信息,穿戴物品,武器,
    function putWear(string _name, string _description, string _attribute ) onlyOwner public{
        if(lock == false){
            Wear memory node = Wear(_name,_description,_attribute);
            wear[number] = node;
            number = number + 1;
            lock = true;
        }
    }

	// 获得信息
    function getWear(uint _number) public view returns(string, string, string) {
        require(_number < number);

        Wear memory item = wear[_number];
        
		return (item.name, item.desciption, item.attribute);
	}
	
	// 数量
	function getWearCount() public view returns(uint){
	    return number;
	}
    
    // 客户留言
    function addGuestbook(address _owner, string message) onlyOwner public{
	    guestbook[_owner] = message;
	}
}		
目录
相关文章
|
23天前
|
存储 传感器 监控
未来智能城市中的区块链技术应用
随着城市化进程不断加速,智能城市成为了未来城市发展的主要趋势之一。区块链技术作为一种去中心化、安全可靠的数据传输和存储方式,将在智能城市建设中发挥关键作用。本文将探讨区块链技术在智能城市中的应用场景,并分析其对城市管理、信息共享和安全保障等方面的积极影响。
14 2
|
30天前
|
存储 供应链 区块链
区块链技术在供应链管理中的应用与展望
随着区块链技术的不断发展,其在供应链管理领域的应用愈发广泛。本文将深入探讨区块链技术在供应链管理中的具体应用及未来发展趋势,分析其对供应链透明度、可追溯性和安全性的影响,并展望区块链技术在未来供应链管理中的潜在作用和挑战。
20 0
|
1月前
|
供应链 安全 区块链
区块链技术在供应链管理中的应用与挑战
传统的供应链管理面临着信息不对称、数据共享困难等问题,而区块链技术的出现为供应链管理带来了新的解决方案。本文将介绍区块链技术在供应链管理中的应用情况,探讨其优势和挑战,并提出未来发展的方向。
48 2
|
1月前
|
存储 人工智能 供应链
区块链技术在供应链管理中的应用
传统的供应链管理系统存在着诸多问题,如信息不对称、数据可信度低等。区块链技术作为一种分布式账本技术,在供应链管理中具有独特优势。本文将探讨区块链技术在供应链管理中的应用,介绍其原理和优势,并分析实际案例以及未来发展趋势。
|
1月前
|
存储 传感器 物联网
新技术驱动未来:区块链、物联网、虚拟现实的蓬勃发展与创新应用
随着科技的迅猛发展,新兴技术如区块链、物联网、虚拟现实等正成为当今社会关注的焦点。本文将探讨这些新技术的发展趋势和广泛应用场景,展示它们在不同领域中的潜力与创新价值。
|
4天前
|
存储 供应链 安全
《区块链简易速速上手小册》第7章:区块链在其他行业的应用(2024 最新版)
《区块链简易速速上手小册》第7章:区块链在其他行业的应用(2024 最新版)
20 1
|
11天前
|
供应链 安全 物联网
未来交织:区块链、物联网和虚拟现实的技术革新与融合应用
【4月更文挑战第13天】 在数字化时代的浪潮中,新兴技术正以前所未有的速度重塑着世界。本文将深入分析区块链技术的去中心化信任机制、物联网(IoT)的智能连接网络,以及虚拟现实(VR)的沉浸式体验如何独立发展并开始交汇融合,共同构建一个更加智能、安全和互动的未来。我们将探讨这些技术的发展趋势,揭示它们在各行各业的潜在应用场景,并讨论它们如何联合起来推动第四次工业革命。
|
11天前
|
供应链 算法 安全
区块链技术的应用与前景展望
区块链技术的应用与前景展望
|
13天前
|
供应链 安全 区块链
区块链技术在供应链管理中的应用及挑战
【4月更文挑战第11天】 随着数字化时代的到来,企业之间的供应链管理日益复杂化,对透明度、安全性和效率的要求也不断提高。区块链技术作为一种分布式账本技术,因其不可篡改性、去中心化以及可追溯性的特点,在供应链管理中展现出了独特的应用价值。本文将探讨区块链技术在供应链领域的具体应用案例,分析其在提高供应链透明度、降低运营成本、增强产品安全等方面的潜在优势,并讨论在实际应用过程中面临的技术挑战和业务实施难题。
|
22天前
|
供应链 监控 物联网
新技术趋势与应用:区块链、物联网和虚拟现实的未来
【4月更文挑战第2天】随着科技的飞速发展,新兴技术如区块链、物联网和虚拟现实等正在逐渐改变我们的生活。本文将探讨这些技术的发展趋势和应用场景,以及它们如何影响我们的日常生活和工作。

热门文章

最新文章