Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
cbdiscord
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Geovanny E. Vera Pazmino
cbdiscord
Commits
8ebe56c2
Commit
8ebe56c2
authored
May 16, 2020
by
Geovanny
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
War participation api
parent
c7632c96
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
176 additions
and
71 deletions
+176
-71
database.js
server/database/database.js
+11
-12
model.js
server/house/model.js
+34
-2
route.js
server/house/route.js
+45
-1
main.js
server/main.js
+64
-56
package-lock.json
server/package-lock.json
+21
-0
package.json
server/package.json
+1
-0
No files found.
server/database/database.js
View file @
8ebe56c2
...
...
@@ -4,17 +4,16 @@ const MySQL = require('promise-mysql');
const
ENV
=
require
(
'../settings'
);
const
db
=
{}
let
connection
=
MySQL
.
createConnection
({
host
:
ENV
.
DB_HOST
,
port
:
ENV
.
DB_PORT
,
user
:
ENV
.
DB_USER
,
password
:
ENV
.
DB_PASS
,
database
:
ENV
.
DB_NAME
});
connection
.
then
((
con
)
=>
{
console
.
log
(
'Database Connected'
);
db
.
con
=
con
;
});
db
.
connectDB
=
async
()
=>
{
const
connection
=
await
MySQL
.
createConnection
({
host
:
ENV
.
DB_HOST
,
port
:
ENV
.
DB_PORT
,
user
:
ENV
.
DB_USER
,
password
:
ENV
.
DB_PASS
,
database
:
ENV
.
DB_NAME
});
console
.
log
(
'Database connected'
);
db
.
con
=
connection
;
}
module
.
exports
=
db
;
\ No newline at end of file
server/house/model.js
View file @
8ebe56c2
...
...
@@ -169,13 +169,45 @@ model.changeHouseLiege = async(liege_id, member_id) => {
}
model
.
getMemberUnits
=
async
(
member_id
)
=>
{
const
sql_txt
=
`SELECT u.*, uu.unit_level, uu.elite_flg
const
sql_t
e
xt
=
`SELECT u.*, uu.unit_level, uu.elite_flg
FROM users as us
LEFT JOIN users_units as uu ON us.id = uu.user_id
LEFT JOIN units as u ON uu.unit_id = u.id
WHERE us.id = ? ORDER BY u.name ASC;`
const
data
=
await
db
.
con
.
query
(
sql_txt
,
[
member_id
]);
const
data
=
await
db
.
con
.
query
(
sql_t
e
xt
,
[
member_id
]);
return
data
;
}
model
.
getCurrentWar
=
async
()
=>
{
const
sql_text
=
'SELECT * FROM war_days WHERE completed = 0 LIMIT 1;'
const
data
=
await
db
.
con
.
query
(
sql_text
);
return
data
[
0
];
}
model
.
insertNewWar
=
async
()
=>
{
const
sql_text
=
'UPDATE war_days SET completed = 1;'
;
const
sql_text2
=
'INSERT INTO war_days (day) VALUES (CURDATE());'
await
db
.
con
.
query
(
'START TRANSACTION;'
);
await
db
.
con
.
query
(
sql_text
);
await
db
.
con
.
query
(
sql_text2
);
await
db
.
con
.
query
(
'COMMIT;'
);
}
model
.
warParticipation
=
async
(
user_id
,
house_id
,
decision
)
=>
{
const
sql_text
=
'SELECT @current_war_id:=id FROM war_days WHERE completed = 0 LIMIT 1;'
;
const
sql_text2
=
`INSERT INTO users_war (user_id, war_id, house_id, decision) VALUES (?, @current_war_id, ?, ?)
ON DUPLICATE KEY UPDATE decision = ?`
;
await
db
.
con
.
query
(
'START TRANSACTION;'
);
await
db
.
con
.
query
(
sql_text
);
await
db
.
con
.
query
(
sql_text2
,
[
user_id
,
house_id
,
decision
,
decision
]);
await
db
.
con
.
query
(
'COMMIT;'
);
}
module
.
exports
=
model
;
\ No newline at end of file
server/house/route.js
View file @
8ebe56c2
const
Koa
=
require
(
'koa'
);
const
Router
=
require
(
'@koa/router'
);
const
CronJob
=
require
(
'cron'
).
CronJob
;
const
router
=
new
Router
();
const
authRouter
=
new
Router
();
const
houseModel
=
require
(
'./model'
);
const
userModel
=
require
(
'../user/model'
)
const
userModel
=
require
(
'../user/model'
)
;
const
HOUSE_ROLES
=
{
lg
:
0
,
...
...
@@ -314,5 +315,48 @@ authRouter.delete('/:house_id', async (context, next) => {
}
});
authRouter
.
post
(
'/participation'
,
async
(
context
,
next
)
=>
{
hasHouse
(
context
);
const
body
=
context
.
request
.
body
;
if
(
!
body
||
!
body
.
decision
){
context
.
throw
(
400
,
'No decision found'
);
}
try
{
await
houseModel
.
warParticipation
(
context
.
user
.
id
,
context
.
user
.
house_id
,
body
.
decision
);
context
.
response
.
status
=
204
;
}
catch
(
error
){
console
.
log
(
error
);
context
.
throw
(
400
,
'Failed to Update Participation'
);
}
});
router
.
get
(
'/current-war'
,
async
(
context
,
next
)
=>
{
try
{
const
data
=
await
houseModel
.
getCurrentWar
();
if
(
!
data
){
throw
Error
(
'Failed to get Current War'
);
}
context
.
response
.
status
=
200
;
context
.
response
.
body
=
data
;
}
catch
(
error
){
console
.
log
(
error
);
context
.
throw
(
400
,
'Failed to get Current War'
);
}
});
async
function
insertNewWar
(){
try
{
await
houseModel
.
insertNewWar
();
console
.
log
(
'Inserted new War'
)
}
catch
(
error
){
console
.
log
(
error
);
}
}
const
war_job
=
new
CronJob
(
'30 22 * * Tue,Sat'
,
()
=>
{
insertNewWar
();
});
war_job
.
start
();
module
.
exports
=
[
router
,
authRouter
];
\ No newline at end of file
server/main.js
View file @
8ebe56c2
'use strict'
;
const
Koa
=
require
(
'koa'
);
const
Router
=
require
(
'@koa/router'
);
const
bodyParser
=
require
(
'koa-body'
);
const
session
=
require
(
'koa-session'
)
const
serve
=
require
(
'koa-static'
);
const
logger
=
require
(
'koa-logger'
)
const
getUser
=
require
(
'./user/model'
);
const
[
userRouter
,
userAuthRouter
]
=
require
(
'./user/route'
);
const
[
houseRouter
,
houseAuthRouter
]
=
require
(
'./house/route'
);
const
unitRouter
=
require
(
'./unit/route'
);
const
SESS_CONFIG
=
require
(
'./session_config'
);
const
ENV
=
require
(
'./settings'
)
const
app
=
new
Koa
();
const
router
=
new
Router
();
const
authRouter
=
new
Router
();
app
.
use
(
serve
(
`
${
__dirname
}
/../web`
))
app
.
use
(
logger
())
app
.
keys
=
[
ENV
.
SESS_KEY
]
app
.
use
(
bodyParser
());
app
.
use
(
session
(
SESS_CONFIG
,
app
));
router
.
use
(
'/api/unit'
,
unitRouter
.
routes
(),
unitRouter
.
allowedMethods
());
router
.
use
(
'/api/user'
,
userRouter
.
routes
(),
userRouter
.
allowedMethods
());
router
.
use
(
'/api/house'
,
houseRouter
.
routes
(),
houseRouter
.
allowedMethods
());
authRouter
.
use
(
'/api/user'
,
userAuthRouter
.
routes
(),
userAuthRouter
.
allowedMethods
());
authRouter
.
use
(
'/api/house'
,
houseAuthRouter
.
routes
(),
houseAuthRouter
.
allowedMethods
());
app
.
use
(
router
.
routes
()).
use
(
router
.
allowedMethods
());
// Make sure user is authorized
app
.
use
(
async
(
context
,
next
)
=>
{
if
(
context
.
session
.
user_id
){
await
next
();
console
.
log
(
context
.
status
)
}
else
{
context
.
response
.
status
=
401
;
}
});
// Add user to context
app
.
use
(
async
(
context
,
next
)
=>
{
const
user
=
await
getUser
.
getUserFullFromId
(
context
.
session
.
user_id
);
if
(
user
){
context
.
user
=
user
;
await
next
();
}
else
{
context
.
response
.
status
=
403
;
}
});
app
.
use
(
authRouter
.
routes
()).
use
(
authRouter
.
allowedMethods
());
app
.
listen
(
3000
,
()
=>
console
.
log
(
'Server Started'
));
\ No newline at end of file
const
Koa
=
require
(
'koa'
);
const
Router
=
require
(
'@koa/router'
);
const
bodyParser
=
require
(
'koa-body'
);
const
session
=
require
(
'koa-session'
)
const
serve
=
require
(
'koa-static'
);
const
logger
=
require
(
'koa-logger'
)
async
function
main
(){
const
db
=
require
(
'./database/database.js'
);
await
db
.
connectDB
();
const
getUser
=
require
(
'./user/model'
);
const
[
userRouter
,
userAuthRouter
]
=
require
(
'./user/route'
);
const
[
houseRouter
,
houseAuthRouter
]
=
require
(
'./house/route'
);
const
unitRouter
=
require
(
'./unit/route'
);
const
SESS_CONFIG
=
require
(
'./session_config'
);
const
ENV
=
require
(
'./settings'
)
const
app
=
new
Koa
();
const
router
=
new
Router
();
const
authRouter
=
new
Router
();
app
.
use
(
serve
(
`
${
__dirname
}
/../web`
))
app
.
use
(
logger
())
app
.
keys
=
[
ENV
.
SESS_KEY
]
app
.
use
(
bodyParser
());
app
.
use
(
session
(
SESS_CONFIG
,
app
));
router
.
use
(
'/api/unit'
,
unitRouter
.
routes
(),
unitRouter
.
allowedMethods
());
router
.
use
(
'/api/user'
,
userRouter
.
routes
(),
userRouter
.
allowedMethods
());
router
.
use
(
'/api/house'
,
houseRouter
.
routes
(),
houseRouter
.
allowedMethods
());
authRouter
.
use
(
'/api/user'
,
userAuthRouter
.
routes
(),
userAuthRouter
.
allowedMethods
());
authRouter
.
use
(
'/api/house'
,
houseAuthRouter
.
routes
(),
houseAuthRouter
.
allowedMethods
());
app
.
use
(
router
.
routes
()).
use
(
router
.
allowedMethods
());
// Make sure user is authorized
app
.
use
(
async
(
context
,
next
)
=>
{
if
(
context
.
session
.
user_id
){
await
next
();
console
.
log
(
context
.
status
)
}
else
{
context
.
response
.
status
=
401
;
}
});
// Add user to context
app
.
use
(
async
(
context
,
next
)
=>
{
const
user
=
await
getUser
.
getUserFullFromId
(
context
.
session
.
user_id
);
if
(
user
){
context
.
user
=
user
;
await
next
();
}
else
{
context
.
response
.
status
=
403
;
}
});
app
.
use
(
authRouter
.
routes
()).
use
(
authRouter
.
allowedMethods
());
app
.
listen
(
3000
,
()
=>
console
.
log
(
'Server Started'
));
}
main
();
\ No newline at end of file
server/package-lock.json
View file @
8ebe56c2
...
...
@@ -256,6 +256,14 @@
"buffer"
:
"^5.1.0"
}
},
"cron"
:
{
"version"
:
"1.8.2"
,
"resolved"
:
"https://registry.npmjs.org/cron/-/cron-1.8.2.tgz"
,
"integrity"
:
"sha512-Gk2c4y6xKEO8FSAUTklqtfSr7oTq0CiPQeLBG5Fl0qoXpZyMcj1SG59YL+hqq04bu6/IuEA7lMkYDAplQNKkyg=="
,
"requires"
:
{
"moment-timezone"
:
"^0.5.x"
}
},
"debug"
:
{
"version"
:
"4.1.1"
,
"resolved"
:
"https://registry.npmjs.org/debug/-/debug-4.1.1.tgz"
,
...
...
@@ -720,6 +728,19 @@
"minimist"
:
"^1.2.5"
}
},
"moment"
:
{
"version"
:
"2.25.3"
,
"resolved"
:
"https://registry.npmjs.org/moment/-/moment-2.25.3.tgz"
,
"integrity"
:
"sha512-PuYv0PHxZvzc15Sp8ybUCoQ+xpyPWvjOuK72a5ovzp2LI32rJXOiIfyoFoYvG3s6EwwrdkMyWuRiEHSZRLJNdg=="
},
"moment-timezone"
:
{
"version"
:
"0.5.29"
,
"resolved"
:
"https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.29.tgz"
,
"integrity"
:
"sha512-qWtUhRIk29zviEFAhttY0fDbM/zsu/OlCRoeQG8vxuH6XcTTuji9ILJkOdxjr+vzIv0J39RsO/SPTuMvzm90wA=="
,
"requires"
:
{
"moment"
:
">= 2.9.0"
}
},
"ms"
:
{
"version"
:
"2.1.2"
,
"resolved"
:
"https://registry.npmjs.org/ms/-/ms-2.1.2.tgz"
,
...
...
server/package.json
View file @
8ebe56c2
...
...
@@ -3,6 +3,7 @@
"dependencies"
:
{
"@koa/router"
:
"^8.0.8"
,
"bcrypt"
:
"^4.0.1"
,
"cron"
:
"^1.8.2"
,
"dotenv"
:
"^8.2.0"
,
"fs"
:
"0.0.1-security"
,
"koa"
:
"^2.11.0"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment