Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/circlo/html5game_a5/vph_kongAPIextension.js
4626 views
1
/* version 1.1
2
3
KONGREGATE (C) API Extension for GameMaker:Studio
4
5
-- Written by Dexter Friedman (July 15, 2012)
6
-- Feel free to redistribute this extension, but don't
7
violate Kongregate's terms of service, if they have any.
8
I just made a Javascript wrapper. Please don't sue me!
9
10
11
------ Version History -------
12
------ Version 1.1
13
14
(March 6, 2013)
15
16
> Added kongGetUserID()
17
> Made error console output print to console.error
18
19
------ Version 1.0 >
20
21
(July 15, 2012)
22
23
> Created most of the wrapper functions for the Kongregate API
24
> Ensured all function calls are surrounded by try-catch so that
25
users can test locally without trouble.
26
27
*/
28
29
var version = "1.1";
30
31
// Load the API
32
// Define control functions
33
34
function kongGetAPI() {
35
// Returns the API object
36
try {
37
if (typeof parent.kongregate === "undefined") {
38
throw "APIUndefinedException";
39
}
40
return parent.kongregate;
41
} catch (e) {
42
if ((e = "APIUndefinedException")) {
43
// Log this to the javascript console
44
45
logError();
46
47
//console.error("GameMaker:Studio <KongAPI Extension by Afrodynamics> - Kongregate API not loaded!");
48
}
49
return -1;
50
}
51
}
52
53
function kongInit() {
54
// Initialization Function for the whole extension
55
try {
56
parent.kongregate.services.connect();
57
console.log("GameMaker:Studio <KongAPI Extension " + version + " by Afrodynamics> - Kongregate API initialized!");
58
console.log("GameMaker:Studio <KongAPI Extension " + version + " by Afrodynamics> - Succesfully connected to Kongregate services!");
59
} catch (e) {
60
console.error("GameMaker:Studio <KongAPI Extension " + version + " by Afrodynamics> - ERROR! Kongregate API not initialized!");
61
console.error("GameMaker:Studio <KongAPI Extension " + version + " by Afrodynamics> - You are not playing on kongregate.com!");
62
return -1;
63
}
64
}
65
66
function logError() {
67
console.error("GameMaker:Studio <KongAPI Extension " + version + " by Afrodynamics> - Kongregate API not loaded!");
68
console.error("Play the game on Kongregate's website in a preview window to avoid this error.");
69
}
70
71
/*
72
* - API - FUNCTIONS -
73
*
74
* All functions will return String or Double to GameMaker:Studio
75
* All functions are within try-catch statements so you can test locally.
76
* If the game isn't hosted in the kongregate preview window, kongregate
77
* API calls will throw execeptions. The try-catch prevents this.
78
*
79
* If exceptions are caught, functions returning double will return -1
80
* If exceptions are caught, functions returning strings will return ""
81
* If exceptions are caught for void functions, -1 is returned as well.
82
*
83
*/
84
85
// -------------- GENERAL KONGREGATE FUNCTIONS --------------
86
87
function kongSubmitStat(argument0, argument1) {
88
/*
89
Submits a stat to the kongregate API, returns true if successful
90
91
arg0 = String (stat name)
92
arg1 = Number (stat)
93
94
You define Kongregate stats on kongregate's website, and
95
refer to them by their name strings (which you defined on
96
their site.)
97
98
*/
99
try {
100
kongregate = kongGetAPI();
101
kongregate.stats.submit(argument0, argument1);
102
return 1;
103
} catch (e) {
104
logError();
105
return -1;
106
}
107
}
108
109
function kongGetUsername() {
110
// Returns a string of the username, or empty string if there's an exception
111
112
try {
113
return kongGetAPI().services.getUsername();
114
} catch (e) {
115
logError();
116
return "";
117
}
118
}
119
120
function kongGetUserID() {
121
// Returns a String containing the unique user ID associated with the current player.
122
// If the player is not signed in or you aren't viewing the game in a Kongregate preview
123
// window, it'll return ""
124
125
try {
126
var id = kongGetAPI().services.getUserID();
127
console.log(id);
128
129
if (id == 0) {
130
return "";
131
} else {
132
return id;
133
}
134
} catch (e) {
135
logError();
136
return "";
137
}
138
}
139
140
function kongIsGuest() {
141
// returns true if user is guest, false if logged in
142
// returns -1 if we aren't on kongregate
143
try {
144
if (kongGetAPI().services.isGuest()) {
145
// If we are a Guest, return true to GM
146
// In JS, there is a boolean object. This is to be precise.
147
return 1;
148
} else {
149
return 0;
150
}
151
} catch (e) {
152
// Remember, if we return -1, that means we aren't on kongregate at all!
153
logError();
154
return -1;
155
}
156
}
157
158
function kongShowRegistrationBox() {
159
// Shows a kongregate registration box
160
161
try {
162
if (kongIsGuest()) {
163
kongGetAPI().services.showRegistrationBox();
164
return 1;
165
} else {
166
return 0;
167
}
168
} catch (e) {
169
logError();
170
return -1;
171
}
172
}
173
174
// ----------- KONGREGATE CHAT API FUNCTIONS -------------
175
176
function kongChatDisplayMessage(argument0, argument1) {
177
// Displays chat message
178
// arg0 = message, arg1 = username
179
try {
180
kongGetAPI().chat.displayMessage(argument0, argument1);
181
} catch (e) {
182
logError();
183
return -1;
184
}
185
}
186
187
function kongChatClearMessages() {
188
// Clears messages in Kongregate Chat Window, returns true
189
try {
190
kongGetAPI().chat.clearMessages();
191
return 1;
192
} catch (e) {
193
logError();
194
return -1;
195
}
196
}
197
198
function kongChatShowTab(argument0, argument1, argument2) {
199
/*
200
arg0 - Name of the tab (word in tab itself)
201
arg1 - Description of the tab
202
arg2 - Relative size of the canvas, 0 being the smallest, 1 being the largest (default 0.5)
203
*/
204
try {
205
kongGetAPI().chat.showTab(argument0, argument1, { size: argument2 });
206
return 1;
207
} catch (e) {
208
logError();
209
return -1;
210
}
211
}
212
213
function kongChatCloseTab() {
214
// Closes the tab opened by kongChatShowTab()
215
try {
216
kongGetAPI().chat.closeTab();
217
return 1;
218
} catch (e) {
219
logError();
220
return -1;
221
}
222
}
223
224
// --------------- OTHER -----------------
225