Commit 9ef63d65 authored by mamat-rahmat's avatar mamat-rahmat
Browse files
parents 23df95bc 56881e9f
...@@ -472,7 +472,8 @@ def playserver(portLaptop): ...@@ -472,7 +472,8 @@ def playserver(portLaptop):
#update database for player_time #update database for player_time
# database.playerCollection.update(kriteriaObject, {"$set":{"player_time":time}}) # database.playerCollection.update(kriteriaObject, {"$set":{"player_time":time}})
response = '{"status" : "ok", "time" : delta_milisecond}' #response = '{"status" : "ok", "time" : delta_milisecond}'
response = '{"status" : "ok", "time" : 1}'
#else: #else:
# response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}' # response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
elif (method == 'field'): elif (method == 'field'):
......
...@@ -4,7 +4,10 @@ import hashlib ...@@ -4,7 +4,10 @@ import hashlib
import pymongo import pymongo
import threading import threading
from pymongo import MongoClient from pymongo import MongoClient
<<<<<<< HEAD
=======
import datetime import datetime
>>>>>>> 2e1c3f425d37b26acefb8d5cf9ad75f7635538c1
from random import randint from random import randint
# informasi port dan socket # informasi port dan socket
...@@ -255,6 +258,13 @@ def handler(a, c): ...@@ -255,6 +258,13 @@ def handler(a, c):
else: else:
response = '{"status" : "fail", "description" : "still available"}' response = '{"status" : "fail", "description" : "still available"}'
else: else:
<<<<<<< HEAD
response = "{'status' : 'fail','description':'not exists'}"
elif (method == "map"):
#tokem from client
token = dataJSON['token']
=======
response = '{"status" : "fail", "description" : "not exists"}' response = '{"status" : "fail", "description" : "not exists"}'
elif (method == "move"): elif (method == "move"):
...@@ -263,49 +273,97 @@ def handler(a, c): ...@@ -263,49 +273,97 @@ def handler(a, c):
posisiXnew = dataJSON["posisiX"] posisiXnew = dataJSON["posisiX"]
posisiYnew = dataJSON["posisiY"] posisiYnew = dataJSON["posisiY"]
>>>>>>> 2e1c3f425d37b26acefb8d5cf9ad75f7635538c1
#from database #from database
user = database.playerCollection.find(token) kriteriaObject = {"token" : token}
width = 5 #user["width"] user = database.playerCollection.find_one(kriteriaObject)
height = 5 #user["height"] username = user['username']
posisiX = user["posisiX"]
posisiY = user["posisiY"]
#datetime #write json file
current_time = datetime.datetime.now() data={
move_time = 1 'map' :
delta_second = (move_time * (abs(posisiXnew-posisiX) + abs(posisiYnew-posisiY))) [
delta_milisecond = delta_second * 1000 [randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9)],
player_time = current_time + datetime.timedelta(seconds=delta_second) [randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9)],
[randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9)],
[randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9)],
[randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9)]
],
'height' : 5,
'width' : 5,
'name' : username
}
map = json.dumps(data,
indent=4, separators=(',', ': '))
with open('map', 'w') as f:
json.dump(data, f,
indent=4, separators=(',', ': '))
response = '{"status" : "ok", "name" : "' + username + '", "width" : 5, "height" : 5}'
elif (method == 'move'):
#from client
token = dataJSON['token']
posisiXnew = dataJSON['posisiX']
posisiYnew = dataJSON['posisiY']
#from database
kriteriaObject = {'token' : token}
user = database.playerCollection.find_one(kriteriaObject)
width = 5
height = 5
posisiX = user['posisiX']
posisiY = user['posisiY']
#update database for player_time
database.playerCollection.update(kriteriaObject, {"$set" : {"player_time" : player_time}})
#comparing datetime #comparing datetime
if (current_time > player_time): if (current_time > time):
#comparing position #comparing position
if ((posisiX >=0) and (posisiY >= 0) and (posisiXnew < 5) and (posisiYnew < 5)): if ((posisiX >=0) and (posisiY >= 0) and (posisiXnew < 5) and (posisiYnew < 5)):
#datetime
current_time = datetime.datetime.now()
move_time = 1
delta_second = (move_time * (abs(posisiXnew-posisiX) + abs(posisiYnew-posisiY)))
delta_milisecond = delta_second * 1000
time = current_time + datetime.timedelta(seconds=delta_second)
#update database for player_time
database.playerCollection.update(kriteriaObject, {"$set":{"player_time":time}})
response = '{"status" : "ok", "time" : delta_milisecond}' response = '{"status" : "ok", "time" : delta_milisecond}'
else: else:
response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}' response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
elif (method == 'field'):
elif (method == "field"):
#from client #from client
token = dataJSON["token"] token = dataJSON['token']
posisiX = dataJSON["posisiX"] posisiX = dataJSON['posisiX']
posisiY = dataJSON["posisiY"] posisiY = dataJSON['posisiY']
#Datetime
current_time = datetime.datetime.now()
kriteriaObject = {'token' : token}
user = database.playerCollection.find_one(kriteriaObject)
time = user['player_time']
#from map.json #from map.json
with open('map', 'r') as f: with open('map', 'r') as f:
field_item = json.load(f) field_item = json.load(f)
item_here = field_item["map"][posisiY][posisiX] item_here = field_item["map"][posisiY][posisiX]
#item
name_item_here = getItemName(item_here)
inventori = database.inventoriCollection.find_one(kriteriaObject)
n_item = inventori[name_item_here]
#update database inventory #update database inventory
kriteriaObject = {"token":token} kriteriaObject = {"token":token}
if (current_time >= player_time):
new_n_item += 1; if (current_time >= time):
database.inventoriCollection.update(kriteriaObject, {"$set" : {getItemName(item_here) : new_n_item}}) new_n_item = n_item+1;
response = '{"status" : "ok", "item" : 2}' database.inventoriCollection.update(kriteriaObject, {"$set":name_item_here:new_n_item}})
response = '{"status" : "ok", "item" : item_here}'
else: else:
<<<<<<< HEAD
response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
=======
response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}' response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
elif (method == "map"): elif (method == "map"):
...@@ -347,6 +405,7 @@ def handler(a, c): ...@@ -347,6 +405,7 @@ def handler(a, c):
response = '{"status" : "ok", "name" : "' + username + '", "width" : 5, "height" : 5}' response = '{"status" : "ok", "name" : "' + username + '", "width" : 5, "height" : 5}'
>>>>>>> 2e1c3f425d37b26acefb8d5cf9ad75f7635538c1
elif (method == "offer"): elif (method == "offer"):
token = dataJSON["token"] token = dataJSON["token"]
offered_item = dataJSON["offered_item"] offered_item = dataJSON["offered_item"]
......
{ {
"height": 5,
"name": "pulpen",
"map": [ "map": [
[ [
<<<<<<< HEAD
3,
7,
9,
8,
5
],
[
0,
6,
9,
8,
3
],
[
3,
8,
9,
0,
2
],
[
2,
1,
9,
1,
5
],
[
2,
9,
7,
2,
7
]
],
"width": 5
=======
7, 7,
7, 7,
7, 7,
...@@ -39,4 +79,5 @@ ...@@ -39,4 +79,5 @@
"height": 5, "height": 5,
"width": 5, "width": 5,
"name": "tujuh" "name": "tujuh"
>>>>>>> 2e1c3f425d37b26acefb8d5cf9ad75f7635538c1
} }
\ 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