<commit_before>-- libs
require 'vendor/AnAL'
require 'socket.http'

local gui = require 'vendor/Quickie'
local gamestate = require 'vendor/hump/gamestate'
local json = require 'vendor/json'

local fonts = {}
local game = {}
game.stats = {}

-- modules
local map = nil
local bot = require 'bot'
local cmd = require 'cmd'
local doodad = require 'doodad'
local flow = require 'flow'
local camera = require 'camera'

local function resetlevel()
  map = flow.map

  bot.x = map.start.x
  bot.y = map.start.y

  camera:setbounds( 0, 0, map.tilewidth * map.width, map.tileheight * map.height )

  cmd.init()
end

function game:keypressed( key )
  local command = cmd.toCommand( key )
  cmd.process( command )
end

-- true if won, false otherwise
local function won( bot, map )
  if bot.x == map.dest.x and bot.y == map.dest.y then
    return true
  else
    return false
  end
end

local function toJSONArray( array )
  local arr = "["
  for i, elem in ipairs( array, elem ) do
    arr = arr .. [["]] .. elem .. [["]]
    if i < table.maxn( array ) then
      arr = arr .. ","
    end
  end
  arr = arr .. "]"
  return arr
end

local function submitcommands( commands )
  local request = [[player=]]..bot.name..[[&level=]]..map.name:gsub( "art/levels/", "" )..[[&commands=]]..toJSONArray( commands )
  local response = {}
  local res, code, _ = socket.http.request ( {
    url = "http://172.16.17.32:7000/submitscore";
    method = "POST";
    headers = { [ "Content-Type" ] = "application/x-www-form-urlencoded";
                [ "Content-Length" ] = #request;
              };
    source = ltn12.source.string( request );
    sink = ltn12.sink.table( response );
  } )

  return res and true, table.remove( response ) or false, 0
end

local function gethighscores()
  local response = {}
  local res, code, _ = socket.http.request ( {
    url = "http://172.16.17.32:7000/gethighscores/"..map.name:gsub( "art/levels/", "" );
    sink = ltn12.sink.table( response );
  } )

  -- debug
  -- table.foreach( response, print )

  return json:decode( table.concat( response ) )
end

function game.stats:enter( from )
  game.stats.parent = from

  local success, percentile = submitcommands( cmd.buffer )
  if success then
    game.stats.percentile = tonumber( percentile )
    game.stats.highscores = gethighscores()
    game.stats.topresult = game.stats.highscores[1]
  else
    game.stats.percentile = nil
  end
end

-- thanks to http://lua-users.org/wiki/SimpleRound
local function round(num, idp)
  return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end

function game.stats:update( dt )
  gui.group.push{ grow = "down", pos = { 250, 240 } }

  love.graphics.setFont( fonts["title"] )
  local usedFewestCommands = ( table.maxn( game.stats.topresult["commands"] ) >= table.maxn( cmd.buffer ) )
  if usedFewestCommands or game.stats.percentile and game.stats.percentile > 85 then
    gui.Label{ text = "AWESOME!!!", align = "center" }
  elseif game.stats.percentile and game.stats.percentile > 65 then
    gui.Label{ text = "GREAT!!", align = "center" }
  else
    gui.Label{ text = "PASS!", align = "center" }
  end
  love.graphics.setFont( fonts["button"] )
  gui.Label{ text = "you used " .. table.maxn( cmd.buffer ) .. " commands in total.", align = "center" }

  if game.stats.percentile then
    if usedFewestCommands or game.stats.percentile > 85 then
      gui.Label{ text = "the top player on this level used " .. table.maxn( game.stats.topresult["commands"] ) .. " commands in total.", align = "center" }
    end
    gui.Label{ text = bot.name .. "'s percentile rank on this level: " .. round( game.stats.percentile, 2 ), align = "center" }
  end

  gui.group.pop()
  gui.group.push{ grow = "down", pos = { 250, 400 } }
  love.graphics.setFont( fonts["button"] )
  if gui.Button{ text = "advance [spc]", align = "center" } then
    flow.advance()
    resetlevel()
    gamestate.pop()
  end
  gui.group.pop()
end

function game.stats:keypressed( key, code )
  if key == ' ' then
    flow.advance()
    resetlevel()
    gamestate.pop()
  end
end

function game.stats:draw()
  game.stats.parent:draw()

  -- draw stats overlay

  local r, g, b, a = love.graphics.getColor()
  love.graphics.setColor( 0, 0, 0, 128 )
  love.graphics.rectangle( "fill", 0, 220, 640, 200 )
  love.graphics.setColor( r, g, b, a )

  gui.core.draw()
end

function game:init()
  -- init all modules
  flow.init()
  cmd.init()
  bot.init()
  doodad.init()
  camera:init()
  resetlevel()
  fonts = {
    ["title"] = love.graphics.newFont( "art/fonts/typeone.ttf", 60 );
    ["button"] = love.graphics.newFont( "art/fonts/BebasNeue.otf", 15 )
  }
end

function game:update( dt )

  -- execute commands
  local oldx, oldy = bot.x, bot.y
  cmd.execute( bot )
<commit_msg>implement helpscreen into game<commit_after>-- libs
require 'vendor/AnAL'
require 'socket.http'

local gui = require 'vendor/Quickie'
local gamestate = require 'vendor/hump/gamestate'
local json = require 'vendor/json'

local fonts = {}
local game = {}
game.stats = {}
game.help = {}

-- modules
local map = nil
local bot = require 'bot'
local cmd = require 'cmd'
local doodad = require 'doodad'
local flow = require 'flow'
local camera = require 'camera'

