fix date formatting

This commit is contained in:
Jill 2022-12-31 17:04:43 +03:00
parent 865c21c4ea
commit 426da44bee
4 changed files with 38 additions and 3 deletions

View File

@ -13,4 +13,12 @@ task :dbmigrate do
DB.open(ENV["DATABASE_URL"])
)
migrator.to_latest
end
desc "Migrate database a single version down"
task :dbmigratedown do
migrator = Migrate::Migrator.new(
DB.open(ENV["DATABASE_URL"])
)
migrator.down
end

View File

@ -42,7 +42,7 @@ CrystalGauntlet.endpoints["/getGJLevelScores211.php"] = ->(body : String): Strin
percent_old, coins_old = DATABASE.query_one("select percent, coins from level_scores where account_id = ? and level_id = ?", account_id, level_id, as: {Int32, Int32})
if percent > percent_old || coins > coins_old
DATABASE.exec("update level_scores set account_id=?, level_id=?, daily_id=?, percent=?, attempts=?, clicks=?, coins=?, progress=?, time=?, set_at=? where account_id = ? and level_id = ?", account_id, level_id, daily_id, percent, attempts, clicks, coins, progress, time, Time.utc.to_s("%Y-%m-%d %H:%M:%S"), account_id, level_id)
DATABASE.exec("update level_scores set account_id=?, level_id=?, daily_id=?, percent=?, attempts=?, clicks=?, coins=?, progress=?, time=?, set_at=? where account_id = ? and level_id = ?", account_id, level_id, daily_id, percent, attempts, clicks, coins, progress, time, Time.utc.to_s(Format::TIME_FORMAT), account_id, level_id)
end
else
DATABASE.exec("insert into level_scores (account_id, level_id, daily_id, percent, attempts, clicks, coins, progress, time) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", account_id, level_id, daily_id, percent, attempts, clicks, coins, progress, time)
@ -98,7 +98,7 @@ CrystalGauntlet.endpoints["/getGJLevelScores211.php"] = ->(body : String): Strin
3 => percent,
6 => i,
13 => coins,
42 => set_at
42 => Format.fmt_timespan(Time.utc - Time.parse(set_at, Format::TIME_FORMAT, Time::Location::UTC))
})
end

View File

@ -21,7 +21,7 @@ CrystalGauntlet.endpoints["/updateGJUserScore22.php"] = ->(body : String): Strin
# todo: cap out demon count at the current amount of uploaded demons? same for stars & user coins. could be expensive though
# todo: cap icon type
DATABASE.exec("update users set username=?, stars=?, demons=?, coins=?, user_coins=?, diamonds=?, icon_type=?, color1=?, color2=?, cube=?, ship=?, ball=?, ufo=?, wave=?, robot=?, spider=?, explosion=?, special=?, glow=?, last_played=? where id=?", params["userName"], params["stars"].to_i32, params["demons"].to_i32, params["coins"].to_i32, params["userCoins"].to_i32, params["diamonds"].to_i32, params["iconType"].to_i32, params["color1"].to_i32, params["color2"].to_i32, params["accIcon"].to_i32, params["accShip"].to_i32, params["accBall"].to_i32, params["accBird"].to_i32, params["accDart"].to_i32, params["accRobot"].to_i32, params["accSpider"].to_i32, params["accExplosion"].to_i32, params["special"].to_i32, params["accGlow"].to_i32, Time.utc.to_s("%Y-%m-%d %H:%M:%S"), user_id)
DATABASE.exec("update users set username=?, stars=?, demons=?, coins=?, user_coins=?, diamonds=?, icon_type=?, color1=?, color2=?, cube=?, ship=?, ball=?, ufo=?, wave=?, robot=?, spider=?, explosion=?, special=?, glow=?, last_played=? where id=?", params["userName"], params["stars"].to_i32, params["demons"].to_i32, params["coins"].to_i32, params["userCoins"].to_i32, params["diamonds"].to_i32, params["iconType"].to_i32, params["color1"].to_i32, params["color2"].to_i32, params["accIcon"].to_i32, params["accShip"].to_i32, params["accBall"].to_i32, params["accBird"].to_i32, params["accDart"].to_i32, params["accRobot"].to_i32, params["accSpider"].to_i32, params["accExplosion"].to_i32, params["special"].to_i32, params["accGlow"].to_i32, Time.utc.to_s(Format::TIME_FORMAT), user_id)
user_id.to_s
}

View File

@ -1,12 +1,39 @@
module CrystalGauntlet::Format
extend self
TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
def fmt_timespan(s : Time::Span) : String
seconds = s.total_seconds.floor()
minutes = s.total_minutes.floor()
hours = s.total_hours.floor()
days = s.total_days.floor()
months = (s.total_days / 30).floor()
years = (s.total_days / 365).floor()
case
when months >= 17
"#{years.to_i} year#{years == 1 ? "" : "s"}"
when days >= 31
"#{months.to_i} month#{months == 1 ? "" : "s"}"
when hours >= 24
"#{days.to_i} day#{days == 1 ? "" : "s"}"
when minutes >= 60
"#{hours.to_i} hour#{hours == 1 ? "" : "s"}"
when seconds >= 60
"#{minutes.to_i} minute#{minutes == 1 ? "" : "s"}"
else
"#{seconds.to_i} second#{seconds == 1 ? "" : "s"}"
end
end
def fmt_value(v) : String
case v
when Bool
v ? "1" : "0"
when String
v
when Time::Span
fmt_span(v)
else
v.to_s
end