def dispatch(messageJson=None): """ dispatch is the microservice dispatcher for IndigoGirls, a 2048-like game. It routes requests for game state transformations to the appropriate functions :param messageJson: JSON string that describes the state of the game needed for the requested transformation :return: A JSON string that describes the state of the game after the requested transformation has taken place. """ def buildErrorString(diagnostic=None): """ returns a dictionary containing the specified key and accompanying diagnostic information :param diagnostic: A string that describes the error :return: A dictionary that contains the specified error key having a value that consists of the specfied error string followed by a free-form diagnostic message """ ERROR_PROPERTY = u'gameStatus' ERROR_PREFIX = u'error: ' return {ERROR_PROPERTY: ERROR_PREFIX + diagnostic} #Validate JSONness of input be converting the string to an equivalent dictionary try: messageDictionary = json.loads(messageJson) except: resultDictionary = json.dumps( buildErrorString('input JSON string is invalid')) return resultDictionary #Validate presence of dispatching code if (u"op" not in messageDictionary): resultDictionary = json.dumps(buildErrorString('op is missing')) return resultDictionary #Perform the game transformation as directed by the value of the "op" key # input to each function: a dictionary containing the name-value pairs of the input JSON string # output of each function: a dictionary containing name-value pairs to be encoded as a JSON string if (messageDictionary[u"op"] == u"initializeGame"): resultDictionary = initializeGame(messageDictionary) elif (messageDictionary[u"op"] == u"swipe"): resultDictionary = swipe(messageDictionary) elif (messageDictionary[u"op"] == u"recommend"): resultDictionary = recommend(messageDictionary) elif (messageDictionary[u"op"] == u"status"): resultDictionary = status(messageDictionary) elif (messageDictionary[u"op"] == u"predict"): resultDictionary = predict(messageDictionary) else: resultDictionary = buildErrorString('op is invalid') #Covert the dictionary back to a string in JSON format resultJson = json.dumps(resultDictionary) return resultJson
def test_swipe_ValidateRow02(self): columnCount = 2 rowCount = 101 grid = [0] * 2 direction = "up" board = { "columnCount": columnCount, "rowCount": rowCount, "grid": grid } input = {"board": board, "direction": direction} output = swipe(input) self.assertIn("error", output["gameStatus"])
def test_swipe_Unmovable01(self): columnCount = 2 rowCount = 2 grid = [1, 0, 4, 0] direction = "left" board = { "columnCount": columnCount, "rowCount": rowCount, "grid": grid } input = {"board": board, "direction": direction} output = swipe(input) self.assertIn("error", output["gameStatus"])
def test_swipe_invalid_grid02(self): columnCount = 3 rowCount = 2 grid = [1, 2, 3, 4, 5, 0, 0, 3, 4, 5, 3, 2] direction = "up" board = { "columnCount": columnCount, "rowCount": rowCount, "grid": grid } input = {"board": board, "direction": direction} output = swipe(input) self.assertIn("error", output["gameStatus"])
def test_swipe_Normal05(self): columnCount = 4 rowCount = 2 grid = [0, 1, 1, 1, 0, 0, 0, 0] direction = "left" board = { "columnCount": columnCount, "rowCount": rowCount, "grid": grid } input = {"board": board, "direction": direction} output = swipe(input) grid = output["board"]["grid"] self.assertEqual(grid[0], 2) self.assertEqual(grid[1], 1) self.assertEqual(output["score"], 4)
def test_swipe_Normal04(self): columnCount = 2 rowCount = 2 grid = [1, 1, 1, 1] direction = "down" board = { "columnCount": columnCount, "rowCount": rowCount, "grid": grid } input = {"board": board, "direction": direction} output = swipe(input) grid = output["board"]["grid"] self.assertEquals(grid[3], 2) self.assertEquals(grid[2], 2) self.assertEquals(output["score"], 8)
def test_swipe_Normal02(self): columnCount = 2 rowCount = 2 grid = [1, 0, 1, 0] direction = "right" board = { "columnCount": columnCount, "rowCount": rowCount, "grid": grid } input = {"board": board, "direction": direction} output = swipe(input) grid = output["board"]["grid"] self.assertEquals(grid[1], 1) self.assertEquals(grid[3], 1) self.assertEquals(output["score"], 0) self.assertTrue(grid[0] != 0 or grid[2] != 0)