summaryrefslogtreecommitdiffstats
path: root/pages/chat.php
blob: a29cf368b628b765d5fe456149450101bfd4c096 (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
<?php
htmlHeader(
	array('stats', 'chat'), 'Chat', 
	'BetaChat for Pathery.com', 
	array('scores', 'dateformat')
);
?>
<body>
<?php
topbar($Links);

if (!$accepted) {
	echo "<center>Please <a href='javascript:showSignin();'>login</a> to chat!</center>";
}



?>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script>
var lastID = 1;
var chatTimerDelayUpper = 9000;
var chatTimerDelayLower = 2000;
var chatTimerDelay = 5000;

var skipNextGetChat = false;

var chatBuffer = new Array();

getChatTimer();
function getChatTimer() {
	setTimeout("getChatTimer()", chatTimerDelay);
	if (chatTimerDelay < chatTimerDelayUpper)
		chatTimerDelay += 150;
	if (skipNextGetChat) {
		skipNextGetChat = false;
		return;
	}
	getChat();
}

function getChatDone(data) {
	var items = [];
	var p; //our prep string
	var newChats = false;
	
	////console.log('datalength', data.length);
	
	if (data.length < 3 || data == 'false')
		return;
		
	//console.log("START BUILD");
	
	//console.log("JSON TO PARSE", data);
	
	json = jQuery.parseJSON(data);
	
	//console.log("JSON PREPED");
	
	$.each(json, function(key, user) {
	
		var postDate = new Date();
		postDate.setTime(postDate.getTime() + user.secondsSince * 1000);
		//var timestamp = postDate.format("ddd h:MM TT");
		//var timestamp = postDate.format("h:MM:ss");
		var timestamp = postDate.format("hh:MM:ss");
	
		//console.log("INSIDE BUILD START");
		p = '';
		p = p+ " <div class='chatColumn1'>";
		p = p+ " 	<span class='chatTimestamp'>["+timestamp+"]</span>";
		p = p+ "		<div class='grid_td chatBadge' style='float:left; width:35px; height:35px; background:"+user.wallColor+" url(images/marks/"+user.wallEmblem+");'>";
		p = p+ "			<div style='background-color:transparent;' class='grid_td_inner grid_td_rocks'>";
		p = p+ "			</div>";
		p = p+ "		</div>";
		p = p+ "	</div>";
		
		p = p+ " <div class='chatColumn2'>";
		
		p = p+ "<span class='chatUsername'><a href='achievements?id="+user.userID+"' style='color:"+user.displayColor+"'>";
		p = p+ user.displayName+"</a>:</span>";
		
		p = p+ "	<span class='chatText'>";
		p = p+ htmlEncode(user.message);
		p = p+ "	</span>";
		p = p+ "	</div>";
		
		lastID = user.ID;
		
		var strClass = '';
		console.log('cpm', user.userID, userObj.ID); 
		if (user.userID == userObj.ID) {
			strClass += ' self';
		}
		
		items.unshift('<div class="chatMessage'+strClass+'" id="' + key + '">' + p + '</div>');
		newChats = true;
	});
	
	//console.log("BUILD DONE");
	
	if (newChats) {
		if (chatTimerDelay > chatTimerDelayLower)	chatTimerDelay -= 1000;
		
		var new_chatDiv = $('<div/>', {
			'class': 'my-new-list',
			'style': 'display: none',
			html: items.join('')
		});
		new_chatDiv.prependTo('#chatContainer').slideDown('fast'); 
	}
	//console.log('(END) lastID', lastID);
}

function prepChat(chat) {
	chat = chat.join('|:|');
	chat = chat.replace(/\&/g,'%26')
	return chat;
}

function getChat(message) {
	//console.log('LASTID:', lastID);
	var dataString = 'getChatFromID='+lastID;
	if (chatBuffer.length > 0) {
		dataString += '&send=true&messages='+prepChat(chatBuffer);
		chatBuffer.length = 0;
	}
	
	$.ajax({
		type: "POST",  
		url: "ajax/chat.ajax.php",  
		data: dataString, 
		success: function(data) {getChatDone(data);}
	});
}

function sendChat() {
	var message = $("input#message").val().replace("|:|", "||");
	if (message == '') return false;
	chatBuffer.push(message);
	$("input#message").val('');
	if (skipNextGetChat == false) {
		skipNextGetChat = true;
		getChat();
	}
	return false;  
}

$(document).ready(function() {

    $('#sendChat').live("submit", function() {
        sendChat()
    });
	 
});

function htmlEncode(value){
    if (value) {
        return jQuery('<div />').text(value).html();
    } else {
        return '';
    }
}

</script>

<div class='wrapper'>
<h3>Pathery Chat - BETA</h3>

<div class='chatContainer2'>
	<form id='sendChat' onsubmit="return false">
	<? if($accepted) { /*Only show the chat button if we're logged in*/ ?>
		<input type="hidden" name="stuff" value="1724">
		<input class='chatButton' type="button" class="send" id='chatSendBtn' value='Send' onClick="sendChat();">
		<input class='chatInputMessage' type="text" name="message" id="message" value="" maxlength="255" autocomplete="off" >
	<? } ?>
	</form>
	<div id='chatContainer'>

	</div>

</div>

</div>

<?


htmlFooter();
?>