Online Games

Your favorite API for Python Server gaming

View the Project on GitHub manuelmeister/OnlineGames

NetGameAPI

The NetGameAPI is currently not optimized for realtime data transfer

Install

Add the gameserver folder to your Project

Embed the API with:

 from gameserver.netgameapi import *


Transmitting Data

  1. Create a instance of the netgameapi class
    api = NetGameApi(name, game, lambda: receiver, [address], [port])

    • name = your username
    • game = name of your game
    • receiver = a function with one parameter, the parameter is in the protocol dictionary form
    • address = the ip address or the domainname of the server [optional]
    • port = port of the server [optional]
  2. Connect to server
    api.makeConnection()

  3. Make a new Thread that listens to the server
    tcpthread = Thread(name='tcp', target=api.startReceiving())

  4. You'll receive the playerlist
    It will only contain the players that play your game and are currently not playing

  5. Select a player from the playerlist
    Use the username given in the playerlist
    api.connectToPlayer(username_of_opponent)

  6. Server Processing

    • The server checks if your opponent already is ingame. If he already is, the error notavailable gets thrown
  7. Your opponent receives your connect as a gameinvitation

  8. Inform server, that you are now ready to play
    Send the server connectionEstablished(opponent)

  9. Now you can send your Gamedata
    api.submitGameData(content)


Protocol

Make a connection using makeConnection()

{
    "action":"connection",
    "data":{
        "username":"your_username",
        "game":"your_game_name"
    }
}

Connect to player

Use connectToPlayer(user_you_wish_to_connect) to invite the other player

{
    "action":"connect",
    "data":{
        "master": "your_username",
        "opponent": user_you_wish_to_connect
    }
}

Game Invitation

You'll receive this if the master player sends a connectToPlayer(user_requesting_connection) to the server

{
    "action": "gameinvitation",
    "data": {
        "master": user_requesting_connection
    }
}

Accept Game Invitation

You accept the invitation with acceptGameInvitation(user_requesting_connection)

{
            "action": "connect_accepted",
            "data": {
                "opponent": user_requesting_connection
     }
}

Refuse Game Invitation

You refuse the invitation with refuseGameInvitation(user_requesting_connection)

{
            "action": "connect_refused",
            "data": {
                "opponent": user_requesting_connection
     }
}

Connection to opponent established

You are the master of the game

{
    "action":"connection_established",
    "data":{
        "username":"opponent_username"
    }
}

Playerlist

Format of playerlist

{
    "action": "listplayers",
    "data": [
        {"playing": 0,"username": "manuel", "game": "tictactoe"},
        {"playing": 1,"username": "lukas", "game": "tictactoe"}
    ]
}

Errors

Errors returned by the server

doubleusername

{
    "action":"error",
    "data":{
        "errorinfo":"doubleusername",
        "helpmessage":"Please connect with different username."
    }
}

notavailable

{
    "action":"error",
    "data":{
        "errorinfo":"notavailable",
        "helpmessage":"User already ingame."
    }
}

connection_refused

{
    "action":"error",
    "data":{
        "errorinfo":"connection_refused",
        "helpmessage":"Your opponent refused."
    }
}