📄 psmclient.py
字号:
print g_programName+'new actor id is %s' % (newActorId,)
newActor = actor.Actor()
newActor.SetAllStateIds([p,m,a])
g_actorDict[newActorId] = newActor
print g_inputPrompt
return data[4:]
def Recv_StateCorrection(data):
"""
A state correction has been recieved from
the server. This means this client allowed
an action for the local actor that was not
allowed on the server, so immediately correct
the local actor to reflect the server specified
state.
"""
actor = g_actorDict[g_actorId]
p, m, a = struct.unpack("<BBB", data[0:3])
data = data[3:]
blockedDict = {}
(numEntries,) = struct.unpack("<B", data[0])
data = data[1:]
for i in range(1, numEntries+1):
stateId, blockCount = struct.unpack("<BB", data[0:2])
blockedDict[shared._GetStateModuleById(stateId)] = blockCount
data = data[2:]
actor.SetAllStateIds([p,m,a])
actor.SetBlockedStateDict(blockedDict)
print g_programName+'received correction to [%s] [%s] [%s] for local actor [%s]' % (_ParseStateName(actor.GetPostureState()), \
_ParseStateName(actor.GetMovementState()), \
_ParseStateName(actor.GetActionState()), \
g_actorId)
print g_inputPrompt
return data
def Recv_StateChange(data):
"""
A normal state change has occured for the
specified actor (who is not local). Set that
actors states to the ones specified in the
message.
"""
actorId, p, m, a = struct.unpack("<BBBB", data[0:4]) #TODO ONLY ONE STATE
actor = g_actorDict[actorId]
actor.SetAllStateIds([p,m,a])
print g_programName+'actor [%s] changed state to [%s] [%s] [%s]' % (actorId, _ParseStateName(actor.GetPostureState()), \
_ParseStateName(actor.GetMovementState()), \
_ParseStateName(actor.GetActionState()))
return data[4:]
def Recv_ActorFrozen(data):
"""
An actor got frozen. Transition them to the
states then prevents them from moving any more
"""
global g_actorDict
(frozenActorId,) = struct.unpack("<B", data[0:1])
if g_actorId == frozenActorId:
print g_programName+'Local actor [%s] is frozen!' % (frozenActorId,)
else:
print g_programName+'Actor [%s] is frozen!' % (frozenActorId,)
frozenActor = g_actorDict[frozenActorId]
frozenActor.ForceDefaultStates()
frozenActor.BlockMovement()
print g_inputPrompt
return data[1:]
def Recv_ActorDead(data):
"""
An actor died. Transition them to the dead
state which makes sure they won't be able to
do anything.
"""
global g_actorDict
(deadActorId,) = struct.unpack("<B", data[0:1])
if g_actorId == deadActorId:
print g_programName+'Local actor [%s] is dead!' % (deadActorId,)
else:
print g_programName+'Actor [%s] is dead!' % (deadActorId,)
deadActor = g_actorDict[deadActorId]
deadActor.TransitionTo(_dead)
print g_inputPrompt
return data[1:]
def Recv_LoginAck(data):
"""
Login acknowledgement from server. Assign
the local actor id to the value passed in
the message and create the actor instance.
The instance is stored for subsequent use
"""
global g_actorId
global g_actorDict
(g_actorId,) = struct.unpack("<B", data[0])
print g_programName+'Login ok, local actor id is %s' % (g_actorId,)
print g_inputPrompt
g_actorDict[g_actorId] = actor.Actor()
return data[1:]
def _ReadNetwork(s):
"""
Selects on the specifid socket s, if any data
comes through, parse the message type and
send the remaining data along to the method
called for the particular message type. The
handling method must return any data it did not
process.
"""
input, ignored, ignored = select.select([s], [], [], 0)
for sock in input:
data = sock.recv(1024)
while len(data):
(msgType,) = struct.unpack("<B", data[0])
data = data[1:]
if msgType == MSG_TYPE_LOGIN_ACK:
data = Recv_LoginAck(data)
elif msgType == MSG_TYPE_NEW_ACTOR:
data = Recv_NewActor(data)
elif msgType == MSG_TYPE_ACTOR_LEFT:
data = Recv_ActorLeft(data)
elif msgType == MSG_TYPE_STATE_CORRECTION:
data = Recv_StateCorrection(data)
elif msgType == MSG_TYPE_STATE_CHANGE:
data = Recv_StateChange(data)
elif msgType == MSG_TYPE_ACTOR_FROZEN:
data = Recv_ActorFrozen(data)
elif msgType == MSG_TYPE_ACTOR_DEAD:
data = Recv_ActorDead(data)
elif msgType == MSG_TYPE_SHUTDOWN:
return 0
else:
print g_programName+'unknown message type %s, abort' % (msgType,)
data = ''
return 1
def main():
"""
main() - main program loop
"""
# Parse the port argument if passed
global g_port
global g_serverName
if len(sys.argv) > 1:
g_serverName = sys.argv[1]
if len(sys.argv) > 2:
value = int(sys.argv[2])
if value > 0:
g_port = value
global g_userInput
global g_inputValue
# connect to the server and send it a login message
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
print g_programName+'connecting to [%s] on port [%s]...' % (g_serverName, g_port)
s.connect((g_serverName, g_port))
data = struct.pack("<B", MSG_TYPE_LOGIN)
s.send(data)
s.setblocking(0)
except:
# server not up, we go down...
print g_programName + 'unable to connect, is server running?'
return
# start a separate thread to get user input
inputThread = threading.Thread(None, _GetInput)
inputThread.start()
# loop forever checking for user input and messages from the server
try:
while 1:
if inputThread.isAlive():
# checking for user input
if g_userInput:
a = threading.Lock()
a.acquire()
# valid user input, call the appropriate method
_InvokeMethod(g_inputValue, s)
g_userInput = 0
g_inputValue = None
a.release()
# check for incoming network messages
if _ReadNetwork(s) == 0:
print g_programName+'got shutdown message. Press [Enter] to continue.'
inputThread._Thread__stop()
s.close()
break
else:
# user selected to exit program
print g_programName+'exiting'
s.close()
break
except KeyboardInterrupt:
# if the user decides to ctrl-c to exit
print g_programName+'exiting'
s.close()
del inputThread
# run main()
if __name__ == '__main__':
print g_programName+'starting up...'
main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -