forked from teeworlds-go/protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
173 lines (145 loc) · 3.99 KB
/
client.go
File metadata and controls
173 lines (145 loc) · 3.99 KB
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
package teeworlds7
import (
"context"
"fmt"
"log"
"net"
"time"
"github.com/teeworlds-go/protocol/messages7"
"github.com/teeworlds-go/protocol/network7"
"github.com/teeworlds-go/protocol/object7"
"github.com/teeworlds-go/protocol/protocol7"
"github.com/teeworlds-go/protocol/snapshot7"
)
const (
UnknownClientId = -1
)
type Player struct {
Info messages7.SvClientInfo
}
type Game struct {
Players []Player
Snap *GameSnap
Input *messages7.Input
LastSentInput messages7.Input
}
type Client struct {
Name string
Clan string
Country int
// chunks to be sent on next packet send
// use client.SendMessage() to put your chunks here
QueuedMessages []messages7.NetMessage
// hooks from the user
Callbacks UserMsgCallbacks
// udp connection
Conn net.Conn
// when the last packet was sent
// tracked to know when to send keepalives
LastSend time.Time
LastInputSend time.Time
// teeworlds session
Session protocol7.Session
// old snapshots used to unpack new deltas
SnapshotStorage *snapshot7.Storage
// teeworlds game state
Game Game
// might be -1 if we do not know our own id yet
LocalClientId int
// cancelation & graceful shutdown handling
// These fields usually do not need to be accessed by the user
// They might only be needed in case you want to change the default behavior
// of the OnDisconnect callback
Ctx context.Context
CancelCause context.CancelCauseFunc
Logger *log.Logger
}
// TODO: add this for all items and move it to a different file
//
// this would be more useful to have on the Snapshot struct directly
// so it can be used everywhere not only in a client
// and the client then can just wrap it to acces the alt snap
func (client *Client) SnapFindCharacter(clientId int) (character *object7.Character, found bool, err error) {
item, found, err := client.SnapshotStorage.FindAltSnapItem(network7.ObjCharacter, clientId)
if err != nil {
return nil, false, err
}
if !found {
return nil, false, nil
}
character, ok := item.(*object7.Character)
if !ok {
panic(fmt.Sprintf("type assertion failed: found client snap item is not a *object7.Character: %T", item))
}
return character, true, nil
}
func NewClient() *Client {
return &Client{
Name: "nameless tee",
SnapshotStorage: snapshot7.NewStorage(),
Game: Game{
Snap: &GameSnap{},
Input: &messages7.Input{},
},
LocalClientId: UnknownClientId,
LastSend: time.Now(),
Logger: log.Default(),
}
}
func (client *Client) sendInputIfNeeded() (sent bool, err error) {
send := false
// at least every 10hz or on change
if time.Since(client.LastSend) > 100*time.Millisecond {
send = true
} else if client.Game.Input != nil && client.Game.LastSentInput != *client.Game.Input {
send = true
}
if !send {
return false, nil
}
err = client.SendInput()
if err != nil {
return false, err
}
return true, nil
}
func (client *Client) defaultGameTickAction() error {
// either input or keepalive
sent, err := client.sendInputIfNeeded()
if err != nil {
return fmt.Errorf("failed to send input: %w", err)
} else if sent {
return nil
}
// keepalive in case we did not send anything
// rounded to seconds, which is why at least 3 seconds need to pass before
// another keepalive is sent
if time.Since(client.LastSend).Seconds() > 2 {
err = client.SendKeepAlive()
if err != nil {
return fmt.Errorf("failed to send keepalive: %w", err)
}
}
return nil
}
func (client *Client) gameTick() error {
var err error
for _, callback := range client.Callbacks.Tick {
err = callback(client.defaultGameTickAction) // dependency injection
if err != nil {
return err
}
}
return nil
}
func (client *Client) handleInternalError(userError error) error {
err := userError // if this is not overwritten by a callback, it will be returned
for _, callback := range client.Callbacks.InternalError {
// first callback to return non-nil error will be returned
err = callback(userError)
if err != nil {
return err
}
}
return err
}