-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsevenchain.js
173 lines (164 loc) · 5.68 KB
/
sevenchain.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const csp = require('content-security-policy');
const JSON =require('json-bigint');
const cspPolicy = {
'default-src' : csp.SRC_ANY,
'script-src' : csp.SRC_ANY,
'connect-src' : csp.SRC_ANY,
'img-src' : csp.SRC_ANY,
'font-src' : csp.SRC_ANY,
'child-src' : csp.SRC_ANY,
'form-src' : csp.SRC_ANY,
'frame-ancestors' : csp.SRC_ANY,
'stlye-src' : csp.SRC_ANY
};
const globalCSP = csp.getCSP(cspPolicy);
var express = require('express');
var app = express();
var bodyParser = require('body-parser-bigint');
var endpoint = 'http://10.44.1.107:8888';
//var endpoint = 'http://10.110.31.38:8888';
var EosApi = require('eosjs-api');
var Eos = require('eosjs');
eos_config = {
httpEndpoint: endpoint,
chainId: 'cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f',
keyProvider: [ '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3' ],
sign: true,
debuf: true
};
var eos_api = EosApi({
httpEndpoint: endpoint
});
var eos = Eos(eos_config);
app.use(globalCSP);
app.use(bodyParser.text());
app.use(bodyParser.urlencoded({extended:true}));
app.get('/', function(req, res){
eos_api.getInfo({}).then(result => {
//console.log(JSON.stringify(result, null, 2));
res.send(result);
});
});
//app.get('/get/table', function(req, res){
// eos_api.getTableRows(true, 'seven.code', 'seven.code', req.query.name, 'id', req.query.id, -1, req.query.limit)
// .then(result => res.send(result));
//});
app.get('/get/tablerows/:name/:id/:limit', function(req, res){
eos_api.getTableRows(true, 'seven.code', 'seven.code', req.params.name, 'id', req.params.id, -1, req.params.limit)
.then(result => {
console.log(result.rows.length);
res.send(result);
}).catch(e => {
console.error(e);
res.send({result:"error"});
});
});
app.get('/get/tableinfo', function(req, res){
eos_api.getTableRows(true, 'seven.code', 'seven.code', 'tableinfo', 'id', 0, -1, 10)
.then(result => {
res.send(result);
}).catch(e => {
console.error(e);
res.send({result:"error"});
});
});
app.get('/get/table/:name/:id', function(req, res){
eos_api.getTableRows(true, 'seven.code', 'seven.code', req.params.name, 'id', req.params.id, req.params.id+1, 1)
.then(result => {
// if(result != null && result.rows.length > 0)
// res.send(result.rows[0]);
// else
res.send(result);
}).catch(e => {
console.error(e);
res.send({result:"error"});
});
});
app.get('/getinfo', function(req, res){
eos_api.getTableRows(true, 'seven.code', 'seven.code', 'poker', 'id', 0, -1, 10)
.then(result => res.send(result));
});
app.post('/slotsave', function(req, res){
var json = JSON.parse(req.body);
eos.contract('seven.code').then(function(sevenchain){
return sevenchain.startslot(json.id, json.participants, json.rng,{authorization:['seven.code@active']});
//return sevenchain.startslot({id:id, participants:JSON.parse(participants), rng:rng},{authorization:['seven.code@active']});
}).then(function(result){
//console.log("result : " + result);
res.send({result:"ok", transaction_id:result.transaction_id});
}).catch(e=>{
console.error("catch : " + e);
res.send({result:"error"});
});
});
app.post('/pokersave', function(req, res){
var json = JSON.parse(req.body);
eos.contract('seven.code').then(function(sevenchain){
return sevenchain.startpoker(json.id, json.participants, json.rng,{authorization:['seven.code@active']});
}).then(function(result){
res.send({result:"ok", transaction_id:result.transaction_id});
}).catch(e=>{
console.error("catch : " + e);
res.send({result:"error"});
});
});
/* for testing gachasave
app.get('/gachasavetest', function(req, res){
var json = {id:1, participants:[], rng:[]};
eos.contract('seven.code').then(function(sevenchain){
return sevenchain.startitem(json.id, json.participants, json.rng,{authorization:['seven.code@active']});
}).then(function(result){
res.send({result:"ok"});
}).catch(e=>{
console.error("catch : " + e);
res.send({result:"error"});
});
});
*/
app.post('/gachasave', function(req, res){
var json = JSON.parse(req.body);
eos.contract('seven.code').then(function(sevenchain){
return sevenchain.startitem(json.id, json.participants, json.rng,{authorization:['seven.code@active']});
}).then(function(result){
res.send({result:"ok", transaction_id:result.transaction_id});
}).catch(e=>{
console.error("catch : " + e);
res.send({result:"error"});
});
});
/*
app.get('/itemlogtest', function(req, res){
var json = {id:'box4', iteminfos:[{name:'sword', count:1}, {name:'armor', count:2}, {name:'staff', count:4}]};
eos.contract('seven.code').then(function(sevenchain){
return sevenchain.saveiteminfo(json.id, json.iteminfos,{authorization:['seven.code@active']});
}).then(function(result){
console.log(JSON.stringify(result, null, 2));
res.send({result:"ok"});
}).catch(e=>{
console.error("catch : " + e);
res.send({result:"error"});
});
});
*/
app.post('/gachalog', function(req, res){
var json = JSON.parse(req.body);
eos.contract('seven.code').then(function(sevenchain){
return sevenchain.saveiteminfo(json.id, json.iteminfos,{authorization:['seven.code@active']});
}).then(function(result){
res.send({result:"ok", transaction_id:result.transaction_id});
}).catch(e=>{
console.error("catch : " + e);
res.send({result:"error"});
});
});
app.get('/get/actions/:account/:pos/:offset', function(req, res){
eos_api.getActions(req.params.account,req.params.pos,req.params.offset).then(result => {
res.send(JSON.stringify(result, null, 3));
});
});
var server = app.listen(80, function(){
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
});
console.log('Server Start');