mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-01 19:57:11 +01:00
basic join() (inserting/updating game records) works
This commit is contained in:
parent
02f05dc867
commit
c661c8a74e
1 changed files with 43 additions and 6 deletions
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import base64, json, mod_python, socket, struct, sys
|
import base64, json, mod_python, socket, struct, sys
|
||||||
|
import psycopg2, random
|
||||||
|
|
||||||
PROTOCOL_VERSION = 0
|
PROTOCOL_VERSION = 0
|
||||||
PRX_DEVICE_GONE = 3
|
PRX_DEVICE_GONE = 3
|
||||||
|
@ -12,6 +13,40 @@ PRX_GET_MSGS = 4
|
||||||
# except ImportError:
|
# except ImportError:
|
||||||
# apacheAvailable = False
|
# apacheAvailable = False
|
||||||
|
|
||||||
|
def join(req, devID, room, lang = 1, nInGame = 2, nHere = 1, inviteID = None):
|
||||||
|
connname = None
|
||||||
|
con = psycopg2.connect(database='xwgames')
|
||||||
|
|
||||||
|
query = """UPDATE games SET njoined = njoined + %d
|
||||||
|
WHERE lang = %d AND nTotal = %d AND room = '%s' AND njoined + %d <= ntotal
|
||||||
|
RETURNING connname"""
|
||||||
|
query = query % (nHere, lang, nInGame, room, nHere)
|
||||||
|
print(query)
|
||||||
|
cur = con.cursor()
|
||||||
|
cur.execute(query)
|
||||||
|
for row in cur:
|
||||||
|
connname = row[0]
|
||||||
|
print 'got:', connname
|
||||||
|
|
||||||
|
if not connname:
|
||||||
|
connname = str(random.randint(0, 10000000000))
|
||||||
|
query = """INSERT INTO games (connname, room, lang, ntotal, njoined)
|
||||||
|
values ('%s', '%s', %d, %d, %d) RETURNING connname; """
|
||||||
|
query %= (connname, room, lang, nInGame, nHere)
|
||||||
|
print(query)
|
||||||
|
cur.execute(query)
|
||||||
|
for row in cur:
|
||||||
|
print row
|
||||||
|
else:
|
||||||
|
print 'got nothing'
|
||||||
|
print 'did an insert'
|
||||||
|
con.commit()
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
result = {'connname': connname}
|
||||||
|
|
||||||
|
return json.dumps(result)
|
||||||
|
|
||||||
def kill(req, params):
|
def kill(req, params):
|
||||||
print(params)
|
print(params)
|
||||||
params = json.loads(params)
|
params = json.loads(params)
|
||||||
|
@ -37,21 +72,21 @@ def kill(req, params):
|
||||||
result = {'err': 0}
|
result = {'err': 0}
|
||||||
return json.dumps(result)
|
return json.dumps(result)
|
||||||
|
|
||||||
|
# winds up in handle_udp_packet() in xwrelay.cpp
|
||||||
def post(req, params, timeoutSecs = 1.0):
|
def post(req, params, timeoutSecs = 1.0):
|
||||||
err = 'none'
|
err = 'none'
|
||||||
dataLen = 0
|
|
||||||
jobj = json.loads(params)
|
jobj = json.loads(params)
|
||||||
data = base64.b64decode(jobj['data'])
|
data = base64.b64decode(jobj['data'])
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
udpSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
sock.settimeout(float(timeoutSecs)) # seconds
|
udpSock.settimeout(float(timeoutSecs)) # seconds
|
||||||
addr = ("127.0.0.1", 10997)
|
addr = ("127.0.0.1", 10997)
|
||||||
sock.sendto(data, addr)
|
udpSock.sendto(data, addr)
|
||||||
|
|
||||||
responses = []
|
responses = []
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
data, server = sock.recvfrom(1024)
|
data, server = udpSock.recvfrom(1024)
|
||||||
responses.append(base64.b64encode(data))
|
responses.append(base64.b64encode(data))
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
#If data is not received back from server, print it has timed out
|
#If data is not received back from server, print it has timed out
|
||||||
|
@ -121,6 +156,8 @@ def main():
|
||||||
# params = json.dumps(params)
|
# params = json.dumps(params)
|
||||||
# print(post(None, params))
|
# print(post(None, params))
|
||||||
None
|
None
|
||||||
|
elif cmd == 'join':
|
||||||
|
result = join(None, 1, args[0])
|
||||||
elif cmd == 'kill':
|
elif cmd == 'kill':
|
||||||
result = kill( None, json.dumps([{'relayID': args[0], 'seed':int(args[1])}]) )
|
result = kill( None, json.dumps([{'relayID': args[0], 'seed':int(args[1])}]) )
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue