Mise à jour effectuée, merci de nous signaler tout dysfonctionnement ! | Upgrade done, please let us know about any dysfunction!

Commit 30606776 authored by ZettaScript's avatar ZettaScript
Browse files

Players

parent e8626fa5
......@@ -47,6 +47,7 @@ Le protocole utilisé entre clients et serveur est très simple.
Pour le moment, seul le JSON est utilisé.
Les données JSON comprennent un champ "error".
0: OK
16: Communication error: decoding header
17: Communication error: unsupported protocol version
......@@ -55,7 +56,9 @@ Les données JSON comprennent un champ "error".
48: Game management error: that game name is already used
49: Game management error: miss new game name
50: Game management error: miss game name
51: Game management error: There is no game with this name
51: Game management error: there is no game with this name
52: Game management error: miss admin password
53: Game management error: wrong admin password
Exemple de paquet valide :
geco42/json/109
......
......@@ -48,10 +48,12 @@ games = []
games_index = {}
class Player():
def __init__(self, name, address):
def __init__(self, name, address, psw, uid):
# Consts
self.name = name
self.address = address
self.psw = psw
self.uid = uid
# Vars
self.age = None
......@@ -61,17 +63,20 @@ class Player():
self.t_wallet = None
class Game():
def __init__(self, name, public, leader_name, leader_address, consts):
def __init__(self, name, public, admin_name, admin_address, admin_psw, consts):
# Consts
self.name = name
self.creation_time = int(time())
self.public = public
self.leader_name = leader_name
self.leader_address = leader_address
self.admin_name = admin_name
self.admin_address = admin_address
self.admin_psw = admin_psw
self.C = consts
# Vars
self.players = [] # players list
self.players_index = {} # players associative index
self.next_uid = 1
self.money_system = None # the actual MoneySystem object
self.turn = None # current turn number
self.endturn = None # end turn timestamp
......@@ -79,8 +84,9 @@ class Game():
# Add one player
# address is ("address", port)
def addPlayer(self, name, address):
player = Player(name, address)
def addPlayer(self, name, address, psw):
player = Player(name, address, psw, self.next_uid)
self.next_uid += 1
self.players.append(player)
return player
......@@ -142,15 +148,6 @@ class Game():
player.age = 0
self.initPlayer(player, True)
# Test routine
def test():
g = Game("lipsum","Yoda",("127.0.0.1",8652),CONSTS.copy())
g.addPlayer("Alice",("127.0.0.1",8653))
g.addPlayer("Bob",("127.0.0.1",8654))
g.addPlayer("Eve",("127.0.0.1",8655))
g.startGame(money_free)
return g
# Read config
def readConfig():
conf = configparser.ConfigParser()
......@@ -255,7 +252,7 @@ Options:\n\
p_resp = False
resp_raw = b""# raw response
resp = {"error":0}# response
resp = {"error":ERROR_OK}# response
resp_lng = "json"# response language
# Parse paquet
......@@ -263,12 +260,12 @@ Options:\n\
seps = [paquet.index(b"/")]
p_ver = paquet[:seps[0]]# protocol version
if not p_ver.decode() in PROTOCOL_VERSIONS:
sendErrorToClient(client, 17, resp_lng)# Communication error: unsupported protocol version
sendErrorToClient(client, ERROR_COM_PROTOCOL, resp_lng)# Communication error: unsupported protocol version
continue
seps.append(paquet.index(b"/", seps[0]+1))
seps.append(paquet.index(b"\n", seps[1]+1))
except ValueError:
sendErrorToClient(client, 16, resp_lng)# Communication error: decoding header
sendErrorToClient(client, ERROR_COM_HEADER, resp_lng)# Communication error: decoding header
continue
p_lng = paquet[seps[0]+1:seps[1]]# data language
......@@ -280,7 +277,7 @@ Options:\n\
try:
p_data = json.loads(p_raw)
except json.JSONDecodeError:
sendErrorToClient(client, 19, resp_lng)# Communication error: decoding JSON
sendErrorToClient(client, ERROR_COM_JSON, resp_lng)# Communication error: decoding JSON
continue
if "query" in p_data:
......@@ -290,11 +287,28 @@ Options:\n\
resp["games"] = []
for game in games:
if game.public:
gp = {"name":game.name, "creation_time":game.creation_time, "leader_name":game.leader_name, "consts":game.C, "n_players":len(game.players), "money_system":None}
gp = {"name":game.name, "creation_time":game.creation_time, "admin_name":game.admin_name, "consts":game.C, "n_players":len(game.players), "money_system":None}
if game.money_system != None:
gp["money_system"] = game.money_system.name
resp["games"].append(gp)
if "game_info" in p_data["query"]:
if not "game_name" in p_data:
sendErrorToClient(client, ERROR_MGAME_MISSGAMENAME, resp_lng)# Game management error: miss game name
continue
g_name = p_data["game_name"]
if not g_name in games_index:
sendErrorToClient(client, ERROR_MGAME_NONAMED, resp_lng)# Game management error: there is no game with this name
continue
game = games_index[g_name]
resp["game_info"] = {}
resp["game_info"]["players"] = []
for player in game.players:
resp["game_info"]["players"].append({"player_name":player.name, "player_uid":player.uid, "player_address":player.address[0], "player_port":player.address[1]})
if "server_info" in p_data["query"]:
resp["server_info"] = {"version":VERSION, "protocol_versions":PROTOCOL_VERSIONS}
......@@ -302,15 +316,20 @@ Options:\n\
p_resp = True
g = p_data["new_game"]
g_leader_address = addr
if "leader_address" in g:
g_leader_address = [g["leader_address"], g_leader_address[1]]
if "leader_port" in g:
g_leader_address = [g_leader_address[0], g["leader_port"]]
g_admin_address = (addr[0], 8655)
if "admin_address" in g:
g_admin_address = [g["admin_address"], g_admin_address[1]]
if "admin_port" in g:
g_admin_address = [g_admin_address[0], g["admin_port"]]
g_leader_name = "Guru"
if "leader_name" in g:
g_leader_name = g["leader_name"]
g_admin_name = "Guru"
if "admin_name" in g:
g_admin_name = g["admin_name"]
if not "admin_psw" in g:
sendErrorToClient(client, ERROR_MGAME_MISSADMINPSW, resp_lng)# Game management error: miss admin password
continue
g_admin_psw = g["admin_psw"]
g_consts = CONSTS.copy()
if "consts" in g:
......@@ -321,45 +340,113 @@ Options:\n\
g_public = g["public"]
if not "name" in g:
sendErrorToClient(client, 49, resp_lng)# Game management error: miss new game name
sendErrorToClient(client, ERROR_MGAME_MISSNEWGAMENAME, resp_lng)# Game management error: miss new game name
continue
if g["name"] in games_index:
sendErrorToClient(client, 48, resp_lng)# Game management error: that game name is already used
sendErrorToClient(client, ERROR_MGAME_USEDGAMENAME, resp_lng)# Game management error: that game name is already used
continue
game = Game(g["name"], g_public, g_leader_name, g_leader_address, g_consts)
game = Game(g["name"], g_public, g_admin_name, g_admin_address, g_admin_psw, g_consts)
games.append(game)
games_index[game.name] = game
resp["new_game_info"] = {"name":game.name, "public":game.public, "creation_time":game.creation_time, "leader_name":game.leader_name, "leader_address":game.leader_address, "consts":game.C}
resp["new_game_info"] = {"name":game.name, "public":game.public, "creation_time":game.creation_time, "admin_name":game.admin_name, "admin_address":game.admin_address, "consts":game.C}
if "edit_game" in p_data:
p_resp = True
if not "game_name" in p_data:
sendErrorToClient(client, 50, resp_lng)# Game management error: miss game name
sendErrorToClient(client, ERROR_MGAME_MISSGAMENAME, resp_lng)# Game management error: miss game name
continue
g_name = p_data["game_name"]
if not g_name in games_index:
sendErrorToClient(client, 51, resp_lng)# Game management error: there is no game with this name
sendErrorToClient(client, ERROR_MGAME_NONAMED, resp_lng)# Game management error: there is no game with this name
continue
game = games_index[g_name]
g_admin_psw = ""
if "admin_psw" in p_data:
g_admin_psw = p_data["admin_psw"]
if g_admin_psw != game.admin_psw:
sendErrorToClient(client, ERROR_MGAME_WRONGADMINPSW, resp_lng)# Game management error: wrong admin password
continue
g = p_data["edit_game"]
if "leader_name" in g:
game.leader_name = g["leader_name"]
if "leader_address" in g:
game.leader_address = (g["leader_address"], game.leader_address[1])
if "leader_port" in g:
game.leader_address = (game.leader_address[0], g["leader_port"])
if "admin_name" in g:
game.admin_name = g["admin_name"]
if "admin_address" in g:
game.admin_address = (g["admin_address"], game.admin_address[1])
if "admin_port" in g:
game.admin_address = (game.admin_address[0], g["admin_port"])
if "admin_psw" in g:
game.admin_psw = g["admin_psw"]
if "public" in g:
game.public = g["public"]
if "consts" in g:
game.C = g["consts"].copy()
resp["edited_game_info"] = {"name":game.name, "public":game.public, "creation_time":game.creation_time, "leader_name":game.leader_name, "leader_address":game.leader_address, "consts":game.C}
resp["edited_game_info"] = {"name":game.name, "public":game.public, "creation_time":game.creation_time, "admin_name":game.admin_name, "admin_address":game.admin_address, "consts":game.C}
if "rm_game" in p_data:
p_resp = True
if not "game_name" in p_data:
sendErrorToClient(client, ERROR_MGAME_MISSGAMENAME, resp_lng)# Game management error: miss game name
continue
g_name = p_data["game_name"]
if not g_name in games_index:
sendErrorToClient(client, ERROR_MGAME_NONAMED, resp_lng)# Game management error: there is no game with this name
continue
game = games_index[g_name]
g_admin_psw = ""
if "admin_psw" in p_data:
g_admin_psw = p_data["admin_psw"]
if g_admin_psw != game.admin_psw:
sendErrorToClient(client, ERROR_MGAME_WRONGADMINPSW, resp_lng)# Game management error: wrong admin password
continue
games_index.pop(game.name)
games.remove(game)
game = None
resp["rm_game_done"] = True
if "new_player" in p_data:
p_resp = True
if not "game_name" in p_data:
sendErrorToClient(client, ERROR_MGAME_MISSGAMENAME, resp_lng)# Game management error: miss game name
continue
g_name = p_data["game_name"]
if not g_name in games_index:
sendErrorToClient(client, ERROR_MGAME_NONAMED, resp_lng)# Game management error: there is no game with this name
continue
game = games_index[g_name]
g = p_data["new_player"]
g_player_name = ""
if "player_name" in g:
g_player_name = g["player_name"]
g_player_address = (addr[0],8654)
if "player_address" in g:
g_player_address = (g["player_address"], g_player_address[1])
if "player_port" in g:
g_player_address = (g_player_address[0], g["player_port"])
g_player_psw = ""
if "player_psw" in g:
g_player_psw = g["player_psw"]
player = game.addPlayer(g_player_name, g_player_address, g_player_psw)
resp["new_player_done"] = {"player_name":player.name, "player_uid":player.uid, "player_address":player.address[0], "player_port":player.address[1]}
else:
sendErrorToClient(client, 18, resp_lng)# Communication error: unknown language
sendErrorToClient(client, ERROR_COM_LANGUAGE, resp_lng)# Communication error: unknown language
continue
if p_resp:
......
......@@ -53,7 +53,7 @@ Options:\n\
NEWGAME_DEFS = {
1:{},# Empty, should return error 49
2:{"name":"Test game"},# Should work once with default values, and return 48 after
3:{"name":"Foobar", "leader_name":"John Doe", "leader_address":"testaddr", "leader_port":1234, "public":False, "consts":CONSTS2}# Should set every parameter
3:{"name":"Foobar", "admin_name":"John Doe", "admin_address":"testaddr", "admin_port":1234, "admin_psw":"admin", "public":False, "consts":CONSTS2}# Should set every parameter
}
server_address = (HOST, PORT)
......@@ -82,17 +82,29 @@ def games():
"""Query games list"""
return sdata({"query":["games"]}, True)
def game(name="Test game"):
"""Query game info"""
return sdata({"game_name":name, "query":["game_info"]}, True)
def newgamesets(sets):
"""New game from predefined settings"""
return sdata({"new_game":sets}, True)
return sdata({"new_game":NEWGAME_DEFS[sets]}, True)
def newgame(name="Test game", leader_name="Guru", leader_address=ADDR, leader_port=8654, public=True, consts=CONSTS):
def newgame(name="Test game", admin_name="Guru", admin_address=ADDR, admin_port=8654, admin_psw="", public=True, consts=CONSTS):
"""New game"""
return sdata({"new_game":{"name":name, "leader_name":leader_name, "leader_address":leader_address, "leader_port":leader_port, "public":public, "consts":consts}}, True)
return sdata({"new_game":{"name":name, "admin_name":admin_name, "admin_address":admin_address, "admin_port":admin_port, "admin_psw":admin_psw, "public":public, "consts":consts}}, True)
def editgame(name, sets):
def editgame(name, admin_psw, sets):
"""Edit game"""
return sdata({"game_name":name, "edit_game":sets}, True)
return sdata({"game_name":name, "admin_psw":admin_psw, "edit_game":sets}, True)
def removegame(name, admin_psw):
"""Remove game"""
return sdata({"game_name":name, "admin_psw":admin_psw, "rm_game":True}, True)
def newplayer(name="Test game", player_name="Little Beetle", player_address=ADDR, player_port=8654, player_psw=""):
"""New player"""
return sdata({"game_name":name, "new_player":{"player_name":player_name, "player_address":player_address, "player_port":player_port, "player_psw":player_psw}}, True)
def h():
"""Show help"""
......
......@@ -17,6 +17,18 @@
along with Pygeconomicus-server. If not, see <https://www.gnu.org/licenses/>.
"""
ERROR_OK = 0
ERROR_COM_HEADER = 16
ERROR_COM_PROTOCOL = 17
ERROR_COM_LANGUAGE = 18
ERROR_COM_JSON = 19
ERROR_MGAME_USEDGAMENAME = 48
ERROR_MGAME_MISSNEWGAMENAME = 49
ERROR_MGAME_MISSGAMENAME = 50
ERROR_MGAME_NONAMED = 51
ERROR_MGAME_MISSADMINPSW = 52
ERROR_MGAME_WRONGADMINPSW = 53
# Server functions
def getargv(argv, arg, default=""):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment