Skip to content

Instantly share code, notes, and snippets.

@ebith
Created September 12, 2014 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebith/9a5236b5fd717b24ebf0 to your computer and use it in GitHub Desktop.
Save ebith/9a5236b5fd717b24ebf0 to your computer and use it in GitHub Desktop.
Battlefield4のフレンドがゲームを開始したらHubotで通知する
# Description:
# Battlefield4のフレンドがゲームを開始したら通知する
#
# Dependencies:
# "request": "^2.42.0"
# "ws": "^0.4.32"
#
# Configuration:
# HUBOT_ORIGIN_EMAIL
# HUBOT_ORIGIN_PASS
# HUBOT_BF4FRIENDS_CHANNEL
#
# Author:
# ebith
module.exports = (robot) ->
request = require 'request'
jar = request.jar()
Friends = {}
do connect = ->
options =
url: 'https://battlelog.battlefield.com/bf4/ja/gate/login/'
form:
email: process.env.HUBOT_ORIGIN_EMAIL
password: process.env.HUBOT_ORIGIN_PASS
submit: 'ログイン'
jar: jar
followAllRedirects: true
request.post options, (err, res, body) ->
if res.statusCode isnt 200 then return
[userId, token] = body.match(/http:\/\/beaconpush1\.battlelog\.com\/session', 'prod', '(\S+)', '(\S+)', \{ debug: false \}/)[1..2]
Friends[userId] = name: body.match(new RegExp("rel=\"#{userId}\">(\\S+)</a>"))[1]
request.get 'https://battlelog.battlefield.com/bf4/ja/comcenter/initComcenter/', jar:jar, (err, res, body) ->
if res.statusCode isnt 200 then return
Friends[friend.userId] = name: friend.username for friend in JSON.parse(body).data.context.friendscomcenter
ws = new (require 'ws') "ws://beaconpush1.battlelog.com/session/#{do threeDigitNums}/#{do eightDigitStrs}/websocket"
ws.on 'open', ->
ws.on 'close', (code, message) ->
console.log code, message
do connect
ws.on 'message', (data, flag) ->
switch data.slice(0, 1)
when 'o'
ws.send stringify [
'handshake-request'
{
operatorId: 'prod'
userId: userId
token: token
clientVersion: '1'
}
]
when 'a'
data = JSON.parse(JSON.parse(data.slice(1)))
if data[0] is 'handshake-accept'
ws.send stringify [
'set-subscriptions-request'
{
subscriptions: [
'web'
]
}
]
if data[0] is 'message'
msg = JSON.parse data[1].message
if msg.data.playingMp and msg.data.presenceStates is '265' and ((Friends[msg.data.userId].updatedAt ? 0) + 300) < msg.data.updatedAt
Friends[msg.data.userId].updatedAt = msg.data.updatedAt
robot.send room: process.env.HUBOT_BF4FRIENDS_CHANNEL, "[BF4]#{Friends[msg.data.userId].name}が#{msg.data.playingMp.serverName}で戦闘中!"
# when 'm', 'c', 'h'
threeDigitNums = -> Math.floor(Math.random() * (1000 - 100) + 100)
eightDigitStrs = -> ('abcdefghijklmnopqrstuvwxyz0123456789'[Math.floor(Math.random() * 36)] for i in [1..8]).join ''
stringify = (obj) -> "[\"#{JSON.stringify(obj).replace(/"/g, '\\"')}\"]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment