def login(app): """ 로그인을 처리 합니다. 호출 http://localhost:8080/[email protected]&password=1234 결과 {"code":0, "msg":""} : 성공 {"code":-1, "msg":"..."} : 필수 파라미터가 빠졌거나 잘못된 경우 """ try: req = get_request(req_proto) userid = str(req['userid']) password = req['password'] with app.userdb() as db: user = app.usermgr.login(db, userid, password) session = req['session'] session['num'] = user.num session['userid'] = user.id session['nickname'] = user.nickname session['lastlogin'] = time.time() session['ip'] = request.remote_addr session.save() return get_response(res_proto, NoError()) except ApplicationError as e: logging.debug(e) return get_response(res_proto, e) except Exception as e: logging.exception(e)
def post(app): """ 포스팅을 합니다. 호출 http://localhost:8080/[email protected]&context="this is posting"&images=http://www.happ.kr/images/01.jpg,http://www.happ.kr/images/02.jpg&status=1 결과 {"code":0, "msg":""} : 성공 {"code":-1, "msg":"..."} : 필수 파라미터가 빠졌거나 잘못된 경우 """ try: req = get_request(req_proto) userid = req['userid'] context = req['context'] images = req['images'].split(",") status = req['status'] with app.postdb() as db: app.postmgr.post(db, userid, images, context, status) return get_response(res_proto, NoError()) except ApplicationError as e: logging.debug(e) return get_response(res_proto, e) except Exception as e: logging.exception(e)
def logout(app): """ 로그아웃 한다. 호출 http://localhost:8080/logout 결과 {"msg": "", "code": 0} : 성공 {"msg": "login required.", "code": -2} : 로그인 하지 않은 상태에서 로그아웃 요청 """ try: req = get_request(req_proto) session = req['session'] userid = session['userid'] session.delete() session.save() with app.userdb() as db: app.usermgr.logout(db, userid) return get_response(res_proto, NoError()) except ApplicationError as e: logging.debug(e) return get_response(res_proto, e) except Exception as e: logging.exception(e)
def execute(self): for i, command in enumerate(self.commands): status = command.execute() if not status.ok(): return status self.max_success_index = i return NoError()
def execute(self) -> Status: if self.steps not in self.dices: return InvalidDiceError(self.steps, self.dices) self.index = self.dices.index(self.steps) self.dices.pop(self.index) return NoError()
def join(app): """ 가입을 처리 합니다. 호출 http://localhost:8080/[email protected]&nickname=MyNickname&password=1234 결과 {"code":0, "msg":""} : 성공 {"code":-1, "msg":"..."} : 필수 파라미터가 빠졌거나 잘못된 경우 {"code":-1020, "msg":"..."} : 이미 해당 아이디가 존재하는 경우 {"code":-1030, "msg":"..."} : 잘못된 아이디(길이, 형식) {"code":-1040, "msg":"..."} : 잘못된 비밀번호(길이, 형식) """ try: req = get_request(req_proto) userid = str(req['userid']) nickname = unicode(req['nickname']) password = req['password'] with app.userdb() as db: app.usermgr.join(db, userid, password, nickname) return get_response(res_proto, NoError()) except ApplicationError as e: logging.debug(e) return get_response(res_proto, e) except Exception as e: logging.exception(e)
def receive_command(self, connection: PlayerConnection, command_str: str) -> Status: if not self.is_playing: return Status(-1, 'Game has not started yet') self._command_queue.put((connection, command_str), block=True) return NoError()
def leave_room(self, sid): session = self.sio.get_session(sid) connection = session['connection'] room_name = session['room_name'] if room_name is None: return _ack(Status(-1, 'Not in a room')) GameRoomDB.leave(connection, room_name) session['room_name'] = None return _ack(NoError())
def move(self, piece: Piece, steps: int) -> Status: piece_location = self.location(piece) if piece_location == self.LOC_OUT_BOARD: return CannotMoveError(piece.name, steps) if self.is_clear(steps - 1, steps - 1) and piece_location == steps - 2: self.clear_location(piece_location) self.set_location(piece, steps - 1) return NoError() return CannotMoveError(piece.name, steps)
def execute(self) -> Status: status = super().execute() if not status.ok(): return status piece_location = self.route.location(self.piece) if self.route.is_at_home_entrance(self.piece): if self.home.is_clear(0, self.steps - 1): self.route.clear_location(piece_location) self.home.set_location(self.piece, self.steps - 1) self.piece.set_place(Piece.PLACE_HOME) return NoError() return CannotMoveError(self.piece.name, self.steps)
def show(app): """ 포스팅한 내용을 rss 형식으로 리턴 합니다. 호출 http://localhost:8080/show?num=1 결과 {"code":0, "msg":"", "num": 1, "wid":"*****@*****.**", "wnick":"test", "wtime":"2014-..", "status":1, "images":"http://...jpg,http://...jpg", "context": "bla, bla..."} : 성공 {"code":-1, "msg":"..."} : 필수 파라미터가 빠졌거나 잘못된 경우 """ try: req = get_request(req_proto) try: session = req['session'] readerid = str(session['userid']) except KeyError: # 세션이 없는 경우는 로그인 하지 않았으므로 public 글만 볼 수 있다. readerid = None num = int(req['num']) with app.postdb() as db: post = app.postmgr.get_post(db, num, readerid) res = NoError() if post: res.num = post.num res.wtime = post.wtime res.status = post.status res.images = post.images res.context = post.context res.wnick = post.wnick else: raise PostNotExistError( "post not exist. readerid=%s, post_num=%s" % (readerid, num)) return get_response(res_proto, res) except ApplicationError as e: logging.debug(e) return get_response(res_proto, e) except Exception as e: logging.exception(e)
def move(self, piece: Piece, steps: int) -> Status: old_location = self.location(piece) new_location = self.norm_location(old_location + steps) # conditions is_clear = self.is_clear(self.norm_location(old_location + 1), self.norm_location(new_location - 1)) is_not_same_player = piece.player != self.piece_at(new_location).player is_not_pass_home = not self.is_pass_home_entrance(piece, steps) if is_clear and is_not_same_player and is_not_pass_home: self.clear_location(old_location) self.set_location(piece, new_location) return NoError() return CannotMoveError(piece.name, steps)
def ready(self, sid): session = self.sio.get_session(sid) room_name = session['room_name'] if room_name is None: return _ack(Status(-1, 'Not in a room')) room = GameRoomDB.rooms[room_name] if room.is_playing: return _ack(IsPlayingError()) room.ready(session['connection']) if room.is_able_to_start(): self.sio.start_background_task(room.start) return _ack(NoError())
def move(self, route: Route, dice: int): if not (dice == 1 or dice == 6): return Status(-1, 'Could not kickstart due to dice value') available_flag = [ piece.place == Piece.PLACE_NEST for piece in self.pieces ] if any(available_flag): index = available_flag.index(True) piece = self.pieces[index] piece_at_nest_door = route.piece_at(self.player.offset) is_not_same_player = self.player != piece_at_nest_door.player if is_not_same_player: piece.set_place(Piece.PLACE_ROUTE) route.set_location(piece, self.player.offset) return NoError() return Status(-1, 'There is no piece left in nest')
def execute(self): print(self.help_str) input('Press any key to continue.') return NoError()
def execute(self): print(f'Player {self.player.name} passed.') return NoError()
def enter(self, connection) -> Status: if self.is_playing or connection in self.connection_ready.keys(): return IsPlayingError() # TODO: Check room full self.connection_ready[connection] = False return NoError()