From f463e71c2864bac327be088937365d29f98c485b Mon Sep 17 00:00:00 2001 From: portakal Date: Sun, 24 May 2026 12:37:14 +0300 Subject: [PATCH] daily commit --- backend/databaseDirect.go | 1 + backend/objects/gameObjects.go | 17 ++++++ backend/objects/sharedCollection.go | 81 +++++++++++++++++++++++++++++ backend/server/homepage.go | 1 + backend/server/login.go | 1 + backend/utils/packet_utils.go | 1 + 6 files changed, 102 insertions(+) create mode 100644 backend/databaseDirect.go create mode 100644 backend/objects/gameObjects.go create mode 100644 backend/objects/sharedCollection.go create mode 100644 backend/server/homepage.go create mode 100644 backend/server/login.go create mode 100644 backend/utils/packet_utils.go diff --git a/backend/databaseDirect.go b/backend/databaseDirect.go new file mode 100644 index 0000000..2480943 --- /dev/null +++ b/backend/databaseDirect.go @@ -0,0 +1 @@ +package backend diff --git a/backend/objects/gameObjects.go b/backend/objects/gameObjects.go new file mode 100644 index 0000000..479645f --- /dev/null +++ b/backend/objects/gameObjects.go @@ -0,0 +1,17 @@ +package objects + +type Player struct { + Name string + X float64 + Y float64 + Radius float64 + Direction float64 + Speed float64 +} + +type Spore struct { + id uint64 + X float64 + Y float64 + Radius float64 +} diff --git a/backend/objects/sharedCollection.go b/backend/objects/sharedCollection.go new file mode 100644 index 0000000..4acd5b7 --- /dev/null +++ b/backend/objects/sharedCollection.go @@ -0,0 +1,81 @@ +package objects + +import "sync" + +// A generic, thread-safe map of objects with auto-incrementing IDs. +type SharedCollection[T any] struct { + objectsMap map[uint64]T + nextId uint64 + mapMux sync.Mutex +} + +func NewSharedCollection[T any](capacity ...int) *SharedCollection[T] { + var newObjMap map[uint64]T + + if len(capacity) > 0 { + newObjMap = make(map[uint64]T, capacity[0]) + } else { + newObjMap = make(map[uint64]T) + } + + return &SharedCollection[T]{ + objectsMap: newObjMap, + nextId: 1, + } +} + +// Add an object to the map with the given ID (if provided) or the next available ID. +// Returns the ID of the object added. +func (s *SharedCollection[T]) Add(obj T, id ...uint64) uint64 { + s.mapMux.Lock() + defer s.mapMux.Unlock() + + thisId := s.nextId + if len(id) > 0 { + thisId = id[0] + } + + s.objectsMap[thisId] = obj + s.nextId++ + return thisId +} + +// Remove removes an object from the map by ID, if it exists +func (s *SharedCollection[T]) Remove(id uint64) { + s.mapMux.Lock() + defer s.mapMux.Unlock() + + delete(s.objectsMap, id) +} + +// Call the callback function for each object in the map. +func (s *SharedCollection[T]) ForEach(callback func(uint64, T)) { + // Create a local copy while holding the lock + s.mapMux.Lock() + localCopy := make(map[uint64]T, len(s.objectsMap)) + for id, obj := range s.objectsMap { + localCopy[id] = obj + } + s.mapMux.Unlock() + + // Iterate over the local copy without holding the lock + for id, obj := range localCopy { + callback(id, obj) + } +} + +// Get an object with the given ID, if it exists, otherwise nil. +// Also returns a boolean indicating whether the object was found. +func (s *SharedCollection[T]) Get(id uint64) (T, bool) { + s.mapMux.Lock() + defer s.mapMux.Unlock() + + obj, found := s.objectsMap[id] + return obj, found +} + +// Get the approximate number of objects in the map. +// The reason this is approximate is because the map is read without holding the lock. +func (s *SharedCollection[T]) Len() int { + return len(s.objectsMap) +} diff --git a/backend/server/homepage.go b/backend/server/homepage.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/backend/server/homepage.go @@ -0,0 +1 @@ +package main diff --git a/backend/server/login.go b/backend/server/login.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/backend/server/login.go @@ -0,0 +1 @@ +package main diff --git a/backend/utils/packet_utils.go b/backend/utils/packet_utils.go new file mode 100644 index 0000000..d4b585b --- /dev/null +++ b/backend/utils/packet_utils.go @@ -0,0 +1 @@ +package utils