forked from alaingilbert/ogame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fleetBuilder.go
301 lines (264 loc) · 7.27 KB
/
fleetBuilder.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package ogame
import (
"errors"
"time"
)
// FleetBuilderFactory ...
type FleetBuilderFactory struct {
b Wrapper
}
// NewFleetBuilderFactory ...
func NewFleetBuilderFactory(b Wrapper) *FleetBuilderFactory {
return &FleetBuilderFactory{b: b}
}
// NewFleet ...
func (f FleetBuilderFactory) NewFleet() *FleetBuilder {
return NewFleetBuilder(f.b)
}
// FleetBuilder ...
type FleetBuilder struct {
b Wrapper
tx Prioritizable
origin Celestial
destination Coordinate
speed Speed
mission MissionID
ships ShipsInfos
resources Resources
err error
fleet Fleet
minimumDeuterium int64
holdingTime int64
unionID int64
allShips bool
recallIn int64
successCallbacks []func(Fleet)
errorCallbacks []func(error)
}
// NewFleetBuilder ...
func NewFleetBuilder(b Wrapper) *FleetBuilder {
fb := new(FleetBuilder)
fb.b = b
fb.mission = Transport
fb.speed = HundredPercent
return fb
}
// SetTx ...
func (f *FleetBuilder) SetTx(tx Prioritizable) *FleetBuilder {
f.tx = tx
return f
}
// SetOrigin ...
func (f *FleetBuilder) SetOrigin(v interface{}) *FleetBuilder {
f.origin = f.b.GetCachedCelestial(v)
return f
}
// SetDestination ...
func (f *FleetBuilder) SetDestination(v interface{}) *FleetBuilder {
var c Celestial
if celestial, ok := v.(Celestial); ok {
f.destination = celestial.GetCoordinate()
} else if planet, ok := v.(Planet); ok {
f.destination = planet.GetCoordinate()
} else if moon, ok := v.(Moon); ok {
f.destination = moon.GetCoordinate()
} else if coord, ok := v.(Coordinate); ok {
f.destination = coord
} else if coordStr, ok := v.(string); ok {
coord, err := ParseCoord(coordStr)
if err != nil {
return f
}
f.destination = coord
} else {
c = f.b.GetCachedCelestial(v)
if c != nil {
f.destination = c.GetCoordinate()
}
}
return f
}
// SetSpeed ...
func (f *FleetBuilder) SetSpeed(speed Speed) *FleetBuilder {
f.speed = speed
return f
}
// SetResources ...
func (f *FleetBuilder) SetResources(resources Resources) *FleetBuilder {
f.resources = resources
return f
}
// SetMetal ...
func (f *FleetBuilder) SetMetal(metal int64) *FleetBuilder {
f.resources.Metal = MaxInt(metal, -1)
return f
}
// SetCrystal ...
func (f *FleetBuilder) SetCrystal(crystal int64) *FleetBuilder {
f.resources.Crystal = MaxInt(crystal, -1)
return f
}
// SetDeuterium ...
func (f *FleetBuilder) SetDeuterium(deuterium int64) *FleetBuilder {
f.resources.Deuterium = MaxInt(deuterium, -1)
return f
}
// SetAllResources will send all resources from the origin
func (f *FleetBuilder) SetAllResources() *FleetBuilder {
f.resources = Resources{Metal: -1, Crystal: -1, Deuterium: -1}
return f
}
// SetAllMetal will send all metal from the origin
func (f *FleetBuilder) SetAllMetal() *FleetBuilder {
f.resources.Metal = -1
return f
}
// SetAllCrystal will send all crystal from the origin
func (f *FleetBuilder) SetAllCrystal() *FleetBuilder {
f.resources.Crystal = -1
return f
}
// SetAllDeuterium will send all deuterium from the origin
func (f *FleetBuilder) SetAllDeuterium() *FleetBuilder {
f.resources.Deuterium = -1
return f
}
// SetMinimumDeuterium set minimum deuterium to keep on celestial
func (f *FleetBuilder) SetMinimumDeuterium(minimumDeuterium int64) *FleetBuilder {
f.minimumDeuterium = minimumDeuterium
return f
}
// SetMission ...
func (f *FleetBuilder) SetMission(mission MissionID) *FleetBuilder {
f.mission = mission
return f
}
// SetDuration set expedition duration
func (f *FleetBuilder) SetDuration(holdingTime int64) *FleetBuilder {
f.holdingTime = holdingTime
return f
}
// SetUnionID set union id to join
func (f *FleetBuilder) SetUnionID(unionID int64) *FleetBuilder {
f.unionID = unionID
return f
}
// AddShips ...
func (f *FleetBuilder) AddShips(id ID, nbr int64) *FleetBuilder {
f.ships.Set(id, f.ships.ByID(id)+nbr)
return f
}
// SetShips ...
func (f *FleetBuilder) SetShips(ships ShipsInfos) *FleetBuilder {
f.ships = ships
return f
}
// SetAllShips ...
func (f *FleetBuilder) SetAllShips() *FleetBuilder {
f.allShips = true
return f
}
// SetRecallIn ...
func (f *FleetBuilder) SetRecallIn(secs int64) *FleetBuilder {
f.recallIn = secs
return f
}
// FlightTime ...
func (f *FleetBuilder) FlightTime() (secs, fuel int64) {
ships := f.ships
if f.allShips {
if f.tx != nil {
ships, _ = f.tx.GetShips(f.origin.GetID())
} else {
ships, _ = f.b.GetShips(f.origin.GetID())
}
}
return f.b.FlightTime(f.origin.GetCoordinate(), f.destination, f.speed, ships, f.mission)
}
func (f *FleetBuilder) sendNow(tx Prioritizable) error {
if f.origin == nil {
f.err = errors.New("invalid origin")
return f.err
}
// Set all ships
if f.allShips {
f.ships, _ = tx.GetShips(f.origin.GetID())
}
var fuel int64
var planetResources Resources
if f.minimumDeuterium > 0 {
planetResources, _ = tx.GetResources(f.origin.GetID())
_, fuel = tx.FlightTime(f.origin.GetCoordinate(), f.destination, f.speed, f.ships, f.mission)
}
if f.minimumDeuterium > 0 && f.resources.Deuterium > 0 {
planetResources.Deuterium = planetResources.Deuterium - (fuel + 10) - f.minimumDeuterium
if f.resources.Deuterium > planetResources.Deuterium {
f.resources.Deuterium = planetResources.Deuterium
}
}
payload := f.resources
// Send all resources
if f.resources.Metal == -1 || f.resources.Crystal == -1 || f.resources.Deuterium == -1 {
// Calculate cargo
techs := tx.GetResearch()
cargoCapacity := f.ships.Cargo(techs, f.b.GetServer().Settings.EspionageProbeRaids == 1, f.b.CharacterClass() == Collector, f.b.IsPioneers())
if f.minimumDeuterium <= 0 {
planetResources, _ = tx.GetResources(f.origin.GetID())
}
if f.resources.Deuterium == -1 {
if f.minimumDeuterium > 0 {
planetResources.Deuterium = planetResources.Deuterium - (fuel + 10) - f.minimumDeuterium
}
payload.Deuterium = MinInt(cargoCapacity, planetResources.Deuterium)
cargoCapacity -= payload.Deuterium
}
if f.resources.Crystal == -1 {
payload.Crystal = MinInt(cargoCapacity, planetResources.Crystal)
cargoCapacity -= payload.Crystal
}
if f.resources.Metal == -1 {
payload.Metal = MinInt(cargoCapacity, planetResources.Metal)
}
}
f.fleet, f.err = tx.EnsureFleet(f.origin.GetID(), f.ships.ToQuantifiables(), f.speed, f.destination, f.mission, payload, f.holdingTime, f.unionID)
return f.err
}
// SendNow send the fleet with defined configurations
func (f *FleetBuilder) SendNow() (Fleet, error) {
var err error
if f.tx != nil {
err = f.sendNow(f.tx)
} else {
err = f.b.Tx(func(tx Prioritizable) error {
return f.sendNow(tx)
})
}
if err != nil {
// On error, call error callbacks
for _, clb := range f.errorCallbacks {
clb(err)
}
} else {
if f.recallIn > 0 {
go func() {
time.Sleep(time.Duration(f.recallIn) * time.Second)
_ = f.b.CancelFleet(f.fleet.ID)
}()
}
// Otherwise, call success callbacks
for _, clb := range f.successCallbacks {
clb(f.fleet)
}
}
return f.fleet, f.err
}
// OnError register an error callback
func (f *FleetBuilder) OnError(clb func(error)) *FleetBuilder {
f.errorCallbacks = append(f.errorCallbacks, clb)
return f
}
// OnSuccess register a success callback
func (f *FleetBuilder) OnSuccess(clb func(Fleet)) *FleetBuilder {
f.successCallbacks = append(f.successCallbacks, clb)
return f
}