summaryrefslogtreecommitdiff
path: root/public/js/game.js
blob: cd8801457d4336cfd057e35f31e5ed4f2b6a2368 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var socket = io.connect(window.location.hostname);

var State = {
 	WAITING: 0,
	DRAWING: 1,
	VOTING: 2,
	WINNING: 3,
	RESET: -1,
}

var Game = {
	nick: null,
	state: State.WAITING,
		
	init: function(){

		// Bind events from the server
		socket.on('event-welcome', Events.receive.welcome);
		socket.on('event-join', Events.receive.join);
		socket.on('event-part', Events.receive.part);
		socket.on('event-message', Events.receive.message);
		socket.on('event-state', Events.receive.state);
		socket.on('event-image', Events.receive.image);
		socket.on('event-vote', Events.receive.vote);
		Auth.init();
		Chat.init();
	}
}

var Events = {

	receive: {
		
		// Player connects to the game
		welcome: function(data){
			for (var i in data.messages) {
				Chat.add( data.messages[i] );
			}
			for (var i in data.players) {
				Roster.add( data.players[i] );
			}
		},
		
		// Another player joins the room
		join: function(data){
			Roster.add( data );
		},
		
		// Another player leaves the room
		part: function(data){
			Roster.remove( data );
		},
		
		// Game state changes
		state: function(data){
			if (Game.state == data.state) return;
			UI[ Game.state ].unload();
			UI[ data.state ].load(data);
			Game.state = data.state;
		},
		
		// Incoming chat
		message: function(msg) {
			Chat.add(msg);
		},
		
		// Incoming image
		image: function(data) {
			// preload the image
			var img = new Image();
			img.src = data.url;
		},

		// Another player votes on an image..
		vote: function(data){
		}

	},

	send: {

		// Joining the game
		join: function(data) {
			socket.emit('event-join', data);
		},

		// Sending a message
		message: function(msg) {
			socket.emit('event-message', msg);
		},
		
		// Sending a vote
		vote: function(user){
			socket.emit('event-vote', user);
		}

	},
	
};

// Explicit changes to the UI when moving from one state to another.
// This is for things like moving divs off the screen, changing bindings, etc.
// Anything a load method does should be undone by the unload method.. for now.
// These are all functions so they can have their own weirdo scope..
// I should have started doing it like this a long time ago!
var UI = {};
UI[ State.WAITING ] = new function() {
	this.load = function(data){
		$("#current-state").html("Waiting");
		$("#drawing").show();
	}
	this.unload = function(){
		$("#drawing").hide();
	}
}

UI[ State.DRAWING ] = new function(){
	this.load = function(data){
		$("#current-state").html("Draw! The word is " + data.word);
		$("#drawing").show();
		$(window).trigger("drawing:start")
	},
	this.unload = function(){
		$("#drawing").hide();
	}
}

UI[ State.VOTING ] = new function(){
	var voted = false;
	this.load = function(data){
		console.log(data);
		$("#current-state").html("Vote!");
		this.build( data );
		$("#voting").show();
	}
	this.build = function(data){
		$("#voting").empty();
		for (var i in data.images) {
			var image = data.images[i];
			var $li = $("<li>");
			$li.data("nick", image.nick);
			$li.html("<img src='" + image.url + "'> " + image.nick );
			$li.click( this.click );
			$("#voting").append($li);
		}
	}
	this.unload = function(){
		$("#voting").hide();
	}
	this.click = function(){
		voted = true;
		var nick = $(this).data("nick");
		console.log("VOTE >> ", nick);
		Events.send.vote({ nick: nick });
	}
}

UI[ State.WINNING ] = new function(){
	this.build = function(data){
		$("#winning").empty();
		var scores = [];
		for (var i in data.scores) {
			scores.push( [data.scores[i], i] );
		}
		console.log(scores);
		var topthree = scores.sort(function(a,b){ a[0] - b[0] }).splice(0, 3);
		console.log(data);
		for (var i in topthree) {
			var score = topthree[i][0]
			var nick = topthree[i][1]
			var image = data.images[ nick ];
			var $li = $("<li>");
			$li.html("<img src='" + image.url + "'> " + image.nick );
		}
	}
	this.load = function(data){
		$("#current-state").html("Win!");
		this.build(data);
		$("#winning").show();
	}
	this.unload = function(){
		$("#winning").hide();
	}
}

UI[ State.RESET ] = new function() {
	this.load = function(data){
		$("#current-state").html("whoahao RESET!");
	}
	this.unload = function(){
	}
}


$(Game.init);