Skip to content

Commit 83bcd82

Browse files
author
minecraft server
committed
Half implemented new on_tick() feature, and proper schedule module.
1 parent a3f8f9a commit 83bcd82

10 files changed

+103
-115
lines changed

helpers.py

+8-26
Original file line numberDiff line numberDiff line change
@@ -57,36 +57,18 @@ def all_users():
5757
return [name[:-4] for name in os.listdir(os.path.join(WORLD_DIR, 'players'))]
5858

5959

60-
locks = set()
61-
def tell(user, message, delay=0, lock=None, prefix=''):
60+
def tell(user, message, prefix=''):
6261
"""Send a server message to user.
6362
Note: Splits multiline messages. See prefix, below.
6463
Optional args:
65-
delay: Wait delay seconds before sending message. Useful mainly with lock. See below.
66-
lock: Until message sent (see delay) allow no new messages with the same user and lock value.
67-
Generally speaking, expected to be a string. But it doesn't really matter.
6864
prefix: Add given prefix to every line sent, eg '<server>: '.
69-
Returns bool of whether message was sent (see lock)
7065
"""
71-
global locks
72-
if lock is not None:
73-
if (user, lock) in locks:
74-
return False
75-
else:
76-
locks.add((user, lock))
7766
message = unicode(message)
78-
def tell_send():
79-
reverse = dict([(y,x) for x,y in packet_names.items()])
80-
for line in message.split('\n'):
81-
packet = Packet()
82-
packet.ident = reverse['Chat message']
83-
packet.direction = SERVER_TO_CLIENT
84-
packet.data = {'text': line}
85-
send_packet(packet, user, False)
86-
if lock is not None:
87-
locks.remove((user, lock))
8867

89-
if delay:
90-
schedule(tell_send, delay)
91-
else:
92-
tell_send()
68+
reverse = dict([(y,x) for x,y in packet_names.items()])
69+
for line in message.split('\n'):
70+
packet = Packet()
71+
packet.ident = reverse['Chat message']
72+
packet.direction = SERVER_TO_CLIENT
73+
packet.data = {'text': line}
74+
send_packet(packet, user, False)

plugins.py

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
plugins.append(log_all)
1313
plugins.append(log_sorted) # Always first to catch all raw packets
1414
plugins.append(usernames) # The earlier the better, name the login packets sooner.
15+
plugins.append(schedule) # Before anything that depends on it.
16+
plugins.append(plugin_helpers) # Lots of things depend on this. Do it early.
1517
plugins.append(timed_events) # Before anything that depends on it. Not working yet.
1618
plugins.append(cmd) # Should probably be before anything that depends on it
1719
plugins.append(welcome) # Has /help, so high priority. But should be after usernames (for the welcome)

plugins/no_changes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Note this is a lengthy operation and should be used sparingly."""
33

44
import packet_decoder
5-
import simple_logging as logging
5+
import logging
66

77
def on_start():
88
pass

plugins/plugin_helpers.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
AUTHOR = "ekimekim"
3+
CONTACT = "[email protected]"
4+
DESCRIPTION = """This, like helpers.py, is for helper functions.
5+
However, it is reserved for functions that require other plugins to work.
6+
This cannot be done from helpers.py.
7+
Hence we have a helper plugin.
8+
"""
9+
10+
import helpers
11+
from schedule import schedule
12+
13+
14+
locks = set()
15+
def tell(user, message, delay=0, lock=None, prefix=''):
16+
"""Send a server message to user.
17+
Note: Splits multiline messages. See prefix, below.
18+
Optional args:
19+
delay: Wait delay seconds before sending message. Useful mainly with lock. See below.
20+
lock: Until message sent (see delay) allow no new messages with the same user and lock value.
21+
Generally speaking, expected to be a string. But it doesn't really matter.
22+
prefix: Add given prefix to every line sent, eg '<server>: '.
23+
Returns bool of whether message was sent (see lock).
24+
25+
Note: Apart from delay and lock args, this function acts like helpers.tell()
26+
"""
27+
global locks
28+
if lock is not None:
29+
if (user, lock) in locks:
30+
return False
31+
else:
32+
locks.add((user, lock))
33+
message = unicode(message)
34+
def tell_send():
35+
helpers.tell(user, message, prefix=prefix)
36+
if lock is not None:
37+
locks.remove((user, lock))
38+
39+
if delay:
40+
schedule(tell_send, delay)
41+
else:
42+
tell_send()

plugins/schedule.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import time
2+
3+
events = [] # (time, event) - time is float epoch
4+
5+
def schedule(delay, callback_fn):
6+
"""Schedule callback_fn to run after (at least) delay seconds."""
7+
new_time = time.time() + delay
8+
for i in range(len(events)):
9+
event_time, event = events[i]
10+
if event_time > new_time:
11+
events.insert(i, (new_time, callback_fn))
12+
break
13+
else:
14+
events.append((new_time, callback_fn))
15+
16+
def on_tick(users):
17+
now = time.time()
18+
while events and now >= events[0][0]:
19+
events[0][1]()
20+
events.pop(0)

plugins/timed_events.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33
CONTACT = '[email protected]'
44
DESCRIPTION = """A module for registering a callback to occur at a later time."""
55

6-
def on_start():
7-
pass
86

9-
def on_packet(packet, user, to_server):
10-
return packet
11-
12-
def on_tick():
13-
pass # TODO
7+
events = [] # ordered list of (time, key, callback)
8+
#TODO UPTO
149

1510
def register(timeout, callback, key=None):
1611
"""Register a callback to occur after given timeout (int or float, seconds).
1712
If key is given, it will replace an old timeout (if any) with the same key.
1813
"""
19-
pass # TODO
14+
new_time = time.time() + delay
15+
for i in range(len(events)):
16+
event_time, event = events[i]
17+
if event_time > new_time:
18+
events.insert(i, (new_time, callback_fn))
19+
break
20+
else:
21+
events.append((new_time, callback_fn))
22+
23+
2024

2125
def check(key):
2226
"""Returns time remaining for timeout with given key.
@@ -29,3 +33,12 @@ def clear(key):
2933
If no such timeout exists, raises KeyError.
3034
"""
3135
pass # TODO
36+
37+
def schedule(delay, callback_fn):
38+
"""Schedule callback_fn to run after (at least) delay seconds."""
39+
40+
def on_tick(users):
41+
now = time.time()
42+
while events and now >= events[0][0]:
43+
events[0][1]()
44+
events.pop(0)

proxy.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ def main():
6969

7070
logging.debug("Started up")
7171

72-
# signal(signal.SIGALRM, handle_tick)
73-
# setitimer(signal.ITIMER_REAL, TICK_INTERVAL, TICK_INTERVAL)
72+
signal(signal.SIGALRM, handle_tick)
73+
setitimer(signal.ITIMER_REAL, TICK_INTERVAL, TICK_INTERVAL)
7474

7575
try:
7676
while 1:
@@ -265,6 +265,12 @@ def daemonise():
265265
sys.exit(0)
266266

267267

268+
def handle_tick(sig, frame):
269+
for plugin in plugins:
270+
if hasattr(plugin, 'on_tick'):
271+
plugin.on_tick(set(user_map.values()))
272+
273+
268274
def handle_packet(packet, user, to_server):
269275
"""
270276
packet: The packet object recieved

schedule.py

-33
This file was deleted.

simple_logging.py

-44
This file was deleted.

usrtrace.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""On import, sets up SIGUSR1 to print a traceback as INFO for simple_logging"""
1+
"""On import, sets up SIGUSR1 to print a traceback as INFO for python logging"""
22

33
import signal, traceback
44
import logging

0 commit comments

Comments
 (0)