This repository has been archived by the owner on Dec 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
115 lines (107 loc) · 4.01 KB
/
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
module.exports = function(mongoose, config) {
var Schema = require('mongoose').Schema;
var _ = require('underscore');
if (!_.has(mongoose.models, 'MapState') || _.isUndefined(mongoose.models.MapState) || _.isNull(mongoose.models.MapState)) {
var PlayerSchema = new Schema({
playerId: { type: String, required: true, index: true, unique: true },
name: { type: String },
team: { type: String },
icon: { type: String },
dead: { type: Boolean, "default": false }
}, {
timestamps: {
createdAt: "created",
updatedAt: "updated"
}
});
var HexSchema = new Schema({
"hexId": {type: String, required: true},
"h": {type: String, required: true},
"v": {type: String, required: true},
"landform": {type: String, "default": null},
"owner": {type: String, "default": null},
"players": {type: [String], "default": []},
"buildings": {type: [String], "default": [null, null, null, null]},
"underAttack": {type: Boolean, "default": false},
"destroyed": {type: Boolean, "default": false},
"warriors": {type: Number, "default": 0},
"gold": {type: Number, "default": 0},
"horses": {type: Number, "default": 0},
"glory": {type: Number, "default": 0}
}, {
timestamps: {
createdAt: "created",
updatedAt: "updated"
}
});
var MapStateSchema = new Schema({
"hexes": [ HexSchema ],
"time": {type: Date, required: true}
}, {
timestamps: {
createdAt: "created",
updatedAt: "updated"
}
});
// MapState static methods
MapStateSchema.statics.getCurrent = function() {
var self = this;
return new Promise(
function(resolve, reject) {
self.model('MapState')
.findOne()
.sort({"created": -1})
.exec(function(err, mapState) {
if (err !== null) {
reject(err);
} else if (_.isUndefined(mapState) || _.isNull(mapState)) {
resolve(null);
} else {
resolve(mapState);
}
}
);
}
);
};
MapStateSchema.statics.getAll = function(limit) {
var self = this;
if (_.isUndefined(limit) || _.isNull(limit) || _.isNaN(limit)) {
limit = 10;
}
return new Promise(
function(resolve, reject) {
self.model('MapState')
.find()
.sort({"created": -1})
.limit(10)
.exec(function(err, results) {
if (err) {
reject(err);
} else {
resolve(results);
}
});
}
);
};
// Player static methods
PlayerSchema.statics.getAll = function() {
var self = this;
return new Promise(
function(resolve, reject) {
self.model('Player').find().exec(function(err, results) {
if (err) {
reject(err);
} else {
resolve(results);
}
});
}
);
};
var MapState = mongoose.model('MapState', MapStateSchema);
var Player = mongoose.model('Player', PlayerSchema);
}
return;
};