-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmountain.js
100 lines (85 loc) · 3.25 KB
/
mountain.js
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
'use strict';
var MGame = function() {
LocalContractStorage.defineProperty(this, {
"MountainWeight" : 100,
"currentControlName" : '',
"currentControlTime" : 0,
"currentState" : '', // ON OVER
"currentGameNum" : 0
});
LocalContractStorage.defineMapProperty(this, "UserNameMap");
LocalContractStorage.defineMapProperty(this, "NameUserMap");
LocalContractStorage.defineMapProperty(this, "NameScoreMap");
LocalContractStorage.defineMapProperty(this, "TimeNameList");//每次玩家 {1 ==> []} includes
};
MGame.prototype = {
init: function() {
this.currentState = 'ON',
this.TimeNameList.set(this.currentGameNum, [])
},
dug: function() {
if(this.UserNameMap.get(Blockchain.transaction.from)) {
return 'Please set a name'
} else if(this.currentControlName != '') {
return this.currentControlName + ' is DUGing'
} else{
this.currentControlName = this.UserNameMap.get(Blockchain.transaction.from)
this.currentControlTime = Blockchain.block.timestamp
return 'Fool Old Man is Working Now'
}
},
_oldManWorkDone: function() {
this.MountainWeight -= 1
const score = this.NameScoreMap.get(`${this.currentControlTime}_${this.currentControlName}`) || 0
this.NameScoreMap.set(`${this.currentControlTime}_${this.currentControlName}`, score+1)
const gamers = this.TimeNameList.get(this.currentGameNum) || []
if(!gamers.includes(this.currentControlName)) {
this.TimeNameList.set(this.currentGameNum, gamers.concat(this.currentControlName))
}
this.currentControlName = ''
this.currentControlTime = 0
this.MountainWeight == 0 && _gameOver()
},
_gameOver: function() {
this.currentControlTime = Date.now()
this.currentState = 'OVER'
},
_resetGame: function() {
this.MountainWeight = 100
this.currentControlTime = 0
this.currentState = 'ON'
this.currentGameNum += 1
},
getState: function() {
if(this.currentState == 'ON'
&& this.currentControlName != ''
&& Blockchain.block.timestamp - 2000 > this.currentControlTime ) {
this._oldManWorkDone()
} else if(Blockchain.block.timestamp - 200000 > this.currentControlTime
&& this.currentState == 'OVER' ) {
this._resetGame()
}
return {
MountainWeight: this.MountainWeight,
currentControlName: this.currentControlName,
currentControlTime: this.currentControlTime,
currentState: this.currentState,
NameScoreMap: this.NameScoreMap,
TimeNameList: this.TimeNameList
}
},
canIUseName: function(name) {
return isNaN(this.NameUserMap.get(name))
},
setName: function(name) {
if(isNaN(this.NameUserMap.get(name))) {
this.NameUserMap.set(name, Blockchain.transaction.from)
this.UserNameMap.set(Blockchain.transaction.from, name)
this.NameScoreMap.set(name, 0)
return 'Success'
} else {
throw new Error('Name has been used')
}
}
};
module.exports = MGame;