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