Skip to content

Commit

Permalink
#267 Made position updating very nice
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed May 10, 2023
1 parent 0de9948 commit 75139b8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 12 deletions.
13 changes: 10 additions & 3 deletions pokete_classes/multiplayer/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pokete_classes import ob_maps as obmp
from pokete_classes.generate import gen_maps, gen_obs
from pokete_classes.input import ask_text, ask_ok
from pokete_classes.multiplayer.pc_manager import pc_manager

END_SECTION = b"<END>"

Expand Down Expand Up @@ -128,9 +129,15 @@ def handshake(self):
self.figure.y,
)
self.figure.remove()
self.figure.add(
obmp.ob_maps[pos["Map"]], pos["X"], pos["Y"]
)
self.figure.add(obmp.ob_maps[pos["Map"]], pos["X"], pos["Y"])
if d["Body"]["Users"]:
for user in d["Body"]["Users"]:
pc_manager.set(
user["Name"],
user["Position"]["Map"],
user["Position"]["X"],
user["Position"]["Y"],
)

def receive_data(self):
data = self.connection.recv(1048576)
Expand Down
22 changes: 22 additions & 0 deletions pokete_classes/multiplayer/pc_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import scrap_engine as se

from pokete_classes import ob_maps as obmp


class PCManager:
def __init__(self):
self.reg = {}

def set(self, name, _map, x, y):
if name not in self.reg:
self.reg[name] = se.Object("a", "float")
self.reg[name].remove()
self.reg[name].add(obmp.ob_maps[_map], x, y)

def remove(self, name):
self.reg[name].remove()
del self.reg[name]


pc_manager = PCManager()

15 changes: 15 additions & 0 deletions pokete_classes/multiplayer/response_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pokete_classes.multiplayer import connector
from pokete_classes.multiplayer.pc_manager import pc_manager


class ResponseManager:
def __call__(self):
while True:
if (d := connector.connector.receive_data())["Type"] == 1:
b = d["Body"]
pc_manager.set(b["Name"], b["Position"]["Map"],
b["Position"]["X"], b["Position"]["Y"])
elif d["Type"] == 4:
pass
elif d["Type"] == 5:
pc_manager.remove(d["Body"])
19 changes: 16 additions & 3 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"github.com/lxgr-linux/pokete/server/config"
"github.com/lxgr-linux/pokete/server/responses"
"github.com/lxgr-linux/pokete/server/status"
"github.com/lxgr-linux/pokete/server/user_repository"
"log"
Expand Down Expand Up @@ -78,6 +79,16 @@ func handleRequests(res []byte, connection *net.Conn) error {
return nil
}

func removeUser(connection *net.Conn) error {
thisUser, err := user_repository.GetByConn(connection)
err = user_repository.RemoveByConn(connection)
for _, user := range user_repository.GetAllUsers() {
err = responses.WriteUserRemovedResponse(user.Conn, thisUser.Name)
}
err = (*connection).Close()
return err
}

func processClient(connection net.Conn) {
for {
buffer := make([]byte, 1024)
Expand All @@ -88,10 +99,12 @@ func processClient(connection net.Conn) {
}
err = handleRequests(buffer[:mLen], &connection)
if err != nil {
log.Print("Error handeling:", err.Error())
log.Print("Error handeling:", err)
break
}
}
_ = user_repository.RemoveByConn(&connection)
connection.Close()
err := removeUser(&connection)
if err != nil {
log.Print("Error closing:", err)
}
}
4 changes: 2 additions & 2 deletions server/requests/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type RequestPosition struct {

func (r RequestPosition) Handle(connection *net.Conn) error {
users := user_repository.GetAllUsers()
err, thisUser := user_repository.GetByConn(connection); if err != nil {
thisUser, err := user_repository.GetByConn(connection); if err != nil {
return err
}
err = user_repository.SetNewPositionToUser(thisUser.Name, r.Position); if err != nil {
Expand All @@ -39,7 +39,7 @@ func (r RequestPosition) Handle(connection *net.Conn) error {
}
return fmt.Errorf("connection closed")
}
err, thisUser = user_repository.GetByConn(connection); if err != nil {
thisUser, err = user_repository.GetByConn(connection); if err != nil {
return err
}
for _, user := range users {
Expand Down
11 changes: 11 additions & 0 deletions server/responses/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
ResponseType_USER_ALLREADY_PRESENT
ResponseType_VERSION_MISMATCH
ResponseType_POSITION_IMPLAUSIBLE
ResponseType_USER_REMOVED
)

func writeResponse(connection *net.Conn, response Response) error {
Expand Down Expand Up @@ -86,6 +87,16 @@ func WriteVersionMismatchResponse(connection *net.Conn) error {
)
}

func WriteUserRemovedResponse(connection *net.Conn, userName string) error {
return writeResponse(
connection,
Response{
Type: ResponseType_USER_REMOVED,
Body: userName,
},
)
}

func WriteMapResponse(connection *net.Conn, position user_repository.Position, users []user_repository.User) error {
return writeResponse(
connection,
Expand Down
8 changes: 4 additions & 4 deletions server/user_repository/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ func Remove(name string) {
delete(*users, name)
}

func GetByConn(conn *net.Conn) (error, User) {
func GetByConn(conn *net.Conn) (User, error) {
for _, user := range *users {
if user.Conn == conn {
return nil, user
return user, nil
}
}
return fmt.Errorf("user with given connection was not found, somebody fucked up badly"), User{}
return User{}, fmt.Errorf("user with given connection was not found, somebody fucked up badly")
}

func RemoveByConn(conn *net.Conn) error {
err, user := GetByConn(conn)
user, err := GetByConn(conn)
if err != nil {
return err
}
Expand Down

0 comments on commit 75139b8

Please sign in to comment.