[go: up one dir, main page]

Menu

[r92]: / main / server.py  Maximize  Restore  History

Download this file

133 lines (107 with data), 4.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- 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)