Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #186 #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ the client.
gcc -std=c99 -O3 -fPIC -shared -o world -I src -I deps/noise deps/noise/noise.c src/world.c
python server.py [HOST [PORT]]

On windows with mingw use the following to build the DLL:

gcc -std=c99 -O3 -fPIC -shared -o world -I src -I deps/noise deps/noise/noise.c src/world.c

### Controls

- WASD to move forward, left, backward, right.
Expand Down
44 changes: 22 additions & 22 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from math import floor
from world import World
import Queue
import SocketServer
import queue
import socketserver
import datetime
import random
import re
Expand Down Expand Up @@ -59,7 +59,7 @@
def log(*args):
now = datetime.datetime.utcnow()
line = ' '.join(map(str, (now,) + args))
print line
print(line)
with open(LOG_PATH, 'a') as fp:
fp.write('%s\n' % line)

Expand Down Expand Up @@ -90,19 +90,19 @@ def tick(self):
self.allowance -= 1
return False # okay

class Server(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
class Server(socketserver.ThreadingMixIn, socketserver.TCPServer):
allow_reuse_address = True
daemon_threads = True

class Handler(SocketServer.BaseRequestHandler):
class Handler(socketserver.BaseRequestHandler):
def setup(self):
self.position_limiter = RateLimiter(100, 5)
self.limiter = RateLimiter(1000, 10)
self.version = None
self.client_id = None
self.user_id = None
self.nick = None
self.queue = Queue.Queue()
self.queue = queue.Queue()
self.running = True
self.start()
def handle(self):
Expand All @@ -114,7 +114,7 @@ def handle(self):
data = self.request.recv(BUFFER_SIZE)
if not data:
break
buf.extend(data.replace('\r\n', '\n'))
buf.extend(data.decode().replace('\r\n', '\n'))
while '\n' in buf:
index = buf.index('\n')
line = ''.join(buf[:index])
Expand Down Expand Up @@ -151,12 +151,12 @@ def run(self):
try:
while True:
buf.append(self.queue.get(False))
except Queue.Empty:
except queue.Empty:
pass
except Queue.Empty:
except queue.Empty:
continue
data = ''.join(buf)
self.request.sendall(data)
self.request.sendall(data.encode())
except Exception:
self.request.close()
raise
Expand All @@ -170,7 +170,7 @@ class Model(object):
def __init__(self, seed):
self.world = World(seed)
self.clients = []
self.queue = Queue.Queue()
self.queue = queue.Queue()
self.commands = {
AUTHENTICATE: self.on_authenticate,
CHUNK: self.on_chunk,
Expand Down Expand Up @@ -210,7 +210,7 @@ def dequeue(self):
try:
func, args, kwargs = self.queue.get(timeout=5)
func(*args, **kwargs)
except Queue.Empty:
except queue.Empty:
pass
def execute(self, *args, **kwargs):
return self.connection.execute(*args, **kwargs)
Expand Down Expand Up @@ -338,7 +338,7 @@ def on_authenticate(self, client, username, access_token):
self.send_talk('%s has joined the game.' % client.nick)
def on_chunk(self, client, p, q, key=0):
packets = []
p, q, key = map(int, (p, q, key))
p, q, key = list(map(int, (p, q, key)))
query = (
'select rowid, x, y, z, w from block where '
'p = :p and q = :q and rowid > :key;'
Expand Down Expand Up @@ -375,7 +375,7 @@ def on_chunk(self, client, p, q, key=0):
packets.append(packet(CHUNK, p, q))
client.send_raw(''.join(packets))
def on_block(self, client, x, y, z, w):
x, y, z, w = map(int, (x, y, z, w))
x, y, z, w = list(map(int, (x, y, z, w)))
p, q = chunked(x), chunked(z)
previous = self.get_block(x, y, z)
message = None
Expand Down Expand Up @@ -432,7 +432,7 @@ def on_block(self, client, x, y, z, w):
)
self.execute(query, dict(x=x, y=y, z=z))
def on_light(self, client, x, y, z, w):
x, y, z, w = map(int, (x, y, z, w))
x, y, z, w = list(map(int, (x, y, z, w)))
p, q = chunked(x), chunked(z)
block = self.get_block(x, y, z)
message = None
Expand All @@ -458,7 +458,7 @@ def on_sign(self, client, x, y, z, face, *args):
client.send(TALK, 'Only logged in users are allowed to build.')
return
text = ','.join(args)
x, y, z, face = map(int, (x, y, z, face))
x, y, z, face = list(map(int, (x, y, z, face)))
if y <= 0 or y > 255:
return
if face < 0 or face > 7:
Expand All @@ -481,7 +481,7 @@ def on_sign(self, client, x, y, z, face, *args):
self.execute(query, dict(x=x, y=y, z=z, face=face))
self.send_sign(client, p, q, x, y, z, face, text)
def on_position(self, client, x, y, z, rx, ry):
x, y, z, rx, ry = map(float, (x, y, z, rx, ry))
x, y, z, rx, ry = list(map(float, (x, y, z, rx, ry)))
client.position = (x, y, z, rx, ry)
self.send_position(client)
def on_talk(self, client, *args):
Expand Down Expand Up @@ -531,7 +531,7 @@ def on_goto(self, client, nick=None):
client.send(YOU, client.client_id, *client.position)
self.send_position(client)
def on_pq(self, client, p, q):
p, q = map(int, (p, q))
p, q = list(map(int, (p, q)))
if abs(p) > 1000 or abs(q) > 1000:
return
client.position = (p * CHUNK_SIZE, 0, q * CHUNK_SIZE, 0, 0)
Expand Down Expand Up @@ -636,7 +636,7 @@ def cleanup():
count = 0
total = 0
delete_query = 'delete from block where x = %d and y = %d and z = %d;'
print 'begin;'
print('begin;')
for p, q in chunks:
chunk = world.create_chunk(p, q)
query = 'select x, y, z, w from block where p = :p and q = :q;'
Expand All @@ -650,10 +650,10 @@ def cleanup():
original = chunk.get((x, y, z), 0)
if w == original or original in INDESTRUCTIBLE_ITEMS:
count += 1
print delete_query % (x, y, z)
print(delete_query % (x, y, z))
conn.close()
print 'commit;'
print >> sys.stderr, '%d of %d blocks will be cleaned up' % (count, total)
print('commit;')
print('%d of %d blocks will be cleaned up' % (count, total), file=sys.stderr)

def main():
if len(sys.argv) == 2 and sys.argv[1] == 'cleanup':
Expand Down