Skip to content

Commit

Permalink
Merge pull request mailgyc#6 from yrong/master
Browse files Browse the repository at this point in the history
bug fix
  • Loading branch information
mailgyc authored Nov 1, 2017
2 parents 95bc8e5 + 1c3ec16 commit 874d9d8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/core/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@ def handle_call_score(self, score):
self.is_called = True

next_seat = (self.seat + 1) % 3
call_end = score == 3 or self.table.players[next_seat].is_called

call_end = score == 3 or self.table.all_called()
if not call_end:
self.table.whose_turn = next_seat
if score > 0:
self.table.last_shot_seat = self.seat
self.table.call_score = score
if score > self.table.max_call_score:
self.table.max_call_score = score
self.table.max_call_score_turn = self.seat

response = [Pt.RSP_CALL_SCORE, self.uid, score, call_end]
for p in self.table.players:
p.send(response)

if call_end:
self.table.call_score_end(score)
self.table.call_score_end()

def handle_shot_poker(self, pokers):
if pokers:
Expand Down
16 changes: 13 additions & 3 deletions src/core/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def __init__(self, uid, room):
self.pokers: List[int] = []
self.multiple = 1
self.call_score = 0
self.max_call_score = 0
self.max_call_score_turn = 0
self.whose_turn = 0
self.last_shot_seat = 0
self.last_shot_poker = []
Expand Down Expand Up @@ -63,7 +65,7 @@ def deal_poker(self):
# if not all(p and p.ready for p in self.players):
# return

self.state = 1
self.state = Table.PLAYING
self.pokers = [i for i in range(54)]
random.shuffle(self.pokers)
for i in range(51):
Expand All @@ -75,8 +77,9 @@ def deal_poker(self):
response = [Pt.RSP_DEAL_POKER, self.turn_player.uid, p.hand_pokers]
p.send(response)

def call_score_end(self, score):
self.call_score = score
def call_score_end(self):
self.call_score = self.max_call_score
self.whose_turn = self.max_call_score_turn
self.turn_player.role = 2
self.turn_player.hand_pokers += self.pokers
response = [Pt.RSP_SHOW_POKER, self.turn_player.uid, self.pokers]
Expand Down Expand Up @@ -153,3 +156,10 @@ def size(self):
def __str__(self):
return '[{}: {}]'.format(self.uid, self.players)

def all_called(self):
for p in self.players:
if not p.is_called:
return False
return True


19 changes: 13 additions & 6 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import tornado.escape
import tornado.ioloop
import tornado.options
from tornado.options import define, options
import tornado.web
import tornado.websocket
from net.socket import SocketHandler
Expand All @@ -17,6 +17,12 @@
logger.setLevel(logging.DEBUG)


define("host", default="localhost", help="DB host")
define("database", default="ddz", help="DB used")
define("user", default="root", help="DB username")
define("password", default="123123", help="DB Password")


class Application(tornado.web.Application):
def __init__(self):
handlers = [
Expand All @@ -34,21 +40,22 @@ def __init__(self):
xsrf_cookies=True,
debug=True,
)
tornado.options.parse_config_file("server.conf")
super(Application, self).__init__(handlers, **settings)
self.db = torndb.Connection(
host='localhost',
database='ddz',
user='root',
password='123123')
host=options.host,
database=options.database,
user=options.user,
password=options.password)
self.executor = ThreadPoolExecutor()


def main():
logger.info('http://127.0.0.1:8080')
tornado.options.parse_command_line()
app = Application()
app.listen(8080)
tornado.ioloop.IOLoop.current().start()
logger.info('listening on 8080')


if __name__ == '__main__':
Expand Down
4 changes: 4 additions & 0 deletions src/server.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
host = "localhost"
database = "ddz"
user = "root"
password = "root"
3 changes: 1 addition & 2 deletions src/static/js/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ PG.Protocol = {
};

PG.Socket = {
wsUri: "ws://127.0.0.1:8080/ws",
websocket: null,
onmessage: null
};
Expand All @@ -52,7 +51,7 @@ PG.Socket.connect = function(onopen, onmessage, onerror) {
return;
}

this.websocket = new WebSocket(this.wsUri);
this.websocket = new WebSocket("ws://" + window.location.host + "/ws");
this.websocket.binaryType = 'arraybuffer';
this.websocket.onopen = function(evt) {
console.log("CONNECTED");
Expand Down

0 comments on commit 874d9d8

Please sign in to comment.