Skip to content

Commit

Permalink
New interface to get user game result
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchuangportal committed Jul 24, 2018
1 parent 8cf5916 commit 0de6b56
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
27 changes: 23 additions & 4 deletions contracts/CryptoHerosGame.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract CryptoHerosGame is Ownable {

struct SingleGame {
address player;
uint256 playerTokenId;
uint256 userResult;
uint256 contractResult;
uint256 playerBet;
uint8 game; // 0: smaller. 1: greater
Expand All @@ -26,12 +26,19 @@ contract CryptoHerosGame is Ownable {

SingleGame[] public singleGames;

mapping(address => uint256[]) public usersSingleGames;

constructor(CryptoHerosToken _cryptoHerosToken) public {
cryptoHerosToken = _cryptoHerosToken;
}

function () payable {

}

function createSingleGame(uint _tokenId) payable public returns (uint256) {
require(msg.value >= minPrice);
require(this.balance >= minHerosToken);
require(cryptoHerosToken.ownerOf(_tokenId) == msg.sender);

uint userTokenNumber;
Expand All @@ -49,21 +56,33 @@ contract CryptoHerosGame is Ownable {

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

} else if (result > 0) {
_singleGame = SingleGame({player: msg.sender, playerTokenId: userTokenNumber, contractResult: contractTokenNumber, playerBet: msg.value, game: game, result: 0});
_singleGame = SingleGame({player: msg.sender, userResult: userTokenNumber, contractResult: contractTokenNumber, playerBet: msg.value, game: game, result: 0});
require(msg.sender.send(msg.value * 150 / 100));

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

maxSingleGameId = singleGames.push(_singleGame) - 1;

uint256[] userSingleGames = usersSingleGames[msg.sender];
userSingleGames.push(maxSingleGameId);

return maxSingleGameId;
}

// function readUserGamesCount(address _address, uint _idx) public returns (uint){
// return usersSingleGames[_address][_idx].length;
// }

function getUserSingleGames() external view returns (uint256[]) {
return usersSingleGames[msg.sender];
}

function rand(uint min, uint max) private returns (uint){
nonce++;
return uint(sha3(nonce))%(min+max)-min;
Expand Down
2 changes: 1 addition & 1 deletion contracts/CryptoHerosToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ contract CryptoHerosToken is ERC721Token, Ownable {
return uint(sha3(nonce))%(min+max)-min;
}

function getHerosLength() public returns (uint) {
function getHerosLength() external view returns (uint) {
return heros.length;
}

Expand Down
21 changes: 16 additions & 5 deletions test/cryptoHerosToken.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ contract("CryptoHeros token", accounts => {
assert.equal(result2.receipt.status, '0x1');
let result3 = await cryptoHerosToken.initNumberAndDescription(1, 'description0');
assert.equal(result3.receipt.status, '0x1');
let result4 = await cryptoHerosToken.initNumberAndDescription(2, 'description0');
assert.equal(result4.receipt.status, '0x1');
let result5 = await cryptoHerosToken.initNumberAndDescription(3, 'description0');
assert.equal(result5.receipt.status, '0x1');
//assert.equal(owner, accounts[0]);
});

Expand Down Expand Up @@ -101,18 +105,25 @@ contract("CryptoHeros token", accounts => {
it("Start a game", async () => {
let cryptoHerosToken = await CryptoHerosToken.deployed();
const res = await cryptoHerosToken.getOwnedTokens(accounts[1]);
console.log('res: ', res);
//console.log('cryptoHerosToken: ', cryptoHerosToken.address);
//console.log('res: ', res);
console.log('cryptoHerosGame: ', cryptoHerosGame.address);
web3.eth.sendTransaction({from: accounts[0], to: cryptoHerosGame.address, value: web3.toWei(10,"ether"), gas: 2000000});
console.log(web3.eth.getBalance(accounts[0]).toNumber());

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);
// let singleGames = await cryptoHerosGame.singleGames(i);
// console.log('game result: ', singleGames[5].toString() + ' | ' + singleGames[4].toString() + ' | ' + singleGames[1].toString() + ' | ' + singleGames[2].toString());
}

const res3 = await cryptoHerosGame.getUserSingleGames({from: accounts[1]});
for (let i=0;i<res3.length;i++) {
console.log(res3[i].toString());
let singleGames = await cryptoHerosGame.singleGames(res3[i].toString());
console.log('game result: ', singleGames[5].toString() + ' | ' + singleGames[4].toString() + ' | ' + singleGames[1].toString() + ' | ' + singleGames[2].toString());
}


});
});
});

0 comments on commit 0de6b56

Please sign in to comment.