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

server.py works with python 3 #189

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
3 changes: 3 additions & 0 deletions builder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# This file allows you to programmatically create blocks in Craft.
# Please use this wisely. Test on your own server first. Do not abuse it.

from builtins import zip
from builtins import range
from builtins import object
import requests
import socket
import sqlite3
Expand Down
56 changes: 32 additions & 24 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from __future__ import division
from __future__ import print_function
from future import standard_library
standard_library.install_aliases()
from builtins import map
from builtins import range
from builtins import object
from past.utils import old_div
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,12 +67,12 @@
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)

def chunked(x):
return int(floor(round(x) / CHUNK_SIZE))
return int(floor(old_div(round(x), CHUNK_SIZE)))

def packet(*args):
return '%s\n' % ','.join(map(str, args))
Expand All @@ -81,7 +89,7 @@ def tick(self):
now = time.time()
elapsed = now - self.last_check
self.last_check = now
self.allowance += elapsed * (self.rate / self.per)
self.allowance += elapsed * (old_div(self.rate, self.per))
if self.allowance > self.rate:
self.allowance = self.rate
if self.allowance < 1:
Expand All @@ -90,19 +98,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 +122,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 +159,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 +178,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 +218,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 +346,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 +383,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 +440,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 +466,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 +489,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 +539,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 +644,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 +658,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
1 change: 1 addition & 0 deletions world.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# gcc -std=c99 -O3 -shared -o world \
# -I src -I deps/noise deps/noise/noise.c src/world.c

from builtins import object
from ctypes import CDLL, CFUNCTYPE, c_float, c_int, c_void_p
from collections import OrderedDict

Expand Down