#!/usr/bin/env python3

import socket
import json

# next create a socket object 
s = socket.socket()          
print ("Socket successfully created")
  
# reserve a port on your computer in our 
# case it is 12345 but it can be anything 
port = 49155                
  
# Next bind to the port 
# we have not typed any ip in the ip field 
# instead we have inputted an empty string 
# this makes the server listen to requests  
# coming from other computers on the network 
s.bind(('192.168.1.120', port))         
print ("socket binded to %s" %(port))
  
# put the socket into listening mode 
s.listen(5)      
print ("socket is listening")            
  
# a forever loop until we interrupt it or  
# an error occurs 
  
while True: 
  
   # Establish connection with client. 
   c, addr = s.accept()      
   print ('Got connection from', addr )

   data = c.recv(1024)
   y = json.loads(data)
   user_position = y["game"]

   f = open("behavior_1/status.txt", "w")
   f.write(y["game"])
   f.close()

   print(user_position)
   #print(y["game"]) 

   # Close the connection with the client 
   c.close() 