Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit a47c5f6cac91c92dc6e7e3127b0ca39676ab8540 Batiste Bieler committed Nov 26, 2009
@@ -0,0 +1 @@
+*.pyc
10 README
@@ -0,0 +1,10 @@
+A we based Role Playing Game
+============================
+
+A Role Playing Game skeleton that use django and gevent. To start the server, run::
+
+ $ python run.py
+
+Example chat room from http://bitbucket.org/denis/gevent/src/tip/examples/webchat/chat/views.py
+
+
No changes.
No changes.
@@ -0,0 +1,13 @@
+from django.db import models
+
+# not use right now
+class MapElement(models.Model):
+
+ name = models.CharField(max_length=50)
+ x_pos = models.IntegerField()
+ y_pos = models.IntegerField()
+
+ content = models.TextField()
+
+ def __unicode__(self):
+ return self.name
@@ -0,0 +1,85 @@
+import uuid
+import simplejson
+from django.shortcuts import render_to_response
+from django.template.loader import render_to_string
+from django.http import HttpResponse
+from gevent.event import Event
+from django.conf import settings
+
+
+class ChatRoom(object):
+
+ def __init__(self):
+ self.last_message = []
+ self.players = []
+ self.cache = []
+ self.new_room_event = Event()
+
+ def main(self, request):
+ return render_to_response(
+ 'index.html',
+ {'MEDIA_URL': settings.MEDIA_URL}
+ )
+
+ def get_player(self, key):
+ for p in self.players:
+ if p['key'] == key:
+ return p
+ return None
+
+ def player_new(self, request):
+ key = request.session.session_key
+ name = request.POST['body']
+ player = {'name':name, 'key':key}
+ if self.get_player(key):
+ return json_response({'new_player':player})
+ self.players.append(player)
+ self.new_room_event.set()
+ self.new_room_event.clear()
+ return json_response({'new_player':player})
+
+ def player_update_position(self, request):
+ key = request.session.session_key
+ player = self.get_player(key)
+ if player is None:
+ return json_response("error reload")
+ position = request.POST['body']
+ player['position'] = position
+ self.new_room_event.set()
+ self.new_room_event.clear()
+ return json_response({'update_player_position':[key, position]})
+
+ def message_new(self, request):
+ key = request.session.session_key
+ msg = request.POST['body']
+ player = self.get_player(key)
+ if player is None:
+ return json_response("error reload")
+ player['last_message'] = msg
+ self.new_room_event.set()
+ self.new_room_event.clear()
+ return json_response({'last_message':[key, msg]})
+
+ def room_updates(self, request):
+ value = self.new_room_event.wait()
+ return json_response({
+ 'players': self.players
+ })
+
+
+room = ChatRoom()
+main = room.main
+message_new = room.message_new
+player_new = room.player_new
+player_update_position = room.player_update_position
+room_updates = room.room_updates
+
+def create_message(from_, body):
+ data = {'id': str(uuid.uuid4()), 'from': from_, 'body': body}
+ data['html'] = render_to_string('message.html', dictionary={'message': data})
+ return data
+
+def json_response(value, **kwargs):
+ kwargs.setdefault('content_type', 'text/javascript; charset=UTF-8')
+ return HttpResponse(simplejson.dumps(value), **kwargs)
+
@@ -0,0 +1,11 @@
+#!/usr/bin/python
+from django.core.management import execute_manager
+try:
+ import settings # Assumed to be in the same directory.
+except ImportError:
+ import sys
+ sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
+ sys.exit(1)
+
+if __name__ == "__main__":
+ execute_manager(settings)
22 run.py
@@ -0,0 +1,22 @@
+#!/usr/bin/python
+from gevent import monkey; monkey.patch_all()
+from gevent.wsgi import WSGIServer
+import sys
+import os
+import traceback
+from django.core.handlers.wsgi import WSGIHandler
+from django.core.management import call_command
+from django.core.signals import got_request_exception
+
+sys.path.append('..')
+os.environ['DJANGO_SETTINGS_MODULE'] = 'game.settings'
+
+def exception_printer(sender, **kwargs):
+ traceback.print_exc()
+
+got_request_exception.connect(exception_printer)
+
+call_command('syncdb')
+print 'Serving on 8088...'
+WSGIServer(('', 8088), WSGIHandler()).serve_forever()
+
@@ -0,0 +1,38 @@
+from os.path import dirname, join, abspath
+__dir__ = dirname(abspath(__file__))
+
+DEBUG = True
+TEMPLATE_DEBUG = DEBUG
+ADMINS = ()
+MANAGERS = ADMINS
+DATABASE_ENGINE = 'sqlite3'
+DATABASE_NAME = 'dev.db'
+DATABASE_USER = ''
+DATABASE_PASSWORD = ''
+DATABASE_HOST = ''
+DATABASE_PORT = ''
+TIME_ZONE = 'America/Chicago'
+LANGUAGE_CODE = 'en-us'
+SITE_ID = 1
+USE_I18N = True
+MEDIA_ROOT = join(__dir__, 'static')
+MEDIA_URL = '/media/'
+SECRET_KEY = 'nv8(yg*&1-lon-8i-3jcs0y!01+rem*54051^5xt#^tzujdj!c'
+TEMPLATE_LOADERS = (
+ 'django.template.loaders.filesystem.load_template_source',
+ 'django.template.loaders.app_directories.load_template_source',
+)
+MIDDLEWARE_CLASSES = (
+ 'django.middleware.common.CommonMiddleware',
+ 'django.contrib.sessions.middleware.SessionMiddleware',
+)
+ROOT_URLCONF = 'game.urls'
+TEMPLATE_DIRS = (
+ join(__dir__, 'templates')
+)
+INSTALLED_APPS = (
+ 'django.contrib.contenttypes',
+ 'django.contrib.sessions',
+ 'django.contrib.sites',
+ 'game.chat',
+)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2009 FriendFeed
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License. You may obtain
+ * a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+body {
+ background: white;
+ margin: 10px;
+}
+
+body,
+input {
+ font-family: sans-serif;
+ font-size: 10pt;
+ color: black;
+}
+
+table {
+ border-collapse: collapse;
+ border: 0;
+}
+
+td {
+ border: 0;
+ padding: 0;
+}
+
+#chat {
+ position: absolute;
+ top: 10px;
+ right: 10px;
+}
+
+#input {
+ margin-top: 0.5em;
+}
+
+#inbox .message {
+ padding-top: 0.25em;
+}
+
+#nav {
+ text-align: right;
+ float: right;
+ z-index: 99;
+}
@@ -0,0 +1,75 @@
+// Copyright 2009 FriendFeed
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License. You may obtain
+// a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations
+// under the License.
+
+$(document).ready(function() {
+
+var updater = {
+ errorSleepTime: 500,
+ cursor: null,
+
+ poll: function() {
+ var args = {"_xsrf": getCookie("_xsrf")};
+ if (updater.cursor) args.cursor = updater.cursor;
+ $.ajax({
+ url: "/a/room/updates",
+ type: "POST",
+ dataType: "text",
+ data: $.param(args),
+ success: updater.onSuccess,
+ error: updater.onError
+ });
+ },
+
+ onSuccess: function(response) {
+ //try {
+ var mapdata = $.evalJSON(response);
+ $.receive_data(mapdata)
+ //var pos = mapdata['players'][0]['position'].split(',');
+ //$.player_target_position[0] = parseInt(pos[0])
+ //$.player_target_position[1] = parseInt(pos[1])
+
+ //} catch (e) {
+ // updater.onError();
+ // return;
+ //}
+ updater.errorSleepTime = 500;
+ window.setTimeout(updater.poll, 0);
+ },
+
+ onError: function(response) {
+ updater.errorSleepTime *= 2;
+ console.log("Poll error; sleeping for", updater.errorSleepTime, "ms");
+ window.setTimeout(updater.poll, updater.errorSleepTime);
+ }
+};
+
+ updater.poll();
+
+
+function getCookie(name) {
+ var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
+ return r ? r[1] : undefined;
+}
+
+jQuery.fn.formToDict = function() {
+ var fields = this.serializeArray();
+ var json = {}
+ for (var i = 0; i < fields.length; i++) {
+ json[fields[i].name] = fields[i].value;
+ }
+ if (json.next) delete json.next;
+ return json;
+};
+
+});
@@ -0,0 +1,56 @@
+#map {
+ position:relative;
+ margin:20px;
+}
+.bloc {
+ background:url(tileset.png) top left no-repeat;
+ position:absolute;
+ background-position:-18px -18px;
+ width:16px;
+ height:16px;
+}
+#select {
+ position:absolute;
+ width:16px;
+ height:16px;
+ border:1px red solid;
+ left:-100px;
+}
+.player {
+ position:absolute;
+ background:url(walk2.png) top left no-repeat;
+ width:24px;
+ height:32px;
+}
+.player span {
+ font-size:11px;
+ color:#fff;
+ background:#333;
+ padding:2px 3px;
+ margin-left:28px;
+ position:absolute;
+}
+
+.player .name {
+ opacity:0.5;
+}
+
+.player .message {
+ margin-top:1.4em;
+}
+
+#input {
+ position:absolute;
+ left:35px;
+ top:505px;
+}
+
+#tileset {
+ top:100px;
+ left:600px;
+ position:absolute;
+}
+#grid-serialized {
+ width:600px;
+ height:100px;
+}
Oops, something went wrong.

0 comments on commit a47c5f6

Please sign in to comment.