-
Notifications
You must be signed in to change notification settings - Fork 0
/
cause_of_death.go
50 lines (46 loc) · 1.56 KB
/
cause_of_death.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
package sirpent
import (
"github.com/davecgh/go-spew/spew"
"net"
)
// Useful for debugging and statistics.
type CauseOfDeath struct {
// Death error.
Err string `json:"error"`
// Tick that the player died in.
// @TODO: Implement setting this.
//DiedAt TickID `json:"died_at"`
// Did the player died because the connection to their server dropped or timed out?
SocketProblem bool `json:"socket_problem"`
SocketTimeout bool `json:"socket_timeout"`
// Did the player die because they specified an unrecognised direction?
InvalidMove bool `json:"invalid_move"`
// Did the player die because they collided with a Player (including themself)? Or nil.
CollisionWithPlayerID *UUID `json:"collision_with_player"`
// Did the player die because they went beyond the boundaries of the world?
CollidedWithBounds bool `json:"collided_with_bounds"`
}
func (cod *CauseOfDeath) DiagnoseError(err error) {
cod.Err = err.Error()
if net_err, ok := err.(net.Error); ok {
if net_err.Timeout() {
cod.SocketTimeout = true
} else {
cod.SocketProblem = true
}
} else if _, ok := err.(DirectionError); ok {
cod.InvalidMove = true
} else if collision_err, ok := err.(CollisionError); ok {
if collision_err.CollidedWithBounds() {
cod.CollidedWithBounds = true
}
if collision_err.CollidedWithPlayer() {
cod.CollisionWithPlayerID = collision_err.CollidedWithPlayerID
}
}
}
// Spew is useful to neatly present Cause Of Death. But it cannot be the String() method.
// https://github.com/davecgh/go-spew/issues/45
func (cod CauseOfDeath) Spew() string {
return spew.Sdump(cod)
}