Skip to content

Commit

Permalink
修改消息格式
Browse files Browse the repository at this point in the history
  • Loading branch information
mailgyc committed Mar 8, 2020
1 parent ac32c0f commit 2ca9625
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
13 changes: 5 additions & 8 deletions doudizhu/apps/game/components/simple.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from random import randint
from typing import TYPE_CHECKING

from tornado.ioloop import IOLoop
Expand All @@ -23,8 +22,8 @@ def __init__(self, uid: int, username: str, room: Room):
def allow_robot(self) -> bool:
return True

def to_server(self, packet):
IOLoop.current().add_callback(self.on_message, packet)
def to_server(self, code, packet):
IOLoop.current().add_callback(self.on_message, code, packet)

def write_message(self, packet):
IOLoop.current().add_callback(self._write_message, packet)
Expand All @@ -50,13 +49,12 @@ def _write_message(self, packet):
self.auto_ready()

def auto_ready(self):
IOLoop.current().add_callback(self.to_server, [Pt.REQ_READY, {'ready': 1}])
IOLoop.current().add_callback(self.to_server, Pt.REQ_READY, {'ready': 1})

def auto_rob(self):
pokers = [poker for poker in (54, 53, 2, 15, 28, 41) if poker in self.hand_pokers]
rob = int(len(pokers) >= 4)
packet = [Pt.REQ_CALL_SCORE, {'rob': rob}]
IOLoop.current().call_later(1.5, self.to_server, packet)
IOLoop.current().call_later(1.5, self.to_server, Pt.REQ_CALL_SCORE, {'rob': rob})

def auto_shot(self):
if not self.room.last_shot_poker or self.room.last_shot_seat == self.seat:
Expand All @@ -71,5 +69,4 @@ def auto_shot(self):
if 53 in pokers and 54 in pokers and left_pokers > 10:
pokers = []

packet = [Pt.REQ_SHOT_POKER, {'pokers': pokers}]
IOLoop.current().call_later(2, self.to_server, packet)
IOLoop.current().call_later(2, self.to_server, Pt.REQ_SHOT_POKER, {'pokers': pokers})
8 changes: 4 additions & 4 deletions doudizhu/apps/game/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,17 @@ def on_timeout(self):
if not self.is_left():
return
if self.state == State.CALL_SCORE:
self.on_message([Pt.REQ_CALL_SCORE, {'rob': 0}])
self.on_message(Pt.REQ_CALL_SCORE, {'rob': 0})
elif self.state == State.PLAYING:
if not self.room.last_shot_poker or self.room.last_shot_seat == self.seat:
self.on_message([Pt.REQ_SHOT_POKER, rule.find_best_shot(self.hand_pokers)])
self.on_message(Pt.REQ_SHOT_POKER, rule.find_best_shot(self.hand_pokers))
else:
self.on_message([Pt.REQ_SHOT_POKER, {'pokers': []}])
self.on_message(Pt.REQ_SHOT_POKER, {'pokers': []})

def handle_leave(self, code: int, packet: Dict[str, Any]):
from .storage import Storage
if code == Pt.REQ_JOIN_ROOM:
self.set_left(False)
self.set_left(0)
room_id, level = packet.get('room', -1), packet.get('level', 1)
if room_id == -1:
self.restart()
Expand Down

0 comments on commit 2ca9625

Please sign in to comment.