ferencd@0: require 'net/http/server' ferencd@0: require 'pp' ferencd@0: require 'ellers' ferencd@0: require 'wilsons' ferencd@0: require 'distance_grid' ferencd@0: require 'pg' ferencd@0: require 'base62-rb' ferencd@0: require 'zlib' ferencd@0: require 'wlang' ferencd@0: require 'set' ferencd@0: require 'descends' ferencd@0: require 'pg' ferencd@0: require 'database' ferencd@0: require 'openssl' ferencd@0: require 'game' ferencd@0: require 'gs_logger' ferencd@0: require 'gamedata' ferencd@0: ferencd@0: # ferencd@0: # Starting the App, updating things that need to be ferencd@0: # ferencd@0: $LOG.info 'gameserver 0.1 starting' ferencd@0: Database.initialize ferencd@0: ferencd@0: # ferencd@0: # Constants ferencd@0: # ferencd@0: CONTENT_TYPE = 'Content-Type' ferencd@0: MAIN_HTML = 'lab.html' ferencd@0: CONTENT_TYPES = ferencd@0: { ferencd@0: :png => 'image/png', ferencd@0: :gif => 'image/gif', ferencd@0: :js => 'application/javascript' ferencd@0: } ferencd@0: ferencd@0: GAME_TYPE_MAZE = 1 ferencd@0: GAME_TYPE_ADVENTURE = 2 ferencd@0: GAME_TYPE_STORY = 3 ferencd@0: ferencd@0: ################################################################################################################# ferencd@0: # Helper classes ferencd@0: ################################################################################################################# ferencd@0: ferencd@0: # ferencd@0: # Used to get some extra numbers ferencd@0: # ferencd@0: class Integer ferencd@0: N_BYTES = [42].pack('i').size ferencd@0: N_BITS = N_BYTES * 16 ferencd@0: MAX = 2 ** (N_BITS - 2) - 1 ferencd@0: MIN = -MAX - 1 ferencd@0: end ferencd@0: ferencd@0: # ferencd@0: # An extension to the plain string class to provide b62 validations ferencd@0: # ferencd@0: class String ferencd@0: def valid_b62? ferencd@0: !!match(/^[[:alnum:]]+$/) ferencd@0: end ferencd@0: ferencd@0: def is_i? ferencd@0: /\A[-+]?\d+\z/ === self ferencd@0: end ferencd@0: ferencd@0: def encrypt(key) ferencd@0: cipher = OpenSSL::Cipher::Cipher.new('DES-EDE3-CBC').encrypt ferencd@0: cipher.key = Digest::SHA1.hexdigest key ferencd@0: s = cipher.update(self) + cipher.final ferencd@0: s.unpack('H*')[0].upcase ferencd@0: end ferencd@0: ferencd@0: def decrypt(key) ferencd@0: cipher = OpenSSL::Cipher::Cipher.new('DES-EDE3-CBC').decrypt ferencd@0: cipher.key = Digest::SHA1.hexdigest key ferencd@0: s = [self].pack('H*').unpack('C*').pack('c*') ferencd@0: cipher.update(s) + cipher.final ferencd@0: end ferencd@0: ferencd@0: end ferencd@0: ferencd@0: # ferencd@0: # Simple class to NOT to escape HTML code in the wlang renderer ferencd@0: # ferencd@0: class SimpleHtmlRenderer < WLang::Dialect ferencd@0: ferencd@0: def highlight(buf, fn) ferencd@0: var_name = render(fn) ferencd@0: var_value = evaluate(var_name) ferencd@0: buf << var_value ferencd@0: end ferencd@0: ferencd@0: tag '$', :highlight ferencd@0: end ferencd@0: ferencd@0: ferencd@0: ################################################################################################################# ferencd@0: # Helper methods # ferencd@0: ################################################################################################################# ferencd@0: ferencd@0: # ferencd@0: # Provides a fully completed template for the template engine ferencd@0: # ferencd@0: def complete_templates(templates) ferencd@0: (1..5).each do | v | ferencd@0: templates["gotext#{v}".to_sym] = '' unless templates.has_key? "gotext#{v}".to_sym ferencd@0: end ferencd@0: templates ferencd@0: end ferencd@0: ferencd@0: # ferencd@0: # Just returns page not found ferencd@0: # ferencd@0: def page_not_found ferencd@0: content_type = {CONTENT_TYPE => 'text/html'} ferencd@0: retv = [ '
404 :-(
' ] ferencd@0: code = 404 ferencd@0: return code, content_type, retv ferencd@0: end ferencd@0: ferencd@0: def internal_server_error ferencd@0: content_type = {CONTENT_TYPE => 'text/html'} ferencd@0: retv = [ '
500 :-(
' ] ferencd@0: code = 500 ferencd@0: return code, content_type, retv ferencd@0: end ferencd@0: ferencd@0: ferencd@0: # ferencd@0: # Will create a random room in the grid ferencd@0: # ferencd@0: def create_room(grid) ferencd@0: ferencd@0: tries = 0 ferencd@0: can_not_make = false ferencd@0: begin ferencd@0: ferencd@0: max_width = [grid.columns/10, 6].max ferencd@0: max_height = [grid.rows/10, 6].max ferencd@0: ferencd@0: grid_width = rand(2 .. max_width) + 2 ferencd@0: grid_height = rand(2 .. max_height) + 2 ferencd@0: ferencd@0: x1 = rand(grid.columns - grid_width - 4) ferencd@0: x2 = x1 + grid_width ferencd@0: y1 = rand(grid.rows - grid_height - 4) ferencd@0: y2 = y1 + grid_height ferencd@0: ferencd@0: tries += 1 ferencd@0: can_not_make = !grid.can_make_room(x1,y1,x2,y2) ferencd@0: ferencd@0: end while can_not_make && tries > 5 ferencd@0: ferencd@0: return '' if tries >= 5 ferencd@0: ferencd@0: grid.create_room x1, y1, x2, y2 ferencd@0: ferencd@0: # Place the door ferencd@0: door_x_cell = x1 + grid_width / 2 ferencd@0: door_y_top_cell = y1 ferencd@0: door_y_bottom_cell = y1 + 1 ferencd@0: ferencd@0: "{r:#{door_y_bottom_cell},c:#{door_x_cell},v:0}, {r:#{door_y_top_cell},c:#{door_x_cell},v:1}" ferencd@0: end ferencd@0: ferencd@0: # ferencd@0: # Calculates the distance from the goal cell on the given grid towards the cell_to_go ferencd@0: # and returns the final_len and final_path ferencd@0: # ferencd@0: def distance_calculator(goal, grid, cell_to_go, final_len, final_path) ferencd@0: another_distances = goal.distances ferencd@0: grid.distances = another_distances.path_to(cell_to_go) ferencd@0: jsv2 = grid.distances_js_array_path ferencd@0: final_len += jsv2.length ferencd@0: final_path += jsv2 ferencd@0: return final_len, final_path ferencd@0: end ferencd@0: ferencd@0: # ferencd@0: # Generates a path for the given walker ferencd@0: # ferencd@0: def create_walker_path(grid, r, c) ferencd@0: # making a new path from the furthest point from there ferencd@0: new_distances = grid[r, c].distances ferencd@0: ferencd@0: # goal will be the cell which is the furthest from the new_start and max_dist the length of it ferencd@0: goal, _ = new_distances.max ferencd@0: grid.distances = new_distances.path_to(goal) ferencd@0: ferencd@0: jsv = grid.distances_js_array_path ferencd@0: final_path = jsv ferencd@0: final_len = final_path.length ferencd@0: ferencd@0: # And the goal is that the walker will take a round around the maze ferencd@0: ferencd@0: # Now add in another path to (0,0) ferencd@0: final_len, final_path = distance_calculator(goal, grid, grid[0, 0],final_len, final_path) ferencd@0: final_len, final_path = distance_calculator(grid[0, 0], grid, grid[0, grid.columns - 1],final_len, final_path) ferencd@0: final_len, final_path = distance_calculator(grid[0, grid.columns - 1], grid, grid[grid.rows - 1, grid.columns - 1],final_len, final_path) ferencd@0: ferencd@0: s = '' ferencd@0: s << "path_len:#{final_len},r:#{r},c:#{c},p:'#{final_path}'" ferencd@0: s ferencd@0: ferencd@0: end ferencd@0: ferencd@0: # will generate a few walking characters ferencd@0: def generate_walkers(grid, max_nr) ferencd@0: walkers = [] ferencd@0: return walkers if max_nr == 0 ferencd@0: ferencd@0: walker_count = rand(1 .. max_nr + 1) ferencd@0: $LOG.info"Generating #{walker_count} walkers\n" ferencd@0: #walkers << {:body => 0, :row => 0, :col => 1, :path => "path_len:3,r:0,c:1,p:'EEE'"} ferencd@0: #walkers << {:body => 0, :row => 0, :col => 4, :path => "path_len:3,r:0,c:4,p:'WWW'"} ferencd@0: dbg_cwa = 0 ferencd@0: (1..walker_count).each do ferencd@0: walker_body = dbg_cwa #rand(3) ferencd@0: dbg_cwa = 1 if dbg_cwa == 0 ferencd@0: walker_row = rand(1 .. grid.rows - 2) + 1 ferencd@0: walker_col = rand(1 .. grid.columns - 2) + 1 ferencd@0: walkers << ferencd@0: { ferencd@0: :body => walker_body, ferencd@0: :row => walker_row, ferencd@0: :col => walker_col, ferencd@0: :path => create_walker_path(grid,walker_row,walker_col) ferencd@0: } ferencd@0: end ferencd@0: walkers ferencd@0: end ferencd@0: ferencd@0: ferencd@0: # ferencd@0: # Will put the loot stuff on the level. food_locations is an array where wealready have placed food ferencd@0: # ferencd@0: def place_loot(grid, food_locations) ferencd@0: loot_js = 'var loot=[' ferencd@0: max_possible_loot = grid.dead_ends[1].length + grid.dead_ends[2].length + grid.dead_ends[4].length + grid.dead_ends[8].length ferencd@0: max_possible_loot = max_possible_loot / 4 + 1 ferencd@0: ferencd@0: max_loot_count = $ITEMS.length # 32 items defined in the png_to_js ferencd@0: ferencd@0: loot_count = [rand(max_possible_loot), max_loot_count].min # We want a lot of loot in the maze ferencd@0: ferencd@0: current_loot_count = 0 ferencd@0: idxs = [1, 2, 4, 8] ferencd@0: idxsc = 0 ferencd@0: current_idx = idxs[idxsc] ferencd@0: loot_js_arr = [] ferencd@0: placed_loots = [] ferencd@0: current_loot_value = 0 ferencd@0: ferencd@0: (1..loot_count).each do |_| ferencd@0: ferencd@0: place_ctr = 0 ferencd@0: can_place = true ferencd@0: begin ferencd@0: cell = grid.dead_ends[current_idx][rand (grid.dead_ends[current_idx].length)] ferencd@0: place_ctr += 1 ferencd@0: already_placed_here = placed_loots.select {|loot_loc| loot_loc[:r] == cell.row && loot_loc[:c] == cell.column} ferencd@0: here_is_not_fine = already_placed_here.length > 0 ferencd@0: ferencd@0: unless here_is_not_fine ferencd@0: food_placed_here = food_locations.select {|food_loc| food_loc[:r] == cell.row && food_loc[:c] == cell.column} ferencd@0: here_is_not_fine |= food_placed_here.length > 0 ferencd@0: end ferencd@0: ferencd@0: end while here_is_not_fine && place_ctr < grid.dead_ends[current_idx].length-1 ferencd@0: ferencd@0: if place_ctr == grid.dead_ends[current_idx].length-1 ferencd@0: if already_placed_here.length > 0 ferencd@0: # try to find the first non empty place in the grid ferencd@0: found = false ferencd@0: (0..grid.dead_ends[current_idx].length - 1).each do |i| ferencd@0: cell = grid.dead_ends[current_idx][i] ferencd@0: ferencd@0: already_placed_here = placed_loots.select {|loot_loc| loot_loc[:r] == cell.row && loot_loc[:c] == cell.column} ferencd@0: can_go_here = already_placed_here.length == 0 ferencd@0: food_placed_here = food_locations.select {|loot_loc| loot_loc[:r] == cell.row && loot_loc[:c] == cell.column} ferencd@0: can_go_here &= food_placed_here.length == 0 ferencd@0: ferencd@0: if can_go_here ferencd@0: found = true ferencd@0: break ferencd@0: end ferencd@0: ferencd@0: end ferencd@0: can_place = false if found ferencd@0: end ferencd@0: end ferencd@0: ferencd@0: if can_place ferencd@0: loot_js_arr << "{r:#{cell.row},c:#{cell.column},t:#{current_loot_count}}" if (cell.row > 0 && cell.column > 0) ferencd@0: ferencd@0: current_loot_value += $ITEMS[current_loot_count][:value] ferencd@0: placed_loots << {:r => cell.row, :c => cell.column} ferencd@0: else ferencd@0: break ferencd@0: end ferencd@0: ferencd@0: current_idx = idxs[idxsc] ferencd@0: ferencd@0: idxsc += 1 ferencd@0: if idxsc == idxs.length ferencd@0: idxsc = 0 ferencd@0: end ferencd@0: current_loot_count += 1 ferencd@0: if current_loot_count == max_loot_count ferencd@0: current_loot_count = 0 ferencd@0: end ferencd@0: end ferencd@0: loot_js << loot_js_arr.join(',') << "];\n" ferencd@0: return loot_js, placed_loots, current_loot_value ferencd@0: end ferencd@0: ferencd@0: # ferencd@0: # Will put the food on the table ... ie. level ferencd@0: # ferencd@0: def place_food(grid, level_number, game_type) ferencd@0: food_js = 'var food=[' ferencd@0: max_possible_food = grid.dead_ends[1].length + grid.dead_ends[2].length + grid.dead_ends[4].length + grid.dead_ends[8].length ferencd@0: max_possible_food = max_possible_food / 4 + 1 ferencd@0: food_count = rand(max_possible_food) ferencd@0: ferencd@0: if game_type == GAME_TYPE_STORY ferencd@0: if level_number > 55 ferencd@0: food_count *= 4 ferencd@0: elsif level_number > 45 ferencd@0: food_count *= 3 ferencd@0: elsif level_number > 20 ferencd@0: food_count *= 2 ferencd@0: elsif level_number < 10 ferencd@0: food_count /= 2 ferencd@0: food_count += 1 ferencd@0: end ferencd@0: end ferencd@0: ferencd@0: max_food_count = 10 # This comes from png_to_js the number of possible food items available ferencd@0: current_food_count = 0 # Counts the food items of the max available ferencd@0: idxs = [1, 2, 4, 8] # The indexes of the cells, which are "dead ends". ferencd@0: idxsc = 0 # Counter for the "dead end" index array ferencd@0: current_idx = idxs[idxsc] # The current "dead end" index ferencd@0: food_js_arr = [] # Will hold the javascript array for the food stuff ferencd@0: placed_foods = [] # Will hold where the food was placed ferencd@0: ferencd@0: (1..food_count).each do |_| ferencd@0: ferencd@0: place_ctr = 0 ferencd@0: can_place = true ferencd@0: ferencd@0: # Find a place (cell) where we can place the food item ferencd@0: begin ferencd@0: cell = grid.dead_ends[current_idx][rand (grid.dead_ends[current_idx].length)] ferencd@0: place_ctr += 1 ferencd@0: already_placed_here = placed_foods.select {|food_loc| food_loc[:r] == cell.row && food_loc[:c] == cell.column} ferencd@0: end while already_placed_here.length>0 && place_ctr < grid.dead_ends[current_idx].length-1 ferencd@0: ferencd@0: if place_ctr == grid.dead_ends[current_idx].length-1 ferencd@0: # Did not find an "empty dead end" cell to put the food there ferencd@0: if already_placed_here.length > 0 ferencd@0: # try to find the first empty cell in the grid ferencd@0: found = false ferencd@0: (0..grid.dead_ends[current_idx].length - 1).each do |i| ferencd@0: cell = grid.dead_ends[current_idx][i] ferencd@0: already_placed_here = placed_foods.select {|food_loc| food_loc[:r] == cell.row && food_loc[:c] == cell.column} ferencd@0: if already_placed_here.length == 0 ferencd@0: found = true ferencd@0: break ferencd@0: end ferencd@0: end ferencd@0: can_place = false if found ferencd@0: end ferencd@0: end ferencd@0: ferencd@0: if can_place ferencd@0: food_js_arr << "{r:#{cell.row},c:#{cell.column},t:#{current_food_count}}" if (cell.row > 0 && cell.column > 0) ferencd@0: placed_foods << {:r => cell.row, :c => cell.column} ferencd@0: else ferencd@0: break ferencd@0: end ferencd@0: ferencd@0: current_idx = idxs[idxsc] ferencd@0: ferencd@0: idxsc += 1 ferencd@0: if idxsc == idxs.length ferencd@0: idxsc = 0 ferencd@0: end ferencd@0: current_food_count += 1 ferencd@0: if current_food_count == max_food_count ferencd@0: current_food_count = 0 ferencd@0: end ferencd@0: end ferencd@0: food_js << food_js_arr.join(',') << "];\n" ferencd@0: return food_js, placed_foods ferencd@0: end ferencd@0: ferencd@0: def place_torches(grid) ferencd@0: torches = grid.identify_torch_locations ferencd@0: torch_data = [] ferencd@0: torches.each do |torch| ferencd@0: torch_data << "{i:#{torch[:i]},j:#{torch[:j]},p:#{torch[:number]}}" ferencd@0: end ferencd@0: torch_str = "var torch_placements=[#{torch_data.join(',')}];\n" ferencd@0: storch_str = Utils.split(torch_str, 80) ferencd@0: storch_str ferencd@0: end ferencd@0: ferencd@0: def place_doors(grid, exit_row, exit_col) ferencd@0: doors_js = [] ferencd@0: # Create a few rooms in the grid ferencd@0: room_count = 0 #rand(4 .. 10) ferencd@0: (1..room_count).each do ferencd@0: room_js = create_room(grid) ferencd@0: if room_js ferencd@0: doors_js << room_js ferencd@0: else ferencd@0: break ferencd@0: end ferencd@0: end ferencd@0: # 0 -> cell with a top door, 1 -> cell with a bottom door in ferencd@0: # adding a door at (0,0) just for visual effects :) ferencd@0: # add another door at (width, height) to indicate exit ferencd@0: doors_js << '{r:0,c:0,v:0}' ferencd@0: doors_js << "{r:#{exit_row},c:#{exit_col},v:5}" ferencd@0: doors_str = "var doors=[ #{doors_js.join(',')}];\nvar exit_row=#{exit_row};\nvar exit_col=#{exit_col};"; ferencd@0: sdoors_str = Utils.split(doors_str, 80) ferencd@0: sdoors_str ferencd@0: end ferencd@0: ferencd@0: def place_weapons(food_locations, grid, height, width) ferencd@0: weapons_js = 'var weapon_locations = [' ferencd@0: weapon_locs = [] ferencd@0: all_weapons = [0, 1, 2, 3] # 0 - spear, armor, shoe, ring ferencd@0: special_weapons = [2, 3].to_set ferencd@0: shoe_distance = 0 ferencd@0: ring_distance = 0 ferencd@0: ferencd@0: all_weapons.each do |weapon| ferencd@0: distance_len = 101 ferencd@0: begin ferencd@0: wcol = 1 + rand(width - 2) ferencd@0: wrow = 1+ rand(height - 2) ferencd@0: ferencd@0: fl = 0 ferencd@0: fp = '' ferencd@0: wep_cell = grid[wrow, wcol] ferencd@0: cell00 = grid[0, 0] ferencd@0: distance_len, _ = distance_calculator(wep_cell, grid, cell00, fl, fp) ferencd@0: ferencd@0: if weapon == 2 ferencd@0: shoe_distance = distance_len ferencd@0: end ferencd@0: ferencd@0: if weapon == 3 ferencd@0: ring_distance = distance_len ferencd@0: end ferencd@0: ferencd@0: foods_already_placed_here = food_locations.select {|food_loc| food_loc[:r] == wrow && food_loc[:c] == wcol} ferencd@0: food_here = foods_already_placed_here.length > 0 ferencd@0: ferencd@0: weapons_placed_here = weapon_locs.select {|weapon_loc| weapon_loc[:r] == wrow && weapon_loc[:c] == wcol} ferencd@0: weapon_here = weapons_placed_here.length > 0 ferencd@0: ferencd@0: end while (distance_len > 101 && special_weapons.include?(weapon)) || food_here || weapon_here ferencd@0: ferencd@0: weapon_locs << {:r => wrow, :c => wcol, :t => weapon} ferencd@0: end ferencd@0: weap_tmp_arr=[] ferencd@0: weapon_locs.each {|wpl| weap_tmp_arr << "{r:#{wpl[:r]},c:#{wpl[:c]},t:#{wpl[:t]}}"} ferencd@0: weapons_js << weap_tmp_arr.join(',') << "];\n" ferencd@0: return weapons_js, shoe_distance, ring_distance ferencd@0: end ferencd@0: ferencd@0: # ferencd@0: # Will create a new maze with the given number as the seed for the random generator. ferencd@0: # ferencd@0: def handle_maze_with_number(level_number, game_id, main_html='lab.html', game_type = GAME_TYPE_STORY) ferencd@0: ferencd@0: current_seed = 0x2ECD06801D ^ level_number ferencd@0: # Set the seed based on the level number ferencd@0: srand(current_seed) ferencd@0: ferencd@0: minwidth = 10 ferencd@0: minheight = 10 ferencd@0: ferencd@0: maxwidth = 30 ferencd@0: maxheight = 30 ferencd@0: ferencd@0: # There are no walkers, so the system should be able to handle it ferencd@0: if game_type == GAME_TYPE_MAZE ferencd@0: maxwidth = 40 ferencd@0: maxheight = 40 ferencd@0: end ferencd@0: ferencd@0: maxwidth = [minwidth + level_number, maxwidth].min ferencd@0: maxheight = [minheight + level_number, maxheight].min ferencd@0: ferencd@0: width = rand(minwidth .. maxwidth) ferencd@0: height = rand(minheight ..maxheight) ferencd@0: ferencd@0: $LOG.info "GameID:#{game_id} Generated: #{width} x #{height} for level #{level_number}" ferencd@0: ferencd@0: grid = DistanceGrid.new(height, width) ferencd@0: wilson = Wilsons.new ferencd@0: wilson.on grid ferencd@0: ferencd@0: maze_js = '' ferencd@0: maze_js << "history.pushState(null, null, '/#{game_id}');" ferencd@0: maze_js << "var w = #{width};\n" ferencd@0: maze_js << "var h = #{height};\n" ferencd@0: maze_js << "#{grid.maze_js_array}\n" ferencd@0: ferencd@0: # Now identify all the places where a torch can go ferencd@0: storch_str = place_torches(grid) ferencd@0: maze_js << storch_str ferencd@0: ferencd@0: # Where the exit is for now ferencd@0: exit_col = width - 1 ferencd@0: exit_row = height- 1 ferencd@0: ferencd@0: ferencd@0: # At a later stage the exit will not be exactly at the end of the maze, but at the furthest cell from (0,0) ferencd@0: # start = grid[0,0] ferencd@0: # distances = start.distances ferencd@0: # new_start, _ = distances.max ferencd@0: #if game_type == GAME_TYPE_ADVENTURE ferencd@0: # exit_col = new_start.column ferencd@0: # exit_row = new_start.row ferencd@0: #end ferencd@0: ferencd@0: # Calculate the shortest path from the entrance to the exit ferencd@0: fp_fl = 0 ferencd@0: fp_fp = '' ferencd@0: fp_wep_cell = grid[exit_row, exit_col] ferencd@0: fp_cell00 = grid[0,0] ferencd@0: full_path_len, _ = distance_calculator(fp_wep_cell, grid, fp_cell00, fp_fl, fp_fp) ferencd@0: $LOG.info "Distance from (0,0) to (#{exit_col}, #{exit_row}) is #{full_path_len} steps" ferencd@0: ferencd@0: loot_value = 0 ferencd@0: walkers = [] ferencd@0: food_locations = [] ferencd@0: ferencd@0: # Create a few rooms with doors. Notes: ferencd@0: # 1. This is not done due to various reasons yet ferencd@0: ferencd@0: sdoors_str = place_doors(grid, exit_row, exit_col) ferencd@0: maze_js << sdoors_str ferencd@0: ferencd@0: # Create a few walkers but only if the game type is not a simple maze runner ferencd@0: if game_type != GAME_TYPE_MAZE ferencd@0: max_walkers = 0 ferencd@0: if game_type == GAME_TYPE_STORY ferencd@0: max_walkers = [ [1, level_number - 3].max, 15].min if level_number > 4 ferencd@0: else ferencd@0: max_walkers = rand(3..15) ferencd@0: end ferencd@0: ferencd@0: walkers = generate_walkers(grid, max_walkers) ferencd@0: walkers_str_array = [] ferencd@0: walkers.each do |walker| ferencd@0: walkers_str_array << "{t:#{walker[:body]},#{walker[:path]}}" ferencd@0: end ferencd@0: walkers_js = "var walkers=[#{walkers_str_array.join(',')}];\n" ferencd@0: maze_js << Utils.split(walkers_js, 80) ferencd@0: maze_js << "var walkers_count = #{walkers.length};" ferencd@0: else ferencd@0: maze_js << "var walkers=[];\nvar walkers_count=0;" ferencd@0: end ferencd@0: ferencd@0: # Food items ferencd@0: if game_type != GAME_TYPE_MAZE ferencd@0: food_js, food_locations = place_food(grid, level_number, game_type) ferencd@0: maze_js << "#{food_js}" ferencd@0: else ferencd@0: maze_js << "var food=[];\n" ferencd@0: end ferencd@0: ferencd@0: # Loot in the maze only if this is an adventure game ferencd@0: if game_type == GAME_TYPE_ADVENTURE ferencd@0: loot_js, loot_locations, loot_value = place_loot(grid, food_locations) ferencd@0: ferencd@0: # Just hack the food locations to contain all the loot ferencd@0: (food_locations << loot_locations).flatten! ferencd@0: else ferencd@0: loot_js = "var loot=[];\n" ferencd@0: end ferencd@0: ferencd@0: maze_js << "#{loot_js}" ferencd@0: ferencd@0: shoe_distance = 0 ferencd@0: ring_distance = 0 ferencd@0: ferencd@0: # Weapons ferencd@0: if game_type != GAME_TYPE_MAZE ferencd@0: weapons_js,shoe_distance,ring_distance = place_weapons(food_locations, grid, height, width) ferencd@0: maze_js << "#{weapons_js}" ferencd@0: else ferencd@0: maze_js << "var weapon_locations=[];\n" ferencd@0: end ferencd@0: ferencd@0: templated_maze = IO.binread(main_html) ferencd@0: if $arrives.has_key? level_number ferencd@0: templates_arrives = $arrives[level_number] ferencd@0: else ferencd@0: templates_arrives = $generic_arrives ferencd@0: end ferencd@0: complete_templates templates_arrives ferencd@0: ferencd@0: templates_arrives[:gid] = "#{game_id}" ferencd@0: templates_arrives[:lid] = "#{level_number}" ferencd@0: templates_arrives[:page_loader] = 'upon_page_load();' ferencd@0: # templates_arrives[:page_loader] = 'setup_labyrinth();' if game_type == GAME_TYPE_ADVENTURE or game_type == GAME_TYPE_MAZE ferencd@0: ferencd@0: templates_arrives[:sysmenu_visible] = 'hidden;' ferencd@0: templates_arrives[:sysmenu_display] = 'none;' ferencd@0: templates_arrives[:menu_line_visible] = 'visible;' ferencd@0: ferencd@0: level_statistics = "Level Number:#{level_number}\nExit at (#{exit_col}, #{exit_row}).\nDistance from (0,0) to (#{exit_col}, #{exit_row}) is #{full_path_len}" ferencd@0: ferencd@0: templates_arrives[:badbrowser_visible] = 'hidden;' ferencd@0: templates_arrives[:badbrowser_display] = 'none;' ferencd@0: templates_arrives[:health_visible] = 'visible' ferencd@0: templates_arrives[:statis_visible] = 'hidden' ferencd@0: templates_arrives[:gameid_size] = '105px' ferencd@0: templates_arrives[:gameid_visible] = 'visible' ferencd@0: templates_arrives[:equipment_visible] = 'hidden' ferencd@0: templates_arrives[:messages_visible] = 'hidden; display:none' ferencd@0: ferencd@0: if game_type == GAME_TYPE_ADVENTURE ferencd@0: templates_arrives[:gold_visible] = 'visible' ferencd@0: templates_arrives[:skeletons_visible] = 'visible' ferencd@0: templates_arrives[:levelctr_visible] = 'hidden; display:none' ferencd@0: templates_arrives[:timer_visible] = 'visible' ferencd@0: templates_arrives[:day_or_level] = 'Level' ferencd@0: templates_arrives[:new_game_type] = 'new_adventure_game' ferencd@0: level_statistics << "\nGold to be found in this dungeon: #{loot_value}\nSkeletons: #{walkers.length}\nSkeletons' acumen: Droidlike\n" ferencd@0: level_statistics << "Boots of Lightness at #{shoe_distance} steps apart from the start\nRing of Health #{ring_distance} steps apart from the start" ferencd@0: ferencd@0: else ferencd@0: templates_arrives[:gold_visible] = 'hidden; display:none' ferencd@0: templates_arrives[:skeletons_visible] = 'hidden; display:none' ferencd@0: templates_arrives[:levelctr_visible] = 'visible' ferencd@0: templates_arrives[:timer_visible] = 'hidden; display:none' ferencd@0: templates_arrives[:new_game_type] = 'new_story_game' ferencd@0: templates_arrives[:day_or_level] = 'Day' ferencd@0: ferencd@0: if game_type == GAME_TYPE_MAZE ferencd@0: templates_arrives[:levelctr_visible] = 'hidden; display:none' ferencd@0: templates_arrives[:health_visible] = 'hidden; display:none' ferencd@0: templates_arrives[:equipment_visible] = 'hidden; display:none' ferencd@0: templates_arrives[:timer_visible] = 'visible' ferencd@0: templates_arrives[:statis_visible] = 'visible' ferencd@0: templates_arrives[:new_game_type] = 'new_timechaser_game' ferencd@0: else ferencd@0: templates_arrives[:messages_visible] = 'visible' ferencd@0: end ferencd@0: ferencd@0: end ferencd@0: ferencd@0: templates_arrives[:story_game_loc] = Game.gen_game_url 'SG' ferencd@0: templates_arrives[:adv_game_loc] = Game.gen_game_url 'AG' ferencd@0: templates_arrives[:timer_game_loc] = Game.gen_game_url 'TG' ferencd@0: ferencd@0: templates_arrives[:level_statistics] = level_statistics ferencd@0: ferencd@0: full_maze = SimpleHtmlRenderer.render(templated_maze, templates_arrives) ferencd@0: full_maze << '' ferencd@0: ferencd@0: # javascripts << '' ferencd@0: # ferencd@0: # javascripts << '' ferencd@0: ferencd@0: # templated_game_js = IO.binread 'game.js' ferencd@0: # game_js = SimpleHtmlRenderer.render(templated_game_js, templates_descends) ferencd@0: ferencd@0: # javascripts << game_js ferencd@0: ferencd@0: full_maze << javascripts ferencd@0: ferencd@0: full_maze << 'request("graphics.js");' ferencd@0: retv = [full_maze] ferencd@0: ferencd@0: return 200, retv ferencd@0: end ferencd@0: ferencd@0: # ferencd@0: # Handles the root request. Creates a new maze number ferencd@0: # ferencd@0: def handle_root ferencd@0: templated_maze = IO.binread(MAIN_HTML) ferencd@0: templates_arrives = $generic_arrives ferencd@0: complete_templates templates_arrives ferencd@0: ferencd@0: templates_arrives[:gid] = '' ferencd@0: templates_arrives[:lid] = '' ferencd@0: ferencd@0: templates_arrives[:page_loader] = 'page_loader();' ferencd@0: ferencd@0: templates_arrives[:sysmenu_visible] = 'visible;' ferencd@0: templates_arrives[:sysmenu_display] = 'block;' ferencd@0: templates_arrives[:menu_line_visible] = 'hidden;' ferencd@0: ferencd@0: templates_arrives[:gameid_visible] = 'hidden' ferencd@0: templates_arrives[:gold_visible] = 'hidden' ferencd@0: templates_arrives[:skeletons_visible] = 'hidden' ferencd@0: templates_arrives[:levelctr_visible] = 'hidden' ferencd@0: templates_arrives[:health_visible] = 'hidden' ferencd@0: templates_arrives[:timer_visible] = 'hidden' ferencd@0: templates_arrives[:gameid_size] = '0px; padding:0' ferencd@0: templates_arrives[:day_or_level] = 'Stage' ferencd@0: templates_arrives[:level_statistics] = 'Nothing here yet' ferencd@0: templates_arrives[:statis_visible] = 'hidden' ferencd@0: templates_arrives[:equipment_visible] = 'hidden' ferencd@0: templates_arrives[:new_game_type] = 'page_loader' ferencd@0: templates_arrives[:messages_visible] = 'hidden' ferencd@0: ferencd@0: templates_arrives[:badbrowser_visible] = 'hidden;' ferencd@0: templates_arrives[:badbrowser_display] = 'none;' ferencd@0: ferencd@0: templates_arrives[:story_game_loc] = Game.gen_game_url 'SG' ferencd@0: templates_arrives[:adv_game_loc] = Game.gen_game_url 'AG' ferencd@0: templates_arrives[:timer_game_loc] = Game.gen_game_url 'TG' ferencd@0: ferencd@0: full_maze = SimpleHtmlRenderer.render(templated_maze, templates_arrives) ferencd@0: full_maze << '' ferencd@0: ferencd@0: retv = [full_maze] ferencd@0: ferencd@0: return 200, retv ferencd@0: end ferencd@0: ferencd@0: # ferencd@0: # Delivers a file to the browser ferencd@0: # ferencd@0: def deliver_file(name) ferencd@0: ferencd@0: code, content_type, retv = page_not_found ferencd@0: ferencd@0: # Will read a file. Supposed to be on the same level as the script ferencd@0: filename = name[1..-1] ferencd@0: if File.exists?(filename) ferencd@0: ferencd@0: extension = filename.partition('.').last ferencd@0: ferencd@0: code = 200 ferencd@0: content_type = { CONTENT_TYPE => CONTENT_TYPES[extension] } ferencd@0: retv = [ IO.binread(name.partition('/').last) ] ferencd@0: ferencd@0: end ferencd@0: ferencd@0: return code, content_type, retv ferencd@0: ferencd@0: end ferencd@0: ferencd@0: def handle_game_loader(loc) ferencd@0: ferencd@0: begin ferencd@0: key = loc[0..5] ferencd@0: enced = loc[6..-1] ferencd@0: deced = enced.decrypt key ferencd@0: rescue ferencd@0: deced = 'SG' ferencd@0: end ferencd@0: game_type = case deced ferencd@0: when 'SG' then ferencd@0: deced ferencd@0: when 'AG' then ferencd@0: deced ferencd@0: when 'TG' then ferencd@0: deced ferencd@0: else ferencd@0: 'SG' ferencd@0: end ferencd@0: code, retv = Game.create_game game_type ferencd@0: return code, retv ferencd@0: end ferencd@0: ferencd@0: def retrieve_game_id(request) ferencd@0: gid = '' ferencd@0: gid = request[:headers]['X-Extra'] if request[:headers].has_key? 'X-Extra' ferencd@0: gid = gid.split('=')[1] if gid && gid.count('=') > 0 ferencd@0: gid ferencd@0: end ferencd@0: ferencd@0: def handle_next_level(gid) ferencd@0: ferencd@0: # See where the player was ferencd@0: level_nr = Database.get_db_level_for_gid gid ferencd@0: ferencd@0: # Move him to the next level ferencd@0: level_nr += 1 ferencd@0: Database.update_level_number_for_gid(level_nr, gid) ferencd@0: ferencd@0: # And let's play that level ferencd@0: code, retv = handle_maze_with_number(level_nr, gid, MAIN_HTML, GAME_TYPE_STORY) ferencd@0: ferencd@0: return code, retv ferencd@0: end ferencd@0: ferencd@0: # ferencd@0: # Each connection will get in here ferencd@0: # ferencd@0: Net::HTTP::Server.run(:port => 8180) do |request, _| ferencd@0: begin ferencd@0: url = request[:uri] ferencd@0: ferencd@0: code, content_type, retv = page_not_found ferencd@0: ferencd@0: lab_id = url[:path] ferencd@0: ferencd@0: # pp lab_id ferencd@0: ferencd@0: # what we are trying to load ferencd@0: what = lab_id.to_s ferencd@0: ferencd@0: # Handle favico ferencd@0: if what == '/favicon.ico' ferencd@0: code = 200 ferencd@0: content_type = {CONTENT_TYPE => 'image/x-icon'} ferencd@0: retv = [ IO.binread('favicon.ico') ] ferencd@0: end ferencd@0: ferencd@0: # Nothing, just load the default, and create a new maze ferencd@0: if what == '/' ferencd@0: code, retv = handle_root ferencd@0: elsif what != '/favicon.ico' ferencd@0: # some file that needs to be delivered? ferencd@0: if what.end_with?('.js') or what.end_with?('.png') or what.end_with?('.gif') ferencd@0: code, content_type, retv = deliver_file(what) ferencd@0: else ferencd@0: # The actual location ferencd@0: loc = what[1..-1] ferencd@0: ferencd@0: # See if this is a gametype loader from the main screen, ie full hex string: ferencd@0: if loc =~ /^[0-9A-F]+$/i and not loc.is_i? ferencd@0: code, retv = handle_game_loader(loc) ferencd@0: else ferencd@0: # gid will be the Game ID of the current game if story mode, otherwise the adventure mode is selected for the ferencd@0: # current maze ferencd@0: gid = retrieve_game_id(request) ferencd@0: ferencd@0: # Crazy locations will lead to random maze games ferencd@0: if loc.length > 9 ferencd@0: loc = 'T' + Utils.random_string(8) ferencd@0: end ferencd@0: if gid == loc ferencd@0: # The player advanced to the next level from the game, we have a valid gid equaling the location of the window ferencd@0: code, retv = handle_next_level(gid) ferencd@0: elsif loc.is_i? ferencd@0: # We get here by a simple "GET" from the browser URL, someone just wants to play a maze game ferencd@0: # Now update in the DB the level number for gid ferencd@0: code, retv = handle_maze_with_number(loc.to_i, gid, MAIN_HTML, GAME_TYPE_MAZE) ferencd@0: elsif loc.length > 0 and (loc[0] == 'S' or loc[0] == 'A' or loc[0] == 'T') ferencd@0: level_number = Database.get_db_level_for_gid(loc) ferencd@0: if level_number == -1 ferencd@0: level_number = Zlib::crc32(loc, 0) ferencd@0: end ferencd@0: code, retv = handle_maze_with_number(level_number, loc, ferencd@0: MAIN_HTML, ferencd@0: if loc[0] == 'S' then GAME_TYPE_STORY elsif loc[0] == 'T' then GAME_TYPE_MAZE ferencd@0: else GAME_TYPE_ADVENTURE end ferencd@0: ) ferencd@0: ferencd@0: end ferencd@0: end ferencd@0: end ferencd@0: end ferencd@0: rescue => ex ferencd@0: $LOG.fatal("A Somewhat critical error: #{ex.to_s}") ferencd@0: code, content_type, retv = internal_server_error ferencd@0: raise ferencd@0: end ferencd@0: ferencd@0: # This will go back to the caller ferencd@0: reply = [code, content_type, retv] ferencd@0: reply ferencd@0: ferencd@0: end ferencd@0: