-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathTavernBase.sol
128 lines (109 loc) · 3.4 KB
/
TavernBase.sol
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
pragma solidity ^0.4.24;
import "../lib/SafeMath.sol";
import "./ITavern.sol";
contract TavernBase is ITavern {
using SafeMath for *;
struct Tavern {
uint256 initAt; // The time of tavern init
int longitude; // The longitude of tavern
int latitude; // The latitude of tavern
uint8 popularity; // The popularity of tavern
uint256 activeness; // The activeness of tavern
}
uint8 public constant decimals = 16; // longitude latitude decimals
mapping(uint256 => Tavern) internal tokenTaverns;
function _tavern(uint256 _tokenId) internal view returns (uint256, int, int, uint8, uint256) {
Tavern storage tavern = tokenTaverns[_tokenId];
return (
tavern.initAt,
tavern.longitude,
tavern.latitude,
tavern.popularity,
tavern.activeness
);
}
function _isBuilt(uint256 _tokenId) internal view returns (bool){
Tavern storage tavern = tokenTaverns[_tokenId];
return (tavern.initAt > 0);
}
function _build(
uint256 _tokenId,
int _longitude,
int _latitude,
uint8 _popularity
) internal {
// Check whether tokenid has been initialized
require(!_isBuilt(_tokenId));
require(_isLongitude(_longitude));
require(_isLatitude(_latitude));
require(_popularity != 0);
uint256 time = block.timestamp;
Tavern memory tavern = Tavern(
time, _longitude, _latitude, _popularity, uint256(0)
);
tokenTaverns[_tokenId] = tavern;
emit Build(time, _tokenId, _longitude, _latitude, _popularity);
}
function _batchBuild(
uint256[] _tokenIds,
int[] _longitudes,
int[] _latitudes,
uint8[] _popularitys
) internal {
uint256 i = 0;
while (i < _tokenIds.length) {
_build(
_tokenIds[i],
_longitudes[i],
_latitudes[i],
_popularitys[i]
);
i += 1;
}
}
function _activenessUpgrade(uint256 _tokenId, uint256 _deltaActiveness) internal {
require(_isBuilt(_tokenId));
Tavern storage tavern = tokenTaverns[_tokenId];
uint256 oActiveness = tavern.activeness;
uint256 newActiveness = tavern.activeness.add(_deltaActiveness);
tavern.activeness = newActiveness;
tokenTaverns[_tokenId] = tavern;
emit ActivenessUpgrade(_tokenId, oActiveness, newActiveness);
}
function _batchActivenessUpgrade(uint256[] _tokenIds, uint256[] __deltaActiveness) internal {
uint256 i = 0;
while (i < _tokenIds.length) {
_activenessUpgrade(_tokenIds[i], __deltaActiveness[i]);
i += 1;
}
}
function _popularitySetting(uint256 _tokenId, uint8 _popularity) internal {
require(_isBuilt(_tokenId));
uint8 oPopularity = tokenTaverns[_tokenId].popularity;
tokenTaverns[_tokenId].popularity = _popularity;
emit PopularitySetting(_tokenId, oPopularity, _popularity);
}
function _batchPopularitySetting(uint256[] _tokenIds, uint8[] _popularitys) internal {
uint256 i = 0;
while (i < _tokenIds.length) {
_popularitySetting(_tokenIds[i], _popularitys[i]);
i += 1;
}
}
function _isLongitude (
int _param
) internal pure returns (bool){
return(
_param <= 180 * int(10 ** uint256(decimals))&&
_param >= -180 * int(10 ** uint256(decimals))
);
}
function _isLatitude (
int _param
) internal pure returns (bool){
return(
_param <= 90 * int(10 ** uint256(decimals))&&
_param >= -90 * int(10 ** uint256(decimals))
);
}
}