/
distance.go
245 lines (188 loc) · 5.85 KB
/
distance.go
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package distance
import (
"context"
"errors"
"time"
"github.com/google/logger"
"github.com/tarkov-database/rest-api/core/database"
"github.com/tarkov-database/rest-api/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type objectID = model.ObjectID
type timestamp = model.Timestamp
// AmmoDistanceStatistics describes the entity of a ammo distance statistics
type AmmoDistanceStatistics struct {
ID objectID `json:"_id" bson:"_id"`
Reference objectID `json:"ammo" bson:"ammo"`
Distance uint64 `json:"distance" bson:"distance"`
Velocity float64 `json:"velocity" bson:"velocity"`
Damage float64 `json:"damage" bson:"damage"`
PenetrationPower float64 `json:"penetrationPower" bson:"penetrationPower"`
TimeOfFlight float64 `json:"timeOfFlight" bson:"timeOfFlight"`
Drop float64 `json:"drop" bson:"drop"`
Modified timestamp `json:"_modified" bson:"_modified"`
}
// Validate validates the fields of a DistanceStatistics
func (d AmmoDistanceStatistics) Validate() error {
if d.Reference.IsZero() {
return errors.New("ammo reference id is missing")
}
return nil
}
// Collection indicates the MongoDB feature collection
const Collection = "statistics.ammunition.distances"
func getOneByFilter(filter interface{}) (*AmmoDistanceStatistics, error) {
c := database.GetDB().Collection(Collection)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
stats := &AmmoDistanceStatistics{}
if err := c.FindOne(ctx, filter).Decode(stats); err != nil {
if err != mongo.ErrNoDocuments {
logger.Error(err)
}
return stats, model.MongoToAPIError(err)
}
return stats, nil
}
// GetByID returns the entity of the given ID
func GetByID(id string) (*AmmoDistanceStatistics, error) {
objID, err := model.ToObjectID(id)
if err != nil {
return &AmmoDistanceStatistics{}, err
}
return getOneByFilter(bson.M{"_id": objID})
}
// Options represents the options for a database operation
type Options struct {
Sort bson.D
Limit int64
Offset int64
}
func getManyByFilter(filter interface{}, opts *Options) (*model.Result, error) {
c := database.GetDB().Collection(Collection)
findOpts := options.Find()
findOpts.SetLimit(opts.Limit)
findOpts.SetSkip(opts.Offset)
findOpts.SetSort(opts.Sort)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var err error
r := &model.Result{}
r.Count, err = c.CountDocuments(ctx, filter)
if err != nil {
logger.Error(err)
return r, model.MongoToAPIError(err)
}
if r.Count == 0 {
return r, nil
}
cur, err := c.Find(ctx, filter, findOpts)
if err != nil {
if err != mongo.ErrNoDocuments {
logger.Error(err)
}
return r, model.MongoToAPIError(err)
}
defer cur.Close(ctx)
for cur.Next(ctx) {
stats := &AmmoDistanceStatistics{}
if err := cur.Decode(stats); err != nil {
logger.Error(err)
return r, model.MongoToAPIError(err)
}
r.Items = append(r.Items, stats)
}
if err := cur.Err(); err != nil {
return r, model.MongoToAPIError(err)
}
return r, nil
}
// GetAll returns a result based on filters
func GetAll(opts *Options) (*model.Result, error) {
return getManyByFilter(bson.D{}, opts)
}
// GetByRefsAndRange returns a result by given IDs and range
func GetByRefsAndRange(ids []string, gte, lte *uint64, opts *Options) (*model.Result, error) {
opts.Sort = append(opts.Sort, bson.D{
bson.E{Key: "ammo", Value: 1},
bson.E{Key: "armor.id", Value: 1},
}...)
filter := bson.D{}
if ids != nil {
objIDs := make([]objectID, len(ids))
for i, id := range ids {
objID, err := model.ToObjectID(id)
if err != nil {
return &model.Result{}, err
}
objIDs[i] = objID
}
filter = append(filter, bson.E{Key: "ammo", Value: bson.D{{Key: "$in", Value: objIDs}}})
}
switch {
case gte != nil && lte != nil:
filter = append(filter, bson.E{Key: "distance", Value: bson.D{
bson.E{Key: "$gte", Value: gte}, bson.E{Key: "$lte", Value: lte},
}})
case gte != nil:
filter = append(filter, bson.E{Key: "distance", Value: bson.D{{Key: "$gte", Value: gte}}})
case lte != nil:
filter = append(filter, bson.E{Key: "distance", Value: bson.D{{Key: "$lte", Value: lte}}})
}
return getManyByFilter(filter, opts)
}
// Create creates a new entity
func Create(stats *AmmoDistanceStatistics) error {
c := database.GetDB().Collection(Collection)
if stats.ID.IsZero() {
stats.ID = primitive.NewObjectID()
}
stats.Modified = timestamp{Time: time.Now()}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if _, err := c.InsertOne(ctx, stats); err != nil {
logger.Error(err)
return model.MongoToAPIError(err)
}
return nil
}
// Replace replaces the data of an existing entity
func Replace(id string, stats *AmmoDistanceStatistics) error {
objID, err := model.ToObjectID(id)
if err != nil {
return err
}
if stats.ID.IsZero() {
stats.ID = objID
}
stats.Modified = timestamp{Time: time.Now()}
c := database.GetDB().Collection(Collection)
opts := options.FindOneAndReplace()
opts.SetUpsert(false)
opts.SetReturnDocument(options.After)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err = c.FindOneAndReplace(ctx, bson.M{"_id": objID}, stats, opts).Decode(stats); err != nil {
logger.Error(err)
return model.MongoToAPIError(err)
}
return nil
}
// Remove removes an entity
func Remove(id string) error {
objID, err := model.ToObjectID(id)
if err != nil {
return err
}
c := database.GetDB().Collection(Collection)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if _, err = c.DeleteOne(ctx, bson.M{"_id": objID}); err != nil {
logger.Error(err)
return model.MongoToAPIError(err)
}
return nil
}