Skip to content

Instantly share code, notes, and snippets.

@esehara
Last active April 11, 2016 07:08
Show Gist options
  • Save esehara/5040e6dd3aa9d1dc82270a9d1045206b to your computer and use it in GitHub Desktop.
Save esehara/5040e6dd3aa9d1dc82270a9d1045206b to your computer and use it in GitHub Desktop.
某ソシャゲSNSのためのお役立ちスクリプト
#!/usr/bin/env ruby
require 'date'
require 'redis'
require 'ncursesw'
#-------------------
# Initiate Ncurses
#-------------------
Ncurses.initscr
Ncurses.cbreak
Ncurses.noecho
Ncurses.nonl
###
# Template Method
###
class Coin
def initialize
@redis = Redis.new
end
public
def print
notify("(#{count})" + gage(count))
curses_print template
end
def curses_print message
Ncurses.printw(message + "\n")
Ncurses.refresh
end
def notify message
system("notify-send '討伐コイン' '#{message}' -t 50000 -i /home/esehara/Archive/etc/126.gif")
end
def count
@redis.get('genmono_life').to_i
end
def count= life
if valid? life
@redis.set('genmono_life', life)
end
end
private
def valid? life
life >= 0 && life <= 5
end
def gage count
"●" * count
end
def template
"討伐コイン(#{count}): #{gage count}"
end
end
$coin = Coin.new
if ARGV[0]
$coin.count = ARGV[0].to_i
end
runtime = DateTime.now - 1
###
# Thread Running...
###
Thread.abort_on_exception = true
Thread.new do
while ch = Ncurses.getch
life = $coin.count
case ch
when 48
life = 0
when 49
life = 1
when 50
life = 2
when 51
life = 3
when 52
life = 4
when 68
life -= 1
when 67
life += 1
end
if life != $coin.count
$coin.count = life
$coin.print
end
end
end
###
# Main Process
###
while true
begin
if runtime + Rational(10, 24 * 60) < DateTime.now
$coin.print
runtime = DateTime.now
$coin.count = $coin.count + 1
end
sleep 100
rescue Interrupt
Ncurses.echo
Ncurses.nocbreak
Ncurses.nl
Ncurses.endwin()
exit 0
end
end
#! /usr/bin/env ruby
require 'net/http'
require 'json'
def url_to_api
url =
if ARGV[0].match('genmono2')
ARGV[0].sub(/game\/genmono2/, 'api')
else
ARGV[0].sub(/group/, 'api/group')
end.sub(/chat/, 'chats')
reply_to = url.match('\d+$')
url = url.sub(/\d+$/,'')
return url + "replies?to=#{reply_to}&error_flavor=json2"
end
def get_from_url
ids = []
url = url_to_api
puts url
while true
j = Net::HTTP.get URI.parse(url)
chats = JSON.parse(j)["chats"]
chats.reverse.each do |message|
if !ids.include? message["id"]
system("notify-send '#{message["user"]["name"]}' '#{message["message"]}' -t 8000")
ids.push message["id"]
end
exit if chats.size > 99
sleep 0.5
end
end
end
get_from_url
#! /usr/bin/env ruby
require 'net/http'
require 'json'
require 'time'
require 'colorize'
ICON_DIR = '/home/esehara/Archive/etc/'
class LobiGroup
def initialize(title, url, icon, color)
@title = title
@url = url
@icon = icon
@color = color
@thread_id = 0
end
def url
@url.sub(/game\/genmono2/, 'api')
.sub(/chat/, 'chats') + "/chats?count=1"
end
def thread_url
return @url + "/chat/" + @thread_id
end
def json_from_url
j = Net::HTTP.get URI.parse(url)
chats = JSON.parse(j)
return chats[0]
end
def to_console
temp = <<EOS
----------------------
#{@message.bold.sub(/'/, "\'")}
#{thread_url}
#{Time.now}
EOS
puts temp.colorize(@color)
end
def to_notify
send = "notify-send '【#{@title}】' '<a href=\"#{thread_url}\">■</a>\n#{@message}' -i #{ICON_DIR + @icon}"
system(send)
end
def show
chat = json_from_url
return if chat.nil?
if @thread_id != chat["id"]
@thread_id = chat["id"]
@message = chat["message"]
to_console
to_notify
end
end
end
rooms = []
room_data =
[
["助け合い",
"https://web.lobi.co/game/genmono2/group/707a385083975a304c07b1de9b61af6bec3609d4",
"021.gif",
:light_green],
["討伐",
"https://web.lobi.co/game/genmono2/group/b1c0ad2a864b010435b23b90b74bfba0da45497b",
"001.gif",
:light_blue],
["レベルアップ支援",
"https://web.lobi.co/game/genmono2/group/ff4d62471301ff5eda8c343f8e1692239f655beb",
"053.gif",
:light_red]
]
room_data.each do |title, url, icon, color|
rooms << LobiGroup.new(title, url, icon, color)
end
while true
rooms.each do |room|
begin
sleep 10
room.show
rescue SocketError
next
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment