- change register error message only showing up once instead of repeating every time you try and get an error. it probably should blank when you start typing again though

- pull the long polling handlers out to their own file like the ajax response handlers
- chat
This commit is contained in:
Andy Janata 2011-12-24 19:37:45 -08:00
parent b73320d614
commit 0183ca5357
9 changed files with 166 additions and 29 deletions

View File

@ -12,23 +12,53 @@
/* * /
display: none;
/* */
height: 768px;
height: 750px;
overflow: none;
position: relative;
width: 1024px;
width: 1000px;
border: 1px solid red;
}
#log {
width: 50%;
height: 200px;
border: 1px solid black;
#chat_area {
width: 500px;
height: 215px;
border: 0px;
position: absolute;
bottom: 0px;
right: 0px;
overflow: none;
}
#log {
width: 500px;
height: 198px;
border: 1px solid black;
position: absolute;
bottom: 17px;
right: 0px;
overflow: auto;
}
#chat {
border: 1px solid black;
width: 451px;
height: 17px;
position: absolute;
bottom: -1px;
right: 49px;
padding: 0px;
}
#chat_submit {
width: 50px;
height: 19px;
position: absolute;
right: 0px;
bottom: -1px;
margin: 0px;
padding: 0px;
}
span.error {
color: red;
}

View File

@ -12,6 +12,7 @@
<%-- TODO make this be dynamic with looking at the filesystem and using jquery --%>
<script type="text/javascript" src="js/cah.log.js"></script>
<script type="text/javascript" src="js/cah.longpoll.js"></script>
<script type="text/javascript" src="js/cah.longpoll.handlers.js"></script>
<script type="text/javascript" src="js/cah.ajax.js"></script>
<script type="text/javascript" src="js/cah.ajax.handlers.js"></script>
<script type="text/javascript" src="js/cah.app.js"></script>
@ -26,10 +27,15 @@
<div id="nickbox">
Nickname: <input type="text" id="nickname" value="" maxlength="30" />
<input type="button" id="nicknameconfirm" value="Set" />
<span id="nickbox_error" class="error"></span>
</div>
<div id="canvass">
<div id="chat_area">
<div id="log"></div>
<input type="text" id="chat" maxlength="200" />
<input type="button" id="chat_submit" value="Chat" />
</div>
</div>
</body>

View File

@ -14,7 +14,7 @@ cah.ajax.SuccessHandlers.register = function(data) {
};
cah.ajax.ErrorHandlers.register = function(data) {
$("#nickbox").append("<span class='error'>" + data.error_message + "</span>");
$("#nickbox_error").text(data.error_message);
$("#nickname").focus();
};
@ -31,7 +31,7 @@ cah.ajax.SuccessHandlers.firstload = function(data) {
};
cah.ajax.ErrorHandlers.firstload = function(data) {
// TODO dunno what to do here
// TODO dunno what to do here, if anything
};
/**
@ -42,3 +42,7 @@ cah.ajax.after_registered = function() {
cah.log.debug("done registering");
cah.longpoll.longPoll();
};
cah.ajax.SuccessHandlers.chat = function(data) {
// pass
};

View File

@ -5,10 +5,16 @@
*/
$(document).ready(function() {
// see if we already exist on the server so we can resume
cah.Ajax.request("firstload", {});
// TODO see if we have a stored nickname somewhere
$("#nicknameconfirm").click(nicknameconfirm_click);
$("#nickbox").keyup(nickbox_keyup);
$("#nickbox").focus();
$("#chat").keyup(chat_keyup);
$("#chat_submit").click(chatsubmit_click);
});
function nickbox_keyup(e) {
@ -19,8 +25,26 @@ function nickbox_keyup(e) {
}
function nicknameconfirm_click(e) {
nickname = $.trim($("#nickname").val());
cah.ajax.request("register", {
nickname : nickname
var nickname = $.trim($("#nickname").val());
cah.Ajax.request("register", {
nickname : nickname,
});
}
function chat_keyup(e) {
if (e.which == 13) {
$("#chat_submit").click();
e.preventDefault();
}
}
function chatsubmit_click(e) {
var text = $.trim($("#chat").val());
// TODO when I get multiple channels working, this needs to know active and pass it
cah.Ajax.request("chat", {
message : text,
});
cah.log.status("&lt;" + cah.nickname + "&gt; " + text);
$("#chat").val("");
$("#chat").focus();
}

View File

@ -0,0 +1,28 @@
/**
* Event handlers from long-poll operations.
*
* @author ajanata
*/
cah.longpoll.ErrorCodeHandlers.not_registered = function(data) {
cah.longpoll.Resume = false;
// TODO disable interface
cah.log.error("The server seems to have restarted. Any in-progress games have been lost.");
cah.log.error("You will need to refresh the page to start a new game.");
};
cah.longpoll.EventHandlers.new_player = function(data) {
cah.log.status(data.nickname + " has connected.");
};
cah.longpoll.EventHandlers.noop = function(data) {
// pass
};
cah.longpoll.EventHandlers.chat = function(data) {
// TODO deal with multiple channels eventually
// don't display our own chat
if (data.from != cah.nickname) {
cah.log.status("&lt;" + data.from + "&gt; " + data.message);
}
};

View File

@ -11,6 +11,7 @@ cah.longpoll.INITIAL_BACKOFF = 500;
cah.longpoll.Backoff = cah.longpoll.INITIAL_BACKOFF;
cah.longpoll.Resume = true;
cah.longpoll.ErrorCodeHandlers = {};
cah.longpoll.EventHandlers = {};
cah.longpoll.longPoll = function() {
cah.log.debug("starting long poll");
@ -42,15 +43,11 @@ cah.longpoll.done = function(data) {
cah.log.error(data.error_message);
}
} else {
// TODO process data
// var req = pendingRequests[data.serial];
// if (req && cah.ajax.SuccessHandlers[req.op]) {
// cah.ajax.SuccessHandlers[req.op](data);
// } else if (req) {
// addLogError("Unhandled response for op " + req.op);
// } else {
// addLogError("Unknown response for serial " + data.serial);
// }
if (cah.longpoll.EventHandlers[data.event]) {
cah.longpoll.EventHandlers[data.event](data);
} else {
cah.log.error("Unhandled event " + data.event);
}
}
// reset the backoff to normal when there's a successful operation
@ -66,10 +63,3 @@ cah.longpoll.error = function(jqXHR, textStatus, errorThrown) {
.error("Error communicating with server. Will try again in " + (cah.longpoll.Backoff / 1000)
+ " second" + (cah.longpoll.Backoff != 1000 ? "s" : "") + ".");
};
cah.longpoll.ErrorCodeHandlers.not_registered = function(data) {
cah.longpoll.Resume = false;
// TODO disable interface
cah.log.error("The server seems to have restarted. Any in-progress games have been lost.");
cah.log.error("You will need to refresh the page to start a new game.");
};

View File

@ -38,7 +38,7 @@ public class QueuedMessage implements Comparable<QueuedMessage> {
*
*/
public enum Type {
PING(0), NEW_PLAYER(5);
PING(0), NEW_PLAYER(5), CHAT(5);
private final int weight;

View File

@ -0,0 +1,54 @@
package net.socialgamer.cah.handlers;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import net.socialgamer.cah.Server;
import net.socialgamer.cah.data.ConnectedUsers;
import net.socialgamer.cah.data.QueuedMessage.Type;
import net.socialgamer.cah.data.User;
import com.google.inject.Inject;
public class ChatHandler extends Handler {
public static final String OP = "chat";
private final ConnectedUsers users;
@Inject
public ChatHandler(final Server server) {
this.users = server.getConnectedUsers();
}
@Override
public Map<String, Object> handle(final Map<String, String[]> parameters,
final HttpSession session) {
final Map<String, Object> data = new HashMap<String, Object>();
final User user = (User) session.getAttribute("user");
assert (user != null);
if (!parameters.containsKey("message") || parameters.get("message").length != 1) {
return error("No message specified.");
} else {
final String message = parameters.get("message")[0].trim();
if (message.length() > 200) {
return error("Messages cannot be longer than 200 characters.");
} else {
final HashMap<String, Object> broadcastData = new HashMap<String, Object>();
broadcastData.put("event", "chat");
broadcastData.put("from", user.getNickname());
broadcastData.put("message", message);
// TODO once there are multiple chat channels, put the destination here
// TODO once there are games and they have their own chat, make it only send to participants
users.broadcastToAll(Type.CHAT, broadcastData);
}
}
return data;
}
}

View File

@ -10,6 +10,7 @@ public class Handlers {
static {
LIST = new HashMap<String, Class<? extends Handler>>();
LIST.put(ChatHandler.OP, ChatHandler.class);
LIST.put(FirstLoadHandler.OP, FirstLoadHandler.class);
LIST.put(RegisterHandler.OP, RegisterHandler.class);
LIST.put(TestHandler.OP, TestHandler.class);