New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Battleship.Pirate.generate_id #7

Merged
merged 2 commits into from May 3, 2016
Jump to file or symbol
Failed to load files and symbols.
+347 −10
Diff settings

Always

Just for now

View
@@ -21,7 +21,9 @@ config :logger, :console,
config :battleship,
ga_tracking_code: "",
id_length: 8
id_length: 8, # Number of bytes for generic unique id
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.
View
@@ -2,6 +2,10 @@ 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)
@name_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
@@ -33,10 +37,24 @@ defmodule Battleship do
@doc """
Generates unique id for the game
"""
def generate_id do
def generate_player_id do
@id_length
|> :crypto.strong_rand_bytes
|> Base.url_encode64()
|> binary_part(0, @id_length)
end
@doc """
Generates unique id for the game
"""
def generate_game_id do
Battleship.Pirate.generate_id(@id_words, @id_number_max)
end
@doc """
Generates unique name for a player
"""
def generate_name do
Battleship.Pirate.generate_name(@name_number_max)
end
end
View
@@ -0,0 +1,317 @@
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",
]
@pirate_names [
# Source: https://en.wikipedia.org/wiki/List_of_fictional_pirates
"Portgas D. Ace",
"Captain Jesamiah Acorne",
"Morgan Adams",
"Johannes Alberic",
"Captain Anton",
"Atomsk",
"Jean Benoit Aubery",
"Captain Henry Avery",
"Tom Ayrton",
"Seth Balmore",
"Balthier",
"Hector Barbossa",
"Captain Barrett",
"Belit",
"Ben Ali",
"Biere Du Boucanier",
"Bikke",
"Black Bellamy",
"Captain Bizzarly",
"The Black Corsair",
"Black John Licorice",
"The Black Pirate",
"Black Vulmea",
"Roger Blease",
"Captain Blood",
"Bloody Bill",
"Captain Bloth",
"Billy Bones",
"Bras Prique",
"Tom Bristol",
"Captain Broom",
"Douglas Dawg Brown",
"Paul Burchill",
"Cannonball",
"Captain Gavin Capacitor",
"Captain Carryall",
"Captain Carlton",
"Capn Crunch",
"Captain Claw",
"Captain Clegg",
"Captain Firebrand",
"Captain Skunkbeard",
"Conan the Barbarian",
"John Connor",
"Captain Corroboc",
"Cortez",
"Conrad",
"Cuthbert Conyers",
"Henriette Cooper",
"Captain Henry Crow",
"Jack Crow",
"Captain Jack Dancer",
"Arabella Drummond",
"Ragnar Danneskjoeld",
"Captain Angstrom Darkwater",
"Charlotte de Berry",
"DeFlorres",
"Cervantes de Leon",
"Willem van der Decken",
"Edward Kenway",
"Elisabet Ramsey",
"Emperor Grog",
"Captain Feathersword",
"The Fishhawk",
"Patty Fleur",
"Captain Flint",
"The Flying Dutchman",
"Foxy the Pirate",
"Jose Gaspar",
"Gilbert",
"Gunpowder Gertie",
"Guinea Pirate",
"Captain Gutt",
"Gokaigers",
"Gol D. Roger",
"Guybrush Threepwood",
"Gangplank",
"Captain Harlock",
"Bob Harvey",
"Dutch Hodgers",
"Captain James Hook",
"Captain Charles Hunter",
"Captain Ironhook",
"Captain Isabela",
"B. Jenet",
"Jezebel Jack",
"Jonathan Jones",
"Don Karnage",
"Kongre",
"Korsars",
"King of the Atlantic",
"Captain Arthur Kirkland",
"Drongo Kane",
"Ker Karraje",
"Maquesta Kar-Thon",
"Captain Kennit",
"Chung Khan",
"Nancy Kington",
"Denise Lafitte",
"Johnny LaFitte",
"Locke Lamora",
"Trafalgar Law",
"John Milton",
"LeChuck",
"Missee Lee",
"Captain LeFwee",
"Captain Levasseur",
"Captain Red Ned Lynch",
"Monkey D. Luffy",
"James Durie",
"Mad Jack the Pirate",
"Malgo",
"Jean Malot",
"Morgan Moonscar McWright",
"McGrath",
"John Merrick",
"MooBeard",
"Mulligan",
"Murdoch Juan",
"Marquise Spinneret Mindfang",
"Manjanungo",
"Elaine Marley",
"Bosun Moon",
"Captain Morgan",
"Andrew Murray",
"Captain Mutiny",
"Harry Markel",
"Captain Marika Kato",
"Nabel",
"Carson Napier",
"Captain Nemo",
"Wolf ODonnell",
"One-Eyed Jane",
"One-Eared Pirate",
"One-Eyed Willy",
"Orm the Red",
"Painty the Pirate",
"Sneaky Pete",
"Pirate Pimm",
"Pirate Beard",
"The Pirate Captain",
"Piet Piraat",
"Captain Pugwash",
"Quartermaster",
"Ragnar Danneskjoeld",
"Sir Raleigh",
"Red Rodney Radcliffe",
"Red Rackham",
"Red Sonja",
"Redbeard",
"Risky Boots",
"Ridley",
"Roan Tom",
"Robert the Terrible",
"Jonathan Rockhal",
"Captain Roger",
"Kaptain K. Rool",
"Russell",
"Rusty Pete",
"Sailor John",
"Captain Stingaree",
"Captain Samuel Salt",
"Captain Shakespeare",
"Captain John Sharkey",
"Captain Sabertooth",
"Sandokan",
"Captain Scarblade",
"Faris Scherwiz",
"Captain Scratch",
"Sea Hawk",
"Searats",
"Sed",
"Minerva Sharpe",
"Sir Arthur Richards of Kent",
"Sissy Le Poop",
"Smelly Pete",
"Shark Bait",
"Captain Skunkbeard",
"Long Joan Silver",
"Long John Silver",
"Kaptain Skurvy",
"Captain Romulus Slag",
"Solomon",
"Steve the Pirate",
"Stickybeard",
"Syndicate of Pirates",
"Captain Syrup",
"Vikram Szpirglas",
"Captain Spade",
"Henry Steel",
"Captain Jack Sparrow",
"Captain Ishmael Squint",
"Elizabeth Swann",
"Taicoon Chang",
"Taicoon Wu",
"Jean Tannen",
"Captain Teague",
"Captain Tempest",
"Tetra",
"Theseus",
"Thoas",
"Abraham Tuizentfloot",
"Gammis Turek",
"William Turner",
"Guybrush Threepwood",
"Captain Vasquez",
"Heinrich Von Marzipan",
"Vyse",
"Jamie Waring",
"Duchy of Waldegren",
"Captain Whisker",
"Yanez de Gomera",
"Yellowbeard",
"Zack",
"Zanzibar",
]
@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
@doc """
Generates a name like "Smelly Pete 8255"
"""
def generate_player_name(number_max) when number_max >= 100 do
[name] = @pirate_names |> Enum.shuffle |> Enum.take(1)
[random_number] = Enum.take_random(100..number_max, 1)
name <> " " <> to_string(random_number)
end
end
@@ -4,10 +4,10 @@ defmodule Battleship.GameChannelTest do
alias Battleship.Game.Supervisor, as: GameSupervisor
alias Battleship.{PlayerSocket, GameChannel, Game}
@player_id Battleship.generate_id
@player_id Battleship.generate_player_id
setup do
game_id = Battleship.generate_id
game_id = Battleship.generate_game_id
{:ok, game} = GameSupervisor.create_game(game_id)
{:ok, socket} = connect(PlayerSocket, %{"id" => @player_id})
@@ -3,7 +3,7 @@ defmodule Battleship.PlayerSocketTest do
alias Battleship.{PlayerSocket}
@id Battleship.generate_id
@id Battleship.generate_player_id
setup do
{:ok, socket} = connect(PlayerSocket, %{"id" => @id})
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.