# -*- coding: utf8 -*-
# This file is part of CatchX.
#
# CatchX is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CatchX is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CatchX. If not, see <http://www.gnu.org/licenses/>.
#import stuff
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import threading
import MySQLdb
#####
##### IMPORTANT: The mysql stuff is not secure yet! (Mysql Intrusion)
#####
class server:
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
def __init__(self, port):
def mysql_connect(user, passwd, db):
self.conn = MySQLdb.connect (host = "localhost",
user = user,
passwd = passwd,
db = db)
self.cursor = self.conn.cursor ()
# Create server
server = SimpleXMLRPCServer(("", port),
requestHandler=self.RequestHandler)
server.register_introspection_functions()
mysql_connect("catchx", "catchthex", "catchx")
# The main function of the server
def sf_ping():
return "pong"
def sf_roomreq(name, room, passwd):
self.cursor.execute("SELECT id, running, password FROM rooms WHERE name = '" + room + "';")
row = self.cursor.fetchone()
try:
unimportant = row[0]
except:
return "password"
if row[1] == 1:
return "running"
if row[2] != passwd:
return "password"
#row = (str(1),)
self.cursor.execute("INSERT INTO users (pid, room, name, pending) VALUES (NULL, '" + str(row[0]) + "', '" + str(name) + "', 'none');")
self.cursor.execute("SELECT pid FROM users WHERE name = '" + str(name) + "' AND room = '" + str(row[0]) + "';")
pid = self.cursor.fetchone()[0]
self.cursor.execute("UPDATE users SET pending = CONCAT(pending,'playerjoin<>" + name + "><') WHERE room = " + str(row[0]) + " AND pid != " + str(pid) + ";")
return(pid, row[0])
def sf_get_players(pid):
players = ()
self.cursor.execute("SELECT room FROM users WHERE pid = '" + str(pid) + "';")
rid = self.cursor.fetchone()[0]
self.cursor.execute("SELECT name,pid FROM users WHERE room = '" + str(rid) + "';")
row = self.cursor.fetchall()
return row
def sf_keepalive(pid):
self.cursor.execute("SELECT pending,room FROM users WHERE pid = '" + str(pid) + "';")
row = self.cursor.fetchone()
pend = row[0]
rid = row[1]
self.cursor.execute("UPDATE users SET pending = 'none' WHERE room = " + str(rid) + ";")
return pend
def sf_chat(pid, msg):
self.cursor.execute("SELECT name,room FROM users WHERE pid = '" + str(pid) + "';")
row = self.cursor.fetchone()
rid = row[1]
name = row[0]
print msg
print name
self.cursor.execute("UPDATE users SET pending = CONCAT(pending,'chatmsg<>" + name + "<>" + msg + "><') WHERE room = " + str(rid) + ";")
return True
def sf_startgame(pid):
self.cursor.execute("SELECT room FROM users WHERE pid = '" + str(pid) + "';")
room = self.cursor.fetchone()[0]
self.cursor.execute("UPDATE rooms SET running = '1' WHERE id = " + str(room) + ";")
self.cursor.execute("SELECT name,pid FROM users WHERE room = '" + str(room) + "' ORDER BY RAND() LIMIT 1;")
ret = self.cursor.fetchone()
return ret
def sf_playerleave(pid):
self.cursor.execute("SELECT name,room FROM users WHERE pid = '" + str(pid) + "';")
row = self.cursor.fetchone()
room = row[1]
name = row[0]
self.cursor.execute("DELETE FROM users WHERE pid = '" + str(pid) + "' LIMIT 1;")
self.cursor.execute("UPDATE users SET pending = CONCAT(pending,'playerleave<>" + name + "><') WHERE room = " + str(row[0]) + ";")
server.register_function(sf_ping, 'ping')
server.register_function(sf_startgame, 'startgame')
server.register_function(sf_get_players, 'get_players')
server.register_function(sf_keepalive, 'keepalive')
server.register_function(sf_chat, 'chat')
server.register_function(sf_roomreq, 'roomreq')
server.register_function(sf_playerleave, 'playerleave')
print("Server started @ Port " + str(port))
# Run the server's main loop
server.serve_forever()
if __name__ == "__main__":
server(20211)