Compare commits
12 Commits
develop
...
b44cdab611
Author | SHA1 | Date | |
---|---|---|---|
b44cdab611 | |||
b73d8de851 | |||
23474c19dc | |||
a48fef0186 | |||
67e08c5d1e | |||
49e2311497 | |||
368fbdbc47 | |||
4b73492ffc | |||
a459e8b4a5 | |||
8290131998 | |||
f91f72c05d | |||
1d6d3ab2ea |
75
README.md
Normal file
75
README.md
Normal 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 &
|
||||
```
|
@ -1,7 +1,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.28.1
|
||||
// protoc v3.21.12
|
||||
// protoc-gen-go v1.36.3
|
||||
// protoc v5.29.2
|
||||
// source: actions.proto
|
||||
|
||||
package actions
|
||||
@ -23,16 +23,22 @@ const (
|
||||
type Action_ActionType int32
|
||||
|
||||
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.
|
||||
var (
|
||||
Action_ActionType_name = map[int32]string{
|
||||
0: "MOVE",
|
||||
1: "CHAT",
|
||||
2: "DISCONNECT",
|
||||
}
|
||||
Action_ActionType_value = map[string]int32{
|
||||
"MOVE": 0,
|
||||
"MOVE": 0,
|
||||
"CHAT": 1,
|
||||
"DISCONNECT": 2,
|
||||
}
|
||||
)
|
||||
|
||||
@ -64,23 +70,21 @@ func (Action_ActionType) EnumDescriptor() ([]byte, []int) {
|
||||
}
|
||||
|
||||
type Action struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Type Action_ActionType `protobuf:"varint,1,opt,name=type,proto3,enum=actions.Action_ActionType" json:"type,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"`
|
||||
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
|
||||
|
||||
Type Action_ActionType `protobuf:"varint,1,opt,name=type,proto3,enum=actions.Action_ActionType" json:"type,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"`
|
||||
PlayerId int32 `protobuf:"varint,4,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"`
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *Action) Reset() {
|
||||
*x = Action{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_actions_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
mi := &file_actions_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *Action) String() string {
|
||||
@ -91,7 +95,7 @@ func (*Action) ProtoMessage() {}
|
||||
|
||||
func (x *Action) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_actions_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
@ -134,33 +138,38 @@ func (x *Action) GetPlayerId() int32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
type ServerMessage struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"` // Only used when initially assigning player ID
|
||||
Players []*PlayerState `protobuf:"bytes,2,rep,name=players,proto3" json:"players,omitempty"` // Player state updates
|
||||
}
|
||||
|
||||
func (x *ServerMessage) Reset() {
|
||||
*x = ServerMessage{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_actions_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
func (x *Action) GetChatMessage() string {
|
||||
if x != nil {
|
||||
return x.ChatMessage
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *ServerMessage) String() string {
|
||||
type ActionBatch struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"`
|
||||
Actions []*Action `protobuf:"bytes,2,rep,name=actions,proto3" json:"actions,omitempty"`
|
||||
Tick int64 `protobuf:"varint,3,opt,name=tick,proto3" json:"tick,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *ActionBatch) Reset() {
|
||||
*x = ActionBatch{}
|
||||
mi := &file_actions_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *ActionBatch) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*ServerMessage) ProtoMessage() {}
|
||||
func (*ActionBatch) ProtoMessage() {}
|
||||
|
||||
func (x *ServerMessage) ProtoReflect() protoreflect.Message {
|
||||
func (x *ActionBatch) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_actions_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
@ -170,42 +179,46 @@ func (x *ServerMessage) ProtoReflect() protoreflect.Message {
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use ServerMessage.ProtoReflect.Descriptor instead.
|
||||
func (*ServerMessage) Descriptor() ([]byte, []int) {
|
||||
// Deprecated: Use ActionBatch.ProtoReflect.Descriptor instead.
|
||||
func (*ActionBatch) Descriptor() ([]byte, []int) {
|
||||
return file_actions_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *ServerMessage) GetPlayerId() int32 {
|
||||
func (x *ActionBatch) GetPlayerId() int32 {
|
||||
if x != nil {
|
||||
return x.PlayerId
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *ServerMessage) GetPlayers() []*PlayerState {
|
||||
func (x *ActionBatch) GetActions() []*Action {
|
||||
if x != nil {
|
||||
return x.Players
|
||||
return x.Actions
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type PlayerState struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
func (x *ActionBatch) GetTick() int64 {
|
||||
if x != nil {
|
||||
return x.Tick
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,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"`
|
||||
type PlayerState struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,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"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *PlayerState) Reset() {
|
||||
*x = PlayerState{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_actions_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
mi := &file_actions_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *PlayerState) String() string {
|
||||
@ -216,7 +229,7 @@ func (*PlayerState) ProtoMessage() {}
|
||||
|
||||
func (x *PlayerState) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_actions_proto_msgTypes[2]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
@ -252,33 +265,184 @@ func (x *PlayerState) GetY() int32 {
|
||||
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 {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
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"`
|
||||
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
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *ServerMessage) Reset() {
|
||||
*x = ServerMessage{}
|
||||
mi := &file_actions_proto_msgTypes[4]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *ServerMessage) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*ServerMessage) ProtoMessage() {}
|
||||
|
||||
func (x *ServerMessage) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_actions_proto_msgTypes[4]
|
||||
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 ServerMessage.ProtoReflect.Descriptor instead.
|
||||
func (*ServerMessage) Descriptor() ([]byte, []int) {
|
||||
return file_actions_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *ServerMessage) GetPlayerId() int32 {
|
||||
if x != nil {
|
||||
return x.PlayerId
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *ServerMessage) GetPlayers() []*PlayerState {
|
||||
if x != nil {
|
||||
return x.Players
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *ServerMessage) GetCurrentTick() int64 {
|
||||
if x != nil {
|
||||
return x.CurrentTick
|
||||
}
|
||||
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_rawDesc = []byte{
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x12, 0x08, 0x0a, 0x04, 0x4d, 0x4f,
|
||||
0x56, 0x45, 0x10, 0x00, 0x22, 0x5c, 0x0a, 0x0d, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 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, 0x12, 0x2e, 0x0a, 0x07, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x73, 0x18, 0x02, 0x20,
|
||||
0x03, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x50, 0x6c,
|
||||
0x61, 0x79, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x07, 0x70, 0x6c, 0x61, 0x79, 0x65,
|
||||
0x72, 0x73, 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, 0x42, 0x2c, 0x5a, 0x2a, 0x67, 0x69,
|
||||
0x74, 0x65, 0x61, 0x2e, 0x62, 0x6f, 0x6e, 0x65, 0x72, 0x2e, 0x62, 0x65, 0x2f, 0x62, 0x64, 0x6e,
|
||||
0x75, 0x67, 0x67, 0x65, 0x74, 0x2f, 0x67, 0x6f, 0x6f, 0x6e, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72,
|
||||
0x2f, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x28, 0x05, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x64, 0x12, 0x21, 0x0a, 0x0c,
|
||||
0x63, 0x68, 0x61, 0x74, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x05, 0x20, 0x01,
|
||||
0x28, 0x09, 0x52, 0x0b, 0x63, 0x68, 0x61, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22,
|
||||
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,
|
||||
0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28,
|
||||
0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x69,
|
||||
0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x74,
|
||||
0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x22, 0xba, 0x01, 0x0a, 0x0d, 0x53, 0x65, 0x72,
|
||||
0x76, 0x65, 0x72, 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, 0x12, 0x2e, 0x0a, 0x07, 0x70, 0x6c, 0x61, 0x79, 0x65,
|
||||
0x72, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x61, 0x63, 0x74, 0x69, 0x6f,
|
||||
0x6e, 0x73, 0x2e, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x07,
|
||||
0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x73, 0x12, 0x21, 0x0a, 0x0c, 0x63, 0x75, 0x72, 0x72, 0x65,
|
||||
0x6e, 0x74, 0x5f, 0x74, 0x69, 0x63, 0x6b, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0b, 0x63,
|
||||
0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x54, 0x69, 0x63, 0x6b, 0x12, 0x39, 0x0a, 0x0d, 0x63, 0x68,
|
||||
0x61, 0x74, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28,
|
||||
0x0b, 0x32, 0x14, 0x2e, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x43, 0x68, 0x61, 0x74,
|
||||
0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x0c, 0x63, 0x68, 0x61, 0x74, 0x4d, 0x65, 0x73,
|
||||
0x73, 0x61, 0x67, 0x65, 0x73, 0x42, 0x2c, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x65, 0x61, 0x2e, 0x62,
|
||||
0x6f, 0x6e, 0x65, 0x72, 0x2e, 0x62, 0x65, 0x2f, 0x62, 0x64, 0x6e, 0x75, 0x67, 0x67, 0x65, 0x74,
|
||||
0x2f, 0x67, 0x6f, 0x6f, 0x6e, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2f, 0x61, 0x63, 0x74, 0x69,
|
||||
0x6f, 0x6e, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
@ -294,21 +458,25 @@ func file_actions_proto_rawDescGZIP() []byte {
|
||||
}
|
||||
|
||||
var file_actions_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
|
||||
var file_actions_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
|
||||
var file_actions_proto_goTypes = []interface{}{
|
||||
var file_actions_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
|
||||
var file_actions_proto_goTypes = []any{
|
||||
(Action_ActionType)(0), // 0: actions.Action.ActionType
|
||||
(*Action)(nil), // 1: actions.Action
|
||||
(*ServerMessage)(nil), // 2: actions.ServerMessage
|
||||
(*ActionBatch)(nil), // 2: actions.ActionBatch
|
||||
(*PlayerState)(nil), // 3: actions.PlayerState
|
||||
(*ChatMessage)(nil), // 4: actions.ChatMessage
|
||||
(*ServerMessage)(nil), // 5: actions.ServerMessage
|
||||
}
|
||||
var file_actions_proto_depIdxs = []int32{
|
||||
0, // 0: actions.Action.type:type_name -> actions.Action.ActionType
|
||||
3, // 1: actions.ServerMessage.players:type_name -> actions.PlayerState
|
||||
2, // [2:2] is the sub-list for method output_type
|
||||
2, // [2:2] is the sub-list for method input_type
|
||||
2, // [2:2] is the sub-list for extension type_name
|
||||
2, // [2:2] is the sub-list for extension extendee
|
||||
0, // [0:2] is the sub-list for field type_name
|
||||
1, // 1: actions.ActionBatch.actions:type_name -> actions.Action
|
||||
3, // 2: actions.ServerMessage.players:type_name -> actions.PlayerState
|
||||
4, // 3: actions.ServerMessage.chat_messages:type_name -> actions.ChatMessage
|
||||
4, // [4:4] is the sub-list for method output_type
|
||||
4, // [4:4] is the sub-list for method input_type
|
||||
4, // [4:4] is the sub-list for extension 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() }
|
||||
@ -316,51 +484,13 @@ func file_actions_proto_init() {
|
||||
if File_actions_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_actions_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*Action); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_actions_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*ServerMessage); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_actions_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*PlayerState); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_actions_proto_rawDesc,
|
||||
NumEnums: 1,
|
||||
NumMessages: 3,
|
||||
NumMessages: 5,
|
||||
NumExtensions: 0,
|
||||
NumServices: 0,
|
||||
},
|
||||
|
@ -7,17 +7,21 @@ option go_package = "gitea.boner.be/bdnugget/goonserver/actions";
|
||||
message Action {
|
||||
enum ActionType {
|
||||
MOVE = 0;
|
||||
CHAT = 1;
|
||||
DISCONNECT = 2;
|
||||
}
|
||||
|
||||
ActionType type = 1;
|
||||
int32 x = 2;
|
||||
int32 y = 3;
|
||||
int32 player_id = 4;
|
||||
string chat_message = 5;
|
||||
}
|
||||
|
||||
message ServerMessage {
|
||||
int32 player_id = 1; // Only used when initially assigning player ID
|
||||
repeated PlayerState players = 2; // Player state updates
|
||||
message ActionBatch {
|
||||
int32 player_id = 1;
|
||||
repeated Action actions = 2;
|
||||
int64 tick = 3;
|
||||
}
|
||||
|
||||
message PlayerState {
|
||||
@ -25,3 +29,16 @@ message PlayerState {
|
||||
int32 x = 2;
|
||||
int32 y = 3;
|
||||
}
|
||||
|
||||
message ChatMessage {
|
||||
int32 player_id = 1;
|
||||
string content = 2;
|
||||
int64 timestamp = 3;
|
||||
}
|
||||
|
||||
message ServerMessage {
|
||||
int32 player_id = 1;
|
||||
repeated PlayerState players = 2;
|
||||
int64 current_tick = 3;
|
||||
repeated ChatMessage chat_messages = 4;
|
||||
}
|
||||
|
169
main.go
169
main.go
@ -1,9 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
pb "gitea.boner.be/bdnugget/goonserver/actions"
|
||||
@ -17,12 +21,19 @@ const (
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
sync.Mutex
|
||||
ID int
|
||||
X, Y int // Position on the game grid
|
||||
}
|
||||
|
||||
var players = make(map[int]*Player)
|
||||
var actionQueue = make(map[int][]*pb.Action) // Queue to store actions for each player
|
||||
var (
|
||||
players = make(map[int]*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
|
||||
mu sync.RWMutex // Add mutex for protecting shared maps
|
||||
chatHistory = make([]*pb.ChatMessage, 0, 100)
|
||||
chatMutex sync.RWMutex
|
||||
)
|
||||
|
||||
func main() {
|
||||
ln, err := net.Listen("tcp", port)
|
||||
@ -32,6 +43,11 @@ func main() {
|
||||
defer ln.Close()
|
||||
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() {
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
@ -43,69 +59,164 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
lastTick := time.Now()
|
||||
for {
|
||||
if time.Since(lastTick) >= tickRate {
|
||||
lastTick = time.Now()
|
||||
// log.Printf("Last tick: %s", lastTick)
|
||||
processActions()
|
||||
}
|
||||
// Main game loop
|
||||
for range ticker.C {
|
||||
processActions()
|
||||
}
|
||||
}
|
||||
|
||||
func handleConnection(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
|
||||
// Assign a new player ID and add the player to the game state
|
||||
mu.Lock()
|
||||
playerID := len(players) + 1
|
||||
players[playerID] = &Player{ID: playerID, X: 5, Y: 5} // Start at default position
|
||||
newPlayer := &Player{ID: playerID, X: 5, Y: 5}
|
||||
players[playerID] = newPlayer
|
||||
playerConns[playerID] = conn
|
||||
mu.Unlock()
|
||||
fmt.Printf("Player %d connected\n", playerID)
|
||||
|
||||
// Send player ID to the client
|
||||
serverMsg := &pb.ServerMessage{
|
||||
PlayerId: int32(playerID),
|
||||
PlayerId: int32(playerID),
|
||||
CurrentTick: 0,
|
||||
}
|
||||
data, err := proto.Marshal(serverMsg)
|
||||
if err != nil {
|
||||
log.Printf("Failed to marshal ServerMessage for player %d: %v", playerID, err)
|
||||
return
|
||||
}
|
||||
if _, err := conn.Write(data); err != nil {
|
||||
if err := writeMessage(conn, serverMsg); err != nil {
|
||||
log.Printf("Failed to send player ID to player %d: %v", playerID, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Listen for incoming actions from this player
|
||||
buf := make([]byte, 4096)
|
||||
reader := bufio.NewReader(conn)
|
||||
for {
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
log.Printf("Error reading from player %d: %v", playerID, err)
|
||||
// Read message length
|
||||
lengthBuf := make([]byte, 4)
|
||||
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(playerConns, playerID)
|
||||
delete(actionQueue, playerID)
|
||||
return
|
||||
}
|
||||
|
||||
action := &pb.Action{}
|
||||
if err := proto.Unmarshal(buf[:n], action); err != nil {
|
||||
log.Printf("Failed to unmarshal action for player %d: %v", playerID, err)
|
||||
batch := &pb.ActionBatch{}
|
||||
if err := proto.Unmarshal(messageBuf, batch); err != nil {
|
||||
log.Printf("Failed to unmarshal action batch for player %d: %v", playerID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Queue the action for processing in the game loop
|
||||
actionQueue[playerID] = append(actionQueue[playerID], action)
|
||||
// Queue the actions for processing
|
||||
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...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
// Update players based on queued actions
|
||||
for playerID, actions := range actionQueue {
|
||||
player := players[playerID]
|
||||
player.Lock()
|
||||
for _, action := range actions {
|
||||
if action.Type == pb.Action_MOVE {
|
||||
switch action.Type {
|
||||
case pb.Action_MOVE:
|
||||
player.X = int(action.X)
|
||||
player.Y = int(action.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)
|
||||
}
|
||||
}
|
||||
actionQueue[playerID] = nil // Clear the queue after processing
|
||||
player.Unlock()
|
||||
actionQueue[playerID] = nil // Clear the action queue after processing
|
||||
}
|
||||
|
||||
// Prepare and broadcast the current game state
|
||||
currentTick := time.Now().UnixNano() / int64(tickRate)
|
||||
state := &pb.ServerMessage{
|
||||
CurrentTick: currentTick,
|
||||
Players: make([]*pb.PlayerState, 0, len(players)),
|
||||
}
|
||||
|
||||
// Convert players to PlayerState
|
||||
for id, p := range players {
|
||||
p.Lock()
|
||||
state.Players = append(state.Players, &pb.PlayerState{
|
||||
PlayerId: int32(id),
|
||||
X: int32(p.X),
|
||||
Y: int32(p.Y),
|
||||
})
|
||||
p.Unlock()
|
||||
}
|
||||
|
||||
// Add chat messages to the state
|
||||
chatMutex.RLock()
|
||||
state.ChatMessages = chatHistory[max(0, len(chatHistory)-5):] // Only send last 5 messages
|
||||
chatMutex.RUnlock()
|
||||
|
||||
// Send to each connected player
|
||||
for _, conn := range playerConns {
|
||||
if err := writeMessage(conn, state); err != nil {
|
||||
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user