Commit e15cef56 authored by opelhoward's avatar opelhoward
Browse files

testing

parent 716885a7
......@@ -185,8 +185,8 @@ public class Helper {
JSONObject json = new JSONObject();
json.put("method", "move");
json.put("token", Global.player.getToken());
json.put("x", x);
json.put("y",y);
json.put("x",Integer.parseInt(x));
json.put("y",Integer.parseInt(y));
System.out.println("Message to server: " + json.toJSONString());
String response = Connect(Global.server_address, Global.port, json.toJSONString());
......@@ -197,12 +197,12 @@ public class Helper {
if(response_json.get("status").equals("ok"))
{
Global.player.setTime((String) response_json.get("time"));
Global.player.setTime( response_json.get("time").toString());
return true;
}
else if (response_json.get("status").equals("fail"))
{
Global.ErrorMessage = (String) response_json.get("description");
Global.ErrorMessage = response_json.get("description").toString();
return false;
}
else
......
......@@ -219,7 +219,7 @@ public class MainScreen extends javax.swing.JFrame {
int pos_x = Integer.parseInt(Global.player.getX());
int pos_y = Integer.parseInt(Global.player.getY());
String time = Global.player.getTime();
String time = Global.player.getTime().toString();
Date date = new Date(Long.parseLong(time) * 1000);
DateFormat format = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Asia/Jakarta"));
......
......@@ -27,8 +27,8 @@
<file leaf-file-name="sconnectionhandler.py" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/sconnectionhandler.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.6909091" vertical-offset="2420" max-vertical-offset="7990">
<caret line="158" column="12" selection-start-line="158" selection-start-column="12" selection-end-line="158" selection-end-column="12" />
<state vertical-scroll-proportion="-6.109091" vertical-offset="4375" max-vertical-offset="8364">
<caret line="119" column="44" selection-start-line="119" selection-start-column="44" selection-end-line="119" selection-end-column="44" />
<folding>
<element signature="e#21#32#0" expanded="true" />
</folding>
......@@ -51,8 +51,8 @@
<file leaf-file-name="tradebox.py" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/tradebox.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="1207">
<caret line="36" column="70" selection-start-line="35" selection-start-column="54" selection-end-line="36" selection-end-column="70" />
<state vertical-scroll-proportion="0.0" vertical-offset="566" max-vertical-offset="1275">
<caret line="58" column="13" selection-start-line="58" selection-start-column="13" selection-end-line="58" selection-end-column="13" />
<folding>
<element signature="e#22#53#0" expanded="true" />
</folding>
......@@ -63,8 +63,8 @@
<file leaf-file-name="market.py" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/market.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="884">
<caret line="43" column="20" selection-start-line="43" selection-start-column="20" selection-end-line="43" selection-end-column="20" />
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="952">
<caret line="21" column="0" selection-start-line="21" selection-start-column="0" selection-end-line="21" selection-end-column="0" />
<folding />
</state>
</provider>
......@@ -73,8 +73,8 @@
<file leaf-file-name="user.py" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/user.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="174" max-vertical-offset="1190">
<caret line="20" column="55" selection-start-line="20" selection-start-column="46" selection-end-line="20" selection-end-column="55" />
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="1207">
<caret line="37" column="60" selection-start-line="37" selection-start-column="60" selection-end-line="37" selection-end-column="60" />
<folding />
</state>
</provider>
......@@ -87,10 +87,10 @@
<list>
<option value="$PROJECT_DIR$/map.py" />
<option value="$PROJECT_DIR$/inventory.py" />
<option value="$PROJECT_DIR$/user.py" />
<option value="$PROJECT_DIR$/sclient.py" />
<option value="$PROJECT_DIR$/tradebox.py" />
<option value="$PROJECT_DIR$/user.py" />
<option value="$PROJECT_DIR$/market.py" />
<option value="$PROJECT_DIR$/sclient.py" />
<option value="$PROJECT_DIR$/sconnectionhandler.py" />
</list>
</option>
......@@ -153,7 +153,7 @@
<property name="restartRequiresConfirmation" value="true" />
<property name="FullScreen" value="false" />
</component>
<component name="RunManager" selected="Python.sclient">
<component name="RunManager" selected="Python.main">
<configuration default="false" name="main" type="PythonConfigurationType" factoryName="Python" temporary="true">
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
......@@ -340,7 +340,7 @@
</component>
<component name="ToolWindowManager">
<frame x="-8" y="-8" width="1382" height="744" extended-state="6" />
<editor active="true" />
<editor active="false" />
<layout>
<window_info id="Changes" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.24304539" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
......@@ -349,7 +349,7 @@
<window_info id="Application Servers" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="8" side_tool="false" content_ui="tabs" />
<window_info id="Python Console" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="9" side_tool="false" content_ui="tabs" />
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="10" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.32913387" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="true" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.32913387" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="11" side_tool="false" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" />
......@@ -384,6 +384,98 @@
<watches-manager />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/sconnectionhandler.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="8245">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding>
<element signature="e#21#32#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/sclient.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="120" max-vertical-offset="2125">
<caret line="20" column="0" selection-start-line="20" selection-start-column="0" selection-end-line="20" selection-end-column="0" />
<folding>
<element signature="e#0#23#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/tradebox.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="566" max-vertical-offset="1275">
<caret line="58" column="13" selection-start-line="58" selection-start-column="13" selection-end-line="58" selection-end-column="13" />
<folding>
<element signature="e#22#53#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/market.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="714" max-vertical-offset="884">
<caret line="43" column="20" selection-start-line="43" selection-start-column="20" selection-end-line="43" selection-end-column="20" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/user.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="174" max-vertical-offset="1190">
<caret line="20" column="55" selection-start-line="20" selection-start-column="46" selection-end-line="20" selection-end-column="55" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/sconnectionhandler.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="8245">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding>
<element signature="e#21#32#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/sclient.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="120" max-vertical-offset="2125">
<caret line="20" column="0" selection-start-line="20" selection-start-column="0" selection-end-line="20" selection-end-column="0" />
<folding>
<element signature="e#0#23#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/tradebox.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="612" max-vertical-offset="1207">
<caret line="36" column="70" selection-start-line="35" selection-start-column="54" selection-end-line="36" selection-end-column="70" />
<folding>
<element signature="e#22#53#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/market.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="714" max-vertical-offset="884">
<caret line="43" column="20" selection-start-line="43" selection-start-column="20" selection-end-line="43" selection-end-column="20" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/user.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="174" max-vertical-offset="1190">
<caret line="20" column="55" selection-start-line="20" selection-start-column="46" selection-end-line="20" selection-end-column="55" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/sconnectionhandler.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="7582">
......@@ -546,14 +638,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/user.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="174" max-vertical-offset="1190">
<caret line="20" column="55" selection-start-line="20" selection-start-column="46" selection-end-line="20" selection-end-column="55" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/sserver.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="1.022613" vertical-offset="409" max-vertical-offset="1921">
......@@ -568,38 +652,46 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/sclient.py">
<entry file="file://$PROJECT_DIR$/tradebox.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="120" max-vertical-offset="2125">
<caret line="20" column="0" selection-start-line="20" selection-start-column="0" selection-end-line="20" selection-end-column="0" />
<state vertical-scroll-proportion="0.0" vertical-offset="566" max-vertical-offset="1275">
<caret line="58" column="13" selection-start-line="58" selection-start-column="13" selection-end-line="58" selection-end-column="13" />
<folding>
<element signature="e#0#23#0" expanded="true" />
<element signature="e#22#53#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/tradebox.py">
<entry file="file://$PROJECT_DIR$/market.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="276" max-vertical-offset="1207">
<caret line="36" column="70" selection-start-line="35" selection-start-column="54" selection-end-line="36" selection-end-column="70" />
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="952">
<caret line="21" column="0" selection-start-line="21" selection-start-column="0" selection-end-line="21" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/sclient.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="120" max-vertical-offset="2125">
<caret line="20" column="0" selection-start-line="20" selection-start-column="0" selection-end-line="20" selection-end-column="0" />
<folding>
<element signature="e#22#53#0" expanded="true" />
<element signature="e#0#23#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/market.py">
<entry file="file://$PROJECT_DIR$/user.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="884">
<caret line="43" column="20" selection-start-line="43" selection-start-column="20" selection-end-line="43" selection-end-column="20" />
<state vertical-scroll-proportion="0.0" vertical-offset="684" max-vertical-offset="1207">
<caret line="37" column="60" selection-start-line="37" selection-start-column="60" selection-end-line="37" selection-end-column="60" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/sconnectionhandler.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.6909091" vertical-offset="2420" max-vertical-offset="7990">
<caret line="158" column="12" selection-start-line="158" selection-start-column="12" selection-end-line="158" selection-end-column="12" />
<state vertical-scroll-proportion="-6.109091" vertical-offset="4375" max-vertical-offset="8364">
<caret line="119" column="44" selection-start-line="119" selection-start-column="44" selection-end-line="119" selection-end-column="44" />
<folding>
<element signature="e#21#32#0" expanded="true" />
</folding>
......
......@@ -15,7 +15,11 @@ class OfferMarket:
self.ip = offer['ip']
self.port = offer['port']
class Market:
time = 0
__market = MongoClient('localhost', 27017)['sister']\
.get_collection("market")
......
......@@ -156,13 +156,21 @@ class ConnectionHandler:
for i in range(0, 10):
for j in range(0, 10):
mixitem[i][j] = -1
user.inventory.items[mixitem] += 1
user.inventory.items[item1] -= 1
user.inventory.items[item2] -= 1
mixitem[0][1] = 4
mixitem[1][2] = 5
mixitem[2][3] = 6
mixitem[4][5] = 7
mixitem[5][6] = 8
mixitem[7][8] = 9
if mixitem[item1][item2] != -1:
user.inventory.items[mixitem[item1][item2]] += 1
user.inventory.items[item1] -= 1
user.inventory.items[item2] -= 1
data = json.dumps({'status':'ok','item': mixitem[item1][item2]})
else:
data = json.dumps({'status': 'fail', 'description': 'Invalid Combination'})
else:
data = json.dumps({'status': 'fail', 'description': 'FAILURE MESSAGE HERE'})
data = json.dumps({'status':'ok','description':'stub mixitem'})
data = json.dumps({'status': 'fail', 'description': 'Not Enough Items'})
try:
self._socketio.write_to_socket(data.encode('ascii'))
except Exception as msg:
......@@ -181,11 +189,15 @@ class ConnectionHandler:
def handle_move(self, token, x, y):
# do something
user = User(token)
(user.location.x, user.location.y) = (x, y);
user.time = time.time()
user.save()
if user.time < time.time():
(user.location.x, user.location.y) = (x, y);
user.time = int(time.time())+User.MOVE_TIME*(abs(int(user.location.x)-x)+abs(int(user.location.y)-y))
user.save()
data = json.dumps({'status': 'ok', 'time': user.time})
else:
data = json.dumps({'status': 'fail', 'description': 'need time'+str(user.time-time.time())})
data = json.dumps({'status': 'ok', 'time': user.time})
try:
self._socketio.write_to_socket(data.encode('ascii'))
except Exception as msg:
......@@ -197,13 +209,16 @@ class ConnectionHandler:
user = User(token)
map = Map.get_map('Bandar Behari')
if (user.location.x == user.prev_location.x) and (user.location.y == user.prev_location.y):
data = json.dumps({'status': 'fail','description': 'FAILURE MESSAGE HERE"'})
data = json.dumps({'status': 'fail','description': 'you have field here"'})
else:
new_item = map.get_item(user.location.x, user.location.y)
user.inventory.items[new_item] += 1;
(user.prev_location.x, user.prev_location.y) = (user.location.x, user.location.y)
user.save()
data = json.dumps({'status': 'ok', 'item': new_item})
if (user.time > time.time()):
data = json.dumps({'status': 'fail', 'description': 'need time'+str(user.time-time.time())})
else:
new_item = map.get_item(user.location.x, user.location.y)
user.inventory.items[new_item] += 1;
(user.prev_location.x, user.prev_location.y) = (user.location.x, user.location.y)
user.save()
data = json.dumps({'status': 'ok', 'item': new_item})
try:
self._socketio.write_to_socket(data.encode('ascii'))
except Exception as msg:
......@@ -260,8 +275,6 @@ class ConnectionHandler:
for thr in thread_list:
# print(thr.get_result())
offer_list.extend(thr.get_result())
print('data')
print(offer_list)
offer_list = Market.get_offers(item)
my_offers = Tradebox.get_offers(token)
......@@ -281,7 +294,7 @@ class ConnectionHandler:
raw_offer_list.append([offer.offered_id, offer.offered_amount, offer.demanded_id, offer.demanded_amount,
offer.availability, offer.offer_token])
data = json.dumps({'status':'ok','offers': raw_offer_list})
data = json.dumps({'status': 'ok','offers': raw_offer_list})
try:
self._socketio.write_to_socket(data.encode('ascii'))
except Exception as msg:
......@@ -309,11 +322,14 @@ class ConnectionHandler:
# invoke accept
def handle_accept(self,offer_token):
data = ''
offer = Tradebox.find_offer_id(offer_token)
if offer.availability:
offer.availability = False
offer.save()
data = json.dumps({'status': 'ok'})
if (Tradebox.exist(offer_token)):
offer = Tradebox.find_offer_id(offer_token)
if offer.availability:
offer.availability = False
offer.save()
data = json.dumps({'status': 'ok'})
else:
data = json.dumps({'status': 'fail','description': 'offer is not available'})
else:
data = json.dumps({'status': 'fail','description': 'offer is not available'})
......@@ -329,7 +345,6 @@ class ConnectionHandler:
if offer.availability:
Market.accept(offer_token)
# ini bagian yafi
# call accept
(socket,host,port) = (Socket.socket(), offer.ip, offer.port)
......@@ -340,7 +355,7 @@ class ConnectionHandler:
print(msg)
socketio = SocketIO(socket)
socketio.write_to_socket(json.dumps({'method':'accept','offer_token':offer.token}).encode('ascii'))
socketio.write_to_socket(json.dumps({'method': 'accept', 'offer_token': offer.offer_token}).encode('ascii'))
target_data = socketio.read_from_socket()
self._socketio.write_to_socket(target_data)
......@@ -351,7 +366,7 @@ class ConnectionHandler:
# invoke map
def handle_fetchitem(self,token,offer_token):
offer = Tradebox.find_offer_id(offer_token)
if (offer.user_id == token) and (offer.availability == True):
if (offer.user_id == token) and (offer.availability):
user = User(token)
user.inventory.items[offer.demanded_id] += offer.demanded_amount
user.save()
......
......@@ -55,6 +55,10 @@ class Tradebox():
_offer = Offer(Tradebox.__market.find_one({'_id': ObjectId(token)}))
return _offer
@staticmethod
def exist(token):
return Tradebox.__market.find_one({'_id': ObjectId(token)}) != None
if __name__ == '__main__':
# MongoClient('localhost', 27017)['sister'].create_collection('tradebox')
tb = Singleton(Tradebox())
......
......@@ -15,6 +15,7 @@ class Location:
class User:
MOVE_TIME = 1
__users = MongoClient('localhost', 27017)['sister'].get_collection('users')
def __init__(self, id):
......
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