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

Commit e8626fa5 authored by ZettaScript's avatar ZettaScript
Browse files

Edit game & test client input

parent db3f37e8
......@@ -47,13 +47,19 @@ 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
> 18: Communication error: unknown language
> 19: Communication error: decoding JSON
> 48: Game management error: that game name is already used
> 49: Game management error: miss new game name
0: OK
16: Communication error: decoding header
17: Communication error: unsupported protocol version
18: Communication error: unknown language
19: Communication error: decoding JSON
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
Exemple de paquet valide :
geco42/json/109
{"error": 0, "server_info": {"version": "1.2.3", "protocol_versions": ["geco40","geco41","geco42","geco43"]}}
## Sources
......
......@@ -253,6 +253,7 @@ Options:\n\
else:
break
p_resp = False
resp_raw = b""# raw response
resp = {"error":0}# response
resp_lng = "json"# response language
......@@ -329,6 +330,33 @@ Options:\n\
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}
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
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
continue
game = games_index[g_name]
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 "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}
else:
sendErrorToClient(client, 18, resp_lng)# Communication error: unknown language
......
......@@ -5,9 +5,22 @@ from utils import *
PROTOCOL_VERSION = b"geco0"
PROTOCOL_LANG = b"json"
BUFFER = 1024
ADDR = socket.gethostname()
# Test game constants (just for testing, do not play with that values)
# Default game constants
CONSTS = {
"N_VALUES": 4, # number of values types
"N_WVALUES": 1, # number of waiting values types
"TURN_DURATION": 300, # in seconds
"N_TURNS": 10, # number of turns in a game
"TURN_YEARS": 8, # number of years by turn
"LIFESPAN": 10, # max age
"N_DIGITS": 0, # number of decimals
"START_VALUES": 4 # number of random values for beginning
}
# Test game constants (just for testing, do not play with that values)
CONSTS2 = {
"N_VALUES": 5, # number of values types
"N_WVALUES": 1, # number of waiting values types
"TURN_DURATION": 600, # in seconds
......@@ -36,55 +49,58 @@ Options:\n\
")
exit()
# New game settings
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":CONSTS}# Should set every parameter
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
}
server_address = (HOST, PORT)
print("Server: "+str(server_address))
print("Type 'help' for a commands list, 'quit' or CTRL+C to quit.")
print("Type 'h()' for help, 'exit()' or CTRL+C to quit.")
while True:
try:
p = input(">")
except KeyboardInterrupt:
print()
exit()
if p == "quit":
break
elif p == "help":
print("""Commands:
quit quit
help show help
i query=server_info
g query=games
n new_game""")
continue
data = {}
if "i" in p:
if not "query" in data:
data["query"] = []
data["query"].append("server_info")
if "g" in p:
if not "query" in data:
data["query"] = []
data["query"].append("games")
if "n" in p:
for k in NEWGAME_DEFS:
if k in p:
data["new_game"] = NEWGAME_DEFS[k]
break
def sdata(data, response_intended, lng="json"):
"""Send encoded data"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(server_address)
raw = json.dumps(data).encode()
msg = PROTOCOL_VERSION+b"/"+PROTOCOL_LANG+b"/"+hex(len(raw))[2:].encode()+b"\n"+raw
sock.sendall(msg)
resp = sock.recv(BUFFER)
print(resp)
resp = None
if response_intended:
resp = sock.recv(BUFFER)
sock.close()
return resp
def info():
"""Query server info"""
return sdata({"query":["server_info"]}, True)
def games():
"""Query games list"""
return sdata({"query":["games"]}, True)
def newgamesets(sets):
"""New game from predefined settings"""
return sdata({"new_game":sets}, True)
def newgame(name="Test game", leader_name="Guru", leader_address=ADDR, leader_port=8654, 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)
def editgame(name, sets):
"""Edit game"""
return sdata({"game_name":name, "edit_game":sets}, True)
def h():
"""Show help"""
help("__main__")
while True:
try:
print(eval(input("g>")))
except KeyboardInterrupt:
print()
exit()
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