summaryrefslogtreecommitdiff
path: root/public/js/game.js
blob: 8aed4966a839d89f9c02d26187067a06a7593c0e (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
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]);
			}
		},
		
		// Another player joins the room
		join: function(data){
			Events.receive.state(data);
		},
		
		// Another player leaves the room
		part: function(data){
			Events.receive.state(data);
		},
		
		// Game state changes
		state: function(data){
			if (Game.state == data.state) return;
			UI[ States.current ].unload();
			UI[ data.state ].load();
			Game.state = data.state;
		},
		
		// Incoming chat
		message: function(msg) {
			Chat.add(msg);
		},
		
		// Incoming image
		image: function(data) {
			console.log(data);
			if (data.nick == Game.nick) return;
			$("#image").attr("src", data.url).show();
		},

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

	},

	send: {

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

	},
	
};

// 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.
var UI = {};
UI[ State.WAITING ] = {
	load: function(){
	},
	unload: function(){
	}
}

UI[ State.DRAWING ] = {
	load: function(){
	},
	unload: function(){
	}
}

UI[ State.VOTING ] = {
	load: function(){
	},
	unload: function(){
	}
}

UI[ State.WINNING ] = {
	load: function(){
	},
	unload: function(){
	}
}

UI[ State.RESET ] = {
	load: function(){
	},
	unload: function(){
	}
}


$(Game.init);