Skip to content

Commit

Permalink
Optimize country map loading speed in parties
Browse files Browse the repository at this point in the history
  • Loading branch information
codergautam committed Jan 11, 2025
1 parent 6b5a600 commit a0908be
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
5 changes: 3 additions & 2 deletions components/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -1047,11 +1047,11 @@ setShowCountryButtons(false)
}, [screen, inCrazyGames])

useEffect(() => {
if(multiplayerState?.connected && inCrazyGames) {
if(multiplayerState?.connected) {

// check if joined via invite link
try {
let code = window.CrazyGames.SDK.game.getInviteParam("code")
let code = inCrazyGames ? window.CrazyGames.SDK.game.getInviteParam("code") : window.location.search.includes("code=") ? window.location.search.split("code=")[1].split("&")[0] : null;
let instantJoin = window.location.search.includes("instantJoin");


Expand All @@ -1068,6 +1068,7 @@ setShowCountryButtons(false)
setOnboardingCompleted(true)
setOnboarding(null)
setLoading(false)
setOnboardingModalShown(false)
setScreen("home")
if(code) {

Expand Down
2 changes: 1 addition & 1 deletion components/playerList.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export default function PlayerList({ multiplayerState, playAgain, backBtn, start
)}

{(multiplayerState?.gameData?.rounds > (multiplayerState?.gameData?.generated)) &&
<p style={{color: "yellow"}}>{text("generating")} ( {multiplayerState?.gameData?.generated||0} / {multiplayerState?.gameData?.rounds} )</p>}
<p style={{color: "yellow"}}>{text("generating")}</p>}

{ waitingForStart && !host && (multiplayerState?.gameData?.rounds== multiplayerState?.gameData?.generated) && (
<p style={{color: "red"}}>{text("waitingForHostToStart")}...</p>
Expand Down
53 changes: 33 additions & 20 deletions ws/classes/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,44 +474,57 @@ export default class Game {

} else {

if(this.location === "all") {

for (let i = 0; i < this.rounds; i++) {
let loc;
if(this.location === "all") {
// get n random from the list
console.log('All locations', allLocations.length);
loc = allLocations[Math.floor(Math.random() * allLocations.length)];
this.maxDist = 20000;
this.extent = null;
} else if(countries.includes(this.location)) {
try {
let data = await fetch('http://localhost:3001/countryLocations/'+this.location, {
headers: {
'Content-Type': 'application/json'
},
});
data = await data.json();
if(data.ready && data.locations) {
loc = data.locations[Math.floor(Math.random() * data.locations.length)];
} else {
loc = await findLatLongRandom({ location: this.location }, getRandomPointInCountry, lookup);

}
} catch (e) {
console.error('Error getting country locations', e);
}

}
this.locations.push(loc);

this.sendAllPlayers({
type: 'generating',
generated: this.locations.length,
})
}
} else {

try {
let loc;
this.maxDist = countryMaxDists[this.location] || 20000;
this.extent = officialCountryMaps.find((c) => c.countryCode === this.location)?.extent || null;
// console.log('Extent', this.extent, this.location);
console.time('Country locations '+this.location);
let data = await fetch('http://localhost:3001/countryLocations/'+this.location, {
headers: {
'Content-Type': 'application/json'
},
});
data = await data.json();
console.timeEnd('Country locations '+this.location);
for(let i = 0; i < this.rounds; i++) {
if(data.ready && data.locations) {
loc = data.locations[Math.floor(Math.random() * data.locations.length)];
data.locations = data.locations.filter((l) => l !== loc);
} else {
loc = await findLatLongRandom({ location: this.location }, getRandomPointInCountry, lookup);

}

this.locations.push(loc);
this.sendAllPlayers({
type: 'generating',
generated: this.locations.length,
})
}
} catch (e) {
console.error('Error getting country locations', e);
}

}

this.sendAllPlayers({
type: 'maxDist',
Expand Down

0 comments on commit a0908be

Please sign in to comment.