Skip to content

Commit

Permalink
feat(game): cli 기능 추가 바 키우기 (#255)
Browse files Browse the repository at this point in the history
* feat(game): 바높이 변경 로직 추가
* docs(game): bar heights logic
  • Loading branch information
middlefitting authored Feb 23, 2024
1 parent 221e50c commit 5214555
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
6 changes: 5 additions & 1 deletion backend/game/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ async def update_game(self, event):
"bar_right_y": event["bar_right_y"],
"circle_x": event["circle_x"],
"circle_y": event["circle_y"],
"left_bar_height": event["left_bar_height"],
"right_bar_height": event["right_bar_height"],
}))

async def info_game(self, event):
Expand All @@ -84,7 +86,9 @@ async def info_game(self, event):
"status": event["status"],
"winner": event["winner"],
"bar_width": event["bar_width"],
"bar_height": event["bar_height"],
# "bar_height": event["bar_height"],
"left_bar_height": event["left_bar_height"],
"right_bar_height": event["right_bar_height"],
"circle_radius": event["circle_radius"],
"screen_height": event["screen_height"],
"screen_width": event["screen_width"],
Expand Down
47 changes: 37 additions & 10 deletions backend/game/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ class MatchService:
SCREEN_HEIGHT = 600
BAR_WIDTH = 18
BAR_HEIGHT = 100
BAR_CHEAT_HEIGHT = 400
CIRCLE_RADIUS = 9

# bar_size cheet
BAR_SIZE_CHEAT = ["sechung is handsome", "jincpark is handsome", "hyeonjun is handsome"]


def __init__(self):
raise RuntimeError("Call get_instance() instead")
Expand Down Expand Up @@ -484,8 +489,10 @@ async def move_bar(self, user_id, command):
else:
if game_info.get("left_user_id") == str(user_id):
await self.update_game_info(game_session, "left_bar_mv", command)
await self.bar_size_cheat(game_session, "left_bar_height", command)
elif game_info.get("right_user_id") == str(user_id):
await self.update_game_info(game_session, "right_bar_mv", command)
await self.bar_size_cheat(game_session, "right_bar_height", command)

async def game_start(self, users_id: list, game_session):
"""
Expand Down Expand Up @@ -562,26 +569,28 @@ async def game_start(self, users_id: list, game_session):
bar_move = 0
await self.update_game_info(game_session, "right_bar_mv", "NONE")
bar_right_y += bar_move
left_bar_height = int(game_info.get("left_bar_height"))
right_bar_height = int(game_info.get("right_bar_height"))

if bar_y >= screen_height - bar_height:
bar_y = screen_height - bar_height
if bar_y >= screen_height - left_bar_height:
bar_y = screen_height - left_bar_height
elif bar_y <= 0:
bar_y = 0

if bar_right_y >= screen_height - bar_height:
bar_right_y = screen_height - bar_height
if bar_right_y >= screen_height - right_bar_height:
bar_right_y = screen_height - right_bar_height
elif bar_right_y <= 0:
bar_right_y = 0

if circle_x < bar_width:
if bar_y - circle_radius <= circle_y <= bar_y + bar_height + circle_radius:
if bar_y - circle_radius <= circle_y <= bar_y + left_bar_height + circle_radius:
circle_x = bar_width
speed_x = -speed_x
speed_x *= 1.1
speed_y *= 1.1

if circle_x + circle_radius >= bar_right_x:
if bar_right_y - circle_radius <= circle_y <= bar_right_y + bar_height + circle_radius:
if bar_right_y - circle_radius <= circle_y <= bar_right_y + right_bar_height + circle_radius:
circle_x = bar_right_x - circle_diameter
speed_x = -speed_x
speed_x *= 1.1
Expand Down Expand Up @@ -617,7 +626,8 @@ async def game_start(self, users_id: list, game_session):
circle_y = screen_height - circle_diameter

for user_id in users_id:
await self.handle_update_message(user_id, bar_x, bar_y, bar_right_x, bar_right_y, circle_x, circle_y)
await self.handle_update_message(user_id, bar_x, bar_y, bar_right_x, bar_right_y, circle_x, circle_y,
left_bar_height, right_bar_height)
start_time = current_time
await asyncio.sleep(1 / 60) # 1/60초 대기

Expand All @@ -630,7 +640,8 @@ async def game_start(self, users_id: list, game_session):


#HANDLE GAME
async def handle_update_message(self, user_id, bar_x, bar_y, bar_right_x, bar_right_y, circle_x, circle_y):
async def handle_update_message(self, user_id, bar_x, bar_y, bar_right_x, bar_right_y, circle_x, circle_y,
left_bar_height, right_bar_height):
"""
게임 실시간 위치 정보 파싱 및 update.game 이벤트 핸들러 호출
"""
Expand All @@ -643,6 +654,8 @@ async def handle_update_message(self, user_id, bar_x, bar_y, bar_right_x, bar_ri
"bar_right_y": bar_right_y,
"circle_x": circle_x,
"circle_y": circle_y,
"left_bar_height": left_bar_height,
"right_bar_height": right_bar_height,
})

async def handle_info_message(self, user_id, game_session):
Expand All @@ -663,7 +676,9 @@ async def handle_info_message(self, user_id, game_session):
"screen_width": self.SCREEN_WIDTH,
"screen_height": self.SCREEN_HEIGHT,
"bar_width": self.BAR_WIDTH,
"bar_height": self.BAR_HEIGHT,
# "bar_height": self.BAR_HEIGHT,
"left_bar_height": game_info.get("left_bar_height"),
"right_bar_height": game_info.get("right_bar_height"),
"circle_radius": self.CIRCLE_RADIUS,
"next_left_player": int(game_info.get("next_left_player")),
"next_right_player": int(game_info.get("next_right_player")),
Expand All @@ -680,9 +695,17 @@ async def handle_wait_message(self, user_id, time):
})


# GAME REDIS
async def bar_size_cheat(self, game_session, key, command):
if command in self.BAR_SIZE_CHEAT:
asyncio.create_task(self.bar_size_cheat_logic(game_session, key))

async def bar_size_cheat_logic(self, game_session, key):
await self.update_game_info(game_session, key, self.BAR_CHEAT_HEIGHT)
await asyncio.sleep(7)
await self.update_game_info(game_session, key, self.BAR_HEIGHT)


# GAME REDIS
async def set_user_game_session(self, user_id, game_session):
"""
게임 세션 키를 생성
Expand Down Expand Up @@ -735,4 +758,8 @@ async def set_game_info(self, left_user_id, right_user_id, game_type, game_sessi
"winner": "0",
"next_left_player": str(next_left_player) if next_left_player is not None else "0",
"next_right_player": str(next_right_player) if next_right_player is not None else "0",
"bar_width": self.BAR_WIDTH,
# "bar_height": self.BAR_HEIGHT,
"left_bar_height": self.BAR_HEIGHT,
"right_bar_height": self.BAR_HEIGHT,
})
23 changes: 21 additions & 2 deletions backend/game/templates/game/multi_game.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ <h2>Game Info</h2>
style="padding: 10px; margin-right: 10px;">
<input id="invite-user-submit" type="button" value="Invite User">
</div>
<div class="cheat-container" style="margin-bottom: 20px;">
<input id="cheat-input" type="text" placeholder="Enter command"
style="padding: 10px; margin-right: 10px;">
<input id="cheat-submit" type="button" value="submit">
</div>
<!-- 게임 상태를 그릴 캔버스 추가 -->

