Created
November 23, 2008 01:51
-
-
Save Oshuma/28010 to your computer and use it in GitHub Desktop.
Ubiquity command set for WeeWar (http://weewar.com/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Oshuma = { | |
| homepage: "http://corrupt.save-state.net", | |
| author: { name: "Dale Campbell", email: "[email protected]"}, | |
| license: "WTFPL" | |
| }; | |
| WeeWar = { | |
| _baseUrl: "http://weewar.com/api1", | |
| _siteIcon: "http://weewar.com/favicon.ico", | |
| _userKeyName: "weewar.username", | |
| _apiKeyName: "weewar.apiKey", | |
| _notSetupError: "You <span style='color: red'>must</span> run '<em>weewar-setup</em>' before using this command.", | |
| setup: function(username, apiKey) { | |
| Application.prefs.setValue(this._userKeyName, username); | |
| Application.prefs.setValue(this._apiKeyName, apiKey); | |
| }, | |
| userDetails: function() { | |
| var username = Application.prefs.getValue(this._userKeyName, undefined); | |
| var apiKey = Application.prefs.getValue(this._apiKeyName, undefined); | |
| return [username, apiKey]; | |
| } | |
| }; | |
| // Saves your WeeWar username and API token. | |
| // Usage: weewar-setup Username api abcdef12345 | |
| CmdUtils.CreateCommand({ | |
| name: "weewar-setup", | |
| icon: WeeWar._siteIcon, | |
| homepage: Oshuma.homepage, | |
| author: Oshuma.author, | |
| license: Oshuma.license, | |
| description: "Saves your WeeWar username and API key. This command <em>must</em> be run before you can interact with the WeeWar site. You can find your API key here: <a href='http://weewar.com/apiToken'>http://weewar.com/apiToken</a>.", | |
| help: "Usage: weewar-setup <username> <strong><em>api</em></strong> <api_key>", | |
| takes: {"username": noun_arb_text}, | |
| modifiers: {"api": noun_arb_text}, | |
| preview: function( pblock, username, mods ) { | |
| var username = username.text; | |
| var apiKey = mods.api.text; | |
| if (username.length == 0 || apiKey.length == 0) { | |
| pblock.innerHTML = this.help; | |
| } else { | |
| var template = "Sets your username as '<em>${username}</em>' and your API key as '<em>${apiKey}</em>'."; | |
| pblock.innerHTML = CmdUtils.renderTemplate(template, {"username": username, "apiKey": apiKey}); | |
| } | |
| }, | |
| execute: function( username, mods ) { | |
| var username = username.text; | |
| var apiKey = mods.api.text; | |
| WeeWar.setup(username, apiKey); | |
| displayMessage("Your details were saved. You may now use the other WeeWar commands."); | |
| } | |
| }); | |
| // Shows a list of your WeeWar games. | |
| CmdUtils.CreateCommand({ | |
| name: "weewar", | |
| icon: WeeWar._siteIcon, | |
| homepage: Oshuma.homepage, | |
| author: Oshuma.author, | |
| license: Oshuma.license, | |
| description: "Shows a list of your WeeWar games. You <em>must</em> run 'weewar-setup' before using this command.", | |
| help: "Shows a list of your WeeWar games.", | |
| preview: function( pblock ) { | |
| pblock.innerHTML = "Getting game information..."; | |
| var url = WeeWar._baseUrl + "/headquarters"; | |
| var username = WeeWar.userDetails()[0]; | |
| var password = WeeWar.userDetails()[1]; // API key passed as basic auth password. | |
| if (username == undefined || password == undefined || | |
| username == '' || password == '') { | |
| pblock.innerHTML = WeeWar._notSetupError; | |
| return; | |
| } | |
| // TODO: This HTML and CSS bullshit should be in a template... | |
| var parseGames = function(xml) { | |
| pblock.innerHTML = "<div style='font: 1.5em, sans-serif;'>Your Games</div>"; | |
| pblock.innerHTML += "<div style='font: 0.5em; color: yellow;'><em>(Click a game name to open it)</em></div>"; | |
| jQuery(xml).find("game").each(function() { | |
| var gameId = jQuery(this).find("id").text(); | |
| var gameName = jQuery(this).find("name").text(); | |
| var gameUrl = jQuery(this).find("url").text(); | |
| var yourTurn = jQuery(this).attr("inNeedOfAttention"); | |
| pblock.innerHTML += "<div class='game' id='" + gameId + "'>"; | |
| pblock.innerHTML += "<a href='" + gameUrl + "'>" + gameName + "</a>"; | |
| if (yourTurn) | |
| pblock.innerHTML += " <em style='color: green;'>(Your Turn)</em> "; | |
| pblock.innerHTML += "</div>"; | |
| }); | |
| }; | |
| var handleError = function(e) { | |
| CmdUtils.log("Error: " + e); | |
| }; | |
| var options = {url: url, username: username, password: password, success: parseGames, error: handleError}; | |
| jQuery.ajax(options); | |
| }, | |
| execute: function() { | |
| displayMessage("Click a game name to open it."); | |
| }, | |
| }); | |
| // Get information on a specific WeeWar player. | |
| CmdUtils.CreateCommand({ | |
| name: "weewar-player", | |
| icon: WeeWar._siteIcon, | |
| homepage: Oshuma.homepage, | |
| author: Oshuma.author, | |
| license: Oshuma.license, | |
| description: "Get information on a specific WeeWar player.", | |
| help: "Usage: weewar-player <username>", | |
| takes: {"username": noun_arb_text}, | |
| userUrl: WeeWar._baseUrl + "/user/", | |
| previewDelay: 500, // Wait 500ms before calling the preview method; lightens the network load a bit. | |
| preview: function( pblock, username ) { | |
| var username = username.text; | |
| var url = this.userUrl + username; | |
| // TODO: This is fucking disgusting and will be changed. | |
| var parseProfile = function(xml) { | |
| pblock.innerHTML = ''; // Clear it when starting out. | |
| var profileImage = jQuery(xml).find("profileImage").text(); | |
| if (profileImage) { | |
| pblock.innerHTML += "<div id='profileImage'><img src='" + profileImage + "' /></div>"; | |
| } | |
| var name = jQuery(xml).find("user").attr("name"); | |
| pblock.innerHTML += "<div style='font-size: 1.5em;'>" + name + "</div>"; | |
| pblock.innerHTML += "<div id='rating'>Rating: " + jQuery(xml).find("points").text() + "</div>"; | |
| var separator = " / "; | |
| pblock.innerHTML += "<div id='playerStats'>"; | |
| pblock.innerHTML += "Victories: " + jQuery(xml).find("victories").text(); | |
| pblock.innerHTML += separator; | |
| pblock.innerHTML += "Losses: " + jQuery(xml).find("losses").text(); | |
| pblock.innerHTML += separator; | |
| pblock.innerHTML += "Draws: " + jQuery(xml).find("draws").text(); | |
| pblock.innerHTML += "</div>"; | |
| pblock.innerHTML += "<div id='profileText'>"; | |
| pblock.innerHTML += jQuery(xml).find("profileText").text(); | |
| pblock.innerHTML += "</div>"; | |
| pblock.innerHTML += "<div id='games' style='border-top: 1px solid gray; margin: 0.5em;'>"; | |
| pblock.innerHTML += "<div><span style='font-size: 1.25em;'>Games:</span> (click to open)</div>"; | |
| jQuery(xml).find("game").each(function() { | |
| var gameLink = "http://weewar.com/game/" + jQuery(this).text(); | |
| var gameName = jQuery(this).attr("name"); | |
| pblock.innerHTML += "<a href='" + gameLink + "'>" + gameName + "</a><br />"; | |
| }); | |
| pblock.innerHTML += "</div>"; | |
| }; | |
| var handleError = function(e) { | |
| if (e.status == '404') | |
| pblock.innerHTML = "No user by that name was found."; | |
| }; | |
| if (username.length < 2) { | |
| pblock.innerHTML = this.help; | |
| } else { | |
| var options = {url: url, success: parseProfile, error: handleError}; | |
| jQuery.ajax(options); | |
| } | |
| }, | |
| execute: function(username) { | |
| var url = "http://weewar.com/user/" + username.text; | |
| Utils.openUrlInBrowser(url); | |
| } | |
| }); | |
| // Displays a list of open games. | |
| // TODO: This could use a suggestion feature for a particular game ID. | |
| CmdUtils.CreateCommand({ | |
| name: "weewar-games", | |
| icon: WeeWar._siteIcon, | |
| homepage: Oshuma.homepage, | |
| author: Oshuma.author, | |
| license: Oshuma.license, | |
| description: "Shows a list of open games.", | |
| preview: function( pblock ) { | |
| var updatePreview = function(xml) { | |
| var gameName = jQuery(xml).find("name").text(); | |
| var gameUrl = jQuery(xml).find("url").text(); | |
| pblock.innerHTML += "<div style='margin-bottom: 1em;'>"; | |
| pblock.innerHTML += "<a style='color: yellow;' href='" + gameUrl + "'>" + gameName + "</a><br />"; | |
| pblock.innerHTML += "<em>Players:</em> "; | |
| jQuery(xml).find("player").each(function() { | |
| var username = jQuery(this).text(); | |
| pblock.innerHTML += "<a href='http://weewar.com/user/" + username + "'>" + username + "</a>"; | |
| }); | |
| pblock.innerHTML += "</div>"; | |
| }; | |
| var listGames = function(xml) { | |
| jQuery(xml).find("game").each(function() { | |
| var gameId = jQuery(this).attr("id"); | |
| var url = WeeWar._baseUrl + "/game/" + gameId; | |
| jQuery.ajax({url: url, success: updatePreview}); | |
| }) | |
| }; | |
| var handleError = function(e) { | |
| CmdUtils.log("Error: " + e); | |
| }; | |
| pblock.innerHTML = "<div><span style='font-size: 1.5em;'>Open Games</span> (click game name to open)</div>"; | |
| var gamesUrl = WeeWar._baseUrl + "/games/open"; | |
| jQuery.ajax({url: gamesUrl, success: listGames, error: handleError}); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment