-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.htm
129 lines (109 loc) · 3.48 KB
/
index.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!doctype html>
<html>
<head>
<title> Reward Game</title>
<meta name="author" content="Wiki.M [email protected]">
<meta name="keywords" content="">
<meta name="description" content="">
<link rel="stylesheet" href="css/style.css" type="text/css"/>
</head>
<body>
<div id="main">
<h3>Import Data£º</h3>
<input type="file" multiple="true" onchange="importDB(this.files)" value="import data"/>
<button type="button" onclick="getRewardAuto()">Auto Reward</button>
<button type="button" onclick="getReward()">Get Reward</button>
<button type="button" onclick="clearDB()">Clear Data</button>
<button type="button" onclick="getLocalStorageLength()">Count</button>
<button type="button" onclick="setImageEveryTime()">setImage</button>
<div id="all-user"><span></span></div>
<div id="reward"></div>
</div>
</body>
</html>
<script type="text/javascript">
/*save reward data in localStorage,
the key is user_index,the value is user's photo name
*/
function importDB(myFiles){
var allUser = {};
var allUserTemp = {};
var rewardUser = {};
allUser.data = new Array();
rewardUser.data = new Array();
for(var i = 0;i < myFiles.length;i++){
var reader = new FileReader();
reader.readAsDataURL(myFiles[i]);
allUser.data[i] = myFiles[i].name;
}
for(var i in allUser){
allUserTemp[i] = allUser[i];
}
localStorage.setItem('alluser',JSON.stringify(allUser));
localStorage.setItem('allusertemp',JSON.stringify(allUserTemp));
localStorage.setItem('rewarduser',JSON.stringify(rewardUser));
alert('import data ok...');
setInterval(function(){
setImageEveryTime(allUser);
},100);
}
/*restore game*/
function clearDB(){
localStorage.clear();
alert('clear data ok...');
}
function getReward(){
var allUserTemp = JSON.parse( localStorage.getItem( 'allusertemp' )) ;
//console.log(allUserTemp.data.length);
if(allUserTemp.data.length == 0){
alert('game over!');
return false;
}
var random = getRandom(0,allUserTemp.data.length - 1);
var reward = allUserTemp.data[random];
var span = document.createElement('span');
span.innerHTML = ['<img class="images" src="pics/', reward,'" title="', reward, '"/>'].join('');
document.getElementById('reward').insertBefore(span, null);
//update allUserTemp
allUserTemp.data.splice(random,1);
localStorage.setItem('allusertemp',JSON.stringify(allUserTemp));
//log reward user info
var rewardUser = JSON.parse( localStorage.getItem( 'rewarduser' )) ;
rewardUser.data.push(reward);
localStorage.setItem('rewarduser',JSON.stringify(rewardUser));
}
function getRewardAuto(){
var count = 0;
var autoRewardId = setInterval(function(){
if(count > 5){
clearInterval(autoRewardId);
alert("it's ok...");
return;
}else{
getReward();
count++;
}
},2000);
}
/*return a random between min and max*/
function getRandom(min,max){
if(min < 0)
min = 0;
if(max < 0)
max = 0;
return parseInt(Math.random()*(max-min+1)+min);
}
function getLocalStorageLength(){
alert( localStorage.length);
}
function setImageEveryTime(allUser){
//var allUser = JSON.parse( localStorage.getItem( 'alluser' )) ;
var random = getRandom(0,allUser.data.length - 1);
var reward = allUser.data[random];
var allUserContainer = document.getElementById('all-user');
var span = document.createElement('span');
span.innerHTML = ['<img class="images" src="pics/', reward,'" title="', reward, '"/>'].join('');
document.getElementById('all-user').insertBefore(span, null);
allUserContainer.replaceChild(span,allUserContainer.firstChild);
}
</script>