Skip to content

Commit

Permalink
Upadte
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchuangportal committed Jul 24, 2018
1 parent 52c31aa commit a1a2817
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
17 changes: 10 additions & 7 deletions contracts/CryptoHerosGame.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ contract CryptoHerosGame is Ownable {
uint256 playerTokenId;
uint256 contractResult;
uint256 playerBet;
uint8 winner; // 0 user, 1 contract, 2 draw
uint8 game; // 0: smaller. 1: greater
uint8 result; // 0 user win, 1 contract win, 2 draw
}

Game[] public games;
Expand All @@ -48,25 +49,27 @@ contract CryptoHerosGame is Ownable {

// 取得 number 進行比大小
uint userTokenNumber = cryptoHerosToken.getTokenProverty(_tokenId);
uint contractNumber = rand(0, 10);
uint contractNumber = rand(0, cryptoHerosToken.getLength());

int result;
if (rand(0, 1) > 0) {
uint8 game = uint8(rand(0, 2));
if (game > 0) {
result = int(userTokenNumber - contractNumber);
} else {
result = int(contractNumber - userTokenNumber);
}

SingleGame memory _singleGame;
if (result == 0) {
_singleGame = SingleGame({player: msg.sender, playerTokenId: _tokenId, contractResult: contractNumber, playerBet: msg.value, winner: 2});
_singleGame = SingleGame({player: msg.sender, playerTokenId: userTokenNumber, contractResult: contractNumber, playerBet: msg.value, game: game, result: 2});
msg.sender.send(msg.value * 1 - gameFee);

} else if (result > 0) {
_singleGame = SingleGame({player: msg.sender, playerTokenId: _tokenId, contractResult: contractNumber, playerBet: msg.value, winner: 0});
//var Ether = msg.value;
_singleGame = SingleGame({player: msg.sender, playerTokenId: userTokenNumber, contractResult: contractNumber, playerBet: msg.value, game: game, result: 0});
msg.sender.send(msg.value * 150 / 100);

} else {
_singleGame = SingleGame({player: msg.sender, playerTokenId: _tokenId, contractResult: contractNumber, playerBet: msg.value, winner: 1});
_singleGame = SingleGame({player: msg.sender, playerTokenId: userTokenNumber, contractResult: contractNumber, playerBet: msg.value, game: game, result: 1});
}

maxSingleGameId = singleGames.push(_singleGame) - 1;
Expand Down
5 changes: 5 additions & 0 deletions contracts/CryptoHerosToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ contract CryptoHerosToken is ERC721Token, Ownable {
nonce++;
return uint(sha3(nonce))%(min+max)-min;
}

function getLength() public returns (uint) {
return heros.length;
}

}
14 changes: 11 additions & 3 deletions test/cryptoHerosToken.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,20 @@ contract("CryptoHeros token", accounts => {
});


it.skip("Should transfer ownership", async () => {
it("Start a game", async () => {
let cryptoHerosToken = await CryptoHerosToken.deployed();
const res = await cryptoHerosToken.getOwnedTokens(accounts[1]);
console.log('cryptoHerosToken: ', cryptoHerosToken.address);
//const res2 = await cryptoHerosGame.createSingleGame(res[5]);
console.log('res: ', res);
//console.log('cryptoHerosToken: ', cryptoHerosToken.address);

let cryptoHerosGame = await CryptoHerosGame.new(cryptoHerosToken.address);
for (let i=0;i<res.length;i++) {
const res2 = await cryptoHerosGame.createSingleGame(res[i], {from: accounts[1], value: web3.toWei(0.02, "ether")});
assert.equal(res2.receipt.status, '0x1');
let singleGames = await cryptoHerosGame.singleGames(i);
console.log('game result: ', singleGames[5].toString() + ' | ' + singleGames[4].toString() + ' | ' + singleGames[1].toString() + ' | ' + singleGames[2].toString());
}


});
});
Expand Down

0 comments on commit a1a2817

Please sign in to comment.