Created
April 1, 2018 10:27
-
-
Save allen-hsu/c47f4bc9a584e9b8d9ca358bd092f5a5 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 classMaxStudent = 8; | |
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; | |
//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; | |
} | |
// array = [] | |
function chose() isOwner public { | |
//Random | |
currentRandomLength = pickClassStudents.length; | |
for(uint i = 0; i < pickClassStudents.length; ++i) { | |
chosedRandom = random(0, currentRandomLength); | |
chosedClassStudents.push(pickClassStudents[chosedRandom]); | |
(pickClassStudents[chosedRandom], pickClassStudents[currentRandomLength - 1]) = (pickClassStudents[currentRandomLength - 1], pickClassStudents[chosedRandom]); | |
chosed[pickClassStudents[chosedRandom]] = true; | |
currentRandomLength.sub(1); | |
if(currentRandomLength == 0) { | |
break; | |
} | |
} | |
} | |
function isChosed() public view returns(bool) { | |
return chosed[msg.sender]; | |
} | |
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