Create game and persist in memory.
Signed-off-by: James Ketrenos <james_gitlab@ketrenos.com>
This commit is contained in:
parent
e48856b4bc
commit
5a16804a7e
11
README.md
11
README.md
@ -79,3 +79,14 @@ EOF
|
||||
sudo systemctl start ketr.ketran
|
||||
```
|
||||
|
||||
## To test
|
||||
|
||||
### New game
|
||||
```bash
|
||||
curl -k -s -X POST http://localhost:8911/ketr.ketran/api/v1/games/
|
||||
```
|
||||
|
||||
### Game status
|
||||
```bash
|
||||
curl -k -s -X GET http://localhost:8911/ketr.ketran/api/v1/games/:id
|
||||
```
|
||||
|
@ -2,13 +2,13 @@
|
||||
"db": {
|
||||
"games": {
|
||||
"dialect": "sqlite",
|
||||
"storage": "../db/photos.db",
|
||||
"storage": "db/games.db",
|
||||
"logging" : false,
|
||||
"timezone": "+00:00"
|
||||
},
|
||||
"users": {
|
||||
"dialect": "sqlite",
|
||||
"storage": "../db/users.db",
|
||||
"storage": "db/users.db",
|
||||
"logging" : false,
|
||||
"timezone": "+00:00"
|
||||
}
|
||||
@ -22,4 +22,4 @@
|
||||
"db": "sessions.db",
|
||||
"store-secret": "m@g1kc00ki3z!"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -244,6 +244,7 @@ app.use(function(err, req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
if (0) {
|
||||
/* Check authentication */
|
||||
app.use(basePath, function(req, res, next) {
|
||||
return users.getSessionUser(req).then(function(user) {
|
||||
@ -256,6 +257,7 @@ app.use(basePath, function(req, res, next) {
|
||||
return res.status(403).send(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* Everything below here requires a successful authentication */
|
||||
app.use(basePath, express.static(frontendPath, { index: false }));
|
||||
|
@ -15,11 +15,145 @@ require("../db/games").then(function(db) {
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.post("/", (req, res/*, next*/) => {
|
||||
console.log("PUT /" + replacements.id, req.query);
|
||||
return res.status(400).send("Invalid request");
|
||||
function shuffle(array) {
|
||||
var currentIndex = array.length, temporaryValue, randomIndex;
|
||||
|
||||
// While there remain elements to shuffle...
|
||||
while (0 !== currentIndex) {
|
||||
|
||||
// Pick a remaining element...
|
||||
randomIndex = Math.floor(Math.random() * currentIndex);
|
||||
currentIndex -= 1;
|
||||
|
||||
// And swap it with the current element.
|
||||
temporaryValue = array[currentIndex];
|
||||
array[currentIndex] = array[randomIndex];
|
||||
array[randomIndex] = temporaryValue;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
const assetData = {
|
||||
tiles: [
|
||||
{ type: "wood", y: 0. / 4. },
|
||||
{ type: "wood", y: 1. / 4. },
|
||||
{ type: "wood", y: 2. / 4. },
|
||||
{ type: "wood", y: 3. / 4. },
|
||||
{ type: "wheat", y: 0. / 4. },
|
||||
{ type: "wheat", y: 1. / 4. },
|
||||
{ type: "wheat", y: 2. / 4. },
|
||||
{ type: "wheat", y: 3. / 4. },
|
||||
{ type: "stone", y: 0. / 4. },
|
||||
{ type: "stone", y: 1. / 4. },
|
||||
{ type: "stone", y: 2. / 4. },
|
||||
{ type: "sheep", y: 0. / 4. },
|
||||
{ type: "sheep", y: 1. / 4. },
|
||||
{ type: "sheep", y: 2. / 4. },
|
||||
{ type: "sheep", y: 3. / 4. },
|
||||
{ type: "brick", y: 0. / 4. },
|
||||
{ type: "brick", y: 1. / 4. },
|
||||
{ type: "brick", y: 2. / 4. },
|
||||
{ type: "robber", y: 0 }
|
||||
],
|
||||
pips: [
|
||||
{ roll: 7, pips: 0, y: 3. / 6., x: 0. / 6. },
|
||||
{ roll: 5, pips: 4, y: 0. / 6., x: 0. / 6. },
|
||||
{ roll: 2, pips: 1, y: 0. / 6., x: 1. / 6. },
|
||||
{ roll: 6, pips: 5, y: 0. / 6., x: 2. / 6. },
|
||||
{ roll: 3, pips: 2, y: 0. / 6., x: 3. / 6. },
|
||||
{ roll: 8, pips: 5, y: 0. / 6., x: 4. / 6. },
|
||||
{ roll: 10, pips: 3, y: 0. / 6., x: 5. / 6. },
|
||||
{ roll: 9, pips: 4, y: 1. / 6., x: 0. / 6. },
|
||||
{ roll: 12, pips: 1, y: 1. / 6., x: 1. / 6. },
|
||||
{ roll: 11, pips: 2, y: 1. / 6., x: 2. / 6. },
|
||||
{ roll: 4, pips: 3, y: 1. / 6., x: 3. / 6. },
|
||||
{ roll: 8, pips: 5, y: 1. / 6., x: 4. / 6. },
|
||||
{ roll: 10, pips: 3, y: 1. / 6., x: 5. / 6. },
|
||||
{ roll: 9, pips: 4, y: 2. / 6., x: 0. / 6. },
|
||||
{ roll: 4, pips: 3, y: 2. / 6., x: 1. / 6. },
|
||||
{ roll: 5, pips: 4, y: 2. / 6., x: 2. / 6. },
|
||||
{ roll: 6, pips: 6, y: 2. / 6., x: 3. / 6. },
|
||||
{ roll: 3, pips: 2, y: 2. / 6., x: 4. / 6. },
|
||||
{ roll: 11, pips: 2, y: 2. / 6., x: 5. / 6. }
|
||||
],
|
||||
borders: [
|
||||
{ file: 'borders-1.6.png', left: "sheep", right: "bank" },
|
||||
{ file: 'borders-2.1.png', center: "sheep" },
|
||||
{ file: 'borders-3.2.png', left: "wheat", right: "bank" },
|
||||
{ file: 'borders-4.3.png', center: "wood" },
|
||||
{ file: 'borders-5.4.png', left: "sheep", right: "bank" },
|
||||
{ file: 'borders-6.5.png', center: "bank" }
|
||||
],
|
||||
developmentCards: []
|
||||
};
|
||||
for (let i = 0; i < 14; i++) {
|
||||
assetData.developmentCards.push("knight");
|
||||
}
|
||||
for (let i = 0; i < 6; i++) {
|
||||
assetData.developmentCards.push("progress");
|
||||
}
|
||||
for (let i = 0; i < 5; i++) {
|
||||
assetData.developmentCards.push("victoryPoint");
|
||||
}
|
||||
|
||||
const games = {};
|
||||
|
||||
router.get("/:id", (req, res/*, next*/) => {
|
||||
console.log("GET /" + req.params.id);
|
||||
if (req.params.id in games) {
|
||||
return res.status(200).send(games[req.params.id]);
|
||||
} else {
|
||||
const error = `Game not found: ${req.params.id}`;
|
||||
return res.status(404).send(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/", (req, res/*, next*/) => {
|
||||
console.log("POST /");
|
||||
const game = {
|
||||
startTime: Date.now(),
|
||||
tiles: [],
|
||||
pips: [],
|
||||
borders: [],
|
||||
tokens: [
|
||||
{ player: "R", roads: 15, cities: 4, settlements: 5, points: 0 },
|
||||
{ player: "O", roads: 15, cities: 4, settlements: 5, points: 0 },
|
||||
{ player: "B", roads: 15, cities: 4, settlements: 5, points: 0 },
|
||||
{ player: "W", roads: 15, cities: 4, settlements: 5, points: 0 }
|
||||
],
|
||||
developmentCards: assetData.developmentCards.slice(),
|
||||
sheep: 19,
|
||||
ore: 19,
|
||||
wool: 19,
|
||||
brick: 19,
|
||||
wheat: 19,
|
||||
longestRoad: null,
|
||||
largestArmy: null,
|
||||
id: crypto.randomBytes(20).toString('hex')
|
||||
};
|
||||
|
||||
[ "tiles", "pips", "borders" ].forEach((field) => {
|
||||
game[field] = []
|
||||
for (let i = 0; i < assetData[field].length; i++) {
|
||||
game[field].push(i);
|
||||
}
|
||||
/* Shuffle an array of indexes */
|
||||
shuffle(game[field]);
|
||||
/* Convert from an index array to a full array */
|
||||
for (let i = 0; i < assetData[field].length; i++) {
|
||||
game[field][i] = assetData[field][game[field][i]];
|
||||
}
|
||||
});
|
||||
|
||||
shuffle(game.developmentCards)
|
||||
|
||||
games[game.id] = game;
|
||||
|
||||
return res.status(200).send(game);
|
||||
});
|
||||
|
||||
|
||||
/*
|
||||
return gameDB.sequelize.query("SELECT " +
|
||||
"photos.*,albums.path AS path,photohashes.hash,modified,(albums.path || photos.filename) AS filepath FROM photos " +
|
||||
@ -28,21 +162,20 @@ router.post("/", (req, res/*, next*/) => {
|
||||
"WHERE photos.id=:id", {
|
||||
replacements: {
|
||||
id: id
|
||||
},
|
||||
type: gameDB.Sequelize.QueryTypes.SELECT,
|
||||
}, type: gameDB.Sequelize.QueryTypes.SELECT,
|
||||
raw: true
|
||||
}).then(function(photos) {
|
||||
if (photos.length == 0) {
|
||||
return null;
|
||||
}
|
||||
*/
|
||||
|
||||
*/
|
||||
if (0) {
|
||||
router.get("/*", (req, res/*, next*/) => {
|
||||
return gameDB.sequelize.query(query, {
|
||||
replacements: replacements,
|
||||
type: gameDB.Sequelize.QueryTypes.SELECT
|
||||
replacements: replacements, type: gameDB.Sequelize.QueryTypes.SELECT
|
||||
}).then((photos) => {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
|
Loading…
x
Reference in New Issue
Block a user