知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
如何將區塊鏈嫁接到遊戲領域,我做了很多思考,經過分析總結,發現下面幾項內容非常適合上鏈。
上鏈內容
積分代幣
如果說區塊鏈應用於遊戲領域,可能99%的人首先會想到是代幣,的確遊戲領域實施區塊鏈連,代幣必不可少。但是區塊鏈不等於代幣。
遊戲裝備
人物屬性
關卡任務
下面我們要思考為什麼需要將遊戲數據放到區塊鏈上,玩遊戲的人都知道私服,私人架設遊戲伺服器,私服上玩遊戲遇到最大的問題就是公平性。管理員可以隨意調整伺服器參數。
私服存在哪些問題呢?
修改遊戲裝備屬性
修改生命與魔法值
關卡參數
人物屬性
隨意封賬號
這是我們在私服上遇到的最大問題,那麼官方伺服器就公平嗎?不一定,對於弱勢的玩家只能相信遊戲公司的承諾。
有了區塊鏈技術,我們能做什麼呢?例如我們將用戶裝備數據等數據上鏈,這樣保證了裝備永遠屬於玩家
區塊鏈能做什麼?
“點” 獎勵採用代幣實現,可以實現流通,兌換,消費等等......
爆出裝備立即上鏈
用戶等級屬性上鏈
用戶狀態上鏈
關卡數據上鏈
了凸顯公平性,我們採用公鏈,查詢用戶數據可以使用介面,也可以直接到公鏈上查詢。
下面詳細講解具體怎麼實現。
傳統幣 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; } }
這個代幣合約實現了,增發,減持,全局鎖,賬號凍結/解凍 等等功能。
下面的合約實現了遊戲玩家上鏈,上鏈信息有玩家屬性,例如膚色,眼睛,頭髮,血統等等。身上的穿戴物品包括武器等等。
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; } }
假設我們開發一個遊戲平台,很多廠商可以在這個平台上出售遊戲。
例如屠龍寶刀只有一把,但是實際情況只要能賺錢,遊戲廠商可以賣給了10個玩家,甚至更多。
為了公平起見,對於稀有的裝備管理,我們要求遊戲廠商在平台上備案。包括裝備屬性,應該歸誰所有等等
區塊鏈還可用於物品合成計算或者叫煉金術等等
很早的時候玩《暗黑破壞神III》 裡面已一個盒子,放入符文,可以根據公式合成其他屬性的符文,我任務這個需求可以使用區塊鏈來完成。
另外在我玩XBOX遊戲《巫師3》 中的煉金術,鑄造,藥水合成等等,我逗人都可以使用區塊鏈完成。
如果着手一個遊戲項目上鏈,我們需要怎麼做呢?
上鏈步驟
收集需求,收集公司的內部上鏈需求,聽取所有部門的建議和訴求。
收集內容例如,代幣發行量多少?代幣小數位數,代幣名稱,是否會增發,是否需要凍結,代幣怎樣流通,怎樣回收
Dapp 的 UI 設計,各種功能流程
分析需求,因為需求來自各種部門,各種崗位等等,他們不一定從事需求分析工作,所以我們需求對他們的需求過濾,分析,然後給出初步的PRD文檔(產品需求文檔)
根據收集的需求設計合約和Dapp
根據需求設計Dapp
系統架構設計,軟件架構設計,技術選型;需要考慮擴展性,靈活性,並發設計,數據安全,部署,後期監控,各種埋點(統計、監控)
準備環境,我們需要三個環境,開發,測試,生產(運營)。
項目啟動
運維部門準備環境,開始建設監控系統
開發部門開發合約和Dapp
測試部門準備測試用例,測試環境
測試
Alpha 階段,將合約部署到測試環境,測試合約的每個函數的工作邏輯,確保無誤。因為合約一旦部署將不能更改,只能廢棄使用新的合約,所以這個測試步驟必須重視。
Beta 階段,將測試合約部署到測試網,例如 Ropsten ,可以邀請公司內部測試
部署生產環境
部署合約,將合約部署到主網
Dapp 部署到生產環境。
驗收測試,在生產環境做最後驗收測試
代幣上交易所