游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1162|回复: 0

游戏领域区块链探索

[复制链接]

8723

主题

8783

帖子

1万

积分

版主

Rank: 7Rank: 7Rank: 7

积分
11952
发表于 2018-3-12 17:42:53 | 显示全部楼层 |阅读模式
文/Mr. Neo Chan, 陈景峯

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

中国广东省深圳市龙华新区民治街道溪山美地
518131
+86 13113668890

<netkiller@msn.com>

文档始创于2018-02-10

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

版权声明

内容摘要

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

为什么会写区块链电子书?因为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 (点) 仅仅是一个数字,数字存在数据库中,例如
  1. Username        | Point (Integer)
  2. -----------------------
  3. Neo                | 1000
  4. Jam                | 500
复制代码

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

  1. update member set point = 1000000000000 where username = 'Neo'        
复制代码

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

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

  1. update member set point = point - 100 where username = 'Neo'
复制代码

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

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

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

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

  1. account                        | coin
  2. ---------------------------------
  3. account1             | 1000000
  4. account2             | 0
  5. account3             | 0
  6. account4             | 0
  7. account5             | 0                                
复制代码

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

  1. account                        | coin
  2. ---------------------------------
  3. account1             | 999990
  4. account2             | 10
  5. account3             | 0
  6. account4             | 0
  7. account5             | 0               
复制代码

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

  1. account                        | coin
  2. ---------------------------------
  3. account1             | 999960
  4. account2             | 10
  5. account3             | 10
  6. account4             | 10
  7. account5             | 10                        
复制代码

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

  1. account                        | coin
  2. ---------------------------------
  3. account1             | 999965
  4. account2             | 10
  5. account3             | 5
  6. account4             | 10
  7. account5             | 10        
