Skip to content

Commit

Permalink
Merge branch 'develop' into fix/room-leave
Browse files Browse the repository at this point in the history
  • Loading branch information
libknt authored Oct 20, 2024
2 parents d4563c4 + aa34db8 commit 8296702
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
20 changes: 20 additions & 0 deletions pong/chat/room_consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,26 @@ def init(self, event):
)
)

def game_notification(self, event):
users = event["users"]
roomId = event["room_id"]

uus = []
for user in users:
u = User.objects.get_user_nickname(user)
uus.append(u)
for u in uus:
if u == self.user:
self.send(
text_data=json.dumps(
{
"notification": {
"url": f"/pong-game-home?room-id={roomId}&name=guest&number-of-players=4",
}
}
)
)

def disconnect(self, close_code):
if hasattr(self, "room_group_name"):
async_to_sync(self.channel_layer.group_discard)(
Expand Down
3 changes: 3 additions & 0 deletions pong/pong/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def create_superuser(self, name, nickname, email, password, **extra_fields):
def get_user_email(self, email):
return self.get(email=email)

def get_user_nickname(self, nickname):
return self.get(nickname=nickname)


class User(AbstractBaseUser, PermissionsMixin):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Expand Down
34 changes: 33 additions & 1 deletion pong/pong/static/pong/components/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class Chat extends Component {
socket.addEventListener("message", (event) => {
const message = JSON.parse(event.data);

if (message.notification) {
this.pongameNotification(message.notification.url);
}

if (message.rooms) {
this.myRoomsContainer.updateRoomsUI(message.rooms);
}
Expand Down Expand Up @@ -201,6 +205,28 @@ export class Chat extends Component {
this.element.parentElement.removeChild(this.headerComponent.element);
}

pongameNotification(url) {
const modal = document.getElementById("gameModal");
const gameLink = document.getElementById("gameLink");

gameLink.href = window.location.origin + url;
gameLink.textContent = window.location.origin + url;
gameLink.innerHTML = "こちらのリンクをクリック!!";

modal.style.display = "block";

const closeButton = document.getElementsByClassName("close-button")[0];
closeButton.onclick = function () {
modal.style.display = "none";
};

window.onclick = function (event) {
if (event.target === modal) {
modal.style.display = "none";
}
};
}

get html() {
if (
!this.myRoomsContainer ||
Expand All @@ -211,12 +237,18 @@ export class Chat extends Component {
}

return `
<div class="parent-container">
${this.myRoomsContainer.html}
${this.chatContainer.html}
${this.directoryContainer.html}
</div>
<div id="gameModal" class="chat-modal">
<div class="modal-content">
<span class="close-button">&times;</span>
<p>ゲームが始まります!次のリンクからゲームに参加してください</p>
<a id="gameLink" href="" target="_blank"></a>
</div>
</div>
`;
}
}
34 changes: 34 additions & 0 deletions pong/pong/static/pong/styles/chat.styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

/* Chat */
.chat-modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.4);
}

.chat-modal .modal-content {
background-color: #fff;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}

.chat-modal .close-button {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}

.chat-modal .close-button:hover,
.chat-modal .close-button:focus {
color: black;
cursor: pointer;
}

/* Direct Messages */
.direct-message-container {
display: flex;
Expand Down
17 changes: 16 additions & 1 deletion pong/realtime_pong_game/roommanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def add_new_participant(self, new_participant, new_participant_nickname):
self.participants.append(new_participant)
self.participants_connection[new_participant] = True
self.participant_nickname_dict[new_participant] = (
f"{new_participant_nickname} #{len(self.participants)}"
f"{new_participant_nickname}"
)
self.set_participant_state(new_participant, ParticipantState.Not_In_Place)
return True
Expand Down Expand Up @@ -203,6 +203,21 @@ def game_dispatcher(self, dummy_data):
},
)
break

channel_layer = get_channel_layer()
users_list = [
self.participant_nickname_dict[player1],
self.participant_nickname_dict[player2],
]
async_to_sync(channel_layer.group_send)(
"room_notifications",
{
"type": "game_notification",
"room_id": self.room_name,
"users": users_list,
},
)

# get change next game player's state
self.change_participants_state_for_game(player1, player2)
# get tournament list
Expand Down

0 comments on commit 8296702

Please sign in to comment.