Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkLightTuna committed Dec 12, 2023
1 parent f10b007 commit db2e9f0
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export const ORONDER_CONFIGURATION_FORM = "oronder_options"
export const VALID_CONFIG = "valid_config"
export const ID_MAP = "id_map"

export const ACTORS="actors"
export const ACTORS = "actors"
export const ORONDER_BASE_URL = window.location.host === 'localhost:65434' ? 'http://localhost:65435' : 'https://api.oronder.com'
// export const ORONDER_BASE_URL = 'https://api.oronder.com'
7 changes: 0 additions & 7 deletions src/module.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ Hooks.once("ready", async () => {
Logger.log(`The GM client calculated: ${result}`);
});


const actor_to_discord_ids = actor =>
Object.entries(actor.ownership)
.filter(([owner_id, perm_lvl]) => perm_lvl === 3)
.map(([owner_id, _]) => game.settings.get(MODULE_ID, ID_MAP)[owner_id])
.filter(discord_id => discord_id)

Hooks.on("createActor", async (actor, data, options, userId) => {
await sync_actor(actor)
})
Expand Down
39 changes: 15 additions & 24 deletions src/settings-form-application.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,7 @@ export class OronderSettingsFormApplication extends FormApplication {
const requestOptions = this._requestOptions(this.object.guild_id, this.object.auth)
let valid_config = false
await fetch(`${ORONDER_BASE_URL}/validate_discord_ids?${queryParams}`, requestOptions)
.then(response => {
if (!response.ok) {
if (response.status === 401) {
Logger.logError(game.i18n.localize("oronder.Invalid-Auth"));
} else if (response.status === 400) {
Logger.logError(game.i18n.localize("oronder.Server-Id-NaN"));
} else {
Logger.logError(game.i18n.localize("oronder.Unexpected-Error"));
}
}
return response.json()
})
.then(self._handle_json_response)
.then(result => {
const invalid_player_names = result.map(invalid_discord_id => {
return this.object.players.find(p => p.discord_id === invalid_discord_id).foundry_name
Expand Down Expand Up @@ -179,18 +168,7 @@ export class OronderSettingsFormApplication extends FormApplication {
const requestOptions = this._requestOptions(this.object.guild_id, this.object.auth)

await fetch(`${ORONDER_BASE_URL}/discord_id?${queryParams}`, requestOptions)
.then(response => {
if (!response.ok) {
if (response.status === 401) {
Logger.logError(game.i18n.localize("oronder.Invalid-Auth"))
} else if (response.status === 400) {
Logger.logError(game.i18n.localize("oronder.Server-Id-NaN"))
} else {
Logger.logError(game.i18n.localize("oronder.Unexpected-Error"))
}
}
return response.json()
})
.then(self._handle_json_response)
.then(async result => {
for (const [foundry_name, discord_user_id] of Object.entries(result)) {
this.object.players.find(p => p.foundry_name === foundry_name).discord_id = discord_user_id
Expand All @@ -206,4 +184,17 @@ export class OronderSettingsFormApplication extends FormApplication {
this.object.fetch_sync_disabled = false
this.render()
}

_handle_json_response(response) {
if (!response.ok) {
if (response.status === 401) {
Logger.logError(game.i18n.localize("oronder.Invalid-Auth"));
} else if (response.status === 400) {
Logger.logError(game.i18n.localize("oronder.Server-Id-NaN"));
} else {
Logger.logError(game.i18n.localize("oronder.Unexpected-Error"));
}
}
return response.json()
}
}
2 changes: 1 addition & 1 deletion src/settings.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ACTORS, AUTH, GUILD_ID, ID_MAP, MODULE_ID, ORONDER_CONFIGURATION_FORM, VALID_CONFIG} from "./constants.mjs";
import {AUTH, GUILD_ID, ID_MAP, MODULE_ID, ORONDER_CONFIGURATION_FORM, VALID_CONFIG} from "./constants.mjs";
import {Logger} from "./util.mjs";
import {OronderSettingsFormApplication} from "./settings-form-application.mjs";

Expand Down
6 changes: 1 addition & 5 deletions src/sync.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,7 @@ export async function full_sync() {
}

export async function sync_actor(actor) {
if (actor.type !== "character")
return Promise.resolve()

let discord_ids = actor_to_discord_ids(actor)
if (!discord_ids.length)
if (actor.type !== "character" || !actor_to_discord_ids(actor).length)
return Promise.resolve()

const old_hash = localStorage.getItem(`${ACTORS}.${actor.id}`)
Expand Down
109 changes: 54 additions & 55 deletions templates/settings-form-application.hbs
Original file line number Diff line number Diff line change
@@ -1,64 +1,63 @@
<form autocomplete="off">
<div>
{{#unless valid_config}}
<h2>Installation Instructions</h2>
<ol>
<li>Invite the <a
href="https://discord.com/api/oauth2/authorize?client_id=1064553830810923048&permissions=18547851095248&scope=bot">
{{localize "oronder.Discord-Bot-Link-Title"}}</a> to your Discord.
</li>
<li>Subscribe to the <a href="https://discord.gg/27npDAXaCA\">
{{localize "oronder.Discord-Server-Link-Title"}}
</a> to gain access to advanced features.
</li>
<li>From your Discord, run "<code>/admin init</code>"</li>
<li>Copy your Discord server id, and the generated token into the fields below.</li>
<li>Clicking "<code>Fetch Discord User Ids</code>" will populate Discord user ids for players whose
Discord name
matches their Foundry
name. For everyone else, you will need to manually add their user id.
</li>
</ol>
<hr>
{{/unless}}
<div>
{{#unless valid_config}}
<h2>Installation Instructions</h2>
<ol>
<li>Invite the <a
href="https://discord.com/api/oauth2/authorize?client_id=1064553830810923048&permissions=18547851095248&scope=bot">
{{localize "oronder.Discord-Bot-Link-Title"}}</a> to your Discord.
</li>
<li>Subscribe to the <a href="https://discord.gg/27npDAXaCA\">
{{localize "oronder.Discord-Server-Link-Title"}}
</a> to gain access to advanced features.
</li>
<li>From your Discord, run "<code>/admin init</code>"</li>
<li>Copy your Discord server id, and the generated token into the fields below.</li>
<li>Clicking "<code>{{localize "oronder.Fetch-Discord-User-Ids"}}</code>" will populate Discord user ids for
players whose Discord name
matches their Foundry name. For everyone else, you will need to manually add their user id.
</li>
</ol>
<hr>
{{/unless}}

</div>
<div class="form-group">
<label for="guild_id">{{localize "oronder.Discord-Server-Id"}}</label>
<input type="text" name="guild_id" value="{{guild_id}}" required inputmode="numeric">
</div>
</div>
<div class="form-group">
<label for="guild_id">{{localize "oronder.Discord-Server-Id"}}</label>
<input type="text" name="guild_id" value="{{guild_id}}" required inputmode="numeric">
</div>

<div class="form-group">
<label for="auth">{{localize "oronder.Auth-Token"}}</label>
<input type="password" name="auth" value="{{auth}}" required>
</div>
<div class="form-group">
<label for="auth">{{localize "oronder.Auth-Token"}}</label>
<input type="password" name="auth" value="{{auth}}" required>
</div>

<div class="form-group">
<button type="button" class='control' data-action='fetch' {{#if fetch_disabled}}disabled{{/if}}>
<i class="{{fetch_button_icon}}"></i> {{fetch_button_msg}}
</button>
</div>
<div class="form-group">
<button type="button" class='control' data-action='sync-all' {{#if full_sync_disabled}}disabled{{/if}}>
<i class="{{full_sync_button_icon}}"></i> {{full_sync_button_msg}}
</button>
</div>
<div class="form-group">
<button type="button" class='control' data-action='fetch' {{#if fetch_disabled}}disabled{{/if}}>
<i class="{{fetch_button_icon}}"></i> {{fetch_button_msg}}
</button>
</div>
<div class="form-group">
<button type="button" class='control' data-action='sync-all' {{#if full_sync_disabled}}disabled{{/if}}>
<i class="{{full_sync_button_icon}}"></i> {{full_sync_button_msg}}
</button>
</div>

{{#if players}}
<hr>
<h3 class="border">{{localize "oronder.Discord-Ids"}}</h3>
{{/if}}
{{#if players}}
<hr>
<h3 class="border">{{localize "oronder.Discord-Ids"}}</h3>
{{/if}}

{{#each players}}
<div class="form-group">
<label for="{{this.foundry_id}}">{{this.foundry_name}}</label>
<input type="text" name="{{this.foundry_id}}" inputmode="numeric" value="{{this.discord_id}}">
</div>
{{/each}}
{{#each players}}
<div class="form-group">
<label for="{{this.foundry_id}}">{{this.foundry_name}}</label>
<input type="text" name="{{this.foundry_id}}" inputmode="numeric" value="{{this.discord_id}}">
</div>
{{/each}}

<hr>
<button type="submit">
<i class="far fa-save"></i> {{ localize "Save Changes" }}
</button>
<hr>
<button type="submit">
<i class="far fa-save"></i> {{ localize "Save Changes" }}
</button>

</form>

0 comments on commit db2e9f0

Please sign in to comment.