local function resetlevel()
  map = flow.map

  bot.x = map.start.x
  bot.y = map.start.y

  camera:setbounds( 0, 0, map.tilewidth * map.width, map.tileheight * map.height )

  cmd.init()
end

function game:keypressed( key )
  local command = cmd.toCommand( key )
  cmd.process( command )
  if key == '/' and ( love.keyboard.isDown( 'lshift' ) or love.keyboard.isDown( 'rshift' ) ) then
    gamestate.push( game.help )
  end
end

-- true if won, false otherwise
local function won( bot, map )
  if bot.x == map.dest.x and bot.y == map.dest.y then
    return true
  else
    return false
  end
end

local function toJSONArray( array )
  local arr = "["
  for i, elem in ipairs( array, elem ) do
    arr = arr .. [["]] .. elem .. [["]]
    if i < table.maxn( array ) then
      arr = arr .. ","
    end
  end
  arr = arr .. "]"
  return arr
end

local function submitcommands( commands )
  local request = [[player=]]..bot.name..[[&level=]]..map.name:gsub( "art/levels/", "" )..[[&commands=]]..toJSONArray( commands )
  local response = {}
  local res, code, _ = socket.http.request ( {
    url = "http://172.16.17.32:7000/submitscore";
    method = "POST";
    headers = { [ "Content-Type" ] = "application/x-www-form-urlencoded";
                [ "Content-Length" ] = #request;
              };
    source = ltn12.source.string( request );
    sink = ltn12.sink.table( response );
  } )

  return res and true, table.remove( response ) or false, 0
end

local function gethighscores()
  local response = {}
  local res, code, _ = socket.http.request ( {
    url = "http://172.16.17.32:7000/gethighscores/"..map.name:gsub( "art/levels/", "" );
    sink = ltn12.sink.table( response );
  } )

  -- debug
  -- table.foreach( response, print )

  return json:decode( table.concat( response ) )
end

function game.stats:enter( from )
  game.stats.parent = from

  local success, percentile = submitcommands( cmd.buffer )
  if success then
    game.stats.percentile = tonumber( percentile )
    game.stats.highscores = gethighscores()
    game.stats.topresult = game.stats.highscores[1]
  else
    game.stats.percentile = nil
  end
end

-- thanks to http://lua-users.org/wiki/SimpleRound
local function round(num, idp)
  return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end

function game.stats:update( dt )
  gui.group.push{ grow = "down", pos = { 250, 240 } }

  love.graphics.setFont( fonts["title"] )
  local usedFewestCommands = ( table.maxn( game.stats.topresult["commands"] ) >= table.maxn( cmd.buffer ) )
  if usedFewestCommands or game.stats.percentile and game.stats.percentile > 85 then
    gui.Label{ text = "AWESOME!!!", align = "center" }
  elseif game.stats.percentile and game.stats.percentile > 65 then
    gui.Label{ text = "GREAT!!", align = "center" }
  else
    gui.Label{ text = "PASS!", align = "center" }
  end
  love.graphics.setFont( fonts["button"] )
  gui.Label{ text = "you used " .. table.maxn( cmd.buffer ) .. " commands in total.", align = "center" }

  if game.stats.percentile then
    if usedFewestCommands or game.stats.percentile > 85 then
      gui.Label{ text = "the top player on this level used " .. table.maxn( game.stats.topresult["commands"] ) .. " commands in total.", align = "center" }
    end
    gui.Label{ text = bot.name .. "'s percentile rank on this level: " .. round( game.stats.percentile, 2 ), align = "center" }
  end

  gui.group.pop()
  gui.group.push{ grow = "down", pos = { 250, 400 } }
  love.graphics.setFont( fonts["button"] )
  if gui.Button{ text = "advance [spc]", align = "center" } then
    flow.advance()
    resetlevel()
    gamestate.pop()
  end
  gui.group.pop()
end

function game.stats:keypressed( key, code )
  if key == ' ' then
    flow.advance()
    resetlevel()
    gamestate.pop()
  end
end

function game.stats:draw()
  game.stats.parent:draw()

  -- draw stats overlay
  local r, g, b, a = love.graphics.getColor()
  love.graphics.setColor( 0, 0, 0, 128 )
  love.graphics.rectangle( "fill", 0, 220, 640, 200 )
  love.graphics.setColor( r, g, b, a )

  gui.core.draw()
end

function game.help:enter( from )
  game.help.parent = from
  game.help.overlayimg = love.graphics.newImage( "art/overlays/helpscreen.png" )
end

function game.help:update( dt )
  gui.group.push{ grow = "down", pos = { 275, 475 } }
  love.graphics.setFont( fonts["button"] )
  if gui.Button{ text = "got it [spc]", align = "center" } then
    gamestate.pop()
  end
  gui.group.pop()
end

function game.help:draw()
  self.parent:draw()
  love.graphics.draw( self.overlayimg, 170, 150 )
  gui.core.draw()
end

function game.help:keypressed( key )
  if key == 'escape' or key == ' ' then
    gamestate.pop()
  end
end

function game:init()
  -- init all modules
  flow.init()
  cmd.init()
  bot.init()
  doodad.init()
  camera:init()
  resetlevel()
  fonts = {
    ["title"] = love.graphics.newFont( "art/fonts/typeone.ttf", 60 );
    ["button"] = love.graphics.newFont( "art/fonts/BebasNeue.otf", 15 )
  }
end

function game:update( dt )

  -- execute commands
  local oldx, oldy = bot.x, bot.y
  cmd.execute( bot )

{'fraction_non_alphanumeric': 0.15487069803616565, 'fraction_numerical': 0.015944001555512347, 'mean_word_length': 3.0484061393152304, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '290441', 'n_tokens_mistral': 3763, 'n_tokens_neox': 3555, 'n_words': 940}