个人自制编写了一个简易球星卡智能合约,暂不包括前端后端
此合约是在以太坊上部署一个集卡类游戏,玩家可以购买卡片获得自己喜爱的稀有的球星卡,玩家也可以在交易所卖出或者买入自己中意的卡片。
功能包括:
- getplayer 查看当前用户拥有的卡片
- getStarcard 查看指定卡片向合约购买的价格(ether)
- getCreatesale 查看制定交易下标返回详细数值
getallCrea 返回当前所有的交易
Starcardbuy 输入指定卡片号码与转账ether向合约购买卡片
- Createsale 输入指定卡片号码与指定数额创建一个交易
- Starcardbuytran 输入指定交易下标与转账ether竞拍交易
- open 输入指定下标,交易发起者可调用此合约结束竞拍,最高者获得指定卡片
代码如下:
pragma solidity ^0.4.20;
contract Starcard {
address Boss;
uint[20] StarID;
uint[20] cardID;
mapping (address => uint[20]) balances;
struct tran{
address Seller;
uint8 ofcard;
uint price;
uint auction;
uint time;
address HighestBidder;
uint HighestPrice;
bool status1;
}
tran[] Tran;
function Starcard() public{
Boss = msg.sender;
for(uint8 i=0;i<20;i++){
StarID[i]=20-i;
}
}
function getStarcard(uint i) public view returns(uint,bool){
if(i<20){
return (StarID[i],true);
}
else
return (0,false);
}
function Starcardbuy(uint i)payable public returns(uint[20],bool){
if(StarID[i]==msg.value/(10**18)){
cardID=balances[msg.sender];
cardID[i] = cardID[i]+1;
balances[msg.sender] = cardID;
return(balances[msg.sender],true);
}
else{
msg.sender.transfer(msg.value);
return(balances[msg.sender],false);
}
}
function getplayer() public view returns(uint[20]){
return balances[msg.sender];
}
function Createsale(uint8 thecard,uint theprice) public returns(uint,bool){
cardID = balances[msg.sender];
tran memory tra;
if(cardID[thecard] != 0){
tra.Seller = msg.sender;
tra.ofcard = thecard;
tra.price = theprice;
tra.auction = tra.price*6/10;
tra.status1 = true;
tra.time = block.number;
Tran.push(tra);
cardID[thecard] = cardID[thecard]-1;
balances[msg.sender] = cardID;
return (Tran.length-1,true);
}
else
return (0,false);