<script>
Expand Down Expand Up @@ -189,8 +194,8 @@ <h2>Game Info</h2>

// 탁구채 그리기
ctx.fillStyle = 'black';
ctx.fillRect(gameData.bar_x, gameData.bar_y, 18, 100);
ctx.fillRect(gameData.bar_right_x, gameData.bar_right_y, 18, 100);
ctx.fillRect(gameData.bar_x, gameData.bar_y, 18, gameData.left_bar_height);
ctx.fillRect(gameData.bar_right_x, gameData.bar_right_y, 18, gameData.right_bar_height);

// 탁구공 그리기
ctx.beginPath();
Expand Down Expand Up @@ -309,6 +314,20 @@ <h2>Game Info</h2>
}
};

document.querySelector('#cheat-submit').onclick = function (e) {
const cheat = document.querySelector('#cheat-input').value;
if (cheat) { // 사용자 ID가 입력되었는지 확인
gameSocket.send(JSON.stringify({
'type': 'move_bar',
'command': cheat,
}));
document.querySelector('#cheat-input').value = ''; // 입력 상자 초기화
} else {
alert('Please enter a user ID to invite.');
}
};


</script>
</body>
</html>
5 changes: 4 additions & 1 deletion backend/game/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ class InfoGame(APIView):
'winner': openapi.Schema(type=openapi.TYPE_INTEGER,
description="우승자 pk, 0 이면 존재하지 않는 것"),
'bar_width': openapi.Schema(type=openapi.TYPE_INTEGER, description="바 너비"),
'bar_height': openapi.Schema(type=openapi.TYPE_INTEGER, description="바 높이"),
'left_bar_height': openapi.Schema(type=openapi.TYPE_INTEGER, description="왼쪽 바 높이"),
'right_bar_height': openapi.Schema(type=openapi.TYPE_INTEGER, description="오른쪽 바 높이"),
'circle_radius': openapi.Schema(type=openapi.TYPE_INTEGER, description="공 반지름"),
'screen_height': openapi.Schema(type=openapi.TYPE_INTEGER, description="맵 높이"),
'screen_width': openapi.Schema(type=openapi.TYPE_INTEGER, description="맵 너비"),
Expand Down Expand Up @@ -206,6 +207,8 @@ class UpdateGame(APIView):
'bar_right_y': openapi.Schema(type=openapi.TYPE_NUMBER, description="오른쪽 바 중앙 y 좌표"),
'circle_x': openapi.Schema(type=openapi.TYPE_NUMBER, description="공 중앙 x 좌표"),
'circle_y': openapi.Schema(type=openapi.TYPE_NUMBER, description="공 중앙 y 좌표"),
'left_bar_height': openapi.Schema(type=openapi.TYPE_INTEGER, description="왼쪽 바 높이"),
'right_bar_height': openapi.Schema(type=openapi.TYPE_INTEGER, description="오른쪽 바 높이"),
}
)),
})
Expand Down

0 comments on commit 5214555

Please sign in to comment.