Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep Alive & Master fallback #110

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions backend/src/main/java/fr/zelytra/session/SessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,33 @@ public Fleet joinSession(String sessionId, Player player) {
* @param player The player to remove from their session.
*/
public void leaveSession(Player player) {
for (Fleet fleet : sessions.values()) {

SotServer sotServer = getSotServerFromPlayer(player);
if (sotServer != null) {
playerLeaveSotServer(player, sotServer);
}
Fleet fleet = getFleetByPlayerName(player.getUsername());

fleet.getPlayers().remove(player);
fleet.getServers().forEach((key, value) -> value.getConnectedPlayers().remove(player));
SessionSocket.broadcastDataToSession(fleet.getSessionId(), MessageType.UPDATE, fleet);
Log.info("[" + fleet.getSessionId() + "] " + player.getUsername() + " Leave the session !");
SotServer sotServer = getSotServerFromPlayer(player);
if (sotServer != null) {
playerLeaveSotServer(player, sotServer);
}

// Clean empty session
if (fleet.getPlayers().isEmpty()) {
sessions.remove(fleet.getSessionId());
Log.info("[" + fleet.getSessionId() + "] Has been disbanded");
}
fleet.getPlayers().remove(player);
fleet.getServers().forEach((key, value) -> value.getConnectedPlayers().remove(player));

// Check if player was master, then give another user the master role
if (player.isMaster() && !fleet.getPlayers().isEmpty()) {
Player newMaster = fleet.getPlayers().get(0);
newMaster.setMaster(true);
Log.info("[" + fleet.getSessionId() + "] Master as left, giving the role to " + newMaster.getUsername());
}

SessionSocket.broadcastDataToSession(fleet.getSessionId(), MessageType.UPDATE, fleet);
Log.info("[" + fleet.getSessionId() + "] " + player.getUsername() + " Leave the session !");

// Clean empty session
if (fleet.getPlayers().isEmpty()) {
sessions.remove(fleet.getSessionId());
Log.info("[" + fleet.getSessionId() + "] Has been disbanded");
}

}

/**
Expand Down
6 changes: 2 additions & 4 deletions backend/src/main/java/fr/zelytra/session/SessionSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void onMessage(String message, Session session, @PathParam("sessionId") S
}
case START_COUNTDOWN -> handleStartCountdown(session);
case CLEAR_STATUS -> handleClearStatus(session);
case KEEP_ALIVE -> {}
case JOIN_SERVER -> {
SotServer sotServer = objectMapper.convertValue(socketMessage.data(), SotServer.class);
handleJoinServerMessage(session, sotServer);
Expand Down Expand Up @@ -146,8 +147,7 @@ private void handleConnectMessage(Player player, Session session, String session
}
return;
}

session.setMaxIdleTimeout(3600000); // 1h of timeout
session.setMaxIdleTimeout(30000); // 1h of timeout

SessionManager manager = SessionManager.getInstance();
player.setSocket(session);
Expand All @@ -168,8 +168,6 @@ private void handleConnectMessage(Player player, Session session, String session
broadcastDataToSession(sessionId, MessageType.UPDATE, fleet);
}
}


}

// Extracted method to handle LEAVE messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public enum MessageType {
LEAVE_SERVER,
CLEAR_STATUS,
OUTDATED_CLIENT,
SESSION_NOT_FOUND
SESSION_NOT_FOUND,
KEEP_ALIVE,
}
1 change: 0 additions & 1 deletion webapp/src/components/Fleet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const gameStatusRefresh: number = setInterval(() => {
})
}, 400);


onUnmounted(() => {
if (UserStore.player.fleet) {
UserStore.player.fleet.leaveSession();
Expand Down
24 changes: 17 additions & 7 deletions webapp/src/components/fleet/FleetLobby.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<img src="@/assets/icons/clipboard.svg" alt="copy-button"
@click="copyIdToClipboard(session.sessionId.toUpperCase())"/>
<transition>
<p v-if="displayIdCopy">{{ t('session.idCopy')}}</p>
<p v-if="displayIdCopy">{{ t('session.idCopy') }}</p>
</transition>
</div>
</div>
Expand All @@ -29,7 +29,7 @@
</BannerTemplate>
<div class="lobby-content">
<div class="player-table">
<ServerContainer v-if="computedsession.servers.size > 0" v-for="[hash,server] of session.servers.entries()"
<ServerContainer v-if="computedSession.servers.size > 0" v-for="[hash,server] of session.servers.entries()"
:server="hash+' | '+server.location">
<PlayerFleet
v-for="player in server.connectedPlayers.sort((a, b) => {
Expand Down Expand Up @@ -90,7 +90,7 @@
</template>

<script setup lang="ts">
import {computed, PropType, ref} from "vue";
import {computed, onUnmounted, PropType, ref} from "vue";
import {Fleet} from "@/objects/Fleet.ts";
import PlayerFleet from "@/vue/fleet/PlayerFleet.vue";
import {useI18n} from "vue-i18n";
Expand All @@ -108,13 +108,23 @@ const props = defineProps({
},
});

const keepAlive: number = setInterval(() => {
if (props.session) {
props.session.sendKeepAlive()
}
}, 30000)

onUnmounted(() => {
clearInterval(keepAlive)
})

function updateStatus() {
UserStore.player.isReady = !UserStore.player.isReady;
props.session.updateToSession();
}

defineEmits(["update:selected-value"]);
const computedsession = computed({
const computedSession = computed({
get: (): Fleet => props.session,
set: (): void => {
},
Expand All @@ -131,14 +141,14 @@ function startSession() {
function getFilteredPlayerList() {
const removedPlayer: string[] = [];
for (const player of props.session!.players) {
computedsession.value.servers.forEach((value, _key) => {
computedSession.value.servers.forEach((value, _key) => {
if (value.connectedPlayers.filter(x => x.username == player.username).length > 0) {
removedPlayer.push(player.username)
return;
}
})
}
return computedsession.value.players.filter(x => !removedPlayer.includes(x.username)).sort((a, b) => {
return computedSession.value.players.filter(x => !removedPlayer.includes(x.username)).sort((a, b) => {
return a.isMaster === b.isMaster ? 0 : a.isMaster ? -1 : 1;
})
}
Expand Down Expand Up @@ -181,7 +191,7 @@ function copyIdToClipboard(id: string) {
cursor: pointer;
}

p{
p {
font-family: BrushTip, sans-serif;
font-size: 20px;
font-weight: 400;
Expand Down
9 changes: 9 additions & 0 deletions webapp/src/objects/Fleet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ export class Fleet {
return this.players.filter((player) => player.isReady);
}

sendKeepAlive(){
if (!this.socket) return;
const message: WebSocketMessage = {
data: null,
messageType: WebSocketMessageType.KEEP_ALIVE,
};
this.socket.send(JSON.stringify(message));
}

public static getFormatedStatus(player: Player) {
return player.status.toString().toLowerCase().replace("_", "-");
}
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/objects/WebSocet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ export enum WebSocketMessageType {
JOIN_SERVER = "JOIN_SERVER",
LEAVE_SERVER = "LEAVE_SERVER",
OUTDATED_CLIENT = "OUTDATED_CLIENT",
SESSION_NOT_FOUND = "SESSION_NOT_FOUND"
SESSION_NOT_FOUND = "SESSION_NOT_FOUND",
KEEP_ALIVE = "KEEP_ALIVE"
}
1 change: 0 additions & 1 deletion webapp/src/objects/stores/UserStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const UserStore = reactive({

if (!this.player.clientVersion) {
this.player.clientVersion = import.meta.env.VITE_VERSION;
console.log(this.player.clientVersion)
}

//@ts-ignore I18N typescript implementation
Expand Down
Loading