Skip to content

Commit

Permalink
Added Battleship.Pirate.generate_id(amount_words, number_max) for gen…
Browse files Browse the repository at this point in the history
…erating game ids like "ahoy-matey-4523".
  • Loading branch information
h4cc committed Apr 27, 2016
1 parent d09b605 commit 33c5c99
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ config :logger, :console,

config :battleship,
ga_tracking_code: "",
id_length: 8
id_words: 2, # Number of words used in game ids "ahoy-matey"
id_number_max: 9999 # Maximal number >= 100 after the words "ahoey-matey-9999".

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
Expand Down
8 changes: 3 additions & 5 deletions lib/battleship.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule Battleship do
use Application

@id_length Application.get_env(:battleship, :id_length)
@id_words Application.get_env(:battleship, :id_words)
@id_number_max Application.get_env(:battleship, :id_number_max)

# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
Expand Down Expand Up @@ -34,9 +35,6 @@ defmodule Battleship do
Generates unique id for the game
"""
def generate_id do
@id_length
|> :crypto.strong_rand_bytes
|> Base.url_encode64()
|> binary_part(0, @id_length)
Battleship.Pirate.generate_id(@id_words, @id_number_max)
end
end
89 changes: 89 additions & 0 deletions lib/battleship/pirate.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
defmodule Battleship.Pirate do
@moduledoc """
Pirate stuff
"""

@pirate_words [
"ahoy",
"matey",

# Source: https://github.com/mikewesthad/pirate-speak/blob/master/lib/pirate-speak.js
"helm",
"grog",
"vast",
"coin",
"coins",
"admiral",
"rum",
"barrel",
"lad",
"mate",
"parrot",
"hornswaggle",
"hails",
"shipshape",
"shanty",
"keelhaul",
"doubloon",
"crew",
"eyepatch",
"debt",
"wench",
"wenches",
"grub",
"shipmate",
"sail",
"maties",
"bluderbuss",
"hook",
"yoho",
"yohoho",
"yohohoho",
"fleebag",
"sunk",
"isle",
"brig",
"lasses",
"lass",
"blimey",
"parchment",
"scallywags",
"starboard",
"cargo",
"yarr",
"puny",
"swoggler",
"booty",
"beauties",
"duty",
"aye",
"ye",
"yer",

# Source: https://gist.github.com/devlaers/1308006#file-pirate_phrases-txt
"yo-ho-ho",
"ahoy",
"avast",
"arrr",
"blaggart",
"foul",
"whar",
"comely",
"broadside",
"fleabag",
"skull",
"scuppers",
"buried",
"treasure",
]

@doc """
Generates a id like "ahoy-matey-4523"
"""
def generate_id(amount_words, number_max) when amount_words >= 1 and number_max >= 100 do
words = @pirate_words |> Enum.shuffle |> Enum.take(amount_words)
[random_number] = Enum.take_random(100..number_max, 1)
Enum.join(words, "-") <> "-" <> to_string(random_number)
end

end

0 comments on commit 33c5c99

Please sign in to comment.