moves last server API logic inside its own module
This commit is contained in:
265
main.js
265
main.js
@@ -6617,7 +6617,19 @@ var $author$project$Main$applyUpdate = F2(
|
|||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var $elm$json$Json$Encode$int = _Json_wrap;
|
var $elm$core$Set$Set_elm_builtin = function (a) {
|
||||||
|
return {$: 'Set_elm_builtin', a: a};
|
||||||
|
};
|
||||||
|
var $elm$core$Set$empty = $elm$core$Set$Set_elm_builtin($elm$core$Dict$empty);
|
||||||
|
var $elm$core$Set$insert = F2(
|
||||||
|
function (key, _v0) {
|
||||||
|
var dict = _v0.a;
|
||||||
|
return $elm$core$Set$Set_elm_builtin(
|
||||||
|
A3($elm$core$Dict$insert, key, _Utils_Tuple0, dict));
|
||||||
|
});
|
||||||
|
var $elm$core$Set$fromList = function (list) {
|
||||||
|
return A3($elm$core$List$foldl, $elm$core$Set$insert, $elm$core$Set$empty, list);
|
||||||
|
};
|
||||||
var $elm$core$Maybe$map = F2(
|
var $elm$core$Maybe$map = F2(
|
||||||
function (f, maybe) {
|
function (f, maybe) {
|
||||||
if (maybe.$ === 'Just') {
|
if (maybe.$ === 'Just') {
|
||||||
@@ -6652,134 +6664,6 @@ var $author$project$Main$itemInSelection = F2(
|
|||||||
$elm$core$Set$member(item.id),
|
$elm$core$Set$member(item.id),
|
||||||
selection));
|
selection));
|
||||||
});
|
});
|
||||||
var $elm$json$Json$Encode$list = F2(
|
|
||||||
function (func, entries) {
|
|
||||||
return _Json_wrap(
|
|
||||||
A3(
|
|
||||||
$elm$core$List$foldl,
|
|
||||||
_Json_addEntry(func),
|
|
||||||
_Json_emptyArray(_Utils_Tuple0),
|
|
||||||
entries));
|
|
||||||
});
|
|
||||||
var $elm$json$Json$Encode$null = _Json_encodeNull;
|
|
||||||
var $elm$json$Json$Encode$object = function (pairs) {
|
|
||||||
return _Json_wrap(
|
|
||||||
A3(
|
|
||||||
$elm$core$List$foldl,
|
|
||||||
F2(
|
|
||||||
function (_v0, obj) {
|
|
||||||
var k = _v0.a;
|
|
||||||
var v = _v0.b;
|
|
||||||
return A3(_Json_addField, k, v, obj);
|
|
||||||
}),
|
|
||||||
_Json_emptyObject(_Utils_Tuple0),
|
|
||||||
pairs));
|
|
||||||
};
|
|
||||||
var $author$project$Main$targetItemsFor = F2(
|
|
||||||
function (mode, model) {
|
|
||||||
switch (mode.$) {
|
|
||||||
case 'Add':
|
|
||||||
return _List_Nil;
|
|
||||||
case 'Buy':
|
|
||||||
return A2($elm$core$Maybe$withDefault, _List_Nil, model.merchantItems);
|
|
||||||
case 'Sell':
|
|
||||||
return A2($elm$core$Maybe$withDefault, _List_Nil, model.loot);
|
|
||||||
default:
|
|
||||||
return A2($elm$core$Maybe$withDefault, _List_Nil, model.groupLoot);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
var $author$project$Main$buildPayload = F2(
|
|
||||||
function (mode, model) {
|
|
||||||
var items = A2(
|
|
||||||
$elm$core$List$filter,
|
|
||||||
$author$project$Main$itemInSelection(model.state.selection),
|
|
||||||
A2($author$project$Main$targetItemsFor, mode, model));
|
|
||||||
switch (mode.$) {
|
|
||||||
case 'Buy':
|
|
||||||
return $elm$json$Json$Encode$object(
|
|
||||||
_List_fromArray(
|
|
||||||
[
|
|
||||||
_Utils_Tuple2(
|
|
||||||
'items',
|
|
||||||
A2(
|
|
||||||
$elm$json$Json$Encode$list,
|
|
||||||
function (i) {
|
|
||||||
return A2(
|
|
||||||
$elm$json$Json$Encode$list,
|
|
||||||
$elm$core$Basics$identity,
|
|
||||||
_List_fromArray(
|
|
||||||
[
|
|
||||||
$elm$json$Json$Encode$int(i.id),
|
|
||||||
$elm$json$Json$Encode$null
|
|
||||||
]));
|
|
||||||
},
|
|
||||||
items)),
|
|
||||||
_Utils_Tuple2('global_mod', $elm$json$Json$Encode$null)
|
|
||||||
]));
|
|
||||||
case 'Sell':
|
|
||||||
return $elm$json$Json$Encode$object(
|
|
||||||
_List_fromArray(
|
|
||||||
[
|
|
||||||
_Utils_Tuple2(
|
|
||||||
'items',
|
|
||||||
A2(
|
|
||||||
$elm$json$Json$Encode$list,
|
|
||||||
function (i) {
|
|
||||||
return A2(
|
|
||||||
$elm$json$Json$Encode$list,
|
|
||||||
$elm$core$Basics$identity,
|
|
||||||
_List_fromArray(
|
|
||||||
[
|
|
||||||
$elm$json$Json$Encode$int(i.id),
|
|
||||||
$elm$json$Json$Encode$null
|
|
||||||
]));
|
|
||||||
},
|
|
||||||
items)),
|
|
||||||
_Utils_Tuple2('global_mod', $elm$json$Json$Encode$null)
|
|
||||||
]));
|
|
||||||
case 'Grab':
|
|
||||||
return $elm$json$Json$Encode$object(
|
|
||||||
_List_fromArray(
|
|
||||||
[
|
|
||||||
_Utils_Tuple2(
|
|
||||||
'items',
|
|
||||||
A2(
|
|
||||||
$elm$json$Json$Encode$list,
|
|
||||||
function (i) {
|
|
||||||
return $elm$json$Json$Encode$int(i.id);
|
|
||||||
},
|
|
||||||
items)),
|
|
||||||
_Utils_Tuple2('global_mod', $elm$json$Json$Encode$null)
|
|
||||||
]));
|
|
||||||
default:
|
|
||||||
return $elm$json$Json$Encode$object(
|
|
||||||
_List_fromArray(
|
|
||||||
[
|
|
||||||
_Utils_Tuple2(
|
|
||||||
'items',
|
|
||||||
A2(
|
|
||||||
$elm$json$Json$Encode$list,
|
|
||||||
function (i) {
|
|
||||||
return $elm$json$Json$Encode$int(i.id);
|
|
||||||
},
|
|
||||||
items)),
|
|
||||||
_Utils_Tuple2('global_mod', $elm$json$Json$Encode$null)
|
|
||||||
]));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
var $elm$core$Set$Set_elm_builtin = function (a) {
|
|
||||||
return {$: 'Set_elm_builtin', a: a};
|
|
||||||
};
|
|
||||||
var $elm$core$Set$empty = $elm$core$Set$Set_elm_builtin($elm$core$Dict$empty);
|
|
||||||
var $elm$core$Set$insert = F2(
|
|
||||||
function (key, _v0) {
|
|
||||||
var dict = _v0.a;
|
|
||||||
return $elm$core$Set$Set_elm_builtin(
|
|
||||||
A3($elm$core$Dict$insert, key, _Utils_Tuple0, dict));
|
|
||||||
});
|
|
||||||
var $elm$core$Set$fromList = function (list) {
|
|
||||||
return A3($elm$core$List$foldl, $elm$core$Set$insert, $elm$core$Set$empty, list);
|
|
||||||
};
|
|
||||||
var $elm$core$Debug$log = _Debug_log;
|
var $elm$core$Debug$log = _Debug_log;
|
||||||
var $elm$core$Platform$Cmd$none = $elm$core$Platform$Cmd$batch(_List_Nil);
|
var $elm$core$Platform$Cmd$none = $elm$core$Platform$Cmd$batch(_List_Nil);
|
||||||
var $elm$browser$Browser$Navigation$pushUrl = _Browser_pushUrl;
|
var $elm$browser$Browser$Navigation$pushUrl = _Browser_pushUrl;
|
||||||
@@ -6883,6 +6767,105 @@ var $author$project$Api$apiResponseDecoder = A5(
|
|||||||
$elm$json$Json$Decode$list($author$project$Api$updatesDecoder))),
|
$elm$json$Json$Decode$list($author$project$Api$updatesDecoder))),
|
||||||
$elm$json$Json$Decode$maybe(
|
$elm$json$Json$Decode$maybe(
|
||||||
A2($elm$json$Json$Decode$field, 'errors', $elm$json$Json$Decode$string)));
|
A2($elm$json$Json$Decode$field, 'errors', $elm$json$Json$Decode$string)));
|
||||||
|
var $elm$json$Json$Encode$int = _Json_wrap;
|
||||||
|
var $elm$json$Json$Encode$list = F2(
|
||||||
|
function (func, entries) {
|
||||||
|
return _Json_wrap(
|
||||||
|
A3(
|
||||||
|
$elm$core$List$foldl,
|
||||||
|
_Json_addEntry(func),
|
||||||
|
_Json_emptyArray(_Utils_Tuple0),
|
||||||
|
entries));
|
||||||
|
});
|
||||||
|
var $elm$json$Json$Encode$null = _Json_encodeNull;
|
||||||
|
var $elm$json$Json$Encode$object = function (pairs) {
|
||||||
|
return _Json_wrap(
|
||||||
|
A3(
|
||||||
|
$elm$core$List$foldl,
|
||||||
|
F2(
|
||||||
|
function (_v0, obj) {
|
||||||
|
var k = _v0.a;
|
||||||
|
var v = _v0.b;
|
||||||
|
return A3(_Json_addField, k, v, obj);
|
||||||
|
}),
|
||||||
|
_Json_emptyObject(_Utils_Tuple0),
|
||||||
|
pairs));
|
||||||
|
};
|
||||||
|
var $author$project$Api$buildPayload = F2(
|
||||||
|
function (mode, items) {
|
||||||
|
switch (mode.$) {
|
||||||
|
case 'Buy':
|
||||||
|
return $elm$json$Json$Encode$object(
|
||||||
|
_List_fromArray(
|
||||||
|
[
|
||||||
|
_Utils_Tuple2(
|
||||||
|
'items',
|
||||||
|
A2(
|
||||||
|
$elm$json$Json$Encode$list,
|
||||||
|
function (i) {
|
||||||
|
return A2(
|
||||||
|
$elm$json$Json$Encode$list,
|
||||||
|
$elm$core$Basics$identity,
|
||||||
|
_List_fromArray(
|
||||||
|
[
|
||||||
|
$elm$json$Json$Encode$int(i.id),
|
||||||
|
$elm$json$Json$Encode$null
|
||||||
|
]));
|
||||||
|
},
|
||||||
|
items)),
|
||||||
|
_Utils_Tuple2('global_mod', $elm$json$Json$Encode$null)
|
||||||
|
]));
|
||||||
|
case 'Sell':
|
||||||
|
return $elm$json$Json$Encode$object(
|
||||||
|
_List_fromArray(
|
||||||
|
[
|
||||||
|
_Utils_Tuple2(
|
||||||
|
'items',
|
||||||
|
A2(
|
||||||
|
$elm$json$Json$Encode$list,
|
||||||
|
function (i) {
|
||||||
|
return A2(
|
||||||
|
$elm$json$Json$Encode$list,
|
||||||
|
$elm$core$Basics$identity,
|
||||||
|
_List_fromArray(
|
||||||
|
[
|
||||||
|
$elm$json$Json$Encode$int(i.id),
|
||||||
|
$elm$json$Json$Encode$null
|
||||||
|
]));
|
||||||
|
},
|
||||||
|
items)),
|
||||||
|
_Utils_Tuple2('global_mod', $elm$json$Json$Encode$null)
|
||||||
|
]));
|
||||||
|
case 'Grab':
|
||||||
|
return $elm$json$Json$Encode$object(
|
||||||
|
_List_fromArray(
|
||||||
|
[
|
||||||
|
_Utils_Tuple2(
|
||||||
|
'items',
|
||||||
|
A2(
|
||||||
|
$elm$json$Json$Encode$list,
|
||||||
|
function (i) {
|
||||||
|
return $elm$json$Json$Encode$int(i.id);
|
||||||
|
},
|
||||||
|
items)),
|
||||||
|
_Utils_Tuple2('global_mod', $elm$json$Json$Encode$null)
|
||||||
|
]));
|
||||||
|
default:
|
||||||
|
return $elm$json$Json$Encode$object(
|
||||||
|
_List_fromArray(
|
||||||
|
[
|
||||||
|
_Utils_Tuple2(
|
||||||
|
'items',
|
||||||
|
A2(
|
||||||
|
$elm$json$Json$Encode$list,
|
||||||
|
function (i) {
|
||||||
|
return $elm$json$Json$Encode$int(i.id);
|
||||||
|
},
|
||||||
|
items)),
|
||||||
|
_Utils_Tuple2('global_mod', $elm$json$Json$Encode$null)
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
});
|
||||||
var $elm$http$Http$jsonBody = function (value) {
|
var $elm$http$Http$jsonBody = function (value) {
|
||||||
return A2(
|
return A2(
|
||||||
_Http_pair,
|
_Http_pair,
|
||||||
@@ -6890,7 +6873,7 @@ var $elm$http$Http$jsonBody = function (value) {
|
|||||||
A2($elm$json$Json$Encode$encode, 0, value));
|
A2($elm$json$Json$Encode$encode, 0, value));
|
||||||
};
|
};
|
||||||
var $author$project$Api$sendRequest = F3(
|
var $author$project$Api$sendRequest = F3(
|
||||||
function (mode, id, payload) {
|
function (mode, id, items) {
|
||||||
var _v0 = function () {
|
var _v0 = function () {
|
||||||
switch (mode.$) {
|
switch (mode.$) {
|
||||||
case 'Add':
|
case 'Add':
|
||||||
@@ -6907,7 +6890,8 @@ var $author$project$Api$sendRequest = F3(
|
|||||||
var method = _v0.b;
|
var method = _v0.b;
|
||||||
return $elm$http$Http$request(
|
return $elm$http$Http$request(
|
||||||
{
|
{
|
||||||
body: $elm$http$Http$jsonBody(payload),
|
body: $elm$http$Http$jsonBody(
|
||||||
|
A2($author$project$Api$buildPayload, mode, items)),
|
||||||
expect: A2($elm$http$Http$expectJson, $author$project$Api$GotActionResult, $author$project$Api$apiResponseDecoder),
|
expect: A2($elm$http$Http$expectJson, $author$project$Api$GotActionResult, $author$project$Api$apiResponseDecoder),
|
||||||
headers: _List_Nil,
|
headers: _List_Nil,
|
||||||
method: method,
|
method: method,
|
||||||
@@ -6956,6 +6940,19 @@ var $author$project$Main$switchSelectionState = F2(
|
|||||||
return A2($elm$core$Debug$log, 'ignore switchSelectionState', $elm$core$Maybe$Nothing);
|
return A2($elm$core$Debug$log, 'ignore switchSelectionState', $elm$core$Maybe$Nothing);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
var $author$project$Main$targetItemsFor = F2(
|
||||||
|
function (mode, model) {
|
||||||
|
switch (mode.$) {
|
||||||
|
case 'Add':
|
||||||
|
return _List_Nil;
|
||||||
|
case 'Buy':
|
||||||
|
return A2($elm$core$Maybe$withDefault, _List_Nil, model.merchantItems);
|
||||||
|
case 'Sell':
|
||||||
|
return A2($elm$core$Maybe$withDefault, _List_Nil, model.loot);
|
||||||
|
default:
|
||||||
|
return A2($elm$core$Maybe$withDefault, _List_Nil, model.groupLoot);
|
||||||
|
}
|
||||||
|
});
|
||||||
var $elm$core$Debug$toString = _Debug_toString;
|
var $elm$core$Debug$toString = _Debug_toString;
|
||||||
var $elm$url$Url$addPort = F2(
|
var $elm$url$Url$addPort = F2(
|
||||||
function (maybePort, starter) {
|
function (maybePort, starter) {
|
||||||
@@ -7243,6 +7240,10 @@ var $author$project$Main$update = F2(
|
|||||||
continue update;
|
continue update;
|
||||||
} else {
|
} else {
|
||||||
var mode = _v12.a;
|
var mode = _v12.a;
|
||||||
|
var items = A2(
|
||||||
|
$elm$core$List$filter,
|
||||||
|
$author$project$Main$itemInSelection(model.state.selection),
|
||||||
|
A2($author$project$Main$targetItemsFor, mode, model));
|
||||||
return _Utils_Tuple2(
|
return _Utils_Tuple2(
|
||||||
model,
|
model,
|
||||||
A2(
|
A2(
|
||||||
@@ -7252,7 +7253,7 @@ var $author$project$Main$update = F2(
|
|||||||
$author$project$Api$sendRequest,
|
$author$project$Api$sendRequest,
|
||||||
mode,
|
mode,
|
||||||
$elm$core$String$fromInt(model.player.id),
|
$elm$core$String$fromInt(model.player.id),
|
||||||
A2($author$project$Main$buildPayload, mode, model))));
|
items)));
|
||||||
}
|
}
|
||||||
case 'UndoLastAction':
|
case 'UndoLastAction':
|
||||||
return _Utils_Tuple2(
|
return _Utils_Tuple2(
|
||||||
|
|||||||
35
src/Api.elm
35
src/Api.elm
@@ -186,22 +186,39 @@ undoLastAction id = Http.request
|
|||||||
, tracker = Nothing
|
, tracker = Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
sendRequest : ViewMode -> String -> E.Value -> Cmd Msg
|
buildPayload : ViewMode -> List Item -> E.Value
|
||||||
sendRequest mode id payload =
|
buildPayload mode items =
|
||||||
|
case mode of
|
||||||
|
Modes.Buy -> E.object
|
||||||
|
[ ( "items", items |> E.list (\i -> E.list identity [E.int i.id, E.null]))
|
||||||
|
, ("global_mod", E.null )
|
||||||
|
]
|
||||||
|
Modes.Sell -> E.object
|
||||||
|
[ ( "items", items |> E.list (\i -> E.list identity [E.int i.id, E.null]))
|
||||||
|
, ("global_mod", E.null )
|
||||||
|
]
|
||||||
|
Modes.Grab -> E.object
|
||||||
|
[ ( "items", items |> E.list (\i -> E.int i.id))
|
||||||
|
, ("global_mod", E.null )
|
||||||
|
]
|
||||||
|
Modes.Add -> E.object
|
||||||
|
[ ( "items", items |> E.list (\i -> E.int i.id))
|
||||||
|
, ("global_mod", E.null )
|
||||||
|
]
|
||||||
|
|
||||||
|
sendRequest : ViewMode -> String -> List Item -> Cmd Msg
|
||||||
|
sendRequest mode id items =
|
||||||
let
|
let
|
||||||
(endpoint, method) = case mode of
|
(endpoint, method) = case mode of
|
||||||
Modes.Add ->
|
Modes.Add ->
|
||||||
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
||||||
, "POST"
|
, "POST" )
|
||||||
)
|
|
||||||
Modes.Buy ->
|
Modes.Buy ->
|
||||||
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
||||||
, "PUT"
|
, "PUT" )
|
||||||
)
|
|
||||||
Modes.Sell ->
|
Modes.Sell ->
|
||||||
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
||||||
, "DELETE"
|
, "DELETE" )
|
||||||
)
|
|
||||||
Modes.Grab ->
|
Modes.Grab ->
|
||||||
( "http://localhost:8088/api/players/" ++ id ++ "/claims"
|
( "http://localhost:8088/api/players/" ++ id ++ "/claims"
|
||||||
, "POST" )
|
, "POST" )
|
||||||
@@ -210,7 +227,7 @@ sendRequest mode id payload =
|
|||||||
{ method = method
|
{ method = method
|
||||||
, headers = []
|
, headers = []
|
||||||
, url = endpoint
|
, url = endpoint
|
||||||
, body = Http.jsonBody payload
|
, body = Http.jsonBody <| buildPayload mode items
|
||||||
, expect = Http.expectJson GotActionResult apiResponseDecoder
|
, expect = Http.expectJson GotActionResult apiResponseDecoder
|
||||||
, timeout = Nothing
|
, timeout = Nothing
|
||||||
, tracker = Nothing
|
, tracker = Nothing
|
||||||
|
|||||||
36
src/Main.elm
36
src/Main.elm
@@ -205,8 +205,18 @@ update msg model =
|
|||||||
case model.state.activeMode of
|
case model.state.activeMode of
|
||||||
Nothing ->
|
Nothing ->
|
||||||
update (ModeSwitched Nothing) model
|
update (ModeSwitched Nothing) model
|
||||||
|
|
||||||
Just mode ->
|
Just mode ->
|
||||||
(model, Cmd.map ApiMsg <| Api.sendRequest mode (String.fromInt model.player.id) (buildPayload mode model))
|
let items = targetItemsFor mode model
|
||||||
|
|> List.filter (itemInSelection model.state.selection)
|
||||||
|
in
|
||||||
|
( model
|
||||||
|
, Cmd.map ApiMsg
|
||||||
|
<| Api.sendRequest
|
||||||
|
mode
|
||||||
|
(String.fromInt model.player.id)
|
||||||
|
items
|
||||||
|
)
|
||||||
|
|
||||||
UndoLastAction ->
|
UndoLastAction ->
|
||||||
(model, Cmd.map ApiMsg <| Api.undoLastAction model.player.id)
|
(model, Cmd.map ApiMsg <| Api.undoLastAction model.player.id)
|
||||||
@@ -226,30 +236,6 @@ targetItemsFor mode model =
|
|||||||
Modes.Sell ->Maybe.withDefault [] model.loot
|
Modes.Sell ->Maybe.withDefault [] model.loot
|
||||||
Modes.Grab -> Maybe.withDefault [] model.groupLoot
|
Modes.Grab -> Maybe.withDefault [] model.groupLoot
|
||||||
|
|
||||||
buildPayload : ViewMode -> Model -> E.Value
|
|
||||||
buildPayload mode model =
|
|
||||||
let
|
|
||||||
items = targetItemsFor mode model
|
|
||||||
|> List.filter (itemInSelection model.state.selection)
|
|
||||||
in
|
|
||||||
case mode of
|
|
||||||
Modes.Buy -> E.object
|
|
||||||
[ ( "items", items |> E.list (\i -> E.list identity [E.int i.id, E.null]))
|
|
||||||
, ("global_mod", E.null )
|
|
||||||
]
|
|
||||||
Modes.Sell -> E.object
|
|
||||||
[ ( "items", items |> E.list (\i -> E.list identity [E.int i.id, E.null]))
|
|
||||||
, ("global_mod", E.null )
|
|
||||||
]
|
|
||||||
Modes.Grab -> E.object
|
|
||||||
[ ( "items", items |> E.list (\i -> E.int i.id))
|
|
||||||
, ("global_mod", E.null )
|
|
||||||
]
|
|
||||||
Modes.Add -> E.object
|
|
||||||
[ ( "items", items |> E.list (\i -> E.int i.id))
|
|
||||||
, ("global_mod", E.null )
|
|
||||||
]
|
|
||||||
|
|
||||||
-- DbUpdates always refer to the active player's loot
|
-- DbUpdates always refer to the active player's loot
|
||||||
applyUpdate : Api.Update -> Model -> Model
|
applyUpdate : Api.Update -> Model -> Model
|
||||||
applyUpdate u model =
|
applyUpdate u model =
|
||||||
|
|||||||
Reference in New Issue
Block a user