#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import requests
import re
from bot import RandomBot, SlowBot

def get_new_game_state(server_host, key, mode='training', number_of_turns = '10'):
    """Get a JSON from the server containing the current state of the game"""

    if(mode=='training'):
        params = { 'key': key, 'turns': number_of_turns}
        r = requests.post(server_host + '/api/training', params)

        if(r.status_code == 200):
            return r.json()
        else:
            print("Error when creating the game")
            print(r.text)
    elif(mode=='arena'):
        params = { 'key': key}
        r = requests.post(server_host + '/api/arena', params)

        if(r.status_code == 200):
            return r.json()
        else:
            print("Error when creating the game")
            print(r.text)

def move(url, direction):
    """Send a move to the server
    
    Moves can be one of: 'Stay', 'North', 'South', 'East', 'West' 
    """

    r = requests.post(url, {'dir': direction})
    if(r.status_code == 200):
        return r.json()
    else:
        print("Error HTTP %d\n%s\n" % (r.status_code, r.text))
        return {'game': {'finished': True}}

def start(server_host, key, mode, bot):
    """Starts a game with all the required parameters"""

    def play(state, games_played = 0):
        """Main game loop"""

        if (not state['game']['finished']):
            url = state['playUrl']
            direction = bot.move(state)
            new_state = move(url, direction)

            print("Playing turn %d with direction %s" % (state['game']['turn'], direction))
            play(new_state, games_played)

    if(mode=='arena'):
        print(u'Connected and waiting for other players to join…')

    state = get_new_game_state(server_host, key, mode)
    print("Start: " + state['viewUrl'])
    play(state)

if __name__ == "__main__":
    if (len(sys.argv) > 4):
        number_of_games = int(sys.argv[4])
        for i in range(number_of_games):
            start(sys.argv[1], sys.argv[2], sys.argv[3], RandomBot())
            print('Game finished: %d/%d' % (i+1, number_of_games))
    else:
        print("Usage: %s <server> <key> <[training|arena]> <number-of-games-to-play>" % (sys.argv[0]))
        print('Example: %s http://vindinium.jousse.org mySecretKey training 20' % (sys.argv[0]))

@@ -123,16 +123,28 @@
 lowBot%0A%0A
+TIMEOUT=15%0A%0A
 def get_
@@ -435,32 +435,49 @@
 raining', params
+, timeout=TIMEOUT
 )%0A%0A        if(r.
@@ -731,16 +731,33 @@
 , params
+, timeout=TIMEOUT
 )%0A%0A     
@@ -1040,24 +1040,37 @@
 ' %0A    %22%22%22%0A%0A
+    try:%0A    
     r = requ
@@ -1102,18 +1102,40 @@
 rection%7D
-)%0A
+, timeout=TIMEOUT)%0A%0A    
     if(r
@@ -1156,32 +1156,36 @@
 = 200):%0A        
+    
 return r.json()%0A
@@ -1184,30 +1184,38 @@
 .json()%0A    
+  
+  
 else:%0A
+    
         prin
@@ -1265,16 +1265,132 @@
 .text))%0A
+            return %7B'game': %7B'finished': True%7D%7D%0A    except requests.exceptions.RequestException, e:%0A        print e%0A
         
@@ -1426,16 +1426,17 @@
 True%7D%7D%0A%0A
+%0A
 def star
@@ -2324,17 +2324,19 @@
   print(
-'
+%22%5Cn
 Game fin
@@ -2347,17 +2347,17 @@
 d: %25d/%25d
-'
+%22
  %25 (i+1,
@@ -2413,17 +2413,8 @@
 : %25s
- %3Cserver%3E
  %3Cke
@@ -2460,16 +2460,29 @@
 to-play%3E
+ %5Bserver-url%5D
 %22 %25 (sys

Add timeouts and exception handling