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

feat: guest mode #31

Merged
merged 1 commit into from
Aug 10, 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ All of the command line arguments are optional.
- `--internal_backend` — Set the internal backend url when running the Screeps server in a local container. This will convert requests to a localhost backend to use the container name where the Screeps server is running.
- `--server_list` — Path to a custom server list json config file.
- `--beautify` — Formats .js files loaded in the client for debugging.
- `--guest` — Enable guest mode for xxscreeps.
- `--debug` — Display verbose errors for development.

## Examples
Expand Down Expand Up @@ -121,6 +122,6 @@ npx screepers-steamless-client --server_list ./custom_server_list.json

## Tips

This client uses "guest mode" by default in [xxscreeps](https://github.com/laverdet/xxscreeps/), providing a read-only view of the server when not signed in. To sign in with your Steam account, select "Sign Out" first, then click the Steam icon to sign in and play as normal.
This client has an optional "guest mode" for [xxscreeps](https://github.com/laverdet/xxscreeps/) that can be enabled with `--guest` and provides a read-only view of the server when not signed in. To sign in with your Steam account, select "Sign Out" first, then click the Steam icon to sign in and play as normal.

![Safari Example](./docs/safari.png)
6 changes: 3 additions & 3 deletions scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ npx semantic-release
# Get the new version from package.json
newVersion=$(node -p "require('./package.json').version")

# Revert package.json to its state in the Git repository
git checkout HEAD -- package.json
# Revert package.json and package-lock.json to their state in the Git repository
git checkout HEAD -- package.json package-lock.json

# If the version has changed, update the version in package.json
if [[ "$currentVersion" != "$newVersion" ]]; then
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
npm version --no-git-tag-version $newVersion
git add package.json
git add package.json package-lock.json
git commit -m "chore: release $newVersion [skip ci]"
git push
fi
10 changes: 9 additions & 1 deletion src/clientApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ const argv = (() => {
type: 'str',
help: 'Path to a custom server list json config file.',
});
parser.add_argument('--guest', {
action: 'store_true',
default: false,
help: 'Enable guest mode for xxscreeps.',
});
parser.add_argument('--beautify', {
action: 'store_true',
default: false,
Expand Down Expand Up @@ -214,7 +219,7 @@ koa.use(async (context, next) => {
const header = '<title>Screeps</title>';
const replaceHeader = [
header,
generateScriptTag(clientAuth, { backend: info.backend }),
generateScriptTag(clientAuth, { backend: info.backend, guest: argv.guest }),
generateScriptTag(removeDecorations, { backend: info.backend }),
generateScriptTag(customMenuLinks, { backend: info.backend, seasonLink, ptrLink, changeServerLink }),
].join('\n');
Expand Down Expand Up @@ -266,6 +271,9 @@ koa.use(async (context, next) => {
const ptrValue = prefix === '/ptr' ? 'true' : 'false';
src = src.replace(/(PTR: )[^,]*/, `$1${ptrValue}`);

const debugValue = argv.debug ? 'true' : 'false';
src = src.replace(/(DEBUG: )[^,]*/, `$1${debugValue}`);

return src;
} else if (context.path.endsWith('.js')) {
let src = await file.async('text');
Expand Down
45 changes: 24 additions & 21 deletions src/inject/clientAuth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This function is injected into the client to manage settings in local storage.
*/
export function clientAuth(backend: string) {
export function clientAuth(backend: string, guest: boolean) {
// Clear the local storage if the backend domain has changed
if (localStorage.backendDomain && localStorage.backendDomain !== backend) {
const keysToPreserve = ['game.room.displayOptions', 'game.world-map.displayOptions2', 'game.editor.hidden'];
Expand All @@ -15,32 +15,35 @@ export function clientAuth(backend: string) {
// Set the backend domain
localStorage.backendDomain = backend;

// Set the auth token to guest if it's not set or if it's been more than an hour since the last token was set
if (
(localStorage.auth === 'null' && localStorage.prevAuth === 'null') ||
60 * 60 * 1000 < Date.now() - localStorage.lastToken ||
(localStorage.prevAuth !== '"guest"' && (localStorage.auth === 'null' || !localStorage.auth))
) {
localStorage.auth = '"guest"';
}

// Set the client to skip tutorials and tip of the day
localStorage.tutorialVisited = 'true';
localStorage.placeSpawnTutorialAsked = '1';
localStorage.tipTipOfTheDay = '-1';

// Set the last token to the current time
localStorage.prevAuth = localStorage.auth;
localStorage.lastToken = Date.now();

// Update the last token if the auth token changes
let auth = localStorage.auth;
setInterval(() => {
if (auth !== localStorage.auth) {
auth = localStorage.auth;
localStorage.lastToken = Date.now();
// Guest mode for xxscreeps
if (guest) {
// Set the auth token to guest if it's not set or if it's been more than an hour since the last token was set
if (
(localStorage.auth === 'null' && localStorage.prevAuth === 'null') ||
60 * 60 * 1000 < Date.now() - localStorage.lastToken ||
(localStorage.prevAuth !== '"guest"' && (localStorage.auth === 'null' || !localStorage.auth))
) {
localStorage.auth = '"guest"';
}
}, 1000);

// Set the last token to the current time
localStorage.prevAuth = localStorage.auth;
localStorage.lastToken = Date.now();

// Update the last token if the auth token changes
let auth = localStorage.auth;
setInterval(() => {
if (auth !== localStorage.auth) {
auth = localStorage.auth;
localStorage.lastToken = Date.now();
}
}, 1000);
}

// The client will just fill this up with data until the application breaks.
if (localStorage['users.code.activeWorld']?.length > 1024 * 1024) {
Expand Down
Loading