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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
# Py2Play
# Copyright (C) 2001-2002 Jean-Baptiste LAMY
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import struct, thread, time, select, socket
import py2play.idler as idler
#from py2play import recvall
from socket import error as SocketError
MINIMUM_QUEUE_LENGHT = 3
ACTION_WAIT = " "
ACTION_STALLED = "S"
class Action:
def __init__(self, action = ACTION_WAIT, round = None):
self.round = round
self.action = action
def __repr__(self):
return "<Action %s at round %s>" % (self.action, self.round)
#def WRITE_ACTION(socket, action):
# socket.sendall(action.action)
# socket.sendall(struct.pack("!i", action.round))
#def READ_ACTION(socket):
# return Action(recvall(socket, 1), struct.unpack("!i", recvall(socket, 4))[0])
class ActiveQueue:
def __init__(self, round = 0):
self.round = round
self.actions = []
self.old_actions = []
self.listeners = []
self.lock = thread.allocate_lock()
self.next_action = None
self.fill()
def plan_action(self, action):
self.next_action = action
def append(self, action):
self.lock.acquire() # Lock to avoid that different threads call this method simultaneously.
action.round = self.round
i = 0
while i < len(self.listeners):
try:
WRITE_ACTION(self.listeners[i], action)
i = i + 1
except SocketError:
print "* Py2Play * An actions listener has close its connexion."
del self.listeners[i]
self.actions.append(action)
self.old_actions.append(action)
if len(self.old_actions) > 5000: del self.old_actions[0]
self.round = self.round + 1
self.lock.release()
def fill(self):
#if len(self.actions) > MINIMUM_QUEUE_LENGHT:
# print "TooManyAction", len(self.actions)
if len(self.actions) < MINIMUM_QUEUE_LENGHT:
if self.next_action:
self.append(self.next_action)
self.next_action = None
while len(self.actions) < MINIMUM_QUEUE_LENGHT: self.append(Action())
def pop(self, round):
action = self.actions.pop(0)
if round != action.round:
print "* Py2Play * ERROR : playing action for round %s but character is at round %s !" % (action.round, round)
return action
def recover_actions(self, start_round, end_round = None):
first = self.old_actions[0].round
if start_round < first:
print "* Py2Play * ERROR : cannot recover action %s, it's too old !" % start_round
return ()
if not end_round:
end_round = self.round
elif end_round >= self.round:
print "* Py2Play * ERROR : cannot recover action %s, i don't have it yet ! I wait for them." % end_round
while end_round >= self.round: time.sleep(0.003)
return self.old_actions[start_round - first : end_round - first]
def __len__(self): return len(self.actions)
_EMPTY = []
class PhantomQueue:
def __init__(self, socket, player):
self.actions = []
self.socket = socket
self.player = player
self.socket = socket
self.check_late = 1
def ____fill(self):
if len(self.actions) < 1:
try:
while 1:
if not select.select([self.socket], _EMPTY, _EMPTY, 0)[0]:
print "attente..."
idler.IDLER.accel(-1)
self.actions.append(READ_ACTION(self.socket))
if not select.select([self.socket], _EMPTY, _EMPTY, 0)[0]: break
except:
import sys
sys.excepthook(*sys.exc_info())
print "* Py2Play * a character is no longer reachable : %s." % self.player.name
self.actions.append(Action())
if self.player.character in self.player.level.characters:
self.player.level.remove_character(self.player.character)
def fill(self):
if len(self.actions) < 1:
try:
if not select.select([self.socket], _EMPTY, _EMPTY, 0)[0]:
t = time.time()
self.actions.append(READ_ACTION(self.socket))
t = t - time.time()
idler.IDLER.accel(t / idler.ROUND_DURATION)
else:
#t = time.time()
self.actions.append(READ_ACTION(self.socket))
#t = t - time.time()
#
#idler.IDLER.accel(t / idler.ROUND_DURATION)
except:
import sys
sys.excepthook(*sys.exc_info())
print "* Py2Play * player %s is no longer reachable ! Removing him/her." % self.player.name
self.actions.append(Action())
if self.player.character in self.player.level.characters:
self.player.level.remove_character(self.player.character)
def ____pop(self, round = None):
if (not round is None) and (self.actions) and (not self.actions[-1].round is None):
#if self.actions[-1].round - round > MINIMUM_QUEUE_LENGHT:
if self.actions[-1].round - round >= MINIMUM_QUEUE_LENGHT:
if self.check_late:
idler.IDLER.accel(self.actions[-1].round - round - MINIMUM_QUEUE_LENGHT + 1)
self.check_late = 0
else: self.check_late = 1
if not self.actions: self.fill()
#return self.actions[0]
action = self.actions.pop(0)
if (not round is None) and (not action.round is None):
if round > action.round:
print "* Py2Play * WARNING : character is at round %s but the next action is for round %s ! Skipping." % (round, action.round)
self.fill()
return self.pop(round)
elif round < action.round:
print "* Py2Play * actions from round %s to round %s are lacking... i ask for them." % (round, action.round)
actions = self.player.question_actions(round, action.round)
if actions is None: print "* Py2Play * ERROR : cannot question for actions %s to % !" % (round, action.round)
self.actions = actions[1:] + [action] + self.actions
return actions[0]
return action
def pop(self, round = None):
#if (not round is None) and (self.actions) and (not self.actions[-1].round is None):
# #if self.actions[-1].round - round > MINIMUM_QUEUE_LENGHT:
# if self.actions[-1].round - round >= MINIMUM_QUEUE_LENGHT:
# if self.check_late:
# idler.IDLER.accel(self.actions[-1].round - round - MINIMUM_QUEUE_LENGHT + 1)
# self.check_late = 0
# else: self.check_late = 1
if not self.actions: self.fill()
action = self.actions.pop(0)
if (not round is None) and (not action.round is None):
if round > action.round:
print "* Py2Play * WARNING : character is at round %s but the next action is for round %s ! Skipping." % (round, action.round)
self.fill()
return self.pop(round)
elif round < action.round:
print "* Py2Play * actions from round %s to round %s are lacking... i ask for them." % (round, action.round)
actions = self.player.question_actions(round, action.round)
if actions is None: print "* Py2Play * ERROR : cannot question for actions %s to % !" % (round, action.round)
self.actions = actions[1:] + [action] + self.actions
return actions[0]
return action
def __len__(self): return len(self.actions)
|