9 Commits

Author SHA1 Message Date
be32dec202 go sum 2025-01-18 23:29:30 +01:00
2d0cb12532 protobuf 2025-01-18 23:22:19 +01:00
b44cdab611 Graceful disconnect 2025-01-18 22:39:43 +01:00
b73d8de851 Use ticker like in client network.go to simplify logic 2025-01-18 14:23:04 +01:00
23474c19dc Try to handle tcp fragmentation 2025-01-15 10:51:43 +01:00
a48fef0186 Only send last 5 chat messages 2025-01-15 10:36:36 +01:00
67e08c5d1e Readme 2025-01-13 15:12:16 +01:00
49e2311497 Merge pull request 'Implement chat' (#1) from feature/chat into master
Reviewed-on: #1
2025-01-13 13:15:25 +00:00
368fbdbc47 Implement chat 2025-01-13 13:23:05 +01:00
6 changed files with 312 additions and 66 deletions

75
README.md Normal file
View File

@ -0,0 +1,75 @@
# GoonServer
The server component for GoonScape, handling multiplayer synchronization and chat.
## Features
- Tick-based game state synchronization
- Player movement validation
- Global chat system
- Client connection management
- Protobuf-based network protocol
## Prerequisites
- Go 1.23 or higher
- Protocol Buffers compiler (protoc)
## Installation
1. Clone the repository:
```bash
git clone https://gitea.boner.be/bdnugget/goonserver.git
cd goonserver
```
2. Install dependencies:
```bash
go mod tidy
```
3. Build and run:
```bash
go run main.go
```
## Configuration
Server settings can be modified in `main.go`:
```go
const (
port = ":6969" // Port to listen on
tickRate = 600 * time.Millisecond // Server tick rate
)
```
## Protocol
The server uses Protocol Buffers for client-server communication. The protocol is defined in `actions/actions.proto`:
- `Action`: Player actions (movement, chat)
- `ActionBatch`: Grouped actions from a player
- `ServerMessage`: Game state updates to clients
- `PlayerState`: Individual player state
- `ChatMessage`: Player chat messages
## Development
After modifying the protocol (`actions.proto`), regenerate the Go code:
```bash
protoc --go_out=. actions/actions.proto
```
## Deployment
The server is designed to run on a single instance. For production deployment:
1. Build the binary:
```bash
go build -o goonserver
```
2. Run with logging:
```bash
./goonserver > server.log 2>&1 &
```

View File

@ -1,6 +1,6 @@
// Code generated by protoc-gen-go. DO NOT EDIT. // Code generated by protoc-gen-go. DO NOT EDIT.
// versions: // versions:
// protoc-gen-go v1.36.2 // protoc-gen-go v1.36.3
// protoc v5.29.2 // protoc v5.29.2
// source: actions.proto // source: actions.proto
@ -24,15 +24,21 @@ type Action_ActionType int32
const ( const (
Action_MOVE Action_ActionType = 0 Action_MOVE Action_ActionType = 0
Action_CHAT Action_ActionType = 1
Action_DISCONNECT Action_ActionType = 2
) )
// Enum value maps for Action_ActionType. // Enum value maps for Action_ActionType.
var ( var (
Action_ActionType_name = map[int32]string{ Action_ActionType_name = map[int32]string{
0: "MOVE", 0: "MOVE",
1: "CHAT",
2: "DISCONNECT",
} }
Action_ActionType_value = map[string]int32{ Action_ActionType_value = map[string]int32{
"MOVE": 0, "MOVE": 0,
"CHAT": 1,
"DISCONNECT": 2,
} }
) )
@ -69,6 +75,7 @@ type Action struct {
X int32 `protobuf:"varint,2,opt,name=x,proto3" json:"x,omitempty"` X int32 `protobuf:"varint,2,opt,name=x,proto3" json:"x,omitempty"`
Y int32 `protobuf:"varint,3,opt,name=y,proto3" json:"y,omitempty"` Y int32 `protobuf:"varint,3,opt,name=y,proto3" json:"y,omitempty"`
PlayerId int32 `protobuf:"varint,4,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"` PlayerId int32 `protobuf:"varint,4,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"`
ChatMessage string `protobuf:"bytes,5,opt,name=chat_message,json=chatMessage,proto3" json:"chat_message,omitempty"`
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
} }
@ -131,6 +138,13 @@ func (x *Action) GetPlayerId() int32 {
return 0 return 0
} }
func (x *Action) GetChatMessage() string {
if x != nil {
return x.ChatMessage
}
return ""
}
type ActionBatch struct { type ActionBatch struct {
state protoimpl.MessageState `protogen:"open.v1"` state protoimpl.MessageState `protogen:"open.v1"`
PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"` PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"`
@ -251,18 +265,79 @@ func (x *PlayerState) GetY() int32 {
return 0 return 0
} }
type ChatMessage struct {
state protoimpl.MessageState `protogen:"open.v1"`
PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"`
Content string `protobuf:"bytes,2,opt,name=content,proto3" json:"content,omitempty"`
Timestamp int64 `protobuf:"varint,3,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *ChatMessage) Reset() {
*x = ChatMessage{}
mi := &file_actions_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *ChatMessage) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ChatMessage) ProtoMessage() {}
func (x *ChatMessage) ProtoReflect() protoreflect.Message {
mi := &file_actions_proto_msgTypes[3]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use ChatMessage.ProtoReflect.Descriptor instead.
func (*ChatMessage) Descriptor() ([]byte, []int) {
return file_actions_proto_rawDescGZIP(), []int{3}
}
func (x *ChatMessage) GetPlayerId() int32 {
if x != nil {
return x.PlayerId
}
return 0
}
func (x *ChatMessage) GetContent() string {
if x != nil {
return x.Content
}
return ""
}
func (x *ChatMessage) GetTimestamp() int64 {
if x != nil {
return x.Timestamp
}
return 0
}
type ServerMessage struct { type ServerMessage struct {
state protoimpl.MessageState `protogen:"open.v1"` state protoimpl.MessageState `protogen:"open.v1"`
PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"` PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"`
Players []*PlayerState `protobuf:"bytes,2,rep,name=players,proto3" json:"players,omitempty"` Players []*PlayerState `protobuf:"bytes,2,rep,name=players,proto3" json:"players,omitempty"`
CurrentTick int64 `protobuf:"varint,3,opt,name=current_tick,json=currentTick,proto3" json:"current_tick,omitempty"` CurrentTick int64 `protobuf:"varint,3,opt,name=current_tick,json=currentTick,proto3" json:"current_tick,omitempty"`
ChatMessages []*ChatMessage `protobuf:"bytes,4,rep,name=chat_messages,json=chatMessages,proto3" json:"chat_messages,omitempty"`
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
} }
func (x *ServerMessage) Reset() { func (x *ServerMessage) Reset() {
*x = ServerMessage{} *x = ServerMessage{}
mi := &file_actions_proto_msgTypes[3] mi := &file_actions_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi) ms.StoreMessageInfo(mi)
} }
@ -274,7 +349,7 @@ func (x *ServerMessage) String() string {
func (*ServerMessage) ProtoMessage() {} func (*ServerMessage) ProtoMessage() {}
func (x *ServerMessage) ProtoReflect() protoreflect.Message { func (x *ServerMessage) ProtoReflect() protoreflect.Message {
mi := &file_actions_proto_msgTypes[3] mi := &file_actions_proto_msgTypes[4]
if x != nil { if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil { if ms.LoadMessageInfo() == nil {
@ -287,7 +362,7 @@ func (x *ServerMessage) ProtoReflect() protoreflect.Message {
// Deprecated: Use ServerMessage.ProtoReflect.Descriptor instead. // Deprecated: Use ServerMessage.ProtoReflect.Descriptor instead.
func (*ServerMessage) Descriptor() ([]byte, []int) { func (*ServerMessage) Descriptor() ([]byte, []int) {
return file_actions_proto_rawDescGZIP(), []int{3} return file_actions_proto_rawDescGZIP(), []int{4}
} }
func (x *ServerMessage) GetPlayerId() int32 { func (x *ServerMessage) GetPlayerId() int32 {
@ -311,42 +386,63 @@ func (x *ServerMessage) GetCurrentTick() int64 {
return 0 return 0
} }
func (x *ServerMessage) GetChatMessages() []*ChatMessage {
if x != nil {
return x.ChatMessages
}
return nil
}
var File_actions_proto protoreflect.FileDescriptor var File_actions_proto protoreflect.FileDescriptor
var file_actions_proto_rawDesc = []byte{ var file_actions_proto_rawDesc = []byte{
0x0a, 0x0d, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0a, 0x0d, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12,
0x07, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x89, 0x01, 0x0a, 0x06, 0x41, 0x63, 0x74, 0x07, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0xc6, 0x01, 0x0a, 0x06, 0x41, 0x63, 0x74,
0x69, 0x6f, 0x6e, 0x12, 0x2e, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x69, 0x6f, 0x6e, 0x12, 0x2e, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28,
0x0e, 0x32, 0x1a, 0x2e, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x41, 0x63, 0x74, 0x69, 0x0e, 0x32, 0x1a, 0x2e, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x41, 0x63, 0x74, 0x69,
0x6f, 0x6e, 0x2e, 0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x6f, 0x6e, 0x2e, 0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74,
0x79, 0x70, 0x65, 0x12, 0x0c, 0x0a, 0x01, 0x78, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x79, 0x70, 0x65, 0x12, 0x0c, 0x0a, 0x01, 0x78, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01,
0x78, 0x12, 0x0c, 0x0a, 0x01, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x79, 0x12, 0x78, 0x12, 0x0c, 0x0a, 0x01, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x79, 0x12,
0x1b, 0x0a, 0x09, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x1b, 0x0a, 0x09, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01,
0x28, 0x05, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x64, 0x22, 0x16, 0x0a, 0x0a, 0x28, 0x05, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x64, 0x12, 0x21, 0x0a, 0x0c,
0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x12, 0x08, 0x0a, 0x04, 0x4d, 0x4f, 0x63, 0x68, 0x61, 0x74, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x05, 0x20, 0x01,
0x56, 0x45, 0x10, 0x00, 0x22, 0x69, 0x0a, 0x0b, 0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x42, 0x61, 0x28, 0x09, 0x52, 0x0b, 0x63, 0x68, 0x61, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22,
0x74, 0x63, 0x68, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x30, 0x0a, 0x0a, 0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x12, 0x08, 0x0a,
0x04, 0x4d, 0x4f, 0x56, 0x45, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x43, 0x48, 0x41, 0x54, 0x10,
0x01, 0x12, 0x0e, 0x0a, 0x0a, 0x44, 0x49, 0x53, 0x43, 0x4f, 0x4e, 0x4e, 0x45, 0x43, 0x54, 0x10,
0x02, 0x22, 0x69, 0x0a, 0x0b, 0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x42, 0x61, 0x74, 0x63, 0x68,
0x12, 0x1b, 0x0a, 0x09, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20,
0x01, 0x28, 0x05, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x64, 0x12, 0x29, 0x0a,
0x07, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0f,
0x2e, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x52,
0x07, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x69, 0x63, 0x6b,
0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x74, 0x69, 0x63, 0x6b, 0x22, 0x46, 0x0a, 0x0b,
0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x70,
0x6c, 0x61, 0x79, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08,
0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x64, 0x12, 0x0c, 0x0a, 0x01, 0x78, 0x18, 0x02, 0x20,
0x01, 0x28, 0x05, 0x52, 0x01, 0x78, 0x12, 0x0c, 0x0a, 0x01, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28,
0x05, 0x52, 0x01, 0x79, 0x22, 0x62, 0x0a, 0x0b, 0x43, 0x68, 0x61, 0x74, 0x4d, 0x65, 0x73, 0x73,
0x61, 0x67, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x5f, 0x69, 0x64,
0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x64,
0x12, 0x29, 0x0a, 0x07, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28,
0x0b, 0x32, 0x0f, 0x2e, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x41, 0x63, 0x74, 0x69, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x69,
0x6f, 0x6e, 0x52, 0x07, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x74,
0x69, 0x63, 0x6b, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x74, 0x69, 0x63, 0x6b, 0x22, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x22, 0xba, 0x01, 0x0a, 0x0d, 0x53, 0x65, 0x72,
0x46, 0x0a, 0x0b, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x1b, 0x76, 0x65, 0x72, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x6c,
0x0a, 0x09, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x61, 0x79, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x70,
0x05, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x64, 0x12, 0x0c, 0x0a, 0x01, 0x78, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x64, 0x12, 0x2e, 0x0a, 0x07, 0x70, 0x6c, 0x61, 0x79, 0x65,
0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x78, 0x12, 0x0c, 0x0a, 0x01, 0x79, 0x18, 0x03, 0x72, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x61, 0x63, 0x74, 0x69, 0x6f,
0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x79, 0x22, 0x7f, 0x0a, 0x0d, 0x53, 0x65, 0x72, 0x76, 0x65, 0x6e, 0x73, 0x2e, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x07,
0x72, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x6c, 0x61, 0x79, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x73, 0x12, 0x21, 0x0a, 0x0c, 0x63, 0x75, 0x72, 0x72, 0x65,
0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x6e, 0x74, 0x5f, 0x74, 0x69, 0x63, 0x6b, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0b, 0x63,
0x79, 0x65, 0x72, 0x49, 0x64, 0x12, 0x2e, 0x0a, 0x07, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x73, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x54, 0x69, 0x63, 0x6b, 0x12, 0x39, 0x0a, 0x0d, 0x63, 0x68,
0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x61, 0x74, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28,
0x2e, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x07, 0x70, 0x6c, 0x0b, 0x32, 0x14, 0x2e, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x43, 0x68, 0x61, 0x74,
0x61, 0x79, 0x65, 0x72, 0x73, 0x12, 0x21, 0x0a, 0x0c, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x0c, 0x63, 0x68, 0x61, 0x74, 0x4d, 0x65, 0x73,
0x5f, 0x74, 0x69, 0x63, 0x6b, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0b, 0x63, 0x75, 0x72, 0x73, 0x61, 0x67, 0x65, 0x73, 0x42, 0x2c, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x65, 0x61, 0x2e, 0x62,
0x72, 0x65, 0x6e, 0x74, 0x54, 0x69, 0x63, 0x6b, 0x42, 0x2c, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x65, 0x6f, 0x6e, 0x65, 0x72, 0x2e, 0x62, 0x65, 0x2f, 0x62, 0x64, 0x6e, 0x75, 0x67, 0x67, 0x65, 0x74,
0x61, 0x2e, 0x62, 0x6f, 0x6e, 0x65, 0x72, 0x2e, 0x62, 0x65, 0x2f, 0x62, 0x64, 0x6e, 0x75, 0x67, 0x2f, 0x67, 0x6f, 0x6f, 0x6e, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2f, 0x61, 0x63, 0x74, 0x69,
0x67, 0x65, 0x74, 0x2f, 0x67, 0x6f, 0x6f, 0x6e, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2f, 0x61, 0x6f, 0x6e, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (
@ -362,23 +458,25 @@ func file_actions_proto_rawDescGZIP() []byte {
} }
var file_actions_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_actions_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
var file_actions_proto_msgTypes = make([]protoimpl.MessageInfo, 4) var file_actions_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
var file_actions_proto_goTypes = []any{ var file_actions_proto_goTypes = []any{
(Action_ActionType)(0), // 0: actions.Action.ActionType (Action_ActionType)(0), // 0: actions.Action.ActionType
(*Action)(nil), // 1: actions.Action (*Action)(nil), // 1: actions.Action
(*ActionBatch)(nil), // 2: actions.ActionBatch (*ActionBatch)(nil), // 2: actions.ActionBatch
(*PlayerState)(nil), // 3: actions.PlayerState (*PlayerState)(nil), // 3: actions.PlayerState
(*ServerMessage)(nil), // 4: actions.ServerMessage (*ChatMessage)(nil), // 4: actions.ChatMessage
(*ServerMessage)(nil), // 5: actions.ServerMessage
} }
var file_actions_proto_depIdxs = []int32{ var file_actions_proto_depIdxs = []int32{
0, // 0: actions.Action.type:type_name -> actions.Action.ActionType 0, // 0: actions.Action.type:type_name -> actions.Action.ActionType
1, // 1: actions.ActionBatch.actions:type_name -> actions.Action 1, // 1: actions.ActionBatch.actions:type_name -> actions.Action
3, // 2: actions.ServerMessage.players:type_name -> actions.PlayerState 3, // 2: actions.ServerMessage.players:type_name -> actions.PlayerState
3, // [3:3] is the sub-list for method output_type 4, // 3: actions.ServerMessage.chat_messages:type_name -> actions.ChatMessage
3, // [3:3] is the sub-list for method input_type 4, // [4:4] is the sub-list for method output_type
3, // [3:3] is the sub-list for extension type_name 4, // [4:4] is the sub-list for method input_type
3, // [3:3] is the sub-list for extension extendee 4, // [4:4] is the sub-list for extension type_name
0, // [0:3] is the sub-list for field type_name 4, // [4:4] is the sub-list for extension extendee
0, // [0:4] is the sub-list for field type_name
} }
func init() { file_actions_proto_init() } func init() { file_actions_proto_init() }
@ -392,7 +490,7 @@ func file_actions_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(), GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_actions_proto_rawDesc, RawDescriptor: file_actions_proto_rawDesc,
NumEnums: 1, NumEnums: 1,
NumMessages: 4, NumMessages: 5,
NumExtensions: 0, NumExtensions: 0,
NumServices: 0, NumServices: 0,
}, },

View File

@ -7,12 +7,15 @@ option go_package = "gitea.boner.be/bdnugget/goonserver/actions";
message Action { message Action {
enum ActionType { enum ActionType {
MOVE = 0; MOVE = 0;
CHAT = 1;
DISCONNECT = 2;
} }
ActionType type = 1; ActionType type = 1;
int32 x = 2; int32 x = 2;
int32 y = 3; int32 y = 3;
int32 player_id = 4; int32 player_id = 4;
string chat_message = 5;
} }
message ActionBatch { message ActionBatch {
@ -27,8 +30,15 @@ message PlayerState {
int32 y = 3; int32 y = 3;
} }
message ChatMessage {
int32 player_id = 1;
string content = 2;
int64 timestamp = 3;
}
message ServerMessage { message ServerMessage {
int32 player_id = 1; int32 player_id = 1;
repeated PlayerState players = 2; repeated PlayerState players = 2;
int64 current_tick = 3; int64 current_tick = 3;
repeated ChatMessage chat_messages = 4;
} }

2
go.mod
View File

@ -2,4 +2,4 @@ module gitea.boner.be/bdnugget/goonserver
go 1.23.0 go 1.23.0
require google.golang.org/protobuf v1.35.1 require google.golang.org/protobuf v1.36.3

4
go.sum
View File

@ -2,5 +2,5 @@ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= google.golang.org/protobuf v1.36.3 h1:82DV7MYdb8anAVi3qge1wSnMDrnKK7ebr+I0hHRN1BU=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= google.golang.org/protobuf v1.36.3/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=

109
main.go
View File

@ -1,7 +1,10 @@
package main package main
import ( import (
"bufio"
"encoding/binary"
"fmt" "fmt"
"io"
"log" "log"
"net" "net"
"sync" "sync"
@ -28,6 +31,8 @@ var (
actionQueue = make(map[int][]*pb.Action) // Queue to store actions for each player actionQueue = make(map[int][]*pb.Action) // Queue to store actions for each player
playerConns = make(map[int]net.Conn) // Map to store player connections playerConns = make(map[int]net.Conn) // Map to store player connections
mu sync.RWMutex // Add mutex for protecting shared maps mu sync.RWMutex // Add mutex for protecting shared maps
chatHistory = make([]*pb.ChatMessage, 0, 100)
chatMutex sync.RWMutex
) )
func main() { func main() {
@ -38,6 +43,11 @@ func main() {
defer ln.Close() defer ln.Close()
fmt.Printf("Server is listening on port %s\n", port) fmt.Printf("Server is listening on port %s\n", port)
// Create ticker for fixed game state updates
ticker := time.NewTicker(tickRate)
defer ticker.Stop()
// Handle incoming connections in a separate goroutine
go func() { go func() {
for { for {
conn, err := ln.Accept() conn, err := ln.Accept()
@ -49,14 +59,11 @@ func main() {
} }
}() }()
lastTick := time.Now() // Main game loop
for { for range ticker.C {
if time.Since(lastTick) >= tickRate {
lastTick = time.Now()
processActions() processActions()
} }
} }
}
func handleConnection(conn net.Conn) { func handleConnection(conn net.Conn) {
defer conn.Close() defer conn.Close()
@ -74,22 +81,29 @@ func handleConnection(conn net.Conn) {
PlayerId: int32(playerID), PlayerId: int32(playerID),
CurrentTick: 0, CurrentTick: 0,
} }
data, err := proto.Marshal(serverMsg) if err := writeMessage(conn, serverMsg); err != nil {
if err != nil {
log.Printf("Failed to marshal ServerMessage for player %d: %v", playerID, err)
return
}
if _, err := conn.Write(data); err != nil {
log.Printf("Failed to send player ID to player %d: %v", playerID, err) log.Printf("Failed to send player ID to player %d: %v", playerID, err)
return return
} }
// Listen for incoming actions from this player // Listen for incoming actions from this player
buf := make([]byte, 4096) reader := bufio.NewReader(conn)
for { for {
n, err := conn.Read(buf) // Read message length
if err != nil { lengthBuf := make([]byte, 4)
log.Printf("Error reading from player %d: %v", playerID, err) if _, err := io.ReadFull(reader, lengthBuf); err != nil {
log.Printf("Error reading message length from player %d: %v", playerID, err)
delete(players, playerID)
delete(playerConns, playerID)
delete(actionQueue, playerID)
return
}
messageLength := binary.BigEndian.Uint32(lengthBuf)
// Read message body
messageBuf := make([]byte, messageLength)
if _, err := io.ReadFull(reader, messageBuf); err != nil {
log.Printf("Error reading message from player %d: %v", playerID, err)
delete(players, playerID) delete(players, playerID)
delete(playerConns, playerID) delete(playerConns, playerID)
delete(actionQueue, playerID) delete(actionQueue, playerID)
@ -97,18 +111,43 @@ func handleConnection(conn net.Conn) {
} }
batch := &pb.ActionBatch{} batch := &pb.ActionBatch{}
if err := proto.Unmarshal(buf[:n], batch); err != nil { if err := proto.Unmarshal(messageBuf, batch); err != nil {
log.Printf("Failed to unmarshal action batch for player %d: %v", playerID, err) log.Printf("Failed to unmarshal action batch for player %d: %v", playerID, err)
continue continue
} }
// Queue the actions for processing // Queue the actions for processing
if batch.PlayerId == int32(playerID) { if batch.PlayerId == int32(playerID) {
for _, action := range batch.Actions {
if action.Type == pb.Action_DISCONNECT {
log.Printf("Player %d disconnected gracefully", playerID)
delete(players, playerID)
delete(playerConns, playerID)
delete(actionQueue, playerID)
return
}
}
actionQueue[playerID] = append(actionQueue[playerID], batch.Actions...) actionQueue[playerID] = append(actionQueue[playerID], batch.Actions...)
} }
} }
} }
func addChatMessage(playerID int32, content string) {
chatMutex.Lock()
defer chatMutex.Unlock()
msg := &pb.ChatMessage{
PlayerId: playerID,
Content: content,
Timestamp: time.Now().UnixNano(),
}
if len(chatHistory) >= 100 {
chatHistory = chatHistory[1:]
}
chatHistory = append(chatHistory, msg)
}
func processActions() { func processActions() {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
@ -118,10 +157,14 @@ func processActions() {
player := players[playerID] player := players[playerID]
player.Lock() player.Lock()
for _, action := range actions { for _, action := range actions {
if action.Type == pb.Action_MOVE { switch action.Type {
case pb.Action_MOVE:
player.X = int(action.X) player.X = int(action.X)
player.Y = int(action.Y) player.Y = int(action.Y)
fmt.Printf("Player %d moved to (%d, %d)\n", playerID, player.X, player.Y) fmt.Printf("Player %d moved to (%d, %d)\n", playerID, player.X, player.Y)
case pb.Action_CHAT:
addChatMessage(int32(playerID), action.ChatMessage)
fmt.Printf("Player %d says: %s\n", playerID, action.ChatMessage)
} }
} }
player.Unlock() player.Unlock()
@ -132,8 +175,10 @@ func processActions() {
currentTick := time.Now().UnixNano() / int64(tickRate) currentTick := time.Now().UnixNano() / int64(tickRate)
state := &pb.ServerMessage{ state := &pb.ServerMessage{
CurrentTick: currentTick, CurrentTick: currentTick,
Players: make([]*pb.PlayerState, 0, len(players)),
} }
// Convert players to PlayerState
for id, p := range players { for id, p := range players {
p.Lock() p.Lock()
state.Players = append(state.Players, &pb.PlayerState{ state.Players = append(state.Players, &pb.PlayerState{
@ -144,16 +189,34 @@ func processActions() {
p.Unlock() p.Unlock()
} }
data, err := proto.Marshal(state) // Add chat messages to the state
if err != nil { chatMutex.RLock()
log.Printf("Failed to marshal game state: %v", err) state.ChatMessages = chatHistory[max(0, len(chatHistory)-5):] // Only send last 5 messages
return chatMutex.RUnlock()
}
// Send to each connected player // Send to each connected player
for _, conn := range playerConns { for _, conn := range playerConns {
if _, err := conn.Write(data); err != nil { if err := writeMessage(conn, state); err != nil {
log.Printf("Failed to send update: %v", err) log.Printf("Failed to send update: %v", err)
} }
} }
} }
// Helper function to write length-prefixed messages
func writeMessage(conn net.Conn, msg proto.Message) error {
data, err := proto.Marshal(msg)
if err != nil {
return err
}
// Write length prefix
lengthBuf := make([]byte, 4)
binary.BigEndian.PutUint32(lengthBuf, uint32(len(data)))
if _, err := conn.Write(lengthBuf); err != nil {
return err
}
// Write message body
_, err = conn.Write(data)
return err
}