def process_msg(self,msg): logging.debug(msg) # login if msg.type == message.MsgType.TLogin: user = msgdb.get_user(msg.login.name) if user is not None: # found the user from users db logging.debug("---login ok!---") # tag protocol,who am i self.user = user # global manage protocols username_protocols[self.user.name] = protocol #if i am line broken if user in broken_users: linebroken.user_come_back(user) #----------------------------------- msg = message.Msg() msg.type = message.MsgType.TLoginOk self.send_msg(msg) else: # the user not in db logging.debug("---login error!---") msg = message.Msg() msg.type = message.MsgType.TLoginFail self.send_msg(msg) else: # no that msg! logging.info("not support the type msg now")
def user_line_broken(self,user): # first,when line broken,protocol is useless users_manager.remove_user(user) self.broken_users.append(user) for game in user.games: #as player if user in game.players: other_user = game_other_user(game.players,user) if not self.is_user_broken(other_user): if not game.line_broken: game.line_broken = True # first,tell other user the game line broken! msg = message.Msg() msg.type = message.MsgType.TLineBroken msg.line_broken.game_id = game.id users_manager.send_user_msg(other_user,msg) else: # already linebroken,nothing to do another pass # line broken count down self.broken_timer.add_game_user(game,user) #as watcher if user in game.watchers: # wather line broken game.watchers.remove(user) # wacher leave msg msg = message.Msg() msg.type = message.MsgType.TWatcherLeave msg.watcher_leave.game_id = game.id msg.watcher_leave.user = user users_manager.send_users_msg(game.players + game.watchers,msg)
def user_come_back(self,user): # line broken user come back. # some game paused for the user line broken,we should rerun it assert user in self.broken_users index = self.broken_users.index(user) myuser = self.broken_users[index] # myuser have games user.games = myuser.games self.broken_users.remove(user) self.broken_timer.remove_user(user) for game in user.games: # as player if user in game.players: # first,give the game data to the comeback user msg = message.Msg() msg.type = message.MsgType.TGameData msg.game_data.game = game users_manager.send_user_msg(user,msg) # can we restart the game? it's about other_user other_user = msgserver.game_other_user(game.players,user) if not self.is_user_broken(other_user): # i come back,and you not line broken,so we can start again assert game.line_broken == True game.line_broken = False msg = message.Msg() msg.type = message.MsgType.TComeBack msg.come_back.game_id = game.id users_manager.send_users_msg(game.players,msg) else: # means other_user linebroken too,and not login now. nothing to do! pass
def create_game(self,rule,user1,user2): game = Game(rule,user1,user2) self.live_games.append(game) msg = message.Msg() msg.type = message.MsgType.TGameData msg.game_data.game = game users_manager.send_user_msg(game.players,msg) return game def end_game(self,game,result): game.game_over(result) for user in game.players: user.remove(game) self.live_games.remove(game) self.dead_games.append(game) # tell broken timer,the game is dead line_broken_manager.broken_timer.game_over(game) msg = message.Msg() msg.type = message.MsgType.TGameOver msg.game_over.game_id = game.id msg.game_over.result.CopyFrom(result) users_manager.send_users_msg(game.players,msg) users_manager.send_users_msg(game.watchers,msg)
def process_msg(self, bin): msg = message.Msg() msg.ParseFromString(bin) logging.debug(msg) # login if msg.type == message.MsgType.LOGIN: player = msgdb.get_player(msg.login.pid, msg.login.passwd) if player is not None: # clear the old player infomation,so we can change my role free self.leave() logging.debug("---login ok!---") self.player = player if self not in protos: protos.append(self) msg = message.Msg() msg.type = message.MsgType.LOGIN_OK msg.login_ok.player.CopyFrom(self.player) self.make_data(msg.login_ok) self.send_msg(msg) else: logging.debug("---login error!---") msg = message.Msg() msg.type = message.MsgType.LOGIN_FAIL self.send_msg(msg) # logout elif msg.type == message.MsgType.LOGOUT: self.leave() # server data elif msg.type == message.MsgType.DATA: msg = message.Msg() msg.type = message.MsgType.DATA players = [p.player for p in protos] self.make_data(msg) self.send_msg(msg) else: logging.info("not support the type msg now")
def on_notification(self, handle, data): message = message_pb2.Msg() print(data) data_striped = data.replace('\x1b%\x00', '') message.ParseFromString(data_striped) # print("{}: {}".format(time.time(), message.val)) self.num_lines = self.num_lines + 1 if "DONE" in message.val: self.set_done(True)
def process_msg(self, msg): logging.debug(msg) if msg.type == message.MsgType.TLogin: # found the user from users db logging.debug("---login ok!---") msg = message.Msg() msg.type = message.MsgType.TLoginOk self.send_msg(msg) else: # no that msg! logging.info("not support the type msg now")
def on_run_button(self,event): self.output_text.Clear() myline = self.input_text.GetValue().split() cmd = myline[0] if cmd == "client_data": self.output_text.Clear() self.output_text.AppendText("----") self.output_text.AppendText("\n") self.output_text.AppendText(str(self.player)) self.output_text.AppendText("\n") self.output_text.AppendText(str(self.players)) self.output_text.AppendText("\n") self.output_text.AppendText(str(self.games)) self.output_text.AppendText("\n") self.output_text.AppendText("----") elif cmd == "server_data": msg = message.Msg() msg.type = message.MsgType.DATA self.send_msg(msg) elif cmd == "login": msg = message.Msg() msg.type = message.MsgType.LOGIN msg.login.pid = myline[1] msg.login.passwd = myline[2] self.send_msg(msg) elif cmd == "logout": self.init_data() msg = message.Msg() msg.type = message.MsgType.LOGOUT self.send_msg(msg) else: logging.info("***no that command!***\n") self.input_text.Clear()
def process_msg(self,msg): logging.debug(msg) # login if msg.type == message.MsgType.TLogin: user = msgdb.get_user(msg.login.name) if user is not None: # found the user from users db logging.debug("---login ok!---") users_manager.add_user(user,self) #if i am line broken if line_broken_manager.is_user_broken(user): line_broken_manager.user_come_back(user) msg = message.Msg() msg.type = message.MsgType.TLoginOk self.send_msg(msg) else: # the user not in db logging.debug("---login error!---") msg = message.Msg() msg.type = message.MsgType.TLoginFail self.send_msg(msg)
def on_run_button(self,event): self.output_text.Clear() myline = self.input_text.GetValue().split() cmd = myline[0] if cmd == "login": msg = message.Msg() msg.type = message.MsgType.TLogin msg.login.name = myline[1] self.send_msg(msg) else: logging.info("***no that command!***\n") self.input_text.Clear()
def end_game(game,result): game.state = message.State.stopped game.result.CopyFrom(result) msg = message.Msg() msg.type = message.MsgType.TGameOver msg.game_over.game_id = game.id msg.game_over.result.CopyFrom(result) del live_id_games[game.id] dead_id_games[game.id] = game # tell broken timer,the game is dead linebroken.broken_timer.game_over(game) send_users_msg(game.users,msg) send_users_msg(game.watchers,msg)
def countdown(self): gu = map(lambda g,u,t : (g,u,t-1),self.game_user_timeout) self.game_user_timeout = filter(lambda g,u,t : t > 0,gu) for game,user,timeout in self.game_user_timeout: msg = message.Msg() msg.type = message.MsgType.TCountdown msg.countdown.game_id = game.id msg.countdown.user = user msg.countdown.timeout = timeout users_manager.send_user_msg(user,msg) users_manager.send_users_msg(game.watchers,msg) for game,user,_ in filter(lambda g,u,t: t <= 0,gu): other_user = game_other_user(game,user) result = message.Result() result.end_type = message.EndType.time_out result.winner = users_manager.game_user_color(game,other_user) games_manager.end_game(game,result)
def data_received(self, data): self.buf += data if len(self.buf) < self.headSize: logging.debug("dataSize < headSize!") return bodySize, = struct.unpack('<I', self.buf[:self.headSize]) logging.debug("bodySize={}".format(bodySize)) if len(self.buf) < self.headSize + bodySize: logging.debug("message data not enougth!") return bin = self.buf[self.headSize:self.headSize + bodySize] msg = message.Msg() msg.ParseFromString(bin) self.process_msg(msg) self.buf = self.buf[self.headSize + bodySize:]
def get_message(): """ This endpoint is hit via an AJAX POST call periodically at intervals specified in the client. Depending on whether or not the USE_PROTOBUFF method is true, the message is sent as JSON or as a protocol buffer. This method grabs all messages from the database that the user has permission to see. """ global USE_PROTOBUFF if USE_PROTOBUFF: PreMsgClient = message_pb2.MsgClient() result = base64.b64decode(request.form['protoString']) PreMsgClient.ParseFromString(result) other_user = str(PreMsgClient.id) select_type = PreMsgClient.chat_type else: other_user = request.form['user_id'] select_type = int(request.form['select_type']) out = [] Session = scoped_session(sessionmaker(bind=engine)) s = Session() #grab the group id for the messages that we want if select_type == USER: user_ids = sorted([current_user.id, other_user]) users_str = '"[' + ", ".join(user_ids) + ']"' query = 'SELECT * from groups WHERE user_ids = %s LIMIT 1' % users_str result_proxy = s.execute(query) result = result_proxy.fetchone() if result is None: s.close() return json.dumps(out) group_id = result[0] else: group_id = other_user #grab all messages for the chat result_proxy = s.execute( 'SELECT messages.id, users.name, messages.message from messages JOIN users ON users.id=messages.user_id where group_id = %s' % group_id) s.close() results = result_proxy.fetchall() #parse messages into return datastructure for result in reversed(results): message_id = result[0] name = result[1] message = result[2] if USE_PROTOBUFF: out.append( message_pb2.Msg(message_id=int(message_id), name=name, message=message)) else: out.append((message_id, name, message)) if USE_PROTOBUFF: PostMsgClient = message_pb2.PostMsgClient(messages=out) return base64.b64encode(PostMsgClient.SerializeToString()) else: return json.dumps(out)
def process_msg(self,bin): msg = message.Msg() msg.ParseFromString(bin) wx.CallAfter(self.ui_obj.receive_msg,msg)