Commit ec8eca4b authored by Muhammad Yafi's avatar Muhammad Yafi
Browse files

Merge branch 'master' of...

Merge branch 'master' of http://gitlab.informatika.org/timothy.pratama/IF3230-Tugas-Besar-Sister-2015
parents 5f611b01 149b9fe9
server
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DaemonCodeAnalyzer">
<disable_hints />
</component>
<component name="DependencyValidationManager">
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</component>
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectModuleManager">
<modules />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.4.3 (C:\Python34\python.exe)" project-jdk-type="Python SDK" />
<component name="RunManager">
<list size="0" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/server.iml" filepath="$PROJECT_DIR$/.idea/server.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<component name="DependencyValidationManager">
<state>
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</state>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
</component>
</project>
\ No newline at end of file
This diff is collapsed.
......@@ -4,6 +4,8 @@ import json
import socket as Socket
import select
import codecs
from user import User
from map import Map
from ssocketio import SocketIO
......@@ -109,8 +111,12 @@ class ConnectionHandler:
# invoke controller signup
def handle_signup(self,username,password):
# do something
data = json.dumps({'status':'fail','description':'stub signup'})
data = ""
if not User.is_exist(username):
data = json.dumps({'status': 'fail', 'description':'username exists'})
else:
User.sign_up(username, password)
data = json.dumps({'status': 'ok'})
try:
self._socketio.write_to_socket(data.encode('ascii'))
except Exception as msg:
......@@ -118,8 +124,18 @@ class ConnectionHandler:
# invoke controller login
def handle_login(self,username,password):
# do somethng
data = json.dumps({'status':'ok','description':'stub login'})
data = ""
if not User.is_valid(username, password):
data = json.dumps({'status': 'fail','description': 'username/password combination is not found'})
else:
user = User.get_user(username)
print(user.id)
print(user.location.x)
print(user.location.y)
print(user.time)
data = json.dumps({'status': 'ok','token': str(user.id),
'x': user.location.x, 'y': user.location.y,
'time': user.time})
try:
self._socketio.write_to_socket(data.encode('ascii'))
except Exception as msg:
......@@ -145,8 +161,8 @@ class ConnectionHandler:
# invoke map
def handle_map(self,token):
# do something
data = json.dumps({'status':'ok','description':'stub map'})
map = Map('Bandar Behari')
data = json.dumps({'status':'ok','name': map.name, 'width': map.width, 'height': map.height})
try:
self._socketio.write_to_socket(data.encode('ascii'))
except Exception as msg:
......@@ -155,6 +171,9 @@ class ConnectionHandler:
# invoke move
def handle_move(self,token,x,y):
# do something
user = User(token)
(user.location.x, user.location.y) = (x, y);
data = json.dumps({'status':'ok','description':'stub move'})
try:
self._socketio.write_to_socket(data.encode('ascii'))
......
......@@ -19,7 +19,8 @@ class Offer:
class Tradebox(object):
pass
__singleton
def __init__(self):
......
__author__ = 'Willy'
from pymongo import MongoClient
from inventory import Inventory
class User:
class Location:
"""
"""
def __init__(self, x, y):
(self.x, self.y) = (x, y)
"""
class User:
__users = MongoClient('localhost', 27017)['sister'].get_collection('users')
def __init__(self, username):
_user = self.__users.find_one({"username": username})
def __init__(self, id):
_user = self.__users.find_one({"_id": id})
self.id = _user['_id']
self.username = _user['username']
self.password = _user['password']
self.x = _user['x']
self.y = _user['y']
self.location = Location(_user['location']['x'], _user['location']['y'])
self.time = _user['time']
self.inventory = Inventory(_user['inventory'])
def save(self):
_user = self.__users.find_one({"username": self.username})
self.__users.find_one_and_replace({'_id': self.id},
{'_id': self.id, 'username': self.username,
'password': self.password, 'x': self.x,
'y': self.y, 'time': self.time});
{'_id': self.id,
'username': self.username, 'password': self.password,
'location': {'x': self.location.x, 'y': self.location.y},
'inventory': self.inventory.items,
'time': self.time})
@staticmethod
def exist(username):
"""
Checks whether the username exist
:param username: string
:return:boolean
"""
if User.__users.find({"username": username}):
def is_exist(username):
if User.__users.find({"username": username}).count():
return True
else:
return False
@staticmethod
def is_valid(username, password):
if User.__users.find({'username': username, 'password': password}).count():
return True
return False
@staticmethod
def sign_up(username, password):
"""
Creates new user instance in database
:param username: string
:param password: string
:return:
"""
User.__users.insert_one({"username": username, "password": password, "x": 0, "y": 0, "time": 0})
User.__users.insert_one({'username': username, 'password': password,
'location': {'x': 0, 'y': 0},
'inventory': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'time': 0})
@staticmethod
def get_user(username):
user = User.__users.find_one({'username' : username})
return User(user['_id'])
if __name__ == "__main__":
user = User("1")
print(user.x)
\ No newline at end of file
# MongoClient('localhost', 27017)['sister'].create_collection('users')
print(User.get_user('user').username)
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment