summaryrefslogtreecommitdiff
path: root/protocol.py
diff options
context:
space:
mode:
authorJesusaves <cpntb1@ymail.com>2020-12-19 17:12:38 -0300
committerJesusaves <cpntb1@ymail.com>2020-12-19 17:12:38 -0300
commit4835dc8a7792e135719558a1982354d9eb213a42 (patch)
tree268fb59d9d147313dbc4131a12b20d8ccbecbbd0 /protocol.py
parent2ea74f4b720936b3502af1a51bb47056bcb61a87 (diff)
downloadserver-4835dc8a7792e135719558a1982354d9eb213a42.tar.gz
server-4835dc8a7792e135719558a1982354d9eb213a42.tar.bz2
server-4835dc8a7792e135719558a1982354d9eb213a42.tar.xz
server-4835dc8a7792e135719558a1982354d9eb213a42.zip
Add token field to connection. Send connection to protocol parser.
Add new constants for the results (non-boolean) so laterâ„¢ score can be smart.
Diffstat (limited to 'protocol.py')
-rw-r--r--protocol.py25
1 files changed, 13 insertions, 12 deletions
diff --git a/protocol.py b/protocol.py
index 9f2154d..33ff203 100644
--- a/protocol.py
+++ b/protocol.py
@@ -21,20 +21,20 @@
import utils
import player
import battle.main as battle
-from consts import ERR_BAD, ERR_DONE
+from consts import ERR_BAD, ERR_DONE, PACKET_NACK, PACKET_ACK
# self.data structure: <Token> ; <Message> ; <JSON Arguments>
-def parse(packet):
+def parse(packet, conn):
##########################################################################
# Maybe it is a RAW packet
if (packet.lower() == "ping"):
- return [True, "PONG"]
+ return [PACKET_ACK, "PONG"]
##########################################################################
# Not a raw packet, so it must adhere to the convention
data=packet.split(';')
if (len(data) != 3):
- return [False, "Invalid packet (RAW)"]
+ return [PACKET_NACK, "Invalid packet (RAW)"]
t=data[0]
##########################################################################
@@ -45,21 +45,22 @@ def parse(packet):
t=utils.create_token()
r=player.get_data(data[2], t)
if r is ERR_BAD:
- return [False, "Login error"]
+ return [PACKET_NACK, "Login error"]
else:
- return [True, r]
+ conn.token = t
+ return [PACKET_ACK, r]
elif data[1] == "register":
r=player.register(data[2], t)
if r is ERR_BAD:
- return [False, "Invalid registration attempt"]
+ return [PACKET_NACK, "Invalid registration attempt"]
else:
- return [True, r]
+ return [PACKET_ACK, r]
##########################################################################
# It is not, so Validate token format
print("Data0: %s | Alpnum: %s" % (str(data[0]), str(data[0].isalnum()) ))
if not data[0].isalnum():
- return [False, "Invalid packet (TOKENF)"]
+ return [PACKET_NACK, "Invalid packet (TOKENF)"]
# TODO: Now validate the token itself
@@ -76,7 +77,7 @@ def parse(packet):
try:
r=battle.reload_battle(t)
except:
- r=[True, "CANCELLED"]
+ r=[PACKET_ACK, "CANCELLED"]
elif data[1] == "begin_quest":
r=battle.begin_quest(data[2], t)
elif data[1] == "get_party":
@@ -85,7 +86,7 @@ def parse(packet):
r=ERR_BAD
if r == ERR_BAD:
- return [True, "ACK"]
+ return [PACKET_NACK, "Invalid Protocol (400)"]
else:
- return [True, r]
+ return [PACKET_ACK, r]