-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduxRoad.js
148 lines (123 loc) · 3.16 KB
/
reduxRoad.js
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
// redux basic concepts: simple game
const initialWagonState = {
supplies: 100,
distance: 0,
days: 0,
cash: 200,
};
const reducer = (state = initialWagonState, action = {}) => {
switch (action.type) {
case "gather":
return {
...state,
supplies: state.supplies + 15,
days: state.days + 1,
};
case "travel":
if (state.supplies > 20 * action.payload) {
return {
...state,
supplies: state.supplies - 20 * action.payload,
distance: state.distance + 10 * action.payload,
days: state.days + action.payload,
};
} else {
console.log("Too broke to travel!");
}
case "tippedWagon":
return {
...state,
supplies: state.supplies - 30,
days: state.days + 1,
};
case "sell":
if (state.supplies >= 20) {
return {
...state,
supplies: state.supplies - 20,
cash: state.cash + 5,
};
} else {
console.log("Too low on supplies to sell");
}
case "buy":
if (action.payload <= state.cash) {
return {
...state,
supplies: state.supplies + 25,
cash: state.cash - 15,
};
} else {
console.log("Not enough cash");
}
case "theft":
if (state.cash > 0) {
console.log("You were robbed!");
return {
...state,
cash: Math.floor(state.cash / 2),
};
} else {
console.log("Too broke to be robbed");
}
default:
return state;
}
};
let wagon = reducer();
const oneDayTravelAction = {
type: "travel",
payload: 1,
};
wagon = reducer(wagon, oneDayTravelAction);
console.log(wagon);
const gatherAction = {
type: "gather",
};
wagon = reducer(wagon, gatherAction);
console.log(wagon);
const tippedAction = {
type: "tippedWagon",
};
wagon = reducer(wagon, tippedAction);
console.log(wagon);
const threeDayTravelAction = {
type: "travel",
payload: 3,
};
wagon = reducer(wagon, threeDayTravelAction);
console.log(wagon);
const sellAction = {
type: "sell",
};
wagon = reducer(wagon, sellAction);
console.log(wagon);
const buyAction = {
type: "buy",
payload: 15,
};
wagon = reducer(wagon, buyAction);
console.log(wagon);
const theftAction = {
type: "theft",
};
wagon = reducer(wagon, theftAction);
console.log(wagon);
wagon = reducer(wagon, theftAction);
console.log(wagon);
wagon = reducer(wagon, theftAction);
console.log(wagon);
wagon = reducer(wagon, theftAction);
console.log(wagon);
wagon = reducer(wagon, theftAction);
console.log(wagon);
wagon = reducer(wagon, theftAction);
console.log(wagon);
wagon = reducer(wagon, theftAction);
console.log(wagon);
wagon = reducer(wagon, theftAction);
console.log(wagon);
wagon = reducer(wagon, theftAction);
console.log(wagon);
wagon = reducer(wagon, threeDayTravelAction);
console.log(wagon);