Skip to content

Commit

Permalink
Logs for server
Browse files Browse the repository at this point in the history
  • Loading branch information
codedecde committed Oct 14, 2016
1 parent 7762ee6 commit f977f86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
18 changes: 11 additions & 7 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Client(Communicator):
def __init__(self):
self.GAME_TIMER = 100000 # in Milli Seconds
self.NETWORK_TIMER = 500
self.NETWORK_TIMER = 150
super(Client,self).__init__()
pass

Expand Down Expand Up @@ -136,7 +136,7 @@ def RecvDataFromServer(self):
if(data['action'] == 'NORMAL' or data['action'] == 'INIT'):
retData = data['data']
elif(data['action'] == 'KILLPROC'):
print 'ERROR : ' + data['meta'] + ' ON OTHER CLIENT'
print 'ERROR : ' + data['meta'] + ' ,ON OTHER CLIENT'
super(Client,self).closeChildProcess()
super(Client,self).closeSocket()
elif(data['action'] == 'FINISH'):
Expand Down Expand Up @@ -183,7 +183,8 @@ def RecvDataFromProcess(self):
if(self.GAME_TIMER > 0):
retData = {'meta':'','action':'NORMAL','data':data}
else:
retData = {'meta':'TIMEOUT','action':'KILLPROC','data':''}
retData = {'meta':'TIMEOUT','action':'KILLPROC','data':''}
print 'ERROR : THIS CLIENT STOPPED UNEXPECTEDLY OR TIMED OUT'
return retData


Expand Down Expand Up @@ -239,37 +240,40 @@ def game_loop(game, args):
while(True):
move = client.RecvDataFromProcess()
if move['action'] == 'KILLPROC':
move['meta'] = move['meta'] + ' BY PLAYER ' + player_id
client.SendData2Server(move)
break
move['data'] = move['data'].strip()
print "You played " + move['data']
success = game.execute_move(move['data'])
message = {}
if success == 0:
# TODO : DECIDE THE SCORING FOR THIS CASE
message['data'] = ''
message['action'] = 'KILLPROC'
message['meta'] = 'INVALID MOVE'
message['meta'] = 'INVALID MOVE BY PLAYER ' + player_id
print 'INVALID MOVE ON THIS CLIENT'
elif success == 2 or success == 3 or success == 4:
# 2 : Player 1 wins
# 3 : Player 2 wins
# 4 : Game Drawn
score = "(" + str(game.calculate_score(0) ) + "," + str(game.calculate_score(1) ) + ")"
message['action'] = 'FINISH'
message['data'] = move['data']
if success == 2:
message['meta'] = '1 wins'
message['meta'] = '1 wins WITH SCORE : '+score
if(player_id == '1'):
print 'YOU WIN!'
else:
print 'YOU LOSE :('
elif success == 3:
message['meta'] = '2 wins'
message['meta'] = '2 wins WITH SCORE : '+score
if(player_id == '2'):
print 'YOU WIN!'
else:
print 'YOU LOSE :('
else:
message['meta'] = 'Game Drawn'
message['meta'] = 'Game Drawn WITH SCORE : '+score
print 'GAME DRAWN'
elif success == 1:
message = move
Expand Down
13 changes: 12 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ def __init__(self):
"""
self.communicator_list = []
self.NETWORK_TIMER = 150

self.log_file_handle = None

def setLogFile(self, filename):
self.log_file_handle = open(filename,'wb')

def BuildServer(self,port_no,num_clients):
"""Builds The server on the port_number port_no for num_clients
Args:
Expand Down Expand Up @@ -149,6 +153,8 @@ def playTak(self,n,timelimit,client_0,client_1):
print data, 'Received from client 0'
data = json.loads(data)
if data['action'] == 'FINISH' or data['action'] == 'KILLPROC':
if not self.log_file_handle is None:
self.log_file_handle.write(data['meta'])
break
data = self.RecvDataFromClient(client_1)
print data, 'Received from client 1'
Expand All @@ -157,6 +163,8 @@ def playTak(self,n,timelimit,client_0,client_1):
break
data = json.loads(data)
if data['action'] == 'FINISH' or data['action'] == 'KILLPROC':
if not self.log_file_handle is None:
self.log_file_handle.write(data['meta'])
break
self.CloseClient(client_0)
self.CloseClient(client_1)
Expand All @@ -172,10 +180,13 @@ def playTak(self,n,timelimit,client_0,client_1):
parser.add_argument('-n', dest = 'n', metavar = 'N', type = int, default = 5, help = 'Tak board size')
parser.add_argument('-NC', dest = 'num_clients', metavar = 'num_clients', type = int, default = 2, help = 'Number of clients connecting to the server')
parser.add_argument('-TL', dest = 'time_limit', metavar = 'time_limit', type = int, default = 120, help = 'Time limit (in s)')
parser.add_argument('-LOG',dest = 'log_file', metavar = 'log_file', type = str, default = "", help = 'Logger File for Evaluation purposes')
args = parser.parse_args()
if args.n < 5 or args.n > 7:
print 'Game size should be 5x5, 6x6 or 7x7.'
sys.exit()
if args.log_file != '':
local_Server.setLogFile(args.log_file)
local_Server.BuildServer(args.port, args.num_clients)
if(local_Server.client_count < 2):
local_Server.SendInitError2Clients()
Expand Down

0 comments on commit f977f86

Please sign in to comment.