Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback structure, important interfaces/classes #37

Merged
merged 6 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/teamproject-competition-server.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions teamprojekt_competition_server/client/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def __init__(self) -> None:
self.client = COMPClient(agent=self)
pass

def step(self, env):
def step(self, obv):
"""this is an abstract method for one step that the
agent makes and should be overwritten

Args:
env (_type_): current enviroment
obv (_type_): current enviroment

Raises:
NotImplementedError: this method should be overwritten
Expand Down
13 changes: 2 additions & 11 deletions teamprojekt_competition_server/client/client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""class for client"""

from twisted.internet import defer, reactor
from twisted.internet import reactor
from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol
from twisted.protocols.amp import CommandLocator

from .client_protocol import COMPClientProtocol

from ..shared.commands import AuthClient


class COMPClient(CommandLocator):
"""client that manages the connection over the protocoll with the server"""
Expand All @@ -23,13 +21,6 @@ def connect_client(self, token: str):
Args:
token (str): token to verify the client
"""
version = int(1)
destination = TCP4ClientEndpoint(reactor, "127.0.0.1", 1234)
auth = connectProtocol(destination, COMPClientProtocol(agent=self.agent))
auth.addCallback(
lambda ampProto: ampProto.callRemote(
AuthClient, token=str.encode(token), version=version
)
)
defer.DeferredList([auth])
connectProtocol(destination, COMPClientProtocol(agent=self.agent, token=token))
reactor.run() # type: ignore[attr-defined]
29 changes: 21 additions & 8 deletions teamprojekt_competition_server/client/client_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
from twisted.protocols import amp
from twisted.internet.protocol import ClientFactory, Protocol

from ..shared.commands import StartGame, EndGame, Step
from ..shared.commands import StartGame, EndGame, Step, Auth


class COMPClientProtocol(amp.AMP):
"""protocol for the client"""

def __init__(self, agent, boxReceiver=None, locator=None):
token: str = "Unknown"

def __init__(self, agent, token, boxReceiver=None, locator=None):
super().__init__(boxReceiver, locator)
self.agent = agent
self.token = token

def start_game(self, game_id: int):
"""is called when the server starts the game
Expand All @@ -23,7 +26,7 @@ def start_game(self, game_id: int):
Returns:
{"ready": boolean}: true if the client is ready to start the game
"""
print(f"--- Started Game --- \nGame ID: {game_id}")
print(f"------ Started Game [Game ID: {game_id}] ------")
return {"ready": True} # dummy ready

StartGame.responder(start_game)
Expand All @@ -38,12 +41,12 @@ def end_game(self, result, stats):
Returns:
{"ready": boolean}: true if the client is ready to start a new game
"""
print(f"--- Ended Game --- \nGame ID: {result} | Stats: {stats}")
print(f"------ Ended Game [Game ID: {result} | Stats: {stats}] ------")
return {"ready": True} # dummy ready

EndGame.responder(end_game)

def step(self, env):
def step(self, obv):
"""is called when the server wants the client to make a step

Args:
Expand All @@ -52,18 +55,28 @@ def step(self, env):
Returns:
{"action": int}: action that should be executed
"""
action = self.agent.step(env=int(env)) # dummy action
print(f"--- Next Step --- \nEnviroment: {env} | Action: {action}")
action = self.agent.step(obv=int(obv)) # dummy action
print(f"Send action: {action}")
return {"action": action}

Step.responder(step)

def auth(self):
"""called for auth the client

Returns:
{"token": String}: the clients auth token
"""
return {"token": str.encode(self.token), "version": 1}

Auth.responder(auth)


class COMPClientFactory(ClientFactory):
"""factory for COMP clients"""

def buildProtocol(self, addr: IAddress) -> Protocol | None:
"""builds the COMP protocol"""
return COMPClientProtocol(
agent=None
agent=None, token="ThisIsSomeCoolDummyToken"
) # TODO: there is something fucked up with the agent...
10 changes: 4 additions & 6 deletions teamprojekt_competition_server/client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@
class MyAgent(COMPAgent):
"""Dummy Agent for testing"""

def step(self, env):
def step(self, obv):
"""dummy step function

Args:
env (int): enviroment
obv (int): enviroment

Returns:
int: action
"""
return int(input(f"Enviroment: {env} \nEnter a move: "))
return int(input(f"Observation: {obv} | Enter a move: "))

agent = MyAgent()
token = "ABC" # dummy token
agent.run(token)
print("Hello")
agent.run("HelloWorldToken")
12 changes: 12 additions & 0 deletions teamprojekt_competition_server/log/client/client.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
2023-12-11T20:25:22+0100 [teamprojekt_competition_server.client.client.COMPClient#info] Inizialized agent: 140432192338960
2023-12-11T20:25:22+0100 [teamprojekt_competition_server.client.client.COMPClient#info] version: 1
2023-12-11T20:25:22+0100 [teamprojekt_competition_server.client.client.COMPClient#info] Connected with token HelloWorldToken
2023-12-11T20:25:23+0100 [teamprojekt_competition_server.client.client.COMPClient#info] Inizialized agent: 140183568677904
2023-12-11T20:25:23+0100 [teamprojekt_competition_server.client.client.COMPClient#info] version: 1
2023-12-11T20:25:23+0100 [teamprojekt_competition_server.client.client.COMPClient#info] Connected with token HelloWorldToken
2023-12-11T20:33:44+0100 [teamprojekt_competition_server.client.client.COMPClient#info] Inizialized agent: 140671271353360
2023-12-11T20:33:44+0100 [teamprojekt_competition_server.client.client.COMPClient#info] version: 1
2023-12-11T20:33:44+0100 [teamprojekt_competition_server.client.client.COMPClient#info] Connected with token HelloWorldToken
2023-12-11T20:33:46+0100 [teamprojekt_competition_server.client.client.COMPClient#info] Inizialized agent: 140397980761104
2023-12-11T20:33:46+0100 [teamprojekt_competition_server.client.client.COMPClient#info] version: 1
2023-12-11T20:33:46+0100 [teamprojekt_competition_server.client.client.COMPClient#info] Connected with token HelloWorldToken
4 changes: 4 additions & 0 deletions teamprojekt_competition_server/log/client/protocol.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
2023-12-11T20:33:44+0100 [teamprojekt_competition_server.client.client_protocol.COMPClientProtocol#info] Initialized protocol: 140671236286992 with agent: 140671271353360, token: 140671262383408
2023-12-11T20:33:46+0100 [teamprojekt_competition_server.client.client_protocol.COMPClientProtocol#info] Initialized protocol: 140397948106592 with agent: 140397980761104, token: 140397969693744
2023-12-11T20:33:46+0100 [teamprojekt_competition_server.client.client_protocol.COMPClientProtocol#info] Receive StartGame command, game_id: 222
2023-12-11T20:33:46+0100 [teamprojekt_competition_server.client.client_protocol.COMPClientProtocol#info] Receive StartGame command, game_id: 222
12 changes: 12 additions & 0 deletions teamprojekt_competition_server/log/server/gameManager.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
2023-12-11T20:25:19+0100 [teamprojekt_competition_server.server.game_manager.GameManager#info] Initialized GameManager -> 140477668476768
2023-12-11T20:25:22+0100 [teamprojekt_competition_server.server.game_manager.GameManager#info] Added player with id: 0
2023-12-11T20:25:22+0100 [teamprojekt_competition_server.server.game_manager.GameManager#info] added player with id: 0 to queue
2023-12-11T20:25:23+0100 [teamprojekt_competition_server.server.game_manager.GameManager#info] Added player with id: 1
2023-12-11T20:25:23+0100 [teamprojekt_competition_server.server.game_manager.GameManager#info] Matched two players: player1 0, player2 1
2023-12-11T20:25:23+0100 [teamprojekt_competition_server.server.game_manager.GameManager#info] Started game: 140477668480752
2023-12-11T20:33:24+0100 [teamprojekt_competition_server.server.game_manager.GameManager#info] Initialized GameManager -> 140512021235168
2023-12-11T20:33:44+0100 [teamprojekt_competition_server.server.game_manager.GameManager#info] Added player with id: 0
2023-12-11T20:33:44+0100 [teamprojekt_competition_server.server.game_manager.GameManager#info] added player with id: 0 to queue
2023-12-11T20:33:46+0100 [teamprojekt_competition_server.server.game_manager.GameManager#info] Added player with id: 1
2023-12-11T20:33:46+0100 [teamprojekt_competition_server.server.game_manager.GameManager#info] Matched two players: player1 0, player2 1
2023-12-11T20:33:46+0100 [teamprojekt_competition_server.server.game_manager.GameManager#info] Started game: 140512021239152
4 changes: 4 additions & 0 deletions teamprojekt_competition_server/log/server/protocol.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
2023-12-11T20:25:22+0100 [teamprojekt_competition_server.server.protocol.COMPServerProtocol#info] Initialized Protocol: 140477668478352
2023-12-11T20:25:23+0100 [teamprojekt_competition_server.server.protocol.COMPServerProtocol#info] Initialized Protocol: 140477668480416
2023-12-11T20:33:44+0100 [teamprojekt_competition_server.server.protocol.COMPServerProtocol#info] Initialized Protocol: 140512021236752
2023-12-11T20:33:46+0100 [teamprojekt_competition_server.server.protocol.COMPServerProtocol#info] Initialized Protocol: 140512021238816
6 changes: 6 additions & 0 deletions teamprojekt_competition_server/log/server/server.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
2023-12-11T20:25:19+0100 [teamprojekt_competition_server.server.server.COMPServer#info] COMPServer 140477692281472 initialized with factory: 140477692281520
2023-12-11T20:25:19+0100 [teamprojekt_competition_server.server.server.COMPServer#info] Server: 140477692281472 now listening to localhost:1234
2023-12-11T20:25:27+0100 [teamprojekt_competition_server.server.server.COMPServer#info] Reactor running
2023-12-11T20:33:24+0100 [teamprojekt_competition_server.server.server.COMPServer#info] COMPServer 140512046928736 initialized with factory: 140512046928496
2023-12-11T20:33:24+0100 [teamprojekt_competition_server.server.server.COMPServer#info] Server: 140512046928736 now listening to localhost:1234
2023-12-11T20:33:51+0100 [teamprojekt_competition_server.server.server.COMPServer#info] Reactor running
21 changes: 21 additions & 0 deletions teamprojekt_competition_server/server/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
""" Hello World """

from twisted.internet.interfaces import IAddress
from twisted.internet.protocol import Protocol, ServerFactory

from .protocol import COMPServerProtocol
from .player import COMPPlayer
from .game_manager import game_manager


class COMPServerFactory(ServerFactory):
"""factory for COMP servers"""

def buildProtocol(self, addr: IAddress) -> Protocol | None:
"""builds the protocoll"""
protocol: COMPServerProtocol = COMPServerProtocol()

new_player = COMPPlayer(protocol)
game_manager.add_player(new_player)

return protocol
39 changes: 0 additions & 39 deletions teamprojekt_competition_server/server/game.py

This file was deleted.

46 changes: 46 additions & 0 deletions teamprojekt_competition_server/server/game_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Structure which handles multiple games"""

import logging as log
from typing import Type

from .interfaces import IPlayer, IGame


class GameManager:
"""manager for the games"""

def __init__(self) -> None:
self.players: list[IPlayer] = []
self.queue: list[int] = []
self.games: list[IGame] = []
self.GameClass: Type[IGame]

def add_player(self, player: IPlayer) -> None:
"""adds a player to the player list and gives it its index as id

Args:
player (IPlayer): player to add
"""
self.players.append(player)

player.id = len(self.players) - 1

def add_player_to_queue(self, player_id: int):
"""adds a player to the queue

Args:
player_id (int): id of the player
"""
if len(self.queue) > 0:
player1 = self.players[self.queue.pop(0)]
player2 = self.players[player_id]
log.debug(f"matched two players: player {player1.id}, player {player2.id}")
new_game = self.GameClass(players=[player1, player2])
self.games.append(new_game)
new_game.start()
else:
self.queue.append(player_id)
log.debug(f"added player to queue. ID: {player_id}")


game_manager = GameManager()
Loading
Loading