Compare commits

..

2 Commits

Author SHA1 Message Date
21650c4011 adds Modes module 2019-11-05 21:20:09 +01:00
c968da15f0 until it compiles... 2019-11-05 21:19:53 +01:00
4 changed files with 615 additions and 591 deletions

564
main.js
View File

@@ -5343,26 +5343,30 @@ var $author$project$Main$State = F6(
function (navKey, route, error, menuOpen, selection, activeMode) { function (navKey, route, error, menuOpen, selection, activeMode) {
return {activeMode: activeMode, error: error, menuOpen: menuOpen, navKey: navKey, route: route, selection: selection}; return {activeMode: activeMode, error: error, menuOpen: menuOpen, navKey: navKey, route: route, selection: selection};
}); });
var $author$project$Main$Player = F4( var $author$project$Api$Player = F4(
function (id, name, debt, wealth) { function (id, name, debt, wealth) {
return {debt: debt, id: id, name: name, wealth: wealth}; return {debt: debt, id: id, name: name, wealth: wealth};
}); });
var $author$project$Main$Wealth = F4( var $author$project$Api$Wealth = F4(
function (cp, sp, gp, pp) { function (cp, sp, gp, pp) {
return {cp: cp, gp: gp, pp: pp, sp: sp}; return {cp: cp, gp: gp, pp: pp, sp: sp};
}); });
var $author$project$Main$blankPlayer = A4( var $author$project$Api$blankPlayer = A4(
$author$project$Main$Player, $author$project$Api$Player,
0, 0,
'Loading', 'Loot-a-lot',
0, 0,
A4($author$project$Main$Wealth, 0, 0, 0, 0)); A4($author$project$Api$Wealth, 0, 0, 0, 0));
var $author$project$Main$ApiMsg = function (a) {
return {$: 'ApiMsg', a: a};
};
var $author$project$Api$OfGroup = {$: 'OfGroup'};
var $author$project$Api$OfShop = {$: 'OfShop'};
var $elm$core$Platform$Cmd$batch = _Platform_batch; var $elm$core$Platform$Cmd$batch = _Platform_batch;
var $author$project$Main$GotLoot = F2( var $author$project$Api$GotLoot = F2(
function (a, b) { function (a, b) {
return {$: 'GotLoot', a: a, b: b}; return {$: 'GotLoot', a: a, b: b};
}); });
var $author$project$Main$OfGroup = {$: 'OfGroup'};
var $elm$json$Json$Decode$decodeString = _Json_runOnString; var $elm$json$Json$Decode$decodeString = _Json_runOnString;
var $elm$http$Http$BadStatus_ = F2( var $elm$http$Http$BadStatus_ = F2(
function (a, b) { function (a, b) {
@@ -6150,7 +6154,7 @@ var $elm$http$Http$get = function (r) {
return $elm$http$Http$request( return $elm$http$Http$request(
{body: $elm$http$Http$emptyBody, expect: r.expect, headers: _List_Nil, method: 'GET', timeout: $elm$core$Maybe$Nothing, tracker: $elm$core$Maybe$Nothing, url: r.url}); {body: $elm$http$Http$emptyBody, expect: r.expect, headers: _List_Nil, method: 'GET', timeout: $elm$core$Maybe$Nothing, tracker: $elm$core$Maybe$Nothing, url: r.url});
}; };
var $author$project$Main$Item = F3( var $author$project$Api$Item = F3(
function (id, name, base_price) { function (id, name, base_price) {
return {base_price: base_price, id: id, name: name}; return {base_price: base_price, id: id, name: name};
}); });
@@ -6158,105 +6162,112 @@ var $elm$json$Json$Decode$field = _Json_decodeField;
var $elm$json$Json$Decode$int = _Json_decodeInt; var $elm$json$Json$Decode$int = _Json_decodeInt;
var $elm$json$Json$Decode$map3 = _Json_map3; var $elm$json$Json$Decode$map3 = _Json_map3;
var $elm$json$Json$Decode$string = _Json_decodeString; var $elm$json$Json$Decode$string = _Json_decodeString;
var $author$project$Main$itemDecoder = A4( var $author$project$Api$itemDecoder = A4(
$elm$json$Json$Decode$map3, $elm$json$Json$Decode$map3,
$author$project$Main$Item, $author$project$Api$Item,
A2($elm$json$Json$Decode$field, 'id', $elm$json$Json$Decode$int), A2($elm$json$Json$Decode$field, 'id', $elm$json$Json$Decode$int),
A2($elm$json$Json$Decode$field, 'name', $elm$json$Json$Decode$string), A2($elm$json$Json$Decode$field, 'name', $elm$json$Json$Decode$string),
A2($elm$json$Json$Decode$field, 'base_price', $elm$json$Json$Decode$int)); A2($elm$json$Json$Decode$field, 'base_price', $elm$json$Json$Decode$int));
var $elm$json$Json$Decode$list = _Json_decodeList; var $elm$json$Json$Decode$list = _Json_decodeList;
var $author$project$Main$lootDecoder = $elm$json$Json$Decode$list($author$project$Main$itemDecoder); var $author$project$Api$lootDecoder = $elm$json$Json$Decode$list($author$project$Api$itemDecoder);
var $author$project$Main$valueDecoder = function (thenDecoder) { var $author$project$Api$valueDecoder = function (thenDecoder) {
return A2($elm$json$Json$Decode$field, 'value', thenDecoder); return A2($elm$json$Json$Decode$field, 'value', thenDecoder);
}; };
var $author$project$Main$fetchGroupLoot = $elm$http$Http$get( var $author$project$Api$fetchLoot = function (dest) {
var url = function () {
switch (dest.$) {
case 'OfPlayer':
var id = dest.a;
return 'http://localhost:8088/api/players/' + ($elm$core$String$fromInt(id) + '/loot');
case 'OfShop':
return 'http://localhost:8088/api/items';
default:
return 'http://localhost:8088/api/players/0/loot';
}
}();
return $elm$http$Http$get(
{ {
expect: A2( expect: A2(
$elm$http$Http$expectJson, $elm$http$Http$expectJson,
$author$project$Main$GotLoot($author$project$Main$OfGroup), $author$project$Api$GotLoot(dest),
$author$project$Main$valueDecoder($author$project$Main$lootDecoder)), $author$project$Api$valueDecoder($author$project$Api$lootDecoder)),
url: 'http://localhost:8088/api/players/0/loot' url: url
}); });
var $author$project$Main$OfShop = {$: 'OfShop'}; };
var $author$project$Main$fetchShopInventory = $elm$http$Http$get( var $author$project$Api$OfPlayer = function (a) {
{ return {$: 'OfPlayer', a: a};
expect: A2( };
$elm$http$Http$expectJson, var $author$project$Api$GotClaims = F2(
$author$project$Main$GotLoot($author$project$Main$OfShop),
$author$project$Main$valueDecoder($author$project$Main$lootDecoder)),
url: 'http://localhost:8088/api/items'
});
var $author$project$Main$GotClaims = F2(
function (a, b) { function (a, b) {
return {$: 'GotClaims', a: a, b: b}; return {$: 'GotClaims', a: a, b: b};
}); });
var $author$project$Main$Claim = F3( var $author$project$Api$Claim = F3(
function (id, player_id, loot_id) { function (id, player_id, loot_id) {
return {id: id, loot_id: loot_id, player_id: player_id}; return {id: id, loot_id: loot_id, player_id: player_id};
}); });
var $author$project$Main$claimDecoder = A4( var $author$project$Api$claimDecoder = A4(
$elm$json$Json$Decode$map3, $elm$json$Json$Decode$map3,
$author$project$Main$Claim, $author$project$Api$Claim,
A2($elm$json$Json$Decode$field, 'id', $elm$json$Json$Decode$int), A2($elm$json$Json$Decode$field, 'id', $elm$json$Json$Decode$int),
A2($elm$json$Json$Decode$field, 'player_id', $elm$json$Json$Decode$int), A2($elm$json$Json$Decode$field, 'player_id', $elm$json$Json$Decode$int),
A2($elm$json$Json$Decode$field, 'loot_id', $elm$json$Json$Decode$int)); A2($elm$json$Json$Decode$field, 'loot_id', $elm$json$Json$Decode$int));
var $author$project$Main$fetchClaims = function (playerId) { var $author$project$Api$fetchClaims = function (playerId) {
return $elm$http$Http$get( return $elm$http$Http$get(
{ {
expect: A2( expect: A2(
$elm$http$Http$expectJson, $elm$http$Http$expectJson,
$author$project$Main$GotClaims(playerId), $author$project$Api$GotClaims(playerId),
$author$project$Main$valueDecoder( $author$project$Api$valueDecoder(
$elm$json$Json$Decode$list($author$project$Main$claimDecoder))), $elm$json$Json$Decode$list($author$project$Api$claimDecoder))),
url: 'http://localhost:8088/api/claims' url: 'http://localhost:8088/api/claims'
}); });
}; };
var $author$project$Main$OfPlayer = {$: 'OfPlayer'}; var $author$project$Api$GotPlayer = function (a) {
var $author$project$Main$fetchLoot = function (id) {
return $elm$http$Http$get(
{
expect: A2(
$elm$http$Http$expectJson,
$author$project$Main$GotLoot($author$project$Main$OfPlayer),
$author$project$Main$valueDecoder($author$project$Main$lootDecoder)),
url: 'http://localhost:8088/api/players/' + ($elm$core$String$fromInt(id) + '/loot')
});
};
var $author$project$Main$GotPlayer = function (a) {
return {$: 'GotPlayer', a: a}; return {$: 'GotPlayer', a: a};
}; };
var $elm$json$Json$Decode$map4 = _Json_map4; var $elm$json$Json$Decode$map4 = _Json_map4;
var $author$project$Main$wealthDecoder = A5( var $author$project$Api$wealthDecoder = A5(
$elm$json$Json$Decode$map4, $elm$json$Json$Decode$map4,
$author$project$Main$Wealth, $author$project$Api$Wealth,
A2($elm$json$Json$Decode$field, 'cp', $elm$json$Json$Decode$int), A2($elm$json$Json$Decode$field, 'cp', $elm$json$Json$Decode$int),
A2($elm$json$Json$Decode$field, 'sp', $elm$json$Json$Decode$int), A2($elm$json$Json$Decode$field, 'sp', $elm$json$Json$Decode$int),
A2($elm$json$Json$Decode$field, 'gp', $elm$json$Json$Decode$int), A2($elm$json$Json$Decode$field, 'gp', $elm$json$Json$Decode$int),
A2($elm$json$Json$Decode$field, 'pp', $elm$json$Json$Decode$int)); A2($elm$json$Json$Decode$field, 'pp', $elm$json$Json$Decode$int));
var $author$project$Main$playerDecoder = A5( var $author$project$Api$playerDecoder = A5(
$elm$json$Json$Decode$map4, $elm$json$Json$Decode$map4,
$author$project$Main$Player, $author$project$Api$Player,
A2($elm$json$Json$Decode$field, 'id', $elm$json$Json$Decode$int), A2($elm$json$Json$Decode$field, 'id', $elm$json$Json$Decode$int),
A2($elm$json$Json$Decode$field, 'name', $elm$json$Json$Decode$string), A2($elm$json$Json$Decode$field, 'name', $elm$json$Json$Decode$string),
A2($elm$json$Json$Decode$field, 'debt', $elm$json$Json$Decode$int), A2($elm$json$Json$Decode$field, 'debt', $elm$json$Json$Decode$int),
$author$project$Main$wealthDecoder); $author$project$Api$wealthDecoder);
var $author$project$Main$fetchPlayer = function (id) { var $author$project$Api$fetchPlayer = function (id) {
return $elm$http$Http$get( return $elm$http$Http$get(
{ {
expect: A2( expect: A2(
$elm$http$Http$expectJson, $elm$http$Http$expectJson,
$author$project$Main$GotPlayer, $author$project$Api$GotPlayer,
$author$project$Main$valueDecoder($author$project$Main$playerDecoder)), $author$project$Api$valueDecoder($author$project$Api$playerDecoder)),
url: 'http://localhost:8088/api/players/' + ($elm$core$String$fromInt(id) + '/') url: 'http://localhost:8088/api/players/' + ($elm$core$String$fromInt(id) + '/')
}); });
}; };
var $elm$core$Platform$Cmd$map = _Platform_map;
var $author$project$Main$initPlayer = function (id) { var $author$project$Main$initPlayer = function (id) {
return $elm$core$Platform$Cmd$batch( return $elm$core$Platform$Cmd$batch(
_List_fromArray( _List_fromArray(
[ [
$author$project$Main$fetchPlayer(id), A2(
$author$project$Main$fetchLoot(id), $elm$core$Platform$Cmd$map,
$author$project$Main$fetchClaims(id) $author$project$Main$ApiMsg,
$author$project$Api$fetchPlayer(id)),
A2(
$elm$core$Platform$Cmd$map,
$author$project$Main$ApiMsg,
$author$project$Api$fetchLoot(
$author$project$Api$OfPlayer(id))),
A2(
$elm$core$Platform$Cmd$map,
$author$project$Main$ApiMsg,
$author$project$Api$fetchClaims(id))
])); ]));
}; };
var $author$project$Main$fetchInitialData = function (playerId) { var $author$project$Main$fetchInitialData = function (playerId) {
@@ -6264,8 +6275,14 @@ var $author$project$Main$fetchInitialData = function (playerId) {
_List_fromArray( _List_fromArray(
[ [
$author$project$Main$initPlayer(playerId), $author$project$Main$initPlayer(playerId),
$author$project$Main$fetchShopInventory, A2(
$author$project$Main$fetchGroupLoot $elm$core$Platform$Cmd$map,
$author$project$Main$ApiMsg,
$author$project$Api$fetchLoot($author$project$Api$OfShop)),
A2(
$elm$core$Platform$Cmd$map,
$author$project$Main$ApiMsg,
$author$project$Api$fetchLoot($author$project$Api$OfGroup))
])); ]));
}; };
var $elm$url$Url$Parser$State = F5( var $elm$url$Url$Parser$State = F5(
@@ -6516,7 +6533,7 @@ var $author$project$Main$init = F3(
A7( A7(
$author$project$Main$Model, $author$project$Main$Model,
A6($author$project$Main$State, key, route, '', false, $elm$core$Maybe$Nothing, $elm$core$Maybe$Nothing), A6($author$project$Main$State, key, route, '', false, $elm$core$Maybe$Nothing, $elm$core$Maybe$Nothing),
$author$project$Main$blankPlayer, $author$project$Api$blankPlayer,
_List_Nil, _List_Nil,
$elm$core$Maybe$Nothing, $elm$core$Maybe$Nothing,
$elm$core$Maybe$Nothing, $elm$core$Maybe$Nothing,
@@ -6529,110 +6546,10 @@ var $elm$core$Platform$Sub$none = $elm$core$Platform$Sub$batch(_List_Nil);
var $author$project$Main$subscriptions = function (_v0) { var $author$project$Main$subscriptions = function (_v0) {
return $elm$core$Platform$Sub$none; return $elm$core$Platform$Sub$none;
}; };
var $author$project$Main$Add = {$: 'Add'}; var $author$project$Modes$Add = {$: 'Add'};
var $author$project$Main$GotActionResult = function (a) {
return {$: 'GotActionResult', a: a};
};
var $author$project$Main$ModeSwitched = function (a) { var $author$project$Main$ModeSwitched = function (a) {
return {$: 'ModeSwitched', a: a}; return {$: 'ModeSwitched', a: a};
}; };
var $author$project$Main$ApiResponse = F4(
function (value, notification, updates, errors) {
return {errors: errors, notification: notification, updates: updates, value: value};
});
var $elm$json$Json$Decode$oneOf = _Json_oneOf;
var $elm$json$Json$Decode$maybe = function (decoder) {
return $elm$json$Json$Decode$oneOf(
_List_fromArray(
[
A2($elm$json$Json$Decode$map, $elm$core$Maybe$Just, decoder),
$elm$json$Json$Decode$succeed($elm$core$Maybe$Nothing)
]));
};
var $author$project$Main$ClaimAdded = function (a) {
return {$: 'ClaimAdded', a: a};
};
var $author$project$Main$ClaimRemoved = function (a) {
return {$: 'ClaimRemoved', a: a};
};
var $author$project$Main$ItemAdded = function (a) {
return {$: 'ItemAdded', a: a};
};
var $author$project$Main$ItemRemoved = function (a) {
return {$: 'ItemRemoved', a: a};
};
var $author$project$Main$WealthUpdated = function (a) {
return {$: 'WealthUpdated', a: a};
};
var $elm$json$Json$Decode$andThen = _Json_andThen;
var $author$project$Main$updatesDecoder = $elm$json$Json$Decode$oneOf(
_List_fromArray(
[
A2(
$elm$json$Json$Decode$field,
'ItemRemoved',
A2(
$elm$json$Json$Decode$andThen,
function (i) {
return $elm$json$Json$Decode$succeed(
$author$project$Main$ItemRemoved(i));
},
$author$project$Main$itemDecoder)),
A2(
$elm$json$Json$Decode$field,
'ItemAdded',
A2(
$elm$json$Json$Decode$andThen,
function (i) {
return $elm$json$Json$Decode$succeed(
$author$project$Main$ItemAdded(i));
},
$author$project$Main$itemDecoder)),
A2(
$elm$json$Json$Decode$field,
'Wealth',
A2(
$elm$json$Json$Decode$andThen,
function (i) {
return $elm$json$Json$Decode$succeed(
$author$project$Main$WealthUpdated(i));
},
$author$project$Main$wealthDecoder)),
A2(
$elm$json$Json$Decode$field,
'ClaimRemoved',
A2(
$elm$json$Json$Decode$andThen,
function (i) {
return $elm$json$Json$Decode$succeed(
$author$project$Main$ClaimRemoved(i));
},
$elm$json$Json$Decode$succeed(_Utils_Tuple0))),
A2(
$elm$json$Json$Decode$field,
'ClaimAdded',
A2(
$elm$json$Json$Decode$andThen,
function (i) {
return $elm$json$Json$Decode$succeed(
$author$project$Main$ClaimAdded(i));
},
$elm$json$Json$Decode$succeed(_Utils_Tuple0)))
]));
var $author$project$Main$apiResponseDecoder = A5(
$elm$json$Json$Decode$map4,
$author$project$Main$ApiResponse,
$elm$json$Json$Decode$maybe(
A2($elm$json$Json$Decode$field, 'value', $elm$json$Json$Decode$string)),
$elm$json$Json$Decode$maybe(
A2($elm$json$Json$Decode$field, 'notification', $elm$json$Json$Decode$string)),
$elm$json$Json$Decode$maybe(
A2(
$elm$json$Json$Decode$field,
'updates',
$elm$json$Json$Decode$list($author$project$Main$updatesDecoder))),
$elm$json$Json$Decode$maybe(
A2($elm$json$Json$Decode$field, 'errors', $elm$json$Json$Decode$string)));
var $elm$core$List$filter = F2( var $elm$core$List$filter = F2(
function (isGood, list) { function (isGood, list) {
return A3( return A3(
@@ -6691,7 +6608,7 @@ var $author$project$Main$applyUpdate = F2(
player: _Utils_update( player: _Utils_update(
player, player,
{ {
wealth: A4($author$project$Main$Wealth, wealth.cp + diff.cp, wealth.sp + diff.sp, wealth.gp + diff.gp, wealth.pp + diff.pp) wealth: A4($author$project$Api$Wealth, wealth.cp + diff.cp, wealth.sp + diff.sp, wealth.gp + diff.gp, wealth.pp + diff.pp)
}) })
}); });
case 'ClaimAdded': case 'ClaimAdded':
@@ -6700,29 +6617,6 @@ var $author$project$Main$applyUpdate = F2(
return model; return model;
} }
}); });
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$Platform$Cmd$none = $elm$core$Platform$Cmd$batch(_List_Nil);
var $author$project$Main$printError = function (error) {
if (error.$ === 'NetworkError') {
return 'Le serveur ne répond pas';
} else {
return 'Erreur inconnue';
}
};
var $elm$browser$Browser$Navigation$pushUrl = _Browser_pushUrl;
var $elm$json$Json$Encode$int = _Json_wrap; var $elm$json$Json$Encode$int = _Json_wrap;
var $elm$core$Maybe$map = F2( var $elm$core$Maybe$map = F2(
function (f, maybe) { function (f, maybe) {
@@ -6873,52 +6767,154 @@ var $author$project$Main$buildPayload = F2(
])); ]));
} }
}); });
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$Platform$Cmd$none = $elm$core$Platform$Cmd$batch(_List_Nil);
var $elm$browser$Browser$Navigation$pushUrl = _Browser_pushUrl;
var $author$project$Api$GotActionResult = function (a) {
return {$: 'GotActionResult', a: a};
};
var $author$project$Api$Response = F4(
function (value, notification, updates, errors) {
return {errors: errors, notification: notification, updates: updates, value: value};
});
var $elm$json$Json$Decode$oneOf = _Json_oneOf;
var $elm$json$Json$Decode$maybe = function (decoder) {
return $elm$json$Json$Decode$oneOf(
_List_fromArray(
[
A2($elm$json$Json$Decode$map, $elm$core$Maybe$Just, decoder),
$elm$json$Json$Decode$succeed($elm$core$Maybe$Nothing)
]));
};
var $author$project$Api$ClaimAdded = function (a) {
return {$: 'ClaimAdded', a: a};
};
var $author$project$Api$ClaimRemoved = function (a) {
return {$: 'ClaimRemoved', a: a};
};
var $author$project$Api$ItemAdded = function (a) {
return {$: 'ItemAdded', a: a};
};
var $author$project$Api$ItemRemoved = function (a) {
return {$: 'ItemRemoved', a: a};
};
var $author$project$Api$WealthUpdated = function (a) {
return {$: 'WealthUpdated', a: a};
};
var $elm$json$Json$Decode$andThen = _Json_andThen;
var $author$project$Api$updatesDecoder = $elm$json$Json$Decode$oneOf(
_List_fromArray(
[
A2(
$elm$json$Json$Decode$field,
'ItemRemoved',
A2(
$elm$json$Json$Decode$andThen,
function (i) {
return $elm$json$Json$Decode$succeed(
$author$project$Api$ItemRemoved(i));
},
$author$project$Api$itemDecoder)),
A2(
$elm$json$Json$Decode$field,
'ItemAdded',
A2(
$elm$json$Json$Decode$andThen,
function (i) {
return $elm$json$Json$Decode$succeed(
$author$project$Api$ItemAdded(i));
},
$author$project$Api$itemDecoder)),
A2(
$elm$json$Json$Decode$field,
'Wealth',
A2(
$elm$json$Json$Decode$andThen,
function (i) {
return $elm$json$Json$Decode$succeed(
$author$project$Api$WealthUpdated(i));
},
$author$project$Api$wealthDecoder)),
A2(
$elm$json$Json$Decode$field,
'ClaimRemoved',
A2(
$elm$json$Json$Decode$andThen,
function (i) {
return $elm$json$Json$Decode$succeed(
$author$project$Api$ClaimRemoved(i));
},
$elm$json$Json$Decode$succeed(_Utils_Tuple0))),
A2(
$elm$json$Json$Decode$field,
'ClaimAdded',
A2(
$elm$json$Json$Decode$andThen,
function (i) {
return $elm$json$Json$Decode$succeed(
$author$project$Api$ClaimAdded(i));
},
$elm$json$Json$Decode$succeed(_Utils_Tuple0)))
]));
var $author$project$Api$apiResponseDecoder = A5(
$elm$json$Json$Decode$map4,
$author$project$Api$Response,
$elm$json$Json$Decode$maybe(
A2($elm$json$Json$Decode$field, 'value', $elm$json$Json$Decode$string)),
$elm$json$Json$Decode$maybe(
A2($elm$json$Json$Decode$field, 'notification', $elm$json$Json$Decode$string)),
$elm$json$Json$Decode$maybe(
A2(
$elm$json$Json$Decode$field,
'updates',
$elm$json$Json$Decode$list($author$project$Api$updatesDecoder))),
$elm$json$Json$Decode$maybe(
A2($elm$json$Json$Decode$field, 'errors', $elm$json$Json$Decode$string)));
var $elm$http$Http$jsonBody = function (value) { var $elm$http$Http$jsonBody = function (value) {
return A2( return A2(
_Http_pair, _Http_pair,
'application/json', 'application/json',
A2($elm$json$Json$Encode$encode, 0, value)); A2($elm$json$Json$Encode$encode, 0, value));
}; };
var $author$project$Main$sendRequest = F2( var $author$project$Api$sendRequest = F3(
function (activeMode, model) { function (mode, id, payload) {
if (activeMode.$ === 'Nothing') { var _v0 = function () {
return $elm$core$Platform$Cmd$none;
} else {
var mode = activeMode.a;
var _v1 = function () {
switch (mode.$) { switch (mode.$) {
case 'Add': case 'Add':
return _Utils_Tuple2( return _Utils_Tuple2('http://localhost:8088/api/players/' + (id + '/loot'), 'POST');
'http://localhost:8088/api/players/' + ($elm$core$String$fromInt(model.player.id) + '/loot'),
'POST');
case 'Buy': case 'Buy':
return _Utils_Tuple2( return _Utils_Tuple2('http://localhost:8088/api/players/' + (id + '/loot'), 'PUT');
'http://localhost:8088/api/players/' + ($elm$core$String$fromInt(model.player.id) + '/loot'),
'PUT');
case 'Sell': case 'Sell':
return _Utils_Tuple2( return _Utils_Tuple2('http://localhost:8088/api/players/' + (id + '/loot'), 'DELETE');
'http://localhost:8088/api/players/' + ($elm$core$String$fromInt(model.player.id) + '/loot'),
'DELETE');
default: default:
return _Utils_Tuple2( return _Utils_Tuple2('http://localhost:8088/api/players/' + (id + '/claims'), 'POST');
'http://localhost:8088/api/players/' + ($elm$core$String$fromInt(model.player.id) + '/claims'),
'POST');
} }
}(); }();
var endpoint = _v1.a; var endpoint = _v0.a;
var method = _v1.b; var method = _v0.b;
return $elm$http$Http$request( return $elm$http$Http$request(
{ {
body: $elm$http$Http$jsonBody( body: $elm$http$Http$jsonBody(payload),
A2($author$project$Main$buildPayload, mode, model)), expect: A2($elm$http$Http$expectJson, $author$project$Api$GotActionResult, $author$project$Api$apiResponseDecoder),
expect: A2($elm$http$Http$expectJson, $author$project$Main$GotActionResult, $author$project$Main$apiResponseDecoder),
headers: _List_Nil, headers: _List_Nil,
method: method, method: method,
timeout: $elm$core$Maybe$Nothing, timeout: $elm$core$Maybe$Nothing,
tracker: $elm$core$Maybe$Nothing, tracker: $elm$core$Maybe$Nothing,
url: endpoint url: endpoint
}); });
}
}); });
var $author$project$Main$setError = F2( var $author$project$Main$setError = F2(
function (error, model) { function (error, model) {
@@ -7005,8 +7001,22 @@ var $elm$url$Url$toString = function (url) {
_Utils_ap(http, url.host)), _Utils_ap(http, url.host)),
url.path))); url.path)));
}; };
var $author$project$Api$undoLastAction = function (id) {
return $elm$http$Http$request(
{
body: $elm$http$Http$emptyBody,
expect: A2($elm$http$Http$expectJson, $author$project$Api$GotActionResult, $author$project$Api$apiResponseDecoder),
headers: _List_Nil,
method: 'DELETE',
timeout: $elm$core$Maybe$Nothing,
tracker: $elm$core$Maybe$Nothing,
url: 'http://localhost:8088/api/players/' + ($elm$core$String$fromInt(id) + '/events/last')
});
};
var $author$project$Main$update = F2( var $author$project$Main$update = F2(
function (msg, model) { function (msg, model) {
update:
while (true) {
switch (msg.$) { switch (msg.$) {
case 'LinkClicked': case 'LinkClicked':
var urlRequest = msg.a; var urlRequest = msg.a;
@@ -7035,7 +7045,7 @@ var $author$project$Main$update = F2(
function () { function () {
if (page.$ === 'NewLoot') { if (page.$ === 'NewLoot') {
return $author$project$Main$ModeSwitched( return $author$project$Main$ModeSwitched(
$elm$core$Maybe$Just($author$project$Main$Add)); $elm$core$Maybe$Just($author$project$Modes$Add));
} else { } else {
var other = page; var other = page;
return $author$project$Main$ModeSwitched($elm$core$Maybe$Nothing); return $author$project$Main$ModeSwitched($elm$core$Maybe$Nothing);
@@ -7058,10 +7068,39 @@ var $author$project$Main$update = F2(
return _Utils_Tuple2( return _Utils_Tuple2(
_Utils_update( _Utils_update(
model, model,
{player: $author$project$Main$blankPlayer}), {player: $author$project$Api$blankPlayer}),
$author$project$Main$initPlayer(newId)); $author$project$Main$initPlayer(newId));
case 'ApiMsg':
var apiMsg = msg.a;
switch (apiMsg.$) {
case 'GotActionResult':
var response = apiMsg.a;
if (response.$ === 'Ok') {
var result = response.a;
var updates = A2($elm$core$Maybe$withDefault, _List_Nil, result.updates);
var notification = result.notification;
var errors = A2($elm$core$Maybe$withDefault, '', result.errors);
return A2(
$author$project$Main$update,
$author$project$Main$ModeSwitched($elm$core$Maybe$Nothing),
A2(
$author$project$Main$setError,
errors,
A2(
$author$project$Main$setNotification,
notification,
A3($elm$core$List$foldl, $author$project$Main$applyUpdate, model, updates))));
} else {
var r = response.a;
return _Utils_Tuple2(
A2(
$author$project$Main$setError,
$elm$core$Debug$toString(r),
model),
$elm$core$Platform$Cmd$none);
}
case 'GotPlayer': case 'GotPlayer':
var result = msg.a; var result = apiMsg.a;
if (result.$ === 'Ok') { if (result.$ === 'Ok') {
var player = result.a; var player = result.a;
return _Utils_Tuple2( return _Utils_Tuple2(
@@ -7074,13 +7113,13 @@ var $author$project$Main$update = F2(
return _Utils_Tuple2( return _Utils_Tuple2(
A2( A2(
$author$project$Main$setError, $author$project$Main$setError,
'Fetching player... ' + $author$project$Main$printError(error), 'Fetching player... ' + $elm$core$Debug$toString(error),
model), model),
$elm$core$Platform$Cmd$none); $elm$core$Platform$Cmd$none);
} }
case 'GotClaims': case 'GotClaims':
var id = msg.a; var id = apiMsg.a;
var result = msg.b; var result = apiMsg.b;
if (result.$ === 'Ok') { if (result.$ === 'Ok') {
var claims = result.a; var claims = result.a;
return _Utils_Tuple2( return _Utils_Tuple2(
@@ -7104,9 +7143,9 @@ var $author$project$Main$update = F2(
model), model),
$elm$core$Platform$Cmd$none); $elm$core$Platform$Cmd$none);
} }
case 'GotLoot': default:
var dest = msg.a; var dest = apiMsg.a;
var result = msg.b; var result = apiMsg.b;
if (result.$ === 'Ok') { if (result.$ === 'Ok') {
var loot = result.a; var loot = result.a;
return _Utils_Tuple2( return _Utils_Tuple2(
@@ -7138,10 +7177,11 @@ var $author$project$Main$update = F2(
return _Utils_Tuple2( return _Utils_Tuple2(
A2( A2(
$author$project$Main$setError, $author$project$Main$setError,
'Fetching loot... ' + $author$project$Main$printError(error), 'Fetching loot... ' + $elm$core$Debug$toString(error),
model), model),
$elm$core$Platform$Cmd$none); $elm$core$Platform$Cmd$none);
} }
}
case 'LootViewItemSwitched': case 'LootViewItemSwitched':
var id = msg.a; var id = msg.a;
var state = model.state; var state = model.state;
@@ -7175,7 +7215,7 @@ var $author$project$Main$update = F2(
return $elm$core$Maybe$Nothing; return $elm$core$Maybe$Nothing;
} else { } else {
if (newMode.a.$ === 'Grab') { if (newMode.a.$ === 'Grab') {
var _v9 = newMode.a; var _v11 = newMode.a;
return $elm$core$Maybe$Just( return $elm$core$Maybe$Just(
$elm$core$Set$fromList( $elm$core$Set$fromList(
A2( A2(
@@ -7194,50 +7234,33 @@ var $author$project$Main$update = F2(
}), }),
$elm$core$Platform$Cmd$none); $elm$core$Platform$Cmd$none);
case 'ConfirmAction': case 'ConfirmAction':
var currentMode = model.state.activeMode; var _v12 = model.state.activeMode;
return _Utils_Tuple2( if (_v12.$ === 'Nothing') {
model, var $temp$msg = $author$project$Main$ModeSwitched($elm$core$Maybe$Nothing),
A2($author$project$Main$sendRequest, currentMode, model)); $temp$model = model;
case 'UndoLastAction': msg = $temp$msg;
var playerId = $elm$core$String$fromInt(model.player.id); model = $temp$model;
return _Utils_Tuple2( continue update;
model,
$elm$http$Http$request(
{
body: $elm$http$Http$emptyBody,
expect: A2($elm$http$Http$expectJson, $author$project$Main$GotActionResult, $author$project$Main$apiResponseDecoder),
headers: _List_Nil,
method: 'DELETE',
timeout: $elm$core$Maybe$Nothing,
tracker: $elm$core$Maybe$Nothing,
url: 'http://localhost:8088/api/players/' + (playerId + '/events/last')
}));
case 'GotActionResult':
var response = msg.a;
if (response.$ === 'Ok') {
var result = response.a;
var updates = A2($elm$core$Maybe$withDefault, _List_Nil, result.updates);
var notification = result.notification;
var errors = A2($elm$core$Maybe$withDefault, '', result.errors);
return A2(
$author$project$Main$update,
$author$project$Main$ModeSwitched($elm$core$Maybe$Nothing),
A2(
$author$project$Main$setError,
errors,
A2(
$author$project$Main$setNotification,
notification,
A3($elm$core$List$foldl, $author$project$Main$applyUpdate, model, updates))));
} else { } else {
var r = response.a; var mode = _v12.a;
return _Utils_Tuple2( return _Utils_Tuple2(
model,
A2( A2(
$author$project$Main$setError, $elm$core$Platform$Cmd$map,
$elm$core$Debug$toString(r), $author$project$Main$ApiMsg,
model), A3(
$elm$core$Platform$Cmd$none); $author$project$Api$sendRequest,
mode,
$elm$core$String$fromInt(model.player.id),
A2($author$project$Main$buildPayload, mode, model))));
} }
case 'UndoLastAction':
return _Utils_Tuple2(
model,
A2(
$elm$core$Platform$Cmd$map,
$author$project$Main$ApiMsg,
$author$project$Api$undoLastAction(model.player.id)));
default: default:
return _Utils_Tuple2( return _Utils_Tuple2(
_Utils_update( _Utils_update(
@@ -7245,6 +7268,7 @@ var $author$project$Main$update = F2(
{notification: $elm$core$Maybe$Nothing}), {notification: $elm$core$Maybe$Nothing}),
$elm$core$Platform$Cmd$none); $elm$core$Platform$Cmd$none);
} }
}
}); });
var $author$project$Main$UndoLastAction = {$: 'UndoLastAction'}; var $author$project$Main$UndoLastAction = {$: 'UndoLastAction'};
var $elm$html$Html$button = _VirtualDom_node('button'); var $elm$html$Html$button = _VirtualDom_node('button');
@@ -7330,9 +7354,9 @@ var $author$project$Main$controlsWhenModeActive = function (mode) {
'danger') 'danger')
]); ]);
}; };
var $author$project$Main$Buy = {$: 'Buy'}; var $author$project$Modes$Buy = {$: 'Buy'};
var $author$project$Main$Grab = {$: 'Grab'}; var $author$project$Modes$Grab = {$: 'Grab'};
var $author$project$Main$Sell = {$: 'Sell'}; var $author$project$Modes$Sell = {$: 'Sell'};
var $author$project$Main$controlsWhenRoute = function (route) { var $author$project$Main$controlsWhenRoute = function (route) {
switch (route.$) { switch (route.$) {
case 'PlayerChest': case 'PlayerChest':
@@ -7341,7 +7365,7 @@ var $author$project$Main$controlsWhenRoute = function (route) {
A4( A4(
$author$project$Main$actionButton, $author$project$Main$actionButton,
$author$project$Main$ModeSwitched( $author$project$Main$ModeSwitched(
$elm$core$Maybe$Just($author$project$Main$Sell)), $elm$core$Maybe$Just($author$project$Modes$Sell)),
'Vendre', 'Vendre',
'coins', 'coins',
'danger') 'danger')
@@ -7352,7 +7376,7 @@ var $author$project$Main$controlsWhenRoute = function (route) {
A4( A4(
$author$project$Main$actionButton, $author$project$Main$actionButton,
$author$project$Main$ModeSwitched( $author$project$Main$ModeSwitched(
$elm$core$Maybe$Just($author$project$Main$Grab)), $elm$core$Maybe$Just($author$project$Modes$Grab)),
'Demander', 'Demander',
'praying-hands', 'praying-hands',
'primary') 'primary')
@@ -7363,7 +7387,7 @@ var $author$project$Main$controlsWhenRoute = function (route) {
A4( A4(
$author$project$Main$actionButton, $author$project$Main$actionButton,
$author$project$Main$ModeSwitched( $author$project$Main$ModeSwitched(
$elm$core$Maybe$Just($author$project$Main$Buy)), $elm$core$Maybe$Just($author$project$Modes$Buy)),
'Acheter', 'Acheter',
'coins', 'coins',
'success') 'success')
@@ -7374,7 +7398,7 @@ var $author$project$Main$controlsWhenRoute = function (route) {
A4( A4(
$author$project$Main$actionButton, $author$project$Main$actionButton,
$author$project$Main$ModeSwitched( $author$project$Main$ModeSwitched(
$elm$core$Maybe$Just($author$project$Main$Add)), $elm$core$Maybe$Just($author$project$Modes$Add)),
'Nouveau loot', 'Nouveau loot',
'plus', 'plus',
'primary') 'primary')
@@ -7443,7 +7467,7 @@ var $author$project$Main$renderIfClaimed = F2(
var $author$project$Main$LootViewItemSwitched = function (a) { var $author$project$Main$LootViewItemSwitched = function (a) {
return {$: 'LootViewItemSwitched', a: a}; return {$: 'LootViewItemSwitched', a: a};
}; };
var $author$project$Main$canSelectIn = function (mode) { var $author$project$Modes$canSelectIn = function (mode) {
switch (mode.$) { switch (mode.$) {
case 'Sell': case 'Sell':
return true; return true;
@@ -7545,7 +7569,7 @@ var $author$project$Main$rowControlsForMode = F3(
A2( A2(
$elm$core$List$cons, $elm$core$List$cons,
itemInfo, itemInfo,
$author$project$Main$canSelectIn(mode) ? _List_fromArray( $author$project$Modes$canSelectIn(mode) ? _List_fromArray(
[ [
A2( A2(
$elm$html$Html$input, $elm$html$Html$input,

View File

@@ -2,9 +2,11 @@ module Api exposing (..)
import Http import Http
import Json.Decode as D import Json.Decode as D
import Json.Decode exposing (Decoder, int, string) import Json.Decode exposing (Decoder, int, string, field, succeed)
import Json.Encode as E import Json.Encode as E
import Modes exposing (ViewMode)
type alias HttpResult a = (Result Http.Error a) type alias HttpResult a = (Result Http.Error a)
type alias Response = type alias Response =
@@ -28,6 +30,51 @@ type Msg
| GotLoot ToChest (HttpResult Loot) | GotLoot ToChest (HttpResult Loot)
| GotActionResult (HttpResult Response) | GotActionResult (HttpResult Response)
---
-- MODELS
---
-- Player
type alias Player =
{ id: Int
, name: String
, debt: Int
, wealth: Wealth
}
blankPlayer =
Player 0 "Loot-a-lot" 0 (Wealth 0 0 0 0)
type alias Wealth =
{ cp: Int
, sp: Int
, gp: Int
, pp: Int
}
-- Loot
type alias Loot = List Item
type alias Item =
{ id: Int
, name: String
, base_price: Int
}
-- Claims
type alias Claims = List Claim
type alias Claim =
{ id: Int
, player_id: Int
, loot_id: Int
}
-- PLAYERS -- PLAYERS
-- --
@@ -35,7 +82,7 @@ fetchPlayer : Int -> Cmd Msg
fetchPlayer id = fetchPlayer id =
Http.get Http.get
{ url = "http://localhost:8088/api/players/" ++ (String.fromInt id) ++ "/" { url = "http://localhost:8088/api/players/" ++ (String.fromInt id) ++ "/"
, expect = Http.expectJson Main.GotPlayer (valueDecoder playerDecoder ) , expect = Http.expectJson GotPlayer (valueDecoder playerDecoder )
} }
playerDecoder : Decoder Player playerDecoder : Decoder Player
@@ -72,13 +119,13 @@ lootDecoder : Decoder Loot
lootDecoder = lootDecoder =
Json.Decode.list itemDecoder Json.Decode.list itemDecoder
fetchLoot : Main.ToChest -> Cmd Msg fetchLoot : ToChest -> Cmd Msg
fetchLoot dest = fetchLoot dest =
let let
url = case dest of url = case dest of
Main.OfPlayer id -> "http://localhost:8088/api/players/" ++ (String.fromInt id) ++ "/loot" OfPlayer id -> "http://localhost:8088/api/players/" ++ (String.fromInt id) ++ "/loot"
Main.OfShop -> "http://localhost:8088/api/items" OfShop -> "http://localhost:8088/api/items"
Main.OfGroup -> "http://localhost:8088/api/players/0/loot" OfGroup -> "http://localhost:8088/api/players/0/loot"
in in
Http.get Http.get
{ url = url { url = url
@@ -109,7 +156,7 @@ valueDecoder thenDecoder =
-- TODO: update server to produce better json -- TODO: update server to produce better json
-- like an object with list of updates of the same type -- like an object with list of updates of the same type
-- { ItemRemoved : [..], Wealth : [ .. ], .. } -- { ItemRemoved : [..], Wealth : [ .. ], .. }
updatesDecoder : Decoder DbUpdate updatesDecoder : Decoder Update
updatesDecoder = updatesDecoder =
-- We expect one update but do not know it's kind -- We expect one update but do not know it's kind
Json.Decode.oneOf Json.Decode.oneOf
@@ -121,9 +168,9 @@ updatesDecoder =
] ]
apiResponseDecoder : Decoder ApiResponse apiResponseDecoder : Decoder Response
apiResponseDecoder = apiResponseDecoder =
Json.Decode.map4 ApiResponse Json.Decode.map4 Response
(D.maybe (field "value" string)) (D.maybe (field "value" string))
(Json.Decode.maybe (field "notification" string)) (Json.Decode.maybe (field "notification" string))
(Json.Decode.maybe (field "updates" (Json.Decode.list updatesDecoder))) (Json.Decode.maybe (field "updates" (Json.Decode.list updatesDecoder)))
@@ -139,35 +186,40 @@ undoLastAction id = Http.request
, tracker = Nothing , tracker = Nothing
} }
sendRequest : Maybe ViewMode -> Model -> Cmd Msg sendRequest : ViewMode -> String -> E.Value -> Cmd Msg
sendRequest activeMode model = sendRequest mode id payload =
case activeMode of
Nothing -> Cmd.none
Just mode ->
let let
(endpoint, method) = case mode of (endpoint, method) = case mode of
Add -> Modes.Add ->
( "http://localhost:8088/api/players/" ++ (String.fromInt model.player.id) ++ "/loot" ( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "POST" , "POST"
) )
Buy -> Modes.Buy ->
( "http://localhost:8088/api/players/" ++ (String.fromInt model.player.id) ++ "/loot" ( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "PUT" , "PUT"
) )
Sell -> Modes.Sell ->
( "http://localhost:8088/api/players/" ++ (String.fromInt model.player.id) ++ "/loot" ( "http://localhost:8088/api/players/" ++ id ++ "/loot"
, "DELETE" , "DELETE"
) )
Grab -> Modes.Grab ->
( "http://localhost:8088/api/players/" ++ (String.fromInt model.player.id) ++ "/claims" ( "http://localhost:8088/api/players/" ++ id ++ "/claims"
, "POST") , "POST")
in in
Http.request Http.request
{ method = method { method = method
, headers = [] , headers = []
, url = endpoint , url = endpoint
, body = Http.jsonBody <| buildPayload mode model , body = Http.jsonBody payload
, expect = Http.expectJson GotActionResult apiResponseDecoder , expect = Http.expectJson GotActionResult apiResponseDecoder
, timeout = Nothing , timeout = Nothing
, tracker = Nothing , tracker = Nothing
} }
printError : Http.Error -> String
printError error =
case error of
Http.NetworkError -> "Le serveur ne répond pas"
_ -> "Erreur inconnue"

View File

@@ -9,8 +9,10 @@ import Html.Events exposing (..)
import Svg.Attributes import Svg.Attributes
import Url.Parser as P exposing (Parser, (</>), oneOf, s) import Url.Parser as P exposing (Parser, (</>), oneOf, s)
import Set exposing (Set) import Set exposing (Set)
import Json.Encode as E
import Api import Api exposing (Player, Loot, Wealth, Item, Claim, Claims)
import Modes exposing (ViewMode)
-- Main -- Main
@@ -57,7 +59,7 @@ init flags url key =
in in
( Model ( Model
(State key route "" False Nothing Nothing) (State key route "" False Nothing Nothing)
blankPlayer Api.blankPlayer
[] []
Nothing Nothing
Nothing Nothing
@@ -74,57 +76,13 @@ fetchInitialData playerId =
, Cmd.map ApiMsg <| Api.fetchLoot Api.OfGroup , Cmd.map ApiMsg <| Api.fetchLoot Api.OfGroup
] ]
---
-- MODELS
---
-- Player
type alias Player =
{ id: Int
, name: String
, debt: Int
, wealth: Wealth
}
blankPlayer =
Player 0 "Loot-a-lot" 0 (Wealth 0 0 0 0)
initPlayer id = initPlayer id =
Cmd.batch Cmd.batch
[ Cmd.map ApiMsg <| Api.fetchPlayer id [ Cmd.map ApiMsg <| Api.fetchPlayer id
, Cmd.map ApiMsg <| Api.fetchLoot (OfPlayer id) , Cmd.map ApiMsg <| Api.fetchLoot (Api.OfPlayer id)
, Cmd.map ApiMsg <| Api.fetchClaims id , Cmd.map ApiMsg <| Api.fetchClaims id
] ]
type alias Wealth =
{ cp: Int
, sp: Int
, gp: Int
, pp: Int
}
-- Loot
type alias Loot = List Item
type alias Item =
{ id: Int
, name: String
, base_price: Int
}
-- Claims
type alias Claims = List Claim
type alias Claim =
{ id: Int
, player_id: Int
, loot_id: Int
}
-- UPDATE -- UPDATE
type Msg type Msg
@@ -160,7 +118,7 @@ update msg model =
{ model | state = { state | route = page }} { model | state = { state | route = page }}
|> update (case page of |> update (case page of
-- Directly enter add mode on NewLoot view -- Directly enter add mode on NewLoot view
NewLoot -> ModeSwitched (Just Add) NewLoot -> ModeSwitched (Just Modes.Add)
other -> ModeSwitched Nothing other -> ModeSwitched Nothing
) )
@@ -168,10 +126,10 @@ update msg model =
( setError "Invalid route" model, Cmd.none ) ( setError "Invalid route" model, Cmd.none )
PlayerChanged newId -> PlayerChanged newId ->
( { model | player = blankPlayer }, initPlayer newId ) ( { model | player = Api.blankPlayer }, initPlayer newId )
ApiMsg apiMsg -> case apiMsg of ApiMsg apiMsg -> case apiMsg of
GotActionResult response -> Api.GotActionResult response ->
case response of case response of
Ok result -> Ok result ->
let let
@@ -185,23 +143,23 @@ update msg model =
|> update (ModeSwitched Nothing) |> update (ModeSwitched Nothing)
Err r -> (setError (Debug.toString r) model, Cmd.none) Err r -> (setError (Debug.toString r) model, Cmd.none)
GotPlayer result -> Api.GotPlayer result ->
case result of case result of
Ok player -> Ok player ->
( { model | player = player } ( { model | player = player }
, Cmd.none , Cmd.none
) )
Err error -> Err error ->
( setError ("Fetching player... " ++ printError error) model ( setError ("Fetching player... " ++ Debug.toString error) model
, Cmd.none , Cmd.none
) )
GotClaims id result -> Api.GotClaims id result ->
case result of case result of
Ok claims -> ( { model | claims = List.filter (\c -> c.player_id == id) claims}, Cmd.none ) Ok claims -> ( { model | claims = List.filter (\c -> c.player_id == id) claims}, Cmd.none )
Err error -> ( setError ("Fetching claims..." ++ Debug.toString error) model, Cmd.none) Err error -> ( setError ("Fetching claims..." ++ Debug.toString error) model, Cmd.none)
GotLoot dest result -> Api.GotLoot dest result ->
case result of case result of
Ok loot -> Ok loot ->
( case dest of ( case dest of
@@ -211,7 +169,7 @@ update msg model =
, Cmd.none , Cmd.none
) )
Err error -> Err error ->
( setError ("Fetching loot... " ++ printError error) model ( setError ("Fetching loot... " ++ Debug.toString error) model
, Cmd.none , Cmd.none
) )
@@ -235,7 +193,7 @@ update msg model =
Nothing -> Nothing ->
Nothing Nothing
Just Grab -> -- Currently claimed object are initially selected Just Modes.Grab -> -- Currently claimed object are initially selected
Just ( Set.fromList <| List.map (\c -> c.loot_id) model.claims) Just ( Set.fromList <| List.map (\c -> c.loot_id) model.claims)
Just others -> Just others ->
@@ -244,13 +202,14 @@ update msg model =
, Cmd.none ) , Cmd.none )
ConfirmAction -> ConfirmAction ->
let case model.state.activeMode of
currentMode = model.state.activeMode Nothing ->
in update (ModeSwitched Nothing) model
(model, Cmd.map ApiMsg Api.sendRequest currentMode model) Just mode ->
(model, Cmd.map ApiMsg <| Api.sendRequest mode (String.fromInt model.player.id) (buildPayload mode model))
UndoLastAction -> UndoLastAction ->
(model, Cmd.map ApiMsg Api.undoLastAction model.player.id) (model, Cmd.map ApiMsg <| Api.undoLastAction model.player.id)
ClearNotification -> ClearNotification ->
( { model | notification = Nothing }, Cmd.none ) ( { model | notification = Nothing }, Cmd.none )
@@ -262,10 +221,10 @@ setNotification notification model =
targetItemsFor : ViewMode -> Model -> List Item targetItemsFor : ViewMode -> Model -> List Item
targetItemsFor mode model = targetItemsFor mode model =
case mode of case mode of
Add -> [] Modes.Add -> []
Buy -> Maybe.withDefault [] model.merchantItems Modes.Buy -> Maybe.withDefault [] model.merchantItems
Sell ->Maybe.withDefault [] model.loot Modes.Sell ->Maybe.withDefault [] model.loot
Grab -> Maybe.withDefault [] model.groupLoot Modes.Grab -> Maybe.withDefault [] model.groupLoot
buildPayload : ViewMode -> Model -> E.Value buildPayload : ViewMode -> Model -> E.Value
buildPayload mode model = buildPayload mode model =
@@ -274,40 +233,33 @@ buildPayload mode model =
|> List.filter (itemInSelection model.state.selection) |> List.filter (itemInSelection model.state.selection)
in in
case mode of case mode of
Buy -> E.object Modes.Buy -> E.object
[ ( "items", items |> E.list (\i -> E.list identity [E.int i.id, E.null])) [ ( "items", items |> E.list (\i -> E.list identity [E.int i.id, E.null]))
, ("global_mod", E.null ) , ("global_mod", E.null )
] ]
Sell -> E.object Modes.Sell -> E.object
[ ( "items", items |> E.list (\i -> E.list identity [E.int i.id, E.null])) [ ( "items", items |> E.list (\i -> E.list identity [E.int i.id, E.null]))
, ("global_mod", E.null ) , ("global_mod", E.null )
] ]
Grab -> E.object Modes.Grab -> E.object
[ ( "items", items |> E.list (\i -> E.int i.id)) [ ( "items", items |> E.list (\i -> E.int i.id))
, ("global_mod", E.null ) , ("global_mod", E.null )
] ]
Add -> E.object Modes.Add -> E.object
[ ( "items", items |> E.list (\i -> E.int i.id)) [ ( "items", items |> E.list (\i -> E.int i.id))
, ("global_mod", E.null ) , ("global_mod", E.null )
] ]
type DbUpdate
= ItemRemoved Item
| ItemAdded Item
| WealthUpdated Wealth
| ClaimAdded ()
| ClaimRemoved ()
-- DbUpdates always refer to the active player's loot -- DbUpdates always refer to the active player's loot
applyUpdate : DbUpdate -> Model -> Model applyUpdate : Api.Update -> Model -> Model
applyUpdate u model = applyUpdate u model =
case u of case u of
ItemRemoved item -> { model | loot = Just Api.ItemRemoved item -> { model | loot = Just
<| List.filter (\i -> i.id /= item.id) <| List.filter (\i -> i.id /= item.id)
<| Maybe.withDefault [] model.loot } <| Maybe.withDefault [] model.loot }
ItemAdded item -> { model | loot = Just Api.ItemAdded item -> { model | loot = Just
<| item :: Maybe.withDefault [] model.loot } <| item :: Maybe.withDefault [] model.loot }
WealthUpdated diff -> Api.WealthUpdated diff ->
let let
player = model.player player = model.player
wealth = player.wealth wealth = player.wealth
@@ -319,8 +271,8 @@ applyUpdate u model =
(wealth.gp + diff.gp) (wealth.gp + diff.gp)
(wealth.pp + diff.pp) (wealth.pp + diff.pp)
)}} )}}
ClaimAdded _ -> model Api.ClaimAdded _ -> model
ClaimRemoved _ -> model Api.ClaimRemoved _ -> model
-- ERRORS -- ERRORS
@@ -334,12 +286,6 @@ setError error model =
{ state | error = error }} { state | error = error }}
printError : Http.Error -> String
printError error =
case error of
Http.NetworkError -> "Le serveur ne répond pas"
_ -> "Erreur inconnue"
-- STATE Utils -- STATE Utils
switchSelectionState : Int -> Maybe Selection -> Maybe Selection switchSelectionState : Int -> Maybe Selection -> Maybe Selection
@@ -362,20 +308,6 @@ subscriptions _ =
-- VIEWS -- VIEWS
--- ---
type ViewMode
= Sell
| Buy
| Grab
| Add
canSelectIn : ViewMode -> Bool
canSelectIn mode =
case mode of
Sell -> True
Buy -> True
Grab -> True
Add -> False
actionButton msg t icon color = actionButton msg t icon color =
button [ class <| "button level-item is-" ++ color button [ class <| "button level-item is-" ++ color
, onClick msg ] , onClick msg ]
@@ -392,10 +324,10 @@ controlsWhenModeActive mode =
controlsWhenRoute : Route -> List (Html Msg) controlsWhenRoute : Route -> List (Html Msg)
controlsWhenRoute route = controlsWhenRoute route =
case route of case route of
PlayerChest -> [actionButton (ModeSwitched (Just Sell)) "Vendre" "coins" "danger"] PlayerChest -> [actionButton (ModeSwitched (Just Modes.Sell)) "Vendre" "coins" "danger"]
GroupLoot -> [actionButton (ModeSwitched (Just Grab)) "Demander" "praying-hands" "primary"] GroupLoot -> [actionButton (ModeSwitched (Just Modes.Grab)) "Demander" "praying-hands" "primary"]
Merchant -> [actionButton (ModeSwitched (Just Buy)) "Acheter" "coins" "success"] Merchant -> [actionButton (ModeSwitched (Just Modes.Buy)) "Acheter" "coins" "success"]
NewLoot -> [actionButton (ModeSwitched (Just Add)) "Nouveau loot" "plus" "primary"] NewLoot -> [actionButton (ModeSwitched (Just Modes.Add)) "Nouveau loot" "plus" "primary"]
view : Model -> Browser.Document Msg view : Model -> Browser.Document Msg
view model = view model =
@@ -487,14 +419,14 @@ rowControlsForMode : ViewMode -> (Item -> Bool) -> Item -> Html Msg
rowControlsForMode mode isSelected item = rowControlsForMode mode isSelected item =
let let
itemInfo = case mode of itemInfo = case mode of
Buy -> p [class "level-item"] [ text (String.fromInt item.base_price ++ "po")] Modes.Buy -> p [class "level-item"] [ text (String.fromInt item.base_price ++ "po")]
Sell -> p [class "level-item"] [ text (String.fromFloat (toFloat item.base_price / 2) ++ "po")] Modes.Sell -> p [class "level-item"] [ text (String.fromFloat (toFloat item.base_price / 2) ++ "po")]
Grab -> p [class "level-item"] [ text "Grab" ] Modes.Grab -> p [class "level-item"] [ text "Grab" ]
Add -> p [class "level-item"] [ text "New !" ] Modes.Add -> p [class "level-item"] [ text "New !" ]
in in
div [ class "level-right" ] div [ class "level-right" ]
<| itemInfo <| itemInfo
:: if canSelectIn mode then :: if Modes.canSelectIn mode then
[input [ class "checkbox level-item" [input [ class "checkbox level-item"
, type_ "checkbox" , type_ "checkbox"
, checked <| isSelected item , checked <| isSelected item

16
src/Modes.elm Normal file
View File

@@ -0,0 +1,16 @@
module Modes exposing (..)
type ViewMode
= Sell
| Buy
| Grab
| Add
canSelectIn : ViewMode -> Bool
canSelectIn mode =
case mode of
Sell -> True
Buy -> True
Grab -> True
Add -> False