Compare commits

...

2 Commits

Author SHA1 Message Date
nannal 2e5f7eafdc async bois!
4 years ago
nannal 80e7b59fe3 Problesm with async
4 years ago

@ -0,0 +1,15 @@
<head>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous">
</script>
<script src="./index.js"></script>
</head>
<body>
<title>Debug</title>
<input type="text" id="location" /> <input type="button" value="send" onclick="send()" /><input type="button" value="get" onclick="get()" />
</br>
<input type="text" id="name" value="Name" />
<input type="text" id="ownerID" value="ownerID" />
<input type="text" id="gameID" value="gameID" />
<input type="text" id="seats" value="seats" />
</body>

@ -0,0 +1,30 @@
url="http://127.0.0.1:3000"
function send() {
data = {name: $('#name').val(), ownerID: $("#ownerID").val(), gameID: $("#gameID").val(), seats: $("#seats").val()}
console.log(JSON.stringify(data))
let xhr = new XMLHttpRequest();
xhr.open("POST", url + "/" + $('#location').val(), true)
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(JSON.parse(xhr.responseText))
}
}
xhr.send(JSON.stringify(data))
}
function get() {
let xhr = new XMLHttpRequest();
xhr.open("GET", url + "/" + $('#location').val(), true)
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(JSON.parse(xhr.responseText))
}
}
xhr.send()
}

@ -1,32 +1,66 @@
const lobby = require("./lobby.js")
const express = require('express')
const bodyParser = require('body-parser');
const app = express()
app.use(bodyParser.json());
const lobby = require("./lobby.js")
app.use(bodyParser.json(),function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
res.setHeader('Access-Control-Allow-Credentials', true)
next()
})
//top level
//get game list
app.get('/gamelist', function (req, res) {
res.send(lobby.list())
app.get('/lobby/list' , async function (req, res) {
lobby.list().then(r => {
console.log(r)
res.send(JSON.stringify({gamesArr: r }))
})
})
//get game details
app.get('/gamedetails', function (req, res) {
let obj=JSON.parse(req.body)
res.send(lobby.details(obj.gameID))
app.get('/lobby/details/:gameID', function (req, res) {
let obj=req.body
lobby.details(obj.gameID).then(r => {
res.send(JSON.stringify(r))
})
})
//post register player
app.post('/playerregister', function (req, res) {
app.post('/lobby/register', function (req, res) {
let obj=req.body
res.send(lobby.register(obj.name))
console.log(req.body)
response=JSON.stringify(lobby.register(obj.name))
res.send(response)
})
//post join an existing game
app.post('/lobby/join', function (req, res) {
let obj=req.body
response=JSON.stringify(lobby.join(obj.ownerID,obj.gameID))
res.send(response)
})
//get an empty game made
app.post('/gamemake', function (req, res) {
let obj=JSON.parse(req.body)
res.send(lobby.register(obj.name,obj.seats,obj.ownerID))
app.post('/lobby/make', function (req, res) {
let obj=req.body
response=JSON.stringify(lobby.make(obj.ownerID,obj.seats))
res.send(response)
})
//start a game
//start a game
app.post('/lobby/start', function (req, res) {
let obj=req.body
response=JSON.stringify(lobby.start(obj.ownerID, obj.gameID))
res.send(response)
})
//gamelevel (??)
// get this game players

@ -2,29 +2,44 @@ var mongo = require("./mongoHelp.js")
const uuidv4 = require('uuid/v4');
var lobby={
list: () => {
mongo.get("embezzle","gamelist",{}).then(result => {
return (result)
})
list: async () => {
result = await mongo.get("embezzle","gamelist",{})
return(result)
},
details: (gameID) => {
mongo.get("embezzle","gamelist",{"gameID":gameID}).then(result => {
return (result)
})
details: async (gameID) => {
result = await mongo.get("embezzle","gamelist",{"gameID":gameID})
return (result)
},
register: (name) => {
id=uuidv4()
console.log({"_id":id,"name":name})
mongo.put("embezzle","players",{"_id":id,"name":name})
return(id)
mongo.put("embezzle","players",{_id:id,name:name})
console.log("yoohoo")
return({ownerID:id})
},
make: (name,seats,ownerID) => {
make: (ownerID,seats) => {
gameID=uuidv4()
mongo.put("embezzle","gamelist",{"gameID":gameID,"ownerID":ownerID,"name":name,"seats":seats, "status":"lobby"})
return(gameID)
//todo write charity name generator
name="Charity Name Example"
mongo.put("embezzle","gamelist",{gameID:gameID,ownerID:ownerID,name:name,seats:seats, status:"lobby", players: []})
// this.join(ownerID,gameID)
return({gameID:gameID})
},
join: (ownerID,gameID) => {
mongo.get("embezzle","gamelist",{"gameID":gameID}).then(value => {
console.log(value[0])
value[0].players.push(ownerID)
mongo.update("embezzle","gamelist",{"gameID":gameID},value[0])
})
return({gameID:gameID})
},
start: (ownerID, gameID) => {
mongo.update("embezzle","gamelist",{"gameID":gameID,"ownerID":ownerID}, {"status":"Playing"})
mongo.get("embezzle","gamelist",{"gameID":gameID}).then(value => {
console.log(value[0])
value[0].status="playing"
mongo.update("embezzle","gamelist",{"gameID":gameID,"ownerID":ownerID}, value[0])
})
return(gameID)
},
}

@ -1,9 +1,13 @@
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost:27017'
var options= {
useNewUrlParser: true,
useUnifiedTopology: true,
promiseLibrary: global.Promise}
function put(db,collection, object){
const client = new MongoClient(url, {useUnifiedTopology:true})
const client = new MongoClient(url, options)
client.connect()
client.db(db).collection(collection).insertOne(object).then((value) =>{client.close();return value})
}
@ -11,7 +15,7 @@ function put(db,collection, object){
exports.put = put
function get(db, collection, object){
const client = new MongoClient(url, {useUnifiedTopology:true})
const client = new MongoClient(url, options)
client.connect()
return client.db(db).collection(collection).find(object).toArray().then((value) =>{client.close();return value})
}
@ -19,7 +23,7 @@ function get(db, collection, object){
exports.get = get
function getUnique(db, collection, field,object){
const client = new MongoClient(url, {useUnifiedTopology:true})
const client = new MongoClient(url, options)
client.connect()
return client.db(db).collection(collection).distinct(field,object).then((value) =>{client.close(); console.log(value);return value})
}
@ -28,7 +32,7 @@ function getUnique(db, collection, field,object){
exports.getUnique = getUnique
function deleteOne(db, collection, object) {
const client = new MongoClient(url, {useUnifiedTopology:true})
const client = new MongoClient(url, options)
client.connect()
client.db(db).collection(collection).deleteOne(object).then((value) =>{client.close();return value})
}
@ -36,9 +40,9 @@ function deleteOne(db, collection, object) {
exports.deleteOne = deleteOne
function update(db, collection, search,update) {
const client = new MongoClient(url, {useUnifiedTopology:true})
const client = new MongoClient(url, options)
client.connect()
client.db(db).collection(collection).update(search,update).then((value) =>{client.close();return value})
client.db(db).collection(collection).updateOne(search,{$set:update}).then((value) =>{client.close();return value})
}
exports.update = update

876
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -15,7 +15,10 @@
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mongodb": "^3.5.3",
"mongodb": "^3.4.1",
"uuid": "^3.4.0"
},
"devDependencies": {
"nodemon": "^2.0.2"
}
}

Loading…
Cancel
Save