Created
April 2, 2018 16:26
-
-
Save allen-hsu/c740f00a382ab1122fe3a793f5b03a3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Classes | |
//Record Studuent pick. | |
//Cal Result | |
//Studuent Pick Class(Not Pick) | |
//Pick End Time. | |
//Query Time | |
pragma solidity ^0.4.8; | |
import "https://github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol"; | |
contract PickClass { | |
using SafeMath for uint; | |
address[] private pickClassStudents; | |
address[] private chosedClassStudents; | |
mapping(address => bool) chosed; | |
mapping(address => bool) picked; | |
address owner; | |
uint private classsNumber; | |
uint private currentRandomLength; | |
uint private chosedRandom; | |
uint256 hash; | |
modifier isValidPick() { | |
require(!picked[msg.sender]); | |
_; | |
} | |
modifier isOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
function PickClass() public { | |
owner = msg.sender; | |
hash = now; | |
} | |
function mockStudent() isValidPick public { | |
//it's for test | |
pickClassStudents.push(0x0); | |
pickClassStudents.push(0x1); | |
pickClassStudents.push(0x2); | |
pickClassStudents.push(0x3); | |
pickClassStudents.push(0x4); | |
pickClassStudents.push(0x5); | |
pickClassStudents.push(0x6); | |
pickClassStudents.push(0x7); | |
pickClassStudents.push(0x8); | |
pickClassStudents.push(0x9); | |
pickClassStudents.push(0x10); | |
pickClassStudents.push(0x11); | |
pickClassStudents.push(0x12); | |
pickClassStudents.push(0x13); | |
pickClassStudents.push(msg.sender); | |
} | |
function pickClass() isValidPick public { | |
pickClassStudents.push(msg.sender); | |
picked[msg.sender] = true; | |
} | |
function chose(uint studentNumber) isOwner public { | |
//Random | |
currentRandomLength = pickClassStudents.length; | |
for(uint i = 0; i < studentNumber; ++i) { | |
chosedRandom = random(0, currentRandomLength); | |
chosedClassStudents.push(pickClassStudents[chosedRandom]); | |
(pickClassStudents[chosedRandom], pickClassStudents[currentRandomLength - 1]) = (pickClassStudents[currentRandomLength - 1], pickClassStudents[chosedRandom]); | |
chosed[pickClassStudents[chosedRandom]] = true; | |
currentRandomLength = currentRandomLength.sub(1); | |
if(currentRandomLength == 0) { | |
break; | |
} | |
} | |
} | |
function isChosed() public view returns(bool) { | |
return chosed[msg.sender]; | |
} | |
function isChosed(address studentAddress) public view returns(bool) { | |
return chosed[studentAddress]; | |
} | |
function chosedStudent() public view returns(address[]) { | |
return chosedClassStudents; | |
} | |
function pickedStudent() public view returns(address[]) { | |
return pickClassStudents; | |
} | |
function random(uint min, uint max) private constant returns(uint) { | |
uint random = hash % (max - min) + min; | |
hash = uint256(keccak256(hash)); | |
return random; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment