-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathshinx.model.js
178 lines (177 loc) · 5.82 KB
/
shinx.model.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
module.exports = (sequelize, DataTypes) => {
const MAX_RANGE = 10;
const getExpFromLevel = require('../../../../util/shinx/getExpFromLevel');
const parseMeetDate = require('../../../../util/shinx/parseMeetDate');
const getLevelFromExp = require('../../../../util/shinx/getLevelFromExp');
const parseMeetDateNow = () => {
const now = new Date()
return parseMeetDate(now.getDate(), now.getMonth(), now.getFullYear())
};
const getDay = () => {
return Math.floor(Date.now() / 86400000)
};
const Shinx = sequelize.define('Shinx', {
user_id: {
type: DataTypes.STRING,
primaryKey: true
},
nickname: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: 'Shinx',
},
belly: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0,
},
experience: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 1,
},
shiny: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
lastmeet: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: Math.floor(Date.now() / 86400000),
},
meetup: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: parseMeetDateNow()
},
user_male: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
},
auto_feed: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false,
},
}, {
timestamps: false,
});
// checkup
Shinx.prototype.checkup = function () {
const today = getDay();
const diff = this.lastmeet - today;
if (diff > 1) {
this.unfeedGeneric(Math.floor(2 * diff));
this.lastmeet = today;
this.save({ fields: ['belly', 'lastmeet'] });
};
};
// Experience
Shinx.prototype.addExperienceGeneric = function (experience) {
this.experience += Math.ceil(experience);
};
Shinx.prototype.addExperience = function (experience) {
this.addExperienceGeneric(experience)
this.save({ fields: ['experience'] });
};
Shinx.prototype.addExperienceAndLevelUp = function (experience) {
const pre = this.getLevel();
this.addExperience(experience);
const post = this.getLevel();
return { pre, post }
};
Shinx.prototype.getExperience = function () {
return this.experience;
};
Shinx.prototype.getNextExperience = function () {
const prev_level = Math.ceil(getExpFromLevel(this.getLevel()))
const next_level = Math.ceil(getExpFromLevel(this.getLevel() + 1))
return {
exp_pts: next_level - this.experience,
curr_percent: (this.experience - prev_level) / (next_level - prev_level)
};
};
Shinx.prototype.feedAndExp = function (food) {
const prev_level = Math.ceil(getExpFromLevel(this.getLevel()))
const next_level = Math.ceil(getExpFromLevel(this.getLevel() + 1))
this.addExperienceGeneric((next_level - prev_level) * (food / MAX_RANGE));
this.feedGeneric(food);
this.save({ fields: ['belly', 'experience'] });
};
Shinx.prototype.addExperienceAndUnfeed = function (experience, food) {
this.addExperienceGeneric(experience);
this.unfeedGeneric(food);
this.save({ fields: ['experience', 'belly'] });
};
// Level
Shinx.prototype.getLevel = function () {
return getLevelFromExp(this.experience);
};
// Shiny
Shinx.prototype.switchShininessAndGet = function () {
this.shiny = !this.shiny;
this.save({ fields: ['shiny'] });
return this.shiny;
};
// Belly
Shinx.prototype.feedGeneric = function (amount) {
this.belly = Math.min(MAX_RANGE, Math.max(0, this.belly) + amount);
}
Shinx.prototype.feed = function (amount) {
this.feedGeneric(amount);
this.save({ fields: ['belly'] });
};
Shinx.prototype.unfeedGeneric = function (amount) {
this.belly = Math.max(0, this.belly - amount);
};
Shinx.prototype.unfeed = function (amount) {
this.unfeedGeneric(amount);
this.save({ fields: ['belly'] });
};
Shinx.prototype.getHunger = function () {
return MAX_RANGE - this.belly;
};
Shinx.prototype.getBelly = () => {
return this.belly;
};
Shinx.prototype.getBellyPercent = function () {
return Math.round(this.belly * 100 / MAX_RANGE).toString() + '%'
};
Shinx.prototype.getBellyProportion = function () {
return this.belly / MAX_RANGE
};
// Nickname
Shinx.prototype.changeNick = function (nick) {
this.nickname = nick;
this.save({ fields: ['nickname'] });
};
// Gender
Shinx.prototype.swapAndGetTrainerGender = function () {
this.user_male = !this.user_male;
this.save({ fields: ['user_male'] });
return this.user_male;
};
// Battle
Shinx.prototype.saveBattle = function (shinxBattle, wins) {
this.experience = Math.floor(shinxBattle.exp * (1 + wins * 0.2));
this.save({ fields: ['experience'] });
};
// Auto feed
Shinx.prototype.setAutoFeedUnchecked = function (mode) {
this.auto_feed = mode;
this.save({ fields: ['auto_feed'] });
return this.auto_feed;
};
Shinx.prototype.setAutoFeed = function (mode) {
if (this.auto_feed == mode) {
return false;
} else {
this.auto_feed = mode;
this.save({ fields: ['auto_feed'] });
return true;
};
};
return Shinx;
};