232 lines
4.4 KiB
TypeScript
232 lines
4.4 KiB
TypeScript
|
|
namespace mod_model.app
|
||
|
|
{
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
export enum enum_condition
|
||
|
|
{
|
||
|
|
logged_out,
|
||
|
|
logged_in,
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
export type type_subject =
|
||
|
|
{
|
||
|
|
condition : (null | enum_condition);
|
||
|
|
loginkey : (null | string);
|
||
|
|
rental_counter : int;
|
||
|
|
rentals :
|
||
|
|
{
|
||
|
|
offer : Record<mod_model.rental.type_id, lib_mvc.type_model<mod_model.rental.type_subject>>;
|
||
|
|
order : Array<mod_model.rental.type_id>;
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
export function inital
|
||
|
|
(
|
||
|
|
loginkey : (null | string)
|
||
|
|
) : type_subject
|
||
|
|
{
|
||
|
|
return {
|
||
|
|
"condition": ((loginkey === null) ? enum_condition.logged_out : enum_condition.logged_in),
|
||
|
|
"loginkey": loginkey,
|
||
|
|
"rental_counter": 0,
|
||
|
|
"rentals": {"offer": {}, "order": []},
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
export async function login
|
||
|
|
(
|
||
|
|
model : lib_mvc.type_model<type_subject>,
|
||
|
|
username : string,
|
||
|
|
password : string
|
||
|
|
) : Promise<void>
|
||
|
|
{
|
||
|
|
const result : any = await lib_nextbike.login(username, password);
|
||
|
|
model.state.loginkey = result["user"]["loginkey"];
|
||
|
|
model.state.condition = enum_condition.logged_in;
|
||
|
|
lib_storage.write("nextbike_loginkey", model.state.loginkey);
|
||
|
|
lib_mvc.model_notify<type_subject, {}>
|
||
|
|
(
|
||
|
|
model,
|
||
|
|
{
|
||
|
|
"type": "login",
|
||
|
|
"data": {}
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @todo end rentals?
|
||
|
|
*/
|
||
|
|
export async function logout
|
||
|
|
(
|
||
|
|
model : lib_mvc.type_model<type_subject>
|
||
|
|
) : Promise<void>
|
||
|
|
{
|
||
|
|
const result : any = await lib_nextbike.logout(model.state.loginkey);
|
||
|
|
lib_storage.kill("nextbike_loginkey");
|
||
|
|
model.state.condition = enum_condition.logged_out;
|
||
|
|
model.state.rentals = {"offer": {}, "order": []};
|
||
|
|
lib_mvc.model_notify<type_subject, {}>
|
||
|
|
(
|
||
|
|
model,
|
||
|
|
{
|
||
|
|
"type": "logout",
|
||
|
|
"data": {}
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
export async function prepare_rental
|
||
|
|
(
|
||
|
|
model : lib_mvc.type_model<type_subject>
|
||
|
|
) : Promise<void>
|
||
|
|
{
|
||
|
|
model.state.rental_counter += 1;
|
||
|
|
const rental_id : mod_model.rental.type_id = model.state.rental_counter.toFixed(0);
|
||
|
|
const rental_subject : mod_model.rental.type_subject = mod_model.rental.initial(rental_id);
|
||
|
|
const rental_model : lib_mvc.type_model<mod_model.rental.type_subject> = lib_mvc.model_make(rental_subject);
|
||
|
|
lib_mvc.model_listen<mod_model.rental.type_subject, any>
|
||
|
|
(
|
||
|
|
rental_model,
|
||
|
|
async (event) =>
|
||
|
|
{
|
||
|
|
switch (event.type)
|
||
|
|
{
|
||
|
|
default:
|
||
|
|
{
|
||
|
|
// do nothing
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case "start":
|
||
|
|
{
|
||
|
|
await start_rental(model, rental_subject.bike_name);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case "pause":
|
||
|
|
{
|
||
|
|
await pause_rental(model, rental_subject.bike_name);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case "open":
|
||
|
|
{
|
||
|
|
await open_lock(model, rental_subject.bike_name);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case "end":
|
||
|
|
{
|
||
|
|
await end_rental(model, rental_subject.id);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
);
|
||
|
|
model.state.rentals.offer[rental_id] = lib_mvc.model_make<mod_model.rental.type_subject>(rental_subject);
|
||
|
|
model.state.rentals.order.push(rental_id);
|
||
|
|
lib_mvc.model_notify<type_subject, {rental_model : lib_mvc.type_model<mod_model.rental.type_subject>}>
|
||
|
|
(
|
||
|
|
model,
|
||
|
|
{
|
||
|
|
"type": "new_rental",
|
||
|
|
"data": {"rental_model": rental_model}
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
async function start_rental
|
||
|
|
(
|
||
|
|
model : lib_mvc.type_model<type_subject>,
|
||
|
|
bike_name : string
|
||
|
|
) : Promise<void>
|
||
|
|
{
|
||
|
|
const result : any = await lib_nextbike.rental_begin(model.state.loginkey, bike_name);
|
||
|
|
lib_mvc.model_notify<type_subject, {}>
|
||
|
|
(
|
||
|
|
model,
|
||
|
|
{
|
||
|
|
"type": "start_rental",
|
||
|
|
"data": {}
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
async function pause_rental
|
||
|
|
(
|
||
|
|
model : lib_mvc.type_model<type_subject>,
|
||
|
|
bike_name : string
|
||
|
|
) : Promise<void>
|
||
|
|
{
|
||
|
|
const result : any = await lib_nextbike.rental_break(model.state.loginkey, bike_name);
|
||
|
|
lib_mvc.model_notify<type_subject, {}>
|
||
|
|
(
|
||
|
|
model,
|
||
|
|
{
|
||
|
|
"type": "pause_rental",
|
||
|
|
"data": {}
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
async function open_lock
|
||
|
|
(
|
||
|
|
model : lib_mvc.type_model<type_subject>,
|
||
|
|
bike_name : string
|
||
|
|
) : Promise<void>
|
||
|
|
{
|
||
|
|
const result : any = await lib_nextbike.open_lock(model.state.loginkey, bike_name);
|
||
|
|
lib_mvc.model_notify<type_subject, {}>
|
||
|
|
(
|
||
|
|
model,
|
||
|
|
{
|
||
|
|
"type": "open_lock",
|
||
|
|
"data": {}
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
function end_rental
|
||
|
|
(
|
||
|
|
model : lib_mvc.type_model<type_subject>,
|
||
|
|
rental_id : mod_model.rental.type_id
|
||
|
|
) : Promise<void>
|
||
|
|
{
|
||
|
|
lib_mvc.model_notify<type_subject, {}>
|
||
|
|
(
|
||
|
|
model,
|
||
|
|
{
|
||
|
|
"type": "end_rental",
|
||
|
|
"data": {"id": rental_id}
|
||
|
|
}
|
||
|
|
);
|
||
|
|
model.state.rentals.order = model.state.rentals.order.filter(x => (x !== rental_id));
|
||
|
|
delete model.state.rentals.offer[rental_id];
|
||
|
|
return Promise.resolve<void>(undefined);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|