Skip to content

Commit

Permalink
feat: add support for switch game function
Browse files Browse the repository at this point in the history
  • Loading branch information
pranshu6 committed Jun 21, 2024
1 parent 877f316 commit 628ce1f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
1 change: 1 addition & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ var Server = TaroClass.extend({
taro.network.define('gameOver', self._onGameOver);
taro.network.define('ping', self._onPing);
taro.network.define('sendPlayerToMap', self._onSomeBullshit);
taro.network.define('sendPlayerToGame', self._onSomeBullshit);

taro.network.define('playerUnitMoved', self._onPlayerUnitMoved);
taro.network.define('playerKeyDown', self._onPlayerKeyDown);
Expand Down
1 change: 1 addition & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ const Client = TaroEventingClass.extend({
defineNetworkEvents: function () {
taro.network.define('ping', this._onPing);
taro.network.define('sendPlayerToMap', this._onSendPlayerToMap);
taro.network.define('sendPlayerToGame', this._onSendPlayerToGame);

taro.network.define('makePlayerSelectUnit', this._onMakePlayerSelectUnit);
taro.network.define('makePlayerCameraTrackUnit', this._onMakePlayerCameraTrackUnit);
Expand Down
34 changes: 25 additions & 9 deletions src/gameClasses/ClientNetworkEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,13 @@ var ClientNetworkEvents = {
},

// when client receives a ping response back from the server
_onPing: function(data) {
_onPing: function (data) {
const self = this;
const now = taro._currentTime;
const latency = now - data.sentAt;

// console.log("onPing", taro._currentTime, data.sentAt, latency);

// start reconciliation based on discrepancy between
// where my unit when ping was sent and where unit is when ping is received
if (taro.client.selectedUnit?.posHistory &&
Expand Down Expand Up @@ -444,7 +444,7 @@ var ClientNetworkEvents = {
}
}
}

taro.pingElement = taro.pingElement || document.getElementById('updateping');
taro.pingElement.innerHTML = Math.floor(latency);
taro.pingLatency = taro.pingLatency || [];
Expand Down Expand Up @@ -849,20 +849,36 @@ var ClientNetworkEvents = {
console.warn(data);
},

_handlePokiSwitch: function (data) {
window.sessionStorage.setItem('redirectToGameData', JSON.stringify(data));
if (window.STATIC_EXPORT_ENABLED) {
window.PokiSDK?.gameplayStop();
}
window.location.reload();
},

_onSendPlayerToMap: function (data) {
if (data && data.type == 'sendPlayerToMap') {
if (window.STATIC_EXPORT_ENABLED) {
window.sessionStorage.setItem('redirectToGameData', JSON.stringify(data));
if (window.STATIC_EXPORT_ENABLED) {
window.PokiSDK?.gameplayStop();
}
window.location.reload();
this._handlePokiSwitch(data);
} else {
const mapUrl = `${window.location.origin}/play/${data.gameSlug}?autojoin=true&autoJoinToken=${data.autoJoinToken}${data.serverId ? '&serverId=' + data.serverId : ''}`;
window.location.href = mapUrl;
}
}
},

_onSendPlayerToGame: function (data) {
if (data && data.type == 'sendPlayerToGame') {
if (window.STATIC_EXPORT_ENABLED) {
this._handlePokiSwitch(data);
} else {
const mapUrl = `${window.location.origin}/play/${data.gameSlug}?autojoin=true&${data.serverId ? '&serverId=' + data.serverId : ''}`;
window.location.href = mapUrl;
}
}
},

};

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
Expand Down
23 changes: 23 additions & 0 deletions src/gameClasses/components/script/ActionComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,29 @@ var ActionComponent = TaroEntity.extend({

break;

case 'sendPlayerToGame':
if (taro.isServer) {
var player = self._script.param.getValue(action.player, vars);
var gameId = self._script.param.getValue(action.gameId, vars);

if (player && player._stats && player._stats.clientId) {
taro.workerComponent.sendPlayerToGame(gameId).then((res) => {
if (res && res.gameSlug) {
// ask client to reload game
taro.network.send(
'sendPlayerToGame',
{
type: 'sendPlayerToGame',
gameSlug: res.gameSlug,
gameId: res.gameId,
},
player._stats.clientId
);
}
});
}
}

case 'sendPlayerGroupToMap':
if (taro.isServer) {
var gameId = self._script.param.getValue(action.gameId, vars);
Expand Down

0 comments on commit 628ce1f

Please sign in to comment.