Skip to content

Commit

Permalink
修改任务调度方式
Browse files Browse the repository at this point in the history
  • Loading branch information
heyunpeng committed Jul 21, 2015
1 parent bccdaa3 commit 66b7471
Show file tree
Hide file tree
Showing 3 changed files with 1,632 additions and 46 deletions.
72 changes: 26 additions & 46 deletions config/initializers/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,17 @@

#current task index
objindex = 0

max_same = 0
day_same = 0

max_color = 0
day_color = 0

max_order = 0
day_order = 0

max_small = 0
day_small = 0

max_normal = 0
day_normal = 0

max_big = 0
day_big = 0

#
left_count = 0
begin_count = 0

def randomGrid

object = []
Expand Down Expand Up @@ -180,41 +172,29 @@ def checkGrid (object)

s.cron '56 05 * * *', :first_at => Time.now + 1, :timeout => '30m' do

#get game config information
gridconfigs = Gridconfig.all
gridconfigs.each do |config|
if config.gridtype == 1
max_same = config.probability*90
elsif config.gridtype == 2
max_color = config.probability*90
elsif config.gridtype == 3
max_order = config.probability*90
elsif config.gridtype == 4
max_big = config.probability*90
elsif config.gridtype == 5
max_normal = config.probability*90
elsif config.gridtype == 6
max_small = config.probability*90
end
#left count of day
if Time.now.hour < 10
left_count = ((Time.now.beginning_of_day+60*60 - Time.now) / 600).to_i
else
left_count = ((Time.now.tomorrow.beginning_of_day+60*60 - Time.now) / 600).to_i
end

users = User.all
users.each do |user|
user.update(todaycoin: 0)
begin_count = 90 - left_count

Rails.logger.debug "left count is #{left_count}"

#reset user daycoin every day at 5am
if Time.now.hour == 5
users = User.all
users.each do |user|
user.update(todaycoin: 0)
end
end

objects = []
objindex = 0
for i in 0..89
begin
object, samenum, ordernum, smallnum, bignum, colornum = randomGrid
if day_same + samenum <= max_same and day_order + ordernum <= max_order and
day_small + smallnum <= max_small and day_big + bignum <= max_big and
day_color + colornum <= max_color
break
end

end while true
for i in 0..left_count-1
object, samenum, ordernum, smallnum, bignum, colornum = randomGrid

day_same += samenum
day_order += ordernum
Expand All @@ -225,9 +205,9 @@ def checkGrid (object)
objects[i] = object
end

for i in 0..89
for i in 0..left_count-1
srand()
index = rand(90)
index = rand(left_count)
tmp = objects[index]
objects[index] = objects[i]
objects[i] = tmp
Expand All @@ -241,7 +221,7 @@ def checkGrid (object)
curtime = Time.new

grid = Grid.new
grid.gameid = curtime.strftime("%Y%m%d")+(objindex+1).to_s
grid.gameid = curtime.strftime("%Y%m%d")+(begin_count+objindex+1).to_s
grid.x1 = objects[objindex][0]
grid.x2 = objects[objindex][1]
grid.x3 = objects[objindex][2]
Expand Down Expand Up @@ -327,12 +307,12 @@ def checkGrid (object)
tasklog.taskdate = curtime.strftime("%Y-%m-%d")
tasklog.runtime = curtime.strftime("%Y-%m-%d %H:%M")
tasklog.nexttime = nexttime.strftime("%Y-%m-%d %H:%M")
tasklog.nextgameid = curtime.strftime("%Y%m%d")+(objindex+2).to_s
tasklog.nextgameid = curtime.strftime("%Y%m%d")+(begin_count+objindex+2).to_s
tasklog.save
else
tasklog.update(currentbar: objindex+1, totalcoin: totalcoin,
prizecoin: prizecoin, runtime: curtime.strftime("%Y-%m-%d %H:%M"),
nextgameid: curtime.strftime("%Y%m%d")+(objindex+2).to_s,
nextgameid: curtime.strftime("%Y%m%d")+(begin_count+objindex+2).to_s,
nexttime: nexttime.strftime("%Y-%m-%d %H:%M"))
end

Expand Down
Binary file modified db/development.sqlite3
Binary file not shown.
Loading

0 comments on commit 66b7471

Please sign in to comment.