Path: blob/main/projects/circlo/html5game_a5/vph_kongAPIextension.js
4626 views
/* version 1.112KONGREGATE (C) API Extension for GameMaker:Studio34-- Written by Dexter Friedman (July 15, 2012)5-- Feel free to redistribute this extension, but don't6violate Kongregate's terms of service, if they have any.7I just made a Javascript wrapper. Please don't sue me!8910------ Version History -------11------ Version 1.11213(March 6, 2013)1415> Added kongGetUserID()16> Made error console output print to console.error1718------ Version 1.0 >1920(July 15, 2012)2122> Created most of the wrapper functions for the Kongregate API23> Ensured all function calls are surrounded by try-catch so that24users can test locally without trouble.2526*/2728var version = "1.1";2930// Load the API31// Define control functions3233function kongGetAPI() {34// Returns the API object35try {36if (typeof parent.kongregate === "undefined") {37throw "APIUndefinedException";38}39return parent.kongregate;40} catch (e) {41if ((e = "APIUndefinedException")) {42// Log this to the javascript console4344logError();4546//console.error("GameMaker:Studio <KongAPI Extension by Afrodynamics> - Kongregate API not loaded!");47}48return -1;49}50}5152function kongInit() {53// Initialization Function for the whole extension54try {55parent.kongregate.services.connect();56console.log("GameMaker:Studio <KongAPI Extension " + version + " by Afrodynamics> - Kongregate API initialized!");57console.log("GameMaker:Studio <KongAPI Extension " + version + " by Afrodynamics> - Succesfully connected to Kongregate services!");58} catch (e) {59console.error("GameMaker:Studio <KongAPI Extension " + version + " by Afrodynamics> - ERROR! Kongregate API not initialized!");60console.error("GameMaker:Studio <KongAPI Extension " + version + " by Afrodynamics> - You are not playing on kongregate.com!");61return -1;62}63}6465function logError() {66console.error("GameMaker:Studio <KongAPI Extension " + version + " by Afrodynamics> - Kongregate API not loaded!");67console.error("Play the game on Kongregate's website in a preview window to avoid this error.");68}6970/*71* - API - FUNCTIONS -72*73* All functions will return String or Double to GameMaker:Studio74* All functions are within try-catch statements so you can test locally.75* If the game isn't hosted in the kongregate preview window, kongregate76* API calls will throw execeptions. The try-catch prevents this.77*78* If exceptions are caught, functions returning double will return -179* If exceptions are caught, functions returning strings will return ""80* If exceptions are caught for void functions, -1 is returned as well.81*82*/8384// -------------- GENERAL KONGREGATE FUNCTIONS --------------8586function kongSubmitStat(argument0, argument1) {87/*88Submits a stat to the kongregate API, returns true if successful8990arg0 = String (stat name)91arg1 = Number (stat)9293You define Kongregate stats on kongregate's website, and94refer to them by their name strings (which you defined on95their site.)9697*/98try {99kongregate = kongGetAPI();100kongregate.stats.submit(argument0, argument1);101return 1;102} catch (e) {103logError();104return -1;105}106}107108function kongGetUsername() {109// Returns a string of the username, or empty string if there's an exception110111try {112return kongGetAPI().services.getUsername();113} catch (e) {114logError();115return "";116}117}118119function kongGetUserID() {120// Returns a String containing the unique user ID associated with the current player.121// If the player is not signed in or you aren't viewing the game in a Kongregate preview122// window, it'll return ""123124try {125var id = kongGetAPI().services.getUserID();126console.log(id);127128if (id == 0) {129return "";130} else {131return id;132}133} catch (e) {134logError();135return "";136}137}138139function kongIsGuest() {140// returns true if user is guest, false if logged in141// returns -1 if we aren't on kongregate142try {143if (kongGetAPI().services.isGuest()) {144// If we are a Guest, return true to GM145// In JS, there is a boolean object. This is to be precise.146return 1;147} else {148return 0;149}150} catch (e) {151// Remember, if we return -1, that means we aren't on kongregate at all!152logError();153return -1;154}155}156157function kongShowRegistrationBox() {158// Shows a kongregate registration box159160try {161if (kongIsGuest()) {162kongGetAPI().services.showRegistrationBox();163return 1;164} else {165return 0;166}167} catch (e) {168logError();169return -1;170}171}172173// ----------- KONGREGATE CHAT API FUNCTIONS -------------174175function kongChatDisplayMessage(argument0, argument1) {176// Displays chat message177// arg0 = message, arg1 = username178try {179kongGetAPI().chat.displayMessage(argument0, argument1);180} catch (e) {181logError();182return -1;183}184}185186function kongChatClearMessages() {187// Clears messages in Kongregate Chat Window, returns true188try {189kongGetAPI().chat.clearMessages();190return 1;191} catch (e) {192logError();193return -1;194}195}196197function kongChatShowTab(argument0, argument1, argument2) {198/*199arg0 - Name of the tab (word in tab itself)200arg1 - Description of the tab201arg2 - Relative size of the canvas, 0 being the smallest, 1 being the largest (default 0.5)202*/203try {204kongGetAPI().chat.showTab(argument0, argument1, { size: argument2 });205return 1;206} catch (e) {207logError();208return -1;209}210}211212function kongChatCloseTab() {213// Closes the tab opened by kongChatShowTab()214try {215kongGetAPI().chat.closeTab();216return 1;217} catch (e) {218logError();219return -1;220}221}222223// --------------- OTHER -----------------224225