Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 159 lines (147 sloc) 4.82 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
-module(bake).
-compile(export_all).
-import(bake_db).

-define(Items, [
    {"flour", {10, " lbs"}},
    {"sugar", {5, " lbs"}},
    {"butter", {4, " lbs"}},
    {"vanilla", {1, " cup"}},
    {"coconut", {1, " lb"}},
    {"chocolate", {2, " lbs"}},
    {"sprinkles", {1, " container"}},
    {"apples", {1, " bushel"}}]).

start() ->
    c:c(bake_db),
    bake_db:start(),
    io:format("\n" ++
              "Welcome to Bake!\n" ++
              "\n" ++
              "Commands\n" ++
              "--------\n" ++
              "start\n" ++
              "stop\n" ++
              "points\n" ++
              "inventory\n" ++
              "\n"),
    Name = get_line_tokens("Enter your name.\n"),
    Host = get_line_tokens("Enter your host. Defaults to localhost.\n"),
    case Host of
        "" -> H = "localhost";
        _ -> H = Host
    end,
    spawn(bake, connect, [H, Name]),
    io:format("Welcome " ++ Name ++ "! " ++ "You have 50 points.\n"),
    main().
    
connect(Host, Name) ->
    {ok, Sock} = gen_tcp:connect(Host, 10104, [binary, {active, false}]),
    bake_db:create_player(Name, Sock),
    loop(Sock).
    
% Loop to receive incoming messages
loop(Sock) ->
    case gen_tcp:recv(Sock, 0) of
        {ok, Packet} ->
            Tokens = string:tokens(binary_to_list(Packet), " "),
            %io:format("Packet Tokens: ~p~n", [Tokens]),
            loop_lookup(Tokens),
            loop(Sock);
        {error, closed} ->
            bake_db:clear_tables()
    end.

main() ->
    lookup(get_line_tokens("bake> ")),
    main().

msg(Data) ->
    gen_tcp:send(bake_db:get_socket(), Data).

% Finds user input commands
lookup(["start"]) ->
    msg("start");
lookup(["stop"]) ->
    gen_tcp:close(bake_db:get_socket()),
    exit(normal);
lookup(["quit"]) ->
    gen_tcp:close(bake_db:get_socket()),
    exit(normal);
lookup(["msg"|T]) ->
    msg(string:join(T, " "));
lookup(["points"]) ->
    Points = bake_db:get_points(),
    io:format("Points: ~p~n", [Points]);
lookup(["inventory"]) ->
    Inventory = bake_db:get_inventory(),
    io:format("Inventory: ~p~n", [Inventory]);
lookup(_) ->
    ok.
   
loop_lookup(["offer", "points"]) ->
    Points = bake_db:get_points(),
    case Points of
        0 ->
            io:format("You've run out of points. GAME OVER. ~n"),
            lookup(["stop"]);
        _ ->
            Line = get_line_tokens("Currently up for auction: 15 points.\n"),
            case Line of
                [H|_] ->
                    Bid = list_to_integer(H),
                    bid(Bid, Points);
                [] ->
                    loop_lookup(["offer", "points"])
            end
    end;
loop_lookup(["offer", Item]) ->
    Points = bake_db:get_points(),
    case Points of
        0 ->
            io:format("You've run out of points. GAME OVER. ~n"),
            lookup(["stop"]);
        _ ->
            {Amount, Units} = proplists:get_value(Item, ?Items),
            Line = get_line_tokens(
                "Currently up for auction: " ++ integer_to_list(Amount) ++ Units ++ " of " ++ Item ++ ".\n" ++
                "Enter your bid...\n"),
            case Line of
                [H|_] ->
                    Bid = list_to_integer(H),
                    bid(Bid, Points);
                [] ->
                    loop_lookup(["offer", Item])
            end
    end;
loop_lookup(["bidwin", Bid, Item]) ->
    bake_db:update_points(Bid),
    case Item of
        "points" ->
            bake_db:update_points("-15"),
            ok;
        X ->
            {Amount, _} = proplists:get_value(Item, ?Items),
            bake_db:update_inventory(X, Amount)
    end,
    io:format("Your bid of " ++ Bid ++ " points won!\n"),
    Win = bake_db:is_winner(),
    case Win of
        true ->
            Name = bake_db:get_name(),
            msg(Name ++ " has won!"),
            io:format("Congratulations! You can open a bakery. ~n"),
            lookup(["stop"]);
        false ->
            ok
    end;
    
loop_lookup(["bidlost", Bid, _]) ->
    io:format("Your bid lost. Winning bid was " ++ Bid ++ " points.\n");

loop_lookup(Data) ->
    io:format("~p~n", [string:join(Data, " ")]),
    ok.

bid(Bid, Points) ->
    case Bid of
        I when I > Points ->
            [NewBid] = get_line_tokens(
                "You only have " ++ integer_to_list(Points) ++ " points.\n"),
            bid(list_to_integer(NewBid), Points);
        I when I =< Points ->
            msg("bid " ++ integer_to_list(Bid));
        _ ->
            ok
    end.

get_line_tokens(Text) ->
    string:tokens(string:strip(io:get_line(Text), right, $\n), " ").
Something went wrong with that request. Please try again.