Your favorite API for Python Server gaming
The NetGameAPI is currently not optimized for realtime data transfer
Add the gameserver folder to your Project
Embed the API with:
from gameserver.netgameapi import *Create a instance of the netgameapi class
api = NetGameApi(name, game, lambda: receiver, [address], [port])
name = your usernamegame = name of your gamereceiver = a function with one parameter, the parameter is in the protocol dictionary formaddress = the ip address or the domainname of the server [optional]port = port of the server [optional]Connect to server
api.makeConnection()
Make a new Thread that listens to the server
tcpthread = Thread(name='tcp', target=api.startReceiving())
You'll receive the playerlist
It will only contain the players that play your game and are currently not playing
Select a player from the playerlist
Use the username given in the playerlist
api.connectToPlayer(username_of_opponent)
Server Processing
Your opponent receives your connect as a gameinvitation
Inform server, that you are now ready to play
Send the server connectionEstablished(opponent)
Now you can send your Gamedata
api.submitGameData(content)
makeConnection()
{
"action":"connection",
"data":{
"username":"your_username",
"game":"your_game_name"
}
}Use connectToPlayer(user_you_wish_to_connect) to invite the other player
{
"action":"connect",
"data":{
"master": "your_username",
"opponent": user_you_wish_to_connect
}
}You'll receive this if the master player sends a connectToPlayer(user_requesting_connection) to the server
{
"action": "gameinvitation",
"data": {
"master": user_requesting_connection
}
}You accept the invitation with acceptGameInvitation(user_requesting_connection)
{
"action": "connect_accepted",
"data": {
"opponent": user_requesting_connection
}
}You refuse the invitation with refuseGameInvitation(user_requesting_connection)
{
"action": "connect_refused",
"data": {
"opponent": user_requesting_connection
}
}You are the master of the game
{
"action":"connection_established",
"data":{
"username":"opponent_username"
}
}Format of playerlist
{
"action": "listplayers",
"data": [
{"playing": 0,"username": "manuel", "game": "tictactoe"},
{"playing": 1,"username": "lukas", "game": "tictactoe"}
]
}Errors returned by the server
{
"action":"error",
"data":{
"errorinfo":"doubleusername",
"helpmessage":"Please connect with different username."
}
}{
"action":"error",
"data":{
"errorinfo":"notavailable",
"helpmessage":"User already ingame."
}
}{
"action":"error",
"data":{
"errorinfo":"connection_refused",
"helpmessage":"Your opponent refused."
}
}