-
Notifications
You must be signed in to change notification settings - Fork 0
/
kiboto_game.js
106 lines (89 loc) · 2.72 KB
/
kiboto_game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function KibotoBotMessages() {
// store responses in global state because
// callback can't access anything in KibotoGame
// object
// NOTE: this should be a singleton but can't really enforce it!
this.kibotoResponses = [];
this.messagesToProcess = false;
this.get = function() {
return this.kibotoResponses;
};
this.push = function(message) {
this.kibotoResponses.push(message);
this.messagesToProcess = true;
}
this.clear = function() {
this.kibotoResponses = [];
this.messagesToProcess = false;
};
}
function KibotoGame(hostname, port, game_id, session_id, player_id) {
this.hostname = hostname;
this.port = port;
this.game_id = game_id;
this.session_id = session_id;
this.player_id = player_id;
// a session and player id should already exist, so might as well use them
// the game id can be made up. either the name of the game, or a guid
this.session_key = game_id + ':' + session_id + ':' + player_id;
this.init_session = function(callback, errorCallback) {
// initializes the session
var url = this.hostname + ':' + this.port + '/session?'
+ 'game_id=' + game_id + '&'
+ 'session_id=' + session_id + '&'
+ 'player_id=' + player_id;
console.log(url);
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function (err) {
if (xhr.readyState == 4) {
if (xhr.status != 200) {
errorCallback(xhr.status, xhr.statusText);
} else {
callback(xhr.status, xhr.statusText);
}
}
}
xhr.send();
};
this.event = function(data, callback, errorCallback, timeoutMS) {
// send an event to kiboto server with
// what ever data the game developer wants.
// should represent state changes for the current player
// as well as other players and the environment.
// the callback should be a function that the game
// specifies. they can do whatever they want in there
var xhr = new XMLHttpRequest();
var url = this.hostname + ':' + this.port + '/event'
xhr.open("POST", url, true);
// handle success
if (callback == null) {
// error. how to notify?
} else {
// wrap the function to notify the bot message
// object that there are messages to process
xhr.onload = function (err) {
// assume they handle their own message state and
// message queue. only pass them what they need
if (xhr.readyState == 4) {
callback(xhr.status, xhr.responseText, xhr.statusText);
}
};
}
// handle errors
if (errorCallback == null) {
// error. how to notify?
} else {
xhr.onerror = function (err) {
errorCallback(xhr.status, xhr.statusText);
};
}
// handle timeouts?
xhr.timeout = timeoutMS;
message = {
'session_key': this.session_key,
'message': data
}
xhr.send(JSON.stringify(message));
};
}