Commit 58957293 authored by opelhoward's avatar opelhoward
Browse files

fix issue #1

parent e15cef56
......@@ -284,14 +284,14 @@
<Component class="javax.swing.JSpinner" name="n1_textfield">
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignAbsoluteLayout" value="org.netbeans.modules.form.compat2.layouts.DesignAbsoluteLayout$AbsoluteConstraintsDescription">
<AbsoluteConstraints x="510" y="440" width="150" height="30"/>
<AbsoluteConstraints x="170" y="440" width="150" height="30"/>
</Constraint>
</Constraints>
</Component>
<Component class="javax.swing.JSpinner" name="n2_textfield">
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignAbsoluteLayout" value="org.netbeans.modules.form.compat2.layouts.DesignAbsoluteLayout$AbsoluteConstraintsDescription">
<AbsoluteConstraints x="170" y="440" width="150" height="30"/>
<AbsoluteConstraints x="510" y="440" width="150" height="30"/>
</Constraint>
</Constraints>
</Component>
......
......@@ -121,7 +121,7 @@ public class OfferItemScreen extends javax.swing.JFrame {
item1_label.setText("item");
getContentPane().add(item1_label);
item1_label.setBounds(130, 400, 25, 16);
item1_label.setBounds(130, 400, 20, 14);
item1_combobox.setMaximumRowCount(5);
item1_combobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--- Select an Item ---", "Honey", "Herbs", "Clay", "Mineral", "Potion", "Incense", "Gems", "Life Elixir", "Mana Crystal", "Philosopher Stone" }));
......@@ -131,15 +131,15 @@ public class OfferItemScreen extends javax.swing.JFrame {
}
});
getContentPane().add(item1_combobox);
item1_combobox.setBounds(170, 400, 150, 25);
item1_combobox.setBounds(170, 400, 150, 20);
n1_label.setText("Quantity");
getContentPane().add(n1_label);
n1_label.setBounds(110, 440, 70, 16);
n1_label.setBounds(110, 440, 70, 14);
item2_label.setText("Item");
getContentPane().add(item2_label);
item2_label.setBounds(470, 400, 25, 16);
item2_label.setBounds(470, 400, 22, 14);
item2_combobox.setMaximumRowCount(5);
item2_combobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--- Select an Item ---", "Honey", "Herbs", "Clay", "Mineral", "Potion", "Incense", "Gems", "Life Elixir", "Mana Crystal", "Philosopher Stone" }));
......@@ -149,11 +149,11 @@ public class OfferItemScreen extends javax.swing.JFrame {
}
});
getContentPane().add(item2_combobox);
item2_combobox.setBounds(510, 400, 150, 25);
item2_combobox.setBounds(510, 400, 150, 20);
n2_label.setText("Quantity");
getContentPane().add(n2_label);
n2_label.setBounds(450, 440, 60, 16);
n2_label.setBounds(450, 440, 60, 14);
save_button.setText("Save");
save_button.addActionListener(new java.awt.event.ActionListener() {
......@@ -162,7 +162,7 @@ public class OfferItemScreen extends javax.swing.JFrame {
}
});
getContentPane().add(save_button);
save_button.setBounds(590, 480, 70, 26);
save_button.setBounds(590, 480, 70, 23);
back_button.setText("Back");
back_button.addActionListener(new java.awt.event.ActionListener() {
......@@ -171,11 +171,11 @@ public class OfferItemScreen extends javax.swing.JFrame {
}
});
getContentPane().add(back_button);
back_button.setBounds(660, 40, 81, 26);
back_button.setBounds(660, 40, 81, 23);
getContentPane().add(n1_textfield);
n1_textfield.setBounds(510, 440, 150, 30);
n1_textfield.setBounds(170, 440, 150, 30);
getContentPane().add(n2_textfield);
n2_textfield.setBounds(170, 440, 150, 30);
n2_textfield.setBounds(510, 440, 150, 30);
background_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
background_label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/marketplace_background.png"))); // NOI18N
......
......@@ -24,11 +24,11 @@
</component>
<component name="FileEditorManager">
<leaf>
<file leaf-file-name="sconnectionhandler.py" pinned="false" current-in-tab="true">
<file leaf-file-name="sconnectionhandler.py" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/sconnectionhandler.py">
<provider selected="true" editor-type-id="text-editor">
<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" />
<state vertical-scroll-proportion="0.0" vertical-offset="5720" max-vertical-offset="8772">
<caret line="346" column="86" selection-start-line="346" selection-start-column="86" selection-end-line="346" selection-end-column="86" />
<folding>
<element signature="e#21#32#0" expanded="true" />
</folding>
......@@ -48,10 +48,10 @@
</provider>
</entry>
</file>
<file leaf-file-name="tradebox.py" pinned="false" current-in-tab="false">
<file leaf-file-name="tradebox.py" pinned="false" current-in-tab="true">
<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">
<state vertical-scroll-proportion="1.0552764" 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" />
......@@ -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="952">
<caret line="21" column="0" selection-start-line="21" selection-start-column="0" selection-end-line="21" selection-end-column="0" />
<state vertical-scroll-proportion="0.0" vertical-offset="554" max-vertical-offset="1020">
<caret line="55" column="15" selection-start-line="55" selection-start-column="15" selection-end-line="55" selection-end-column="15" />
<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="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" />
<state vertical-scroll-proportion="0.0" vertical-offset="605" max-vertical-offset="1207">
<caret line="63" column="32" selection-start-line="63" selection-start-column="32" selection-end-line="63" selection-end-column="32" />
<folding />
</state>
</provider>
......@@ -90,8 +90,8 @@
<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$/sconnectionhandler.py" />
<option value="$PROJECT_DIR$/market.py" />
</list>
</option>
</component>
......@@ -340,7 +340,7 @@
</component>
<component name="ToolWindowManager">
<frame x="-8" y="-8" width="1382" height="744" extended-state="6" />
<editor active="false" />
<editor active="true" />
<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" />
......@@ -652,24 +652,16 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/tradebox.py">
<entry file="file://$PROJECT_DIR$/sconnectionhandler.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" />
<state vertical-scroll-proportion="0.0" vertical-offset="5720" max-vertical-offset="8772">
<caret line="346" column="86" selection-start-line="346" selection-start-column="86" selection-end-line="346" selection-end-column="86" />
<folding>
<element signature="e#22#53#0" expanded="true" />
<element signature="e#21#32#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="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">
......@@ -682,18 +674,26 @@
</entry>
<entry file="file://$PROJECT_DIR$/user.py">
<provider selected="true" editor-type-id="text-editor">
<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" />
<state vertical-scroll-proportion="0.0" vertical-offset="605" max-vertical-offset="1207">
<caret line="63" column="32" selection-start-line="63" selection-start-column="32" selection-end-line="63" selection-end-column="32" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/sconnectionhandler.py">
<entry file="file://$PROJECT_DIR$/market.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="554" max-vertical-offset="1020">
<caret line="55" column="15" selection-start-line="55" selection-start-column="15" selection-end-line="55" selection-end-column="15" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/tradebox.py">
<provider selected="true" editor-type-id="text-editor">
<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" />
<state vertical-scroll-proportion="1.0552764" 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#21#32#0" expanded="true" />
<element signature="e#22#53#0" expanded="true" />
</folding>
</state>
</provider>
......
......@@ -49,4 +49,8 @@ class Market:
@staticmethod
def accept(token):
OfferMarket(Market.__market.find_one_and_replace({'offer_token': token}, {'availability': False}))
\ No newline at end of file
OfferMarket(Market.__market.find_one_and_replace({'offer_token': token}, {'availability': False}))
@staticmethod
def exist(token):
return Market.__market.find_one_and_replace({'offer_token': token}, {'availability': False}) != None
\ No newline at end of file
......@@ -313,6 +313,7 @@ class ConnectionHandler:
for offer in offers:
raw_offers.append([offer.offered_id, offer.offered_amount, offer.demanded_id, offer.demanded_amount,
offer.availability, str(offer.id)])
print(raw_offers)
data = json.dumps({'status': 'ok', 'offers': raw_offers})
try:
self._socketio.write_to_socket(data.encode('ascii'))
......@@ -341,26 +342,49 @@ class ConnectionHandler:
# invoke send accept
def handle_sendaccept(self, token, offer_token):
data = ''
offer = Market.find(offer_token)
if offer.availability:
Market.accept(offer_token)
# call accept
(socket,host,port) = (Socket.socket(), offer.ip, offer.port)
socket.settimeout(SocketIO.SERVER_CONNECTION_TIMEOUT)
try :
socket.connect((host,port))
except Exception as msg:
print(msg)
socketio = SocketIO(socket)
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)
if (not Market.exist(offer_token)):
data = json.dumps({'status': 'fail', 'description': 'offer cannot be found'})
else:
data = json.dumps({'status': 'fail', 'description': 'offer is not available'})
offer = Market.find(offer_token)
user = User(token)
if (offer.availability) and (offer.demanded_amount <= user.inventory.items[offer.demanded_id]):
Market.accept(offer_token)
# call accept
(socket,host,port) = (Socket.socket(), offer.ip, offer.port)
socket.settimeout(SocketIO.SERVER_CONNECTION_TIMEOUT)
try :
socket.connect((host,port))
except Exception as msg:
print(msg)
#buka koneksi ke server target (yang punya offer token tersebut)
socketio = SocketIO(socket)
socketio.write_to_socket(json.dumps({'method': 'accept', 'offer_token': offer.offer_token}).encode('ascii'))
#ambil response dari server target
target_data = socketio.read_from_socket()
target_response = json.loads(target_data.decode('utf-8'))
data = ''
#cek status dari server target
#jika ok, maka client akan menambahkan inventorynya
if (target_response.get('status') == 'ok'):
user.inventory.items[offer.demanded_id] -= offer.demanded_amount
user.inventory.items[offer.offered_id] += offer.offered_amount
data = (json.dumps({'status':'ok'})).encode('ascii')
#call db
elif (target_response.get('status') == 'fail'):
#fail
data = (json.dumps({'status':'fail','description':target_response.get('description')}).encode('ascii'))
else:
#error
data = (json.dumps({'status':'error'}).enocde('ascii'))
else:
data = json.dumps({'status': 'fail', 'description': 'offer is not available'})
self._socketio.write_to_socket(data)
# invoke map
......@@ -460,7 +484,6 @@ class FindOfferThread(Thread):
return
socketio = SocketIO(socket)
socketio.write_to_socket((json.dumps({'method':'findoffer','item':self._item})).encode('ascii'))
print('read offering from ',self._host,':',self._port)
#catch connection timeout
try:
data = socketio.read_from_socket()
......@@ -474,6 +497,7 @@ class FindOfferThread(Thread):
server_offer_list = response.get('offers',[])
Market.add(self._item, server_offer_list, self._host, self._port)
self._result = server_offer_list
print('read offering from ',self._host,':',self._port)
print(self._result)
except ValueError as ve:
return
......
......@@ -55,7 +55,7 @@ class User:
User.__users.insert_one({'username': username, 'password': password,
'location': {'x': 0, 'y': 0},
'prev_location': {'x': -1, 'y': -1},
'inventory': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'inventory': [1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000],
'time': 0})
@staticmethod
......
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