-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
154 lines (141 loc) · 4.93 KB
/
server.ts
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
/// <reference path="./typings/global.d.ts" />
/// <reference path="./typings/ws-wrapper.d.ts" />
import { v4 as uuidv4 } from 'uuid';
import { WebSocketServer as WSS } from 'ws';
import config from './config.js';
import { store } from './storeServer.js';
import createItemTypes from './data/types.js';
import initialObjectActions from './data/initialObjects.js';
import { loginClientSalted, loginWithToken, createAccountWithTokenClientSalted, requestChangePasswordClientSalted } from './concerns/account.js';
import { accountsActions } from './concerns/account.js';
import * as tgoActions from './actions/tgo.js';
import * as playerActions from './actions/player.js';
import { mapActions, MapSize } from './concerns/map.js';
import * as clientActions from './actions/client.js';
import { set as allSet } from './actions/allSet.js';
import { getType } from 'typesafe-actions';
import { AnyAction } from 'redux';
import { extendedSocket } from './reducers/client.js';
import { withClient } from './actions/withClient.js';
// import { setGoals } from './actions/goals.js';
import { moveGoal } from './actions/moveGoal.js';
import { consumeGoal } from './actions/consumeGoal.js';
import { RootStateType } from './reducers/index.js';
import { transaction } from './concerns/transaction.js';
import { setRunning as tickerSetRunning } from './concerns/ticker.js';
import { claimLand, payRent } from './concerns/rentOffice.js';
import { consumerActions } from './concerns/consumer.js';
import { deployableActions } from './concerns/deployable.js';
// Start the server
const wss = new WSS({ port: config.gameServer.port });
console.log(`Started game server. Binding to ${config.gameServer.bind}:${config.gameServer.port}`);
try {
// When a connection is established
wss.on('connection', (socket) => {
console.log('Opened connection 🎉');
console.log('Creating a client');
const clientId = uuidv4();
const extended: extendedSocket = Object.create(socket, {
'sendAction': {
enumerable: true,
value: function(action: AnyAction) {
this.send(JSON.stringify({
action
}));
},
writable: true,
}
});
store.dispatch(clientActions.add({
clientId,
socket: extended,
}));
// When data is received
socket.on('message', function parseMessage (this, wsData, isBinary?: boolean) {
if (!Buffer.isBuffer(wsData)) {
console.log('Unknown message of type: ', typeof wsData);
}
const message = wsData.toString();
if (message) {
try {
const data = JSON.parse(message);
console.log(data);
if (!data.action || !data.action.type) {
console.log('malformed action in data: ', data);
return;
}
switch (data.action.type) {
case 'GET_ALL_OBJECTS':
extended.sendAction(
allSet({
...store.getState(),
clients: {},
} as RootStateType),
);
break;
case getType(accountsActions.accountRequest):
store.dispatch(accountsActions.accountRequestWithClient({
...data.action.payload,
clientId,
}));
break;
case getType(playerActions.playerRequest):
store.dispatch(playerActions.playerRequestServer({
...data.action.payload,
clientId,
}));
break;
case getType(loginClientSalted):
case getType(loginWithToken):
store.dispatch(withClient(data.action, clientId));
break;
case getType(createAccountWithTokenClientSalted):
case getType(requestChangePasswordClientSalted):
// case getType(setGoals):
case getType(transaction):
case 'HARVEST':
case 'STORE_TRANSACTION_REQUEST':
case 'GOVERNMENT_CLAIM_CITIZENSHIP':
case 'GOVERNMENT_CLAIM_STIPEND':
case getType(claimLand):
case getType(payRent):
case getType(moveGoal):
case getType(consumeGoal):
case getType(consumerActions.consume):
case getType(deployableActions.deployType):
case getType(deployableActions.deployTgo):
store.dispatch(data.action);
break;
default:
console.log(`Unhandled data action of type ${data.action.type}`);
}
} catch (jsonEx) {
console.log('malformed JSON in message: ', wsData, '|', jsonEx);
return;
}
// } else if (typeof message === 'object') {
// if (message.action) {
// }
} else {
console.log('Unknown message of type: ', typeof wsData);
}
});
// The connection was closed
socket.on('close', () => {
store.dispatch(clientActions.remove(clientId));
console.log('Closed Connection 😱');
});
// The connection was closed
socket.on('error', (e) => {
store.dispatch(clientActions.remove(clientId));
console.log('Erred Connection 😱', e);
});
});
} catch (e) { console.log(e); }
const init = () => {
createItemTypes(store.dispatch);
initialObjectActions().forEach(o => store.dispatch(o));
store.dispatch(mapActions.generate({ size: { x: 200, y: 30 } as MapSize, seed: 1233321 }));
store.dispatch(tickerSetRunning(true));
};
init();