text
stringlengths
0
601k
let _ = Random . init 42
let encode ( x , y ) = ( x lsl 8 ) lor y
let decode x = ( x / 256 , x mod 256 )
let snake = Array . make 10 ( encode ( 0 , 0 ) )
let ptr_head = ref 1
let ptr_tail = ref 0
let size = ref 1
let new_position ( ) = ( Random . int 64 , Random . int 32 )
let apple = ref ( new_position ( ) )
let left_of = function South -> East | North -> West | East -> North | West -> South
let right_of = function South -> West | North -> East | East -> South | West -> North
let button_direction = let old_left = ref HIGH in let old_right = ref HIGH in fun dir -> let new_left = digital_read button_left in let new_right = digital_read button_right in let new_dir = match new_left , new_right with | LOW , _ when ! old_left <> LOW -> left_of dir | _ , LOW when ! old_right <> LOW -> right_of dir...
let collides_with_itself ( ) = let i = ref ! ptr_tail in try while true do if ! i = ! ptr_head then raise Break ; if snake . ( ! i ) = snake . ( ! ptr_head ) then raise Lose ; incr i ; i := ! i mod Array . length snake ; done with Break -> ( )
let new_head dir = let ( cx , cy ) = decode ( snake . ( ! ptr_head ) ) in match dir with | South -> ( cx , ( cy + 1 ) mod 32 ) | North -> ( cx , ( cy - 1 + 32 ) mod 32 ) | West -> ( ( cx - 1 + 64 ) mod 64 , cy ) | East -> ( ( cx + 1 ) mod 64 , cy )
let draw_apple ( ) = let x , y = ! apple in Oled . draw x y true
let eats_apple ( ) = snake . ( ! ptr_head ) = encode ( ! apple )
let rec game_loop dir = let ( xh , yh ) as head = new_head dir in let ( xt , yt ) = decode ( snake . ( ! ptr_tail ) ) in let new_dir = button_direction dir in if ( eats_apple ( ) ) then begin apple := new_position ( ) ; incr size ; if ! size = Array . length snake then raise Win ; end else begin Oled . draw xt yt false...
let ( ) = init ( ) ; try game_loop South with Lose -> digital_write r LOW | Win -> digital_write g LOW
let direction = ref " right "
let speed = ref 100
let ticker : Html . interval_id ref = ref ( Html . window ## setInterval ( ( Js . wrap_callback ( fun _ -> ( ) ) ) , 10 . ) )
let fruit_cell : ( int * int ) ref = ref ( 15 , 15 )
let score = ref 0
let snake_cells = let arr = jsnew Js . array_empty ( ) in ignore ( arr ## push ( ( 10 , 14 ) ) ) ; ignore ( arr ## push ( ( 10 , 13 ) ) ) ; ignore ( arr ## push ( ( 10 , 12 ) ) ) ; ignore ( arr ## push ( ( 10 , 11 ) ) ) ; ignore ( arr ## push ( ( 10 , 10 ) ) ) ; ignore ( arr ## push ( ( 10 , 9 ) ) ) ; ignore ( arr ## p...
let snake_head = ref ( 10 , 14 )
let render_snake ( ) = let _ = ( jQ " td " ) ## removeClass ( Js . some ( _s " snakeCell " ) ) in let _ = ( jQ " td " ) ## removeClass ( Js . some ( _s " snakeHead " ) ) in for i = 0 to ( snake_cells ## length - 1 ) do let cell = Js . Optdef . to_option ( Js . array_get snake_cells i ) in let cell = match cell with | N...
let get_random_number limit = int_of_float ( ( Js . to_float ( Js . math ## random ( ) ) ) . * ( float_of_int ( limit mod limit ) ) )
let get_fruit_cell ( ) = fruit_cell := ( Random . int ( ( jQ " tr " ) ## length ) , Random . int ( ( jQ " tr : eq ( 0 ) > td " ) ## length ) )
let game_over ( ) = let fast = Js . some ( of_duration Fast ) and slow = Js . some ( of_duration Slow ) in let _ = ( jQ " div . gameOver " ) ## show ( fast , Js . null , Js . some ( Js . wrap_callback ( fun _ -> let this = get_element_by_id " gameOver " in this ## style ## top <- _s " 250 " ; ( jQ " this " ) ## animate...
let render_fruit_cell ( ) = ignore ( ( jQ " td " ) ## removeClass ( Js . some ( _s " fruitCell " ) ) ) ; ignore ( ( ( ( ( jQ " tr " ) ## eq ( fst ! fruit_cell ) ## find ( ( jQ " td " ) ) ) ## eq ( snd ! fruit_cell ) ) ## addClass ( _s " fruitCell " ) ) )
let rec start_game ( ) = ticker := window ## setInterval ( Js . wrap_callback ( fun ( ) -> update_snake_cell ( ) ) , float_of_int ! speed ) let snake_new_head = ref ( 0 , 0 ) in let ( ) = match ! direction with | " left " -> snake_new_head := fst ! snake_head , ( snd ! snake_head ) - 1 | " up " -> snake_new_head := ( f...
let get_new_direction keyCode = let codes = function | 37 -> " left " | 38 -> " up " | 39 -> " right " | 40 -> " down " | _ -> " " in let new_direction = codes keyCode and change_direction = ref true in let ( ) = match ! direction with | " left " -> change_direction := new_direction <> " right " | " up " -> change_dire...
let render_board ( ) = let rowhtml = ref " " in for i = 0 to size do rowhtml := ! rowhtml ^ " < td cellpadding " =\ 0 " \ cellspacing " =\ 0 " \></ td " > done ; let html = jsnew Js . array_empty ( ) in for i = 0 to size do ignore ( html ## push ( _s ( " < tr cellpadding " =\ 0 " \ cellspacing " =\ 0 " " \> ^ ! rowhtml...
let main ( ) = ( jQ " document " ) ## ready ( Js . wrap_callback ( fun _ -> render_board ( ) ; render_fruit_cell ( ) ; Html . document ## onkeydown <- ( Html . handler ( fun e -> get_new_direction ( e ## keyCode ) ; Js . _true ) ) ; start_game ( ) ) )
let _ = main ( )
module Colors = struct let black = Graphics . rgb 000 000 000 let green = Graphics . rgb 000 255 000 let head_color = Graphics . rgb 100 100 125 let apple_color = Graphics . rgb 255 000 000 let game_in_progress = Graphics . rgb 100 100 200 let game_lost = Graphics . rgb 200 100 100 let game_won = Graphics . rgb 100 200...
module Constants = struct let play_area_height = 400 let header_height = 50 let play_area_width = 450 let block_size = 18 end
let only_one : bool ref = ref false
let init_exn ( ) = let open Constants in if ! only_one then failwith " Can only call init_exn once " else only_one := true ; Graphics . open_graph ( Printf . sprintf " % dx % d " ( play_area_height + header_height ) play_area_width ) ; let height = play_area_height / block_size in let width = play_area_width / block_si...
let draw_block { Position . row ; col } ~ color = let open Constants in let col = col * block_size in let row = row * block_size in Graphics . set_color color ; Graphics . fill_rect ( col + 1 ) ( row + 1 ) ( block_size - 1 ) ( block_size - 1 ) ; ;
let draw_header ~ game_state = let open Constants in let header_color = match ( game_state : Game_state . t ) with | In_progress -> Colors . game_in_progress | Game_over _ -> Colors . game_lost | Win -> Colors . game_won in Graphics . set_color header_color ; Graphics . fill_rect 0 play_area_height play_area_width head...
let draw_play_area ( ) = let open Constants in Graphics . set_color Colors . black ; Graphics . fill_rect 0 0 play_area_width play_area_height ; ;
let draw_apple apple = let apple_location = Apple . location apple in draw_block apple_location ~ color : Colors . apple_color ; ;
let draw_snake snake_locations = List . iter snake_locations ~ f ( : draw_block ~ color : Colors . green ) ; draw_block ~ color : Colors . head_color ( List . hd_exn snake_locations ) ; ;
let render game = let snake = Game . snake game in let apple = Game . apple game in let game_state = Game . game_state game in let snake_locations = Snake . locations snake in draw_header ~ game_state ; draw_play_area ( ) ; draw_apple apple ; draw_snake snake_locations ; Graphics . display_mode true ; Graphics . synchr...
let read_key ( ) = if Graphics . key_pressed ( ) then Some ( Graphics . read_key ( ) ) else None
let step_n_times t n = List . fold ( List . range 0 n ) ~ init ( : Some t ) ~ f ( : fun t _ -> match t with | Some t -> step t | None -> failwith " can ' t call step when previous step returned [ None ] " ) ! ; ; let t = create ~ length : 5 in let t = step_n_times t 5 in Stdio . printf " { !% sexp : t option } \ n " %!...
type cc_site = { site_link : ( int * int ) option ; site_state : int option ; }
type cc_node = { node_type : int ; node_id_in_witness : int ; node_sites : cc_site array ; }
type connected_component = cc_node array
let rec agents_are_compatibles a b don = function | [ ] -> true | ( x , y ) :: q -> let o = a . ( x ) in let p = b . ( y ) in o . node_type = p . node_type && let i_ok = Tools . array_fold_left2i ( fun _ b x y -> b && match x . site_state , y . site_state with | Some a , Some b -> ( a = b ) | None , None -> true | ( So...
let classify_by_type sigs mix = let len = Signature . size sigs in let out = Array . make len ( 0 , [ ] ) in let classify id ag = let nb , ags = out . ( ag . node_type ) in out . ( ag . node_type ) <- ( succ nb , id :: ags ) in let ( ) = Array . iteri classify mix in out
let equal cbt_a a cbt_b b = match Tools . array_min_equal_not_null cbt_a cbt_b with | None -> false | Some ( [ ] , ags ) -> ags = [ ] | Some ( h1 :: _ , ags ) -> List . fold_left ( fun bool ag -> bool || agents_are_compatibles a b [ ] [ h1 , ag ] ) false ags
let coarse_hash cbt = Array . fold_right ( fun ( l , _ ) acc -> l + hash_prime * acc ) cbt 0
type t = ( int * ( int * int list ) array * connected_component ) list Mods . IntMap . t
let increment_in_snapshot ~ raw sigs x s = let cbt_x = classify_by_type sigs x in let hs = coarse_hash cbt_x in let l = Mods . IntMap . find_default [ ] hs s in let rec aux_increment = function | [ ] -> [ 1 , cbt_x , x ] | ( n , cbt_y , y as h ) :: t -> if equal cbt_x x cbt_y y then ( succ n , cbt_y , y ) :: t else h :...
let rec counter_value cc ( nid , sid ) count = let ag = cc . ( nid ) in Tools . array_fold_lefti ( fun id acc si -> if ( id = sid ) then acc else match si . site_link with | None -> acc | Some x -> counter_value cc x ( acc + 1 ) ) count ag . node_sites
let cc_to_user_cc ~ debugMode ~ raw sigs cc = let r = Renaming . empty ( ) in let ( cc_list , indexes , _ ) = Tools . array_fold_lefti ( fun i ( acc , indexes , pos ) ag -> match Signature . ports_if_counter_agent sigs ag . node_type with | None -> let indexes ' = if i = pos then indexes else match Renaming . add ~ deb...
let fold f x s = Mods . IntMap . fold ( fun _ l acc -> List . fold_left ( fun a ( nb , _ , cc ) -> f a nb cc ) acc l ) s x
let export ~ debugMode ~ raw sigs s = fold ( fun a x y -> ( x , cc_to_user_cc ~ debugMode ~ raw sigs y ) :: a ) [ ] s
module Version = struct type t = int let ( version_encoding : t Data_encoding . t ) = let open Data_encoding in obj1 ( req " version " int31 ) let current_version = 2 end
type error += | Incompatible_history_mode of { requested : History_mode . t ; stored : History_mode . t ; } | Invalid_export_block of { block : Block_hash . t option ; reason : [ ` Pruned | ` Pruned_pred | ` Unknown | ` Unknown_ancestor | ` Caboose | ` Genesis | ` Not_enough_pred ] ; } | Invalid_export_path of string |...
let ( ) = let open Data_encoding in register_error_kind ` Permanent ~ id " : snapshots . incompatible_export " ~ title " : Incompatible snapshot export " ~ description : " The requested history mode for the snapshot is not compatible with the \ given storage . " ~ pp ( : fun ppf ( requested , stored ) -> Format . fprin...
type metadata = { chain_name : Distributed_db_version . Name . t ; history_mode : History_mode . t ; block_hash : Block_hash . t ; level : Int32 . t ; timestamp : Time . Protocol . t ; context_elements : int ; }
let metadata_encoding = let open Data_encoding in conv ( fun { chain_name ; history_mode ; block_hash ; level ; timestamp ; context_elements ; } -> ( chain_name , history_mode , block_hash , level , timestamp , context_elements ) ) ( fun ( chain_name , history_mode , block_hash , level , timestamp , context_elements ) ...
type header = Version . t * metadata
type snapshot_header = Current_header of header
let pp_snapshot_header ppf = function | Current_header ( version , { chain_name ; history_mode ; block_hash ; level ; timestamp ; _ } ) -> Format . fprintf ppf " chain % a , block hash % a at level % ld , timestamp % a in % a ( snapshot \ version % d ) " Distributed_db_version . Name . pp chain_name Block_hash . pp blo...
let version = function Current_header ( version , _ ) -> version
type snapshot_format = Tar | Raw
let snapshot_format_encoding = Data_encoding . string_enum [ ( " Tar " , Tar ) ; ( " Raw " , Raw ) ]
let pp_snapshot_format ppf = function | Tar -> Format . fprintf ppf " tar ( single file ) " | Raw -> Format . fprintf ppf " directory "
type block_data = { block_header : Block_header . t ; operations : Operation . t list list ; predecessor_header : Block_header . t ; predecessor_block_metadata_hash : Block_metadata_hash . t option ; predecessor_ops_metadata_hash : Operation_metadata_list_list_hash . t option ; }
let block_data_encoding = let open Data_encoding in conv ( fun { block_header ; operations ; predecessor_header ; predecessor_block_metadata_hash ; predecessor_ops_metadata_hash ; } -> ( operations , block_header , predecessor_header , predecessor_block_metadata_hash , predecessor_ops_metadata_hash ) ) ( fun ( operatio...
let default_snapshot_filename metadata = let { chain_name ; block_hash ; level ; history_mode ; _ } = metadata in let default_name = Format . asprintf " % a -% a -% ld . % a " Distributed_db_version . Name . pp chain_name Block_hash . pp block_hash level History_mode . pp_short history_mode in let unique_name name = le...
let ensure_valid_tmp_snapshot_path snapshot_tmp_dir = fail_when ( Sys . file_exists ( Naming . dir_path snapshot_tmp_dir ) ) ( Cannot_create_tmp_export_directory ( Naming . dir_path snapshot_tmp_dir ) )
let ensure_valid_export_path = function | Some path -> fail_when ( Sys . file_exists path ) ( Invalid_export_path path ) | None -> return_unit
let clean_all paths = List . iter_s ( fun path -> Unit . catch_s ( fun ( ) -> if Sys . is_directory path then Lwt_utils_unix . remove_dir path else Lwt_unix . unlink path ) ) paths
module Onthefly : sig type file type o type i val open_out : file : string -> o Lwt . t val close_out : o -> unit Lwt . t val add_raw_and_finalize : o -> f ( : Lwt_unix . file_descr -> ' a Lwt . t ) -> filename : string -> ' a Lwt . t val add_file_and_finalize : o -> file : string -> filename : string -> unit Lwt . t v...
module type EXPORTER = sig type t val init : string option -> t tzresult Lwt . t val write_block_data : t -> predecessor_header : Block_header . t -> predecessor_block_metadata_hash : Block_metadata_hash . t option -> predecessor_ops_metadata_hash : Operation_metadata_list_list_hash . t option -> export_block : Store ....
module Raw_exporter : EXPORTER = struct type t = { snapshot_dir : string option ; snapshot_tmp_dir : [ ` Snapshot_tmp_dir ] Naming . directory ; snapshot_cemented_dir : [ ` Cemented_blocks_dir ] Naming . directory ; snapshot_protocol_dir : [ ` Protocol_dir ] Naming . directory ; } let init snapshot_dir = let open Lwt_t...
module Tar_exporter : EXPORTER = struct type t = { snapshot_file : string option ; snapshot_tar : [ ` Tar_archive ] Naming . directory ; snapshot_tar_file : [ ` Snapshot_tar_file ] Naming . file ; snapshot_tmp_dir : [ ` Snapshot_tmp_dir ] Naming . directory ; snapshot_tmp_cemented_dir : [ ` Cemented_blocks_dir ] Naming...
module type Snapshot_exporter = sig type t val export : ? snapshot_path : string -> ? rolling : bool -> block : Block_services . block -> store_dir : string -> context_dir : string -> chain_name : Distributed_db_version . Name . t -> Genesis . t -> unit tzresult Lwt . t end
module Make_snapshot_exporter ( Exporter : EXPORTER ) : Snapshot_exporter = struct type t = Exporter . t let init = Exporter . init let copy_cemented_blocks snapshot_exporter ~ should_filter_indexes ( files : Cemented_block_store . cemented_blocks_file list ) = let open Lwt_tzresult_syntax in let open Cemented_block_st...
module type LOADER = sig type t val load : string -> t Lwt . t val load_snapshot_header : t -> header tzresult Lwt . t val close : t -> unit Lwt . t end
module Raw_loader : LOADER = struct type t = { snapshot_dir : [ ` Snapshot_dir ] Naming . directory } let load snapshot_path = let snapshot_dir = Naming . snapshot_dir ~ snapshot_path ( ) in Lwt . return { snapshot_dir } let load_snapshot_version t = let open Lwt_result_syntax in let snapshot_file = Naming . ( snapshot...
module Tar_loader : LOADER = struct type t = { tar : Onthefly . i ; snapshot_file : [ ` Snapshot_file ] Naming . file ; snapshot_tar : [ ` Tar_archive ] Naming . directory ; } let load snapshot_path = let open Lwt_syntax in let snapshot_dir = Naming . snapshot_dir ~ snapshot_path ( : Filename . dirname snapshot_path ) ...
module type Snapshot_loader = sig type t val load_snapshot_header : snapshot_path : string -> header tzresult Lwt . t end
module Make_snapshot_loader ( Loader : LOADER ) : Snapshot_loader = struct type t = Loader . t let load = Loader . load let close = Loader . close let load_snapshot_header ~ snapshot_path = let open Lwt_syntax in let * loader = load snapshot_path in trace ( Wrong_snapshot_file { filename = snapshot_path } ) @@ protect ...
module type IMPORTER = sig type t val init : snapshot_path : string -> dst_store_dir [ ` : Store_dir ] Naming . directory -> Chain_id . t -> t Lwt . t val load_snapshot_header : t -> header tzresult Lwt . t val load_block_data : t -> block_data tzresult Lwt . t val restore_context : t -> Context . index -> expected_con...
module Raw_importer : IMPORTER = struct type t = { snapshot_dir : [ ` Snapshot_dir ] Naming . directory ; snapshot_cemented_dir : [ ` Cemented_blocks_dir ] Naming . directory ; snapshot_protocol_dir : [ ` Protocol_dir ] Naming . directory ; dst_cemented_dir : [ ` Cemented_blocks_dir ] Naming . directory ; dst_protocol_...
module Tar_importer : IMPORTER = struct type t = { snapshot_file : [ ` Snapshot_file ] Naming . file ; snapshot_tar : [ ` Tar_archive ] Naming . directory ; snapshot_cemented_blocks_dir : [ ` Cemented_blocks_dir ] Naming . directory ; dst_store_dir : [ ` Store_dir ] Naming . directory ; dst_chain_dir : [ ` Chain_dir ] ...
module type Snapshot_importer = sig type t val read_snapshot_header : t -> header tzresult Lwt . t val import : snapshot_path : string -> ? patch_context ( : Context . t -> Context . t tzresult Lwt . t ) -> ? block : Block_hash . t -> ? check_consistency : bool -> dst_store_dir [ ` : Store_dir ] Naming . directory -> d...
module Make_snapshot_importer ( Importer : IMPORTER ) : Snapshot_importer = struct type t = Importer . t let init = Importer . init let close = Importer . close let read_snapshot_header = Importer . load_snapshot_header let restore_cemented_blocks ( ? check_consistency = true ) ~ dst_chain_dir ~ genesis_hash snapshot_i...
let snapshot_file_kind ~ snapshot_path = let open Lwt_tzresult_syntax in let is_valid_uncompressed_snapshot file = let ( module Loader ) = ( module Make_snapshot_loader ( Tar_loader ) : Snapshot_loader ) in Error_monad . catch_es ( fun ( ) -> let * _header = Loader . load_snapshot_header ~ snapshot_path ( : Naming . fi...
let export ? snapshot_path export_format ? rolling ~ block ~ store_dir ~ context_dir ~ chain_name genesis = let ( module Exporter ) = match export_format with | Tar -> ( module Make_snapshot_exporter ( Tar_exporter ) : Snapshot_exporter ) | Raw -> ( module Make_snapshot_exporter ( Raw_exporter ) : Snapshot_exporter ) i...
let read_snapshot_header ~ snapshot_path = let open Lwt_tzresult_syntax in let * kind = snapshot_file_kind ~ snapshot_path in let ( module Loader ) = match kind with | Tar -> ( module Make_snapshot_loader ( Tar_loader ) : Snapshot_loader ) | Raw -> ( module Make_snapshot_loader ( Raw_loader ) : Snapshot_loader ) in let...
let import ~ snapshot_path ? patch_context ? block ? check_consistency ~ dst_store_dir ~ dst_context_dir ~ chain_name ~ user_activated_upgrades ~ user_activated_protocol_overrides genesis = let open Lwt_tzresult_syntax in let * kind = snapshot_file_kind ~ snapshot_path in let ( module Importer ) = match kind with | Tar...
module Event = struct include Internal_event . Simple let section = [ " node " ; " snapshots " ] let export_info = declare_2 ~ section ~ level : Notice ~ name " : export_info " ~ msg : " exporting a snapshot in { history_mode } mode , targeting block hash \ { block } " ~ pp1 : History_mode . pp_short ( " history_mode "...
let set_eval_constraints b = Runner . eval_constraints := b