复制代码

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

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

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

  1. pragma solidity ^0.4.20;

  2. /******************************************/
  3. /*       Netkiller ADVANCED TOKEN         */
  4. /******************************************/
  5. /* Author netkiller <netkiller@msn.com>   */
  6. /* Home http://www.netkiller.cn           */
  7. /* Version 2018-03-05                     */
  8. /* Version 2018-03-06 - Add Global lock   */
  9. /******************************************/

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

  11. contract NetkillerAdvancedToken {
  12.     address public owner;
  13.     // Public variables of the token
  14.     string public name;
  15.     string public symbol;
  16.     uint8 public decimals = 2;
  17.     // 18 decimals is the strongly suggested default, avoid changing it
  18.     uint256 public totalSupply;
  19.    
  20.     uint256 public sellPrice;
  21.     uint256 public buyPrice;

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

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

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

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

  34.     bool lock = true;

  35.     /**
  36.      * Constrctor function
  37.      *
  38.      * Initializes contract with initial supply tokens to the creator of the contract
  39.      */
  40.     function NetkillerAdvancedToken(
  41.         uint256 initialSupply,
  42.         string tokenName,
  43.         string tokenSymbol
  44.     ) public {
  45.         owner = msg.sender;
  46.         totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount
  47.         balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens
  48.         name = tokenName;                                   // Set the name for display purposes
  49.         symbol = tokenSymbol;                               // Set the symbol for display purposes
  50.     }

  51.     modifier onlyOwner {
  52.         require(msg.sender == owner);
  53.         _;
  54.     }

  55.     modifier isLock {
  56.         require(!lock);
  57.         _;
  58.     }
  59.    
  60.     function setLock(bool _lock) onlyOwner {
  61.         lock = _lock;
  62.     }

  63.     function transferOwnership(address newOwner) onlyOwner public {
  64.         owner = newOwner;
  65.     }

  66.     /* Internal transfer, only can be called by this contract */
  67.     function _transfer(address _from, address _to, uint _value) isLock internal {
  68.         require (_to != 0x0);                               // Prevent transfer to 0x0 address. Use burn() instead
  69.         require (balanceOf[_from] >= _value);               // Check if the sender has enough
  70.         require (balanceOf[_to] + _value > balanceOf[_to]); // Check for overflows
  71.         require(!frozenAccount[_from]);                     // Check if sender is frozen
  72.         require(!frozenAccount[_to]);                       // Check if recipient is frozen
  73.         balanceOf[_from] -= _value;                         // Subtract from the sender
  74.         balanceOf[_to] += _value;                           // Add the same to the recipient
  75.         Transfer(_from, _to, _value);
  76.     }

  77.     /**
  78.      * Transfer tokens
  79.      *
  80.      * Send `_value` tokens to `_to` from your account
  81.      *
  82.      * @param _to The address of the recipient
  83.      * @param _value the amount to send
  84.      */
  85.     function transfer(address _to, uint256 _value) public {
  86.         _transfer(msg.sender, _to, _value);
  87.     }

  88.     /**
  89.      * Transfer tokens from other address
  90.      *
  91.      * Send `_value` tokens to `_to` in behalf of `_from`
  92.      *
  93.      * @param _from The address of the sender
  94.      * @param _to The address of the recipient
  95.      * @param _value the amount to send
  96.      */
  97.     function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
  98.         require(_value <= allowance[_from][msg.sender]);     // Check allowance
  99.         allowance[_from][msg.sender] -= _value;
  100.         _transfer(_from, _to, _value);
  101.         return true;
  102.     }

  103.     /**
  104.      * Set allowance for other address
  105.      *
  106.      * Allows `_spender` to spend no more than `_value` tokens in your behalf
  107.      *
  108.      * @param _spender The address authorized to spend
  109.      * @param _value the max amount they can spend
  110.      */
  111.     function approve(address _spender, uint256 _value) public
  112.         returns (bool success) {
  113.         allowance[msg.sender][_spender] = _value;
  114.         Approval(msg.sender, _spender, _value);
  115.         return true;
  116.     }

  117.     /**
  118.      * Set allowance for other address and notify
  119.      *
  120.      * Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it
  121.      *
  122.      * @param _spender The address authorized to spend
  123.      * @param _value the max amount they can spend
  124.      * @param _extraData some extra information to send to the approved contract
  125.      */
  126.     function approveAndCall(address _spender, uint256 _value, bytes _extraData)
  127.         public
  128.         returns (bool success) {
  129.         tokenRecipient spender = tokenRecipient(_spender);
  130.         if (approve(_spender, _value)) {
  131.             spender.receiveApproval(msg.sender, _value, this, _extraData);
  132.             return true;
  133.         }
  134.     }

  135.     /**
  136.      * Destroy tokens
  137.      *
  138.      * Remove `_value` tokens from the system irreversibly
  139.      *
  140.      * @param _value the amount of money to burn
  141.      */
  142.     function burn(uint256 _value) onlyOwner public returns (bool success) {
  143.         require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough
  144.         balanceOf[msg.sender] -= _value;            // Subtract from the sender
  145.         totalSupply -= _value;                      // Updates totalSupply
  146.         Burn(msg.sender, _value);
  147.         return true;
  148.     }

  149.     /**
  150.      * Destroy tokens from other account
  151.      *
  152.      * Remove `_value` tokens from the system irreversibly on behalf of `_from`.
  153.      *
  154.      * @param _from the address of the sender
  155.      * @param _value the amount of money to burn
  156.      */
  157.     function burnFrom(address _from, uint256 _value) onlyOwner public returns (bool success) {
  158.         require(balanceOf[_from] >= _value);                // Check if the targeted balance is enough
  159.         require(_value <= allowance[_from][msg.sender]);    // Check allowance
  160.         balanceOf[_from] -= _value;                         // Subtract from the targeted balance
  161.         allowance[_from][msg.sender] -= _value;             // Subtract from the sender's allowance
  162.         totalSupply -= _value;                              // Update totalSupply
  163.         Burn(_from, _value);
  164.         return true;
  165.     }

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

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

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

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

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

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

  210.     transferFrom(_from, _to, _value);

  211.     require(_to.call(_data));
  212.     return true;
  213.   }

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

  216.     approve(_spender, _value);

  217.     require(_spender.call(_data));

  218.     return true;
  219.   }
  220.    
  221. }
复制代码

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

33.4.2. 玩家属性与游戏装备

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

  1. pragma solidity ^0.4.20;

  2. contract Player {

  3.     address public owner;
  4.    
  5.     string name;
  6.     bool lock = false;        //合约锁
  7.     uint number = 1;
  8.     uint attr_number = 1;
  9.    
  10.     mapping (address  => string) guestbook; //客户留言本        

  11.         struct Attribute {
  12.         string key;                // 属性的名字
  13.         string value;        // 属性值
  14.         
  15.     }
  16.     mapping (uint  => Attribute) attribute;

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

  21.     }
  22.     mapping (uint  => Wear) wear;
  23.    
  24.     function Player(string _name) public {
  25.         name = _name;
  26.         }
  27.         
  28.         modifier onlyOwner {
  29.         require(msg.sender == owner);
  30.         _;
  31.     }
  32.         
  33.     // 名称
  34.     function getName() public view returns(string){
  35.         return name;
  36.     }
  37.     function setLock(bool _lock) onlyOwner public {
  38.         lock = _lock;
  39.     }
  40.      // 增加人物属性,例如肤色,眼睛,头发等等
  41.     function putAttribute(string _key, string _value) onlyOwner public{
  42.         if(lock == false){
  43.                         Attribute memory item = Attribute(_key, _value);
  44.                         attribute[attr_number] = item;
  45.                         attr_number = attr_number + 1;
  46.         }
  47.     }

  48.         // 获得属性
  49.     function getAttribute(uint _attr_number) public view returns(string, string) {
  50.         require(_attr_number < attr_number);
  51.         Attribute memory item = attribute[_attr_number];
  52.         
  53.                 return (item.key, item.value);
  54.         }
  55.    
  56.     // 增加装备信息,穿戴物品,武器,
  57.     function putWear(string _name, string _description, string _attribute ) onlyOwner public{
  58.         if(lock == false){
  59.             Wear memory node = Wear(_name,_description,_attribute);
  60.             wear[number] = node;
  61.             number = number + 1;
  62.             lock = true;
  63.         }
  64.     }

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

  68.         Wear memory item = wear[_number];
  69.         
  70.                 return (item.name, item.desciption, item.attribute);
  71.         }
  72.         
  73.         // 数量
  74.         function getWearCount() public view returns(uint){
  75.             return number;
  76.         }
  77.    
  78.     // 客户留言
  79.     function addGuestbook(address _owner, string message) onlyOwner public{
  80.             guestbook[_owner] = message;
  81.         }
  82. }        
复制代码

33.4.3. 物品合成计算

区块链还可用于物品合成计算或者叫炼金术等等

很早的时候玩《暗黑破坏神III》 里面已一个盒子,放入符文,可以根据公式合成其他属性的符文,我任务这个需求可以使用区块链来完成。

另外在我玩XBOX游戏《巫师3》 中的炼金术,铸造,药水合成等等,我逗人都可以使用区块链完成。

33.4.4. 实施步骤

如果着手一个游戏项目上链,我们需要怎么做呢?

上链步骤


  • 收集需求,收集公司的内部上链需求,听取所有部门的建议和诉求。

收集内容例如,代币发行量多少?代币小数位数,代币名称,是否会增发,是否需要冻结,代币怎样流通,怎样回收
Dapp 的 UI 设计,各种功能流程

  • 分析需求,因为需求来自各种部门,各种岗位等等,他们不一定从事需求分析工作,所以我们需求对他们的需求过滤,分析,然后给出初步的PRD文档(产品需求文档)
  • 根据收集的需求设计合约
根据需求设计Dapp
系统架构设计,软件架构设计,技术选型;需要考虑扩展性,灵活性,并发设计,数据安全,部署,后期监控,各种埋点(统计、监控)

  • 准备环境,我们需要三个环境,开发,测试,生产(运营)。
  • 项目启动

运维部门准备环境
开发部门开发合约和Dapp
测试部门准备测试用例,测试环境

  • 测试

Alpha 阶段,将合约部署到测试环境,测试合约的每个函数的工作逻辑,确保无误。因为合约一旦部署将不能更改,只能废弃使用新的合约,所以这个测试步骤必须重视。
Beta 阶段,将测试合约部署到测试网,例如 Ropsten ,可以邀请公司内部测试

  • 部署生产环境

部署合约,将合约部署到主网
Dapp 部署到生产环境。

  • 验收测试,在生产环境做最后验收测试

原文地址:https://cloud.tencent.com/developer/article/1053033

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2024-4-19 18:38

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表