Permalink
Browse files

lol

  • Loading branch information...
1 parent d8b4517 commit 700f9b804986de392a9dde79f9a5804e11629bb9 @alexbft committed May 6, 2011
Showing with 69 additions and 13 deletions.
  1. +21 −2 tornado/actions.py
  2. +9 −4 tornado/main.py
  3. +23 −2 tornado/static/js/scripts.js
  4. +4 −0 tornado/templates/js/loc_arena.html
  5. +12 −5 tornado/user.py
View
@@ -1,3 +1,9 @@
+from pymongo.objectid import ObjectId
+from user import User
+
+class ActionException(Exception):
+ pass
+
class Action:
def __init__(self, user, q):
@@ -7,12 +13,25 @@ def __init__(self, user, q):
def _checkloc(self, *args):
if self.user["loc"] not in args:
- raise Exception("Invalid user location (" + self.user["loc"] + ") for action: " + self.action)
+ self.error("Invalid user location (" + self.user["loc"] + ")")
+
+ def error(self, msg):
+ raise ActionException("Error in action %s: %s" % (self.action, msg))
def go_arena(self):
self._checkloc("default")
self.user.update({"loc": "arena"})
def go_default(self):
self._checkloc("arena")
- self.user.update({"loc": "default"})
+ self.user.update({"loc": "default"})
+
+ def duel(self):
+ id = ObjectId(self.q["id"])
+ other = User(id)
+ if self.user._id == id or not other.online or other["loc"] != "arena":
+ self.error("Invalid other")
+ self.error("Not implemented")
+
+ def refresh(self):
+ pass
View
@@ -7,13 +7,15 @@
import os.path
import json
from chat import Chat
-from actions import Action
+from actions import Action, ActionException
class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
try:
# TODO: sessions
- return User.get(ObjectId(self.get_secure_cookie("user")))
+ u = User.get(ObjectId(self.get_secure_cookie("user")))
+ u.update_activity()
+ return u
except:
return None
@@ -55,9 +57,12 @@ class QueryHandler(BaseHandler):
def post(self):
try:
res = ServiceResponse.ok(self.handle(json.loads(self.request.body)))
- except Exception as ex:
+ except ActionException as ex:
+ log_error(ex)
+ res = ServiceResponse.error("invalid query")
+ except:
log_exception()
- res = ServiceResponse.error(str(ex))
+ res = ServiceResponse.error("server error")
self.write(to_json(res))
class ChatHandler(QueryHandler):
@@ -48,6 +48,25 @@ function action(actionName, params) {
});
}
+function makeUserLink(u) {
+ return '<a href="#" data-userid="' + u.id + '">' + u.name + '</a>';
+}
+
+function makeUserList(ul, clickHandler) {
+ var list = $($.map(ul, function(u) {return "<li>" + makeUserLink(u) + "</li>"}).join(''));
+ var cont = $('<ul class="userlist"></ul>').append(list);
+ cont.find('a').click(function(ev) {ev.preventDefault(); var id = $(this).attr('data-userid'); clickHandler(id);});
+ return cont;
+}
+
+function nodefault(func) {
+ return function(ev) {ev.preventDefault(); func.call(this, ev);}
+}
+
+function actFunc(actionName, params) {
+ return nodefault(function() {action(actionName, params)});
+}
+
var chat;
(function() {
var cache = {};
@@ -99,9 +118,11 @@ var chat;
var loc = {};
loc.init = {
"default": function() {
- $("#go_arena").click(function(ev) {action("go_arena"); ev.preventDefault();});
+ $("#go_arena").click(actFunc("go_arena"));
},
"arena": function() {
- $("#go_default").click(function(ev) {action("go_default"); ev.preventDefault();});
+ $("#go_default").click(actFunc("go_default"));
+ $("#people").html(makeUserList(u.people, function(id) {action("duel", {id: id})}));
+ $("#refresh").click(actFunc("refresh"));
}
};
@@ -1,2 +1,6 @@
<h2> This is arena! </h2>
+<h3>Будем биться?</h3>
+<div id='people'></div>
+<a href="#" id='refresh'>Обновить список</a>
+<br /><br />
<a href="#" id="go_default">Вернуться</a>
View
@@ -1,10 +1,15 @@
from db import db
from utils import *
from chat import Chat
+from pymongo.objectid import ObjectId
+from loc import Loc
class User:
def __init__(self, id):
- row = db().user.find_one({"_id": id})
+ if isinstance(id, ObjectId):
+ row = db().user.find_one({"_id": id})
+ else:
+ row = id
if row:
self.__dict__.update(row)
else:
@@ -30,18 +35,20 @@ def create(name):
def login(name, password):
user = db().user.find_one({"name": name, "pass": password})
if user:
- user["online"] = 1
- user["last_action"] = now()
- db().user.save(user)
- return User(user["_id"])
+ res = User(user)
+ res.update_activity()
else:
return None
def js(self):
res = {"name": self.name, "id": str(self._id)}
+ res.update(Loc.js(self))
res.update(self.data)
return res
+ def js_link(self):
+ return {"name": self.name, "id": str(self._id)}
+
def js_all(self):
return {"user": self.js(), "chat": Chat.js(self.name)}

0 comments on commit 700f9b8

Please sign in to comment.