Building a Card Playing Twitter Bot: gameplay dialog

I’ve built a couple of other Twitter bots, I have @kevinhookebot which generates random tweets generated from a trained ML model:

and I have a product name generator which generates humorous product names using the Tracery template library:

For my next project I’m thinking about what it would involve to build a multiplayer card game playing bot, a simple card game to get started, like Blackjack. The Twitter REST apis I’ve used so far will be reusable for this project, but the interesting parts are the interaction between a player and the bot, the game logic, and the persistence of game state (each of which I’ll discuss in future posts).

I’ve been thinking about the interaction for the gamevand think it will look something like this:

Player: @blackjackcard deal

Bot: @player bot deals you 4 Clubs and 7 Spades. Reply hit or stick

Player: @blackjackcard hit

Bot: @player bot deals you 4 Hearts. You now have 4 Clubs, 7 Spades, 4 Hearts. Reply hit or stick

Player: @blackjackcard stick

Bot: @player the bot currently has 3 Hearts, 9 Clubs, and takes a card

Bot: @player the bot takes 10 Diamonds and now has 3 Hearts, 9 Clubs, 10 Diamonds. Bust! You win!

The interesting part of the gameplay interaction is that there’s only 3 commands:

  • deal: start a game (get dealt your initial two cards)
  • hit: get dealt another card
  • stick: keep current hard

This makes the options that the bot needs to handle pretty simple. Next up, I’ll talk about persisting the game state to AWS DynamoDB.

AWS Lambda error: HTTP 502 ‘Execution failed due to configuration error: Malformed Lambda proxy response’

Calling an AWS Lambda via API Gateway with the Lambda Proxy Integration option, you might see an HTTP 502 response and this message:

Execution failed due to configuration error: Malformed Lambda proxy response"

Wed May 30 05:00:41 UTC 2018 : Execution failed due to configuration error: Malformed Lambda proxy response
Wed May 30 05:00:41 UTC 2018 : Method completed with status: 502

This is a rather cryptic message, but  it’s saying is the response is not in the expected format.

Per this doc, the expected response should be in this format:

{
    "isBase64Encoded": true|false,
    "statusCode": httpStatusCode,
    "headers": { "headerName": "headerValue", ... },
    "body": "..."
}

AWS Lambda node.js template

The node.js based implementation of AWS Lambdas has the following structure:

Using ES6 fat arrow syntax:

exports.handlerName = (event, context, callback) => {
  callback(error, result);
}

where error is either null for a successful execution, or an error, and result is a response to return to your caller.

Using ES5 syntax:

exports.handlerName = function(event, context, callback) {
  callback(error, result);
}

For more info, see here.

Game development in progress: Space Invaders clone – update 3 (18 months later)

It’s been a year and a half since I’ve given an update on my Space Invaders clone on Android. Admittedly I haven’t been working continuously in my spare time on this project for 18 months, in fact the last time I remember working on it was several months back. What I got stuck on was updating the animation routines so the animation would display with a constant frame rate across different Android devices, and the display of the sprites to look consistent for devices with different sized screens, resolutions and pixel densities.

If you’re a seasoned game developer this is probably nothing new to you. If you only develop for a platform with identical hardware specs (like a game console) then this is probably something you don’t have to worry (too much?) about. However with the broad range of specs for Android devices, even testing with the device emulator for different phones, it’s pretty obvious unless your code handles these differences, your game might be fast on one device but slow on another, or the sprite layout may look at intended at the resolution on one phone but be too small on another phone.

I spent a bunch of time reading game dev articles about implementing approaches for constant frame rates, and tried to incorporate what I’d learned, but still, I’ve got some weird quirks I need to iron out.

Here’s what the game looks like on an emulated Pixel:

I was initially developing and testing on the emulated Nexus devices as my baseline test target, but it runs fine, as expected on the Pixel too (it’s harder to play when pressing the buttons with the mouse!).

Now running on an emulated Pixel 2 XL, it runs fine for a while until the number of invaders gets down to where I speed up. The speed up is too fast, and then for some reason that I haven’t found yet they animation stops before the game ends but the game is still playing:
Clearly I’ve still got some work to do here, but it’s getting close.