-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbc.py
147 lines (119 loc) · 5.63 KB
/
cbc.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Script tool for progressively applying a large number of block changes to the map.
Usage:
# At the top of the file
import cbc
# in your apply_script() function
apply_script(protocol, connection, config)
protocol, connection = cbc.apply_script(protocol, connection, config)
# start
generator = self.create_generator_function()
handle = self.protocol.cbc_add(generator)
# or
handle = self.protocol.cbc_add(generator, update_interval, self.callback_function, *callback_args)
# update_interval is the time (in seconds) between calls to `self.callback_function`
# stop
self.protocol.cbc_cancel(handle)
Callback receives these args:
def callback_function(self, cbc_type, progress, total_elapsed_seconds, *callback_args):
The generator function should `yield <packets>, <progress>` for each unique packet sent to clients
Where packets is the number of packets sent this iteration, and progress is the current progress percentage
Author: infogulch
"""
from twisted.internet.task import LoopingCall
import time
import random
class _CbcInfo:
generator = None
update_interval = 0.0
callback = None
callback_args = None
last_update = time.time()
start = time.time()
progress = 0.0
def __init__(self, generator, update_interval, callback, callback_args):
self.generator = generator
self.update_interval = update_interval
self.callback = callback
self.callback_args = callback_args
#note: client crashes when this goes over ~50
class ServerPlayer(object):
server_players = set()
def __init__(self):
id = 33
while id in ServerPlayer.server_players:
id += 1
self.player_id = id
ServerPlayer.server_players.add(id)
def __del__(self):
ServerPlayer.server_players.discard(self.player_id)
TIME_BETWEEN_CYCLES = 0.06
MAX_UNIQUE_PACKETS = 30 # per 'cycle', each block op is at least 1
MAX_PACKETS = 300 # per 'cycle' cap for (unique packets * players)
MAX_TIME = 0.03 # max time each cycle takes
def apply_script(protocol, connection, config):
if hasattr(protocol, 'cbc_add'):
return protocol, connection
class CycleBlockCoiteratorProtocol(protocol):
CBC_UPDATE, CBC_CANCELLED, CBC_FINISHED = range(3)
def __init__(self, *args, **kwargs):
protocol.__init__(self, *args, **kwargs)
self._cbc_running = False
self._cbc_generators = {}
self._cbc_call = LoopingCall(self._cbc_cycle)
def cbc_add(self, generator, update_time = 10.0, callback = None, *args):
info = _CbcInfo(generator, update_time, callback, args)
handle = max(self._cbc_generators.keys() + [0]) + 1
self._cbc_generators[handle] = info
if not self._cbc_running:
self._cbc_running = True
self._cbc_call.start(TIME_BETWEEN_CYCLES, False)
#this handle lets you cancel in the middle later
return handle
def cbc_cancel(self, handle):
if self._cbc_generators.has_key(handle):
info = self._cbc_generators[handle]
if info.callback is not None:
info.callback(CANCELLED, info.progress, time.time() - info.start, *info.callback_args)
del self._cbc_generators[handle]
def _cbc_cycle(self):
sent_unique = sent_total = progress = 0
current_handle = None
cycle_time = time.time()
while self._cbc_generators:
try:
for handle, info in self._cbc_generators.iteritems():
if sent_unique > MAX_UNIQUE_PACKETS:
return
if sent_total > MAX_PACKETS:
return
if time.time() - cycle_time > MAX_TIME:
return
current_handle = handle
sent, progress = info.generator.next()
sent_unique += sent
sent_total += sent * len(self.players)
if (time.time() - info.last_update > info.update_interval):
info.last_update = time.time()
info.progress = progress
if not info.callback is None:
info.callback(self.CBC_UPDATE, progress, time.time() - info.start, *info.callback_args)
except StopIteration:
info = self._cbc_generators[current_handle]
if info.callback is not None:
info.callback(self.CBC_FINISHED, progress, time.time() - info.start, *info.callback_args)
del self._cbc_generators[current_handle]
else:
self._cbc_call.stop()
self._cbc_running = False
def on_map_change(self, map):
if hasattr(self, '_cbc_generators'):
for handle in self._cbc_generators.keys():
self.cbc_cancel(handle)
protocol.on_map_change(self, map)
def on_map_leave(self):
if hasattr(self, '_cbc_generators'):
for handle in self._cbc_generators.keys():
self.cbc_cancel(handle)
protocol.on_map_leave(self)
return CycleBlockCoiteratorProtocol, connection