Skip to content
Snippets Groups Projects
Commit ced974af authored by mcmlxxix's avatar mcmlxxix
Browse files

It will work eventually :|

parent a7443880
No related branches found
No related tags found
No related merge requests found
......@@ -8,21 +8,20 @@ function playGame(profile,game) {
/* main shit */
function open() {
/* game status constants */
status = {WAITING:-1,STARTING:0,PLAYING:1,FINISHED:2};
status = {WAITING:-1,STARTING:0,SYNCING:1,PLAYING:2,FINISHED:3};
/* directional constants */
direction = {UP:0,DOWN:1,LEFT:2,RIGHT:3};
client.subscribe(game_id,"metadata." + game.gameNumber);
last_update=Date.now();
pause = settings.pause;
queue = client.read(game_id,"metadata." + game.gameNumber + ".queue",1);
gameFrame=new Frame(undefined,undefined,undefined,undefined,undefined,frame);
gameFrame.open();
initPlayers();
pause = settings.pause;
queue = client.read(game_id,"metadata." + game.gameNumber + ".queue",1);
drawScore(localPlayer);
client.subscribe(game_id,"metadata." + game.gameNumber);
}
function close() {
if(localPlayer) {
if(localPlayer != undefined) {
if(countMembers(players) > 1) {
if(game.winner == localPlayer.name)
scoreWin();
......@@ -44,7 +43,7 @@ function playGame(profile,game) {
return;
last_update=Date.now();
if(!localPlayer || !localPlayer.active || !everyoneReady())
if(!localPlayer || !localPlayer.active || game.status !== status.PLAYING)
return;
//TODO: this might not be necessary anymore
......@@ -73,8 +72,7 @@ function playGame(profile,game) {
}
}
function main() {
while(game.status == status.PLAYING) {
while(!js.terminated) {
cycle();
var k=console.inkey(K_NOCRLF|K_NOSPIN|K_NOECHO|K_UPPER,5);
......@@ -86,91 +84,96 @@ function playGame(profile,game) {
break;
}
if(!localPlayer.active || localPlayer.currentPiece == undefined || !everyoneReady())
continue;
if(game.status == status.SYNCING) {
switch(k) {
case " ":
case "0":
if(!localPlayer.swapped) {
localPlayer.swapped=true;
swapPiece();
if(!readyMsg)
readyMsg = showMessage("Waiting for players");
}
else if(game.status == status.PLAYING) {
if(readyMsg) {
readyMsg.close();
delete readyMsg;
}
break;
case "8":
case KEY_UP:
fullDrop();
send("MOVE");
setPiece();
getLines();
getNewPiece();
break;
case "2":
case KEY_DOWN:
if(move(direction.DOWN))
if(!localPlayer.active || localPlayer.currentPiece == undefined)
continue;
switch(k) {
case " ":
case "0":
if(!localPlayer.swapped) {
localPlayer.swapped=true;
swapPiece();
}
break;
case "8":
case KEY_UP:
fullDrop();
send("MOVE");
else {
setPiece();
getLines();
getNewPiece();
break;
case "2":
case KEY_DOWN:
if(move(direction.DOWN))
send("MOVE");
else {
setPiece();
getLines();
getNewPiece();
}
break;
case "4":
case KEY_LEFT:
if(move(direction.LEFT))
send("MOVE");
break;
case "6":
case KEY_RIGHT:
if(move(direction.RIGHT))
send("MOVE");
break;
case "5":
if(rotate(direction.RIGHT))
send("MOVE");
break;
case "7":
case "A":
if(rotate(direction.LEFT))
send("MOVE");
break;
case "9":
case "D":
if(rotate(direction.RIGHT))
send("MOVE");
break;
default:
break;
}
break;
case "4":
case KEY_LEFT:
if(move(direction.LEFT))
send("MOVE");
break;
case "6":
case KEY_RIGHT:
if(move(direction.RIGHT))
send("MOVE");
break;
case "5":
if(rotate(direction.RIGHT))
send("MOVE");
break;
case "7":
case "A":
if(rotate(direction.LEFT))
send("MOVE");
break;
case "9":
case "D":
if(rotate(direction.RIGHT))
send("MOVE");
break;
default:
}
else if(game.status == status.FINISHED) {
var msg = ["Game Over"];
if(game.winner == localPlayer.name)
msg.push("\1g\1hYou win!");
else
msg.push("\1r\1hYou lose!");
msg.push("\1n\1yPress [\1hQ\1n\1y] to exit");
showMessage(msg);
while(console.inkey(K_NOCRLF|K_NOSPIN|K_NOECHO|K_UPPER)!="Q");
break;
}
}
var msg = ["Game Over"];
if(game.winner == localPlayer.name)
msg.push("\1g\1hYou win!");
else
msg.push("\1r\1hYou lose!");
msg.push("\1n\1yPress [\1hQ\1n\1y] to exit");
showMessage(msg);
while(console.inkey(K_NOCRLF|K_NOSPIN|K_NOECHO|K_UPPER)!="Q");
/* shutdown gameplay and return to lobby */
gameFrame.close();
return;
}
/* init shit */
function everyoneReady() {
for each(var p in players) {
if(!p.ready) {
if(!readyMsg)
readyMsg = showMessage("Waiting for players");
return false;
}
}
if(readyMsg) {
readyMsg.close();
delete readyMsg;
}
return true;
}
function initPlayers() {
players = {};
var x=1;
......@@ -192,13 +195,6 @@ function playGame(profile,game) {
players[p].open();
index++;
}
var online = onlinePlayers();
for each(var p in online) {
if(p.nick == profile.name)
continue;
if(players[p.nick])
players[p.nick].ready = true;
}
}
function getMatrix(h,w) {
var grid=new Array(h);
......@@ -241,6 +237,8 @@ function playGame(profile,game) {
break;
case "PLAYER":
break;
case "READY":
break;
case "GRID":
data.grid=localPlayer.grid;
break;
......@@ -254,28 +252,30 @@ function playGame(profile,game) {
}
function send(cmd) {
var d=packageData(cmd);
if(d)
client.write(game_id,"metadata." + game.gameNumber + ".players." + localPlayer.name,d,2);
if(d) {
client.write(game_id,"metadata." + game.gameNumber + ".players." +
localPlayer.name,d,2);
}
}
function processUpdate(packet) {
if(packet.oper == "WRITE" && packet.data) {
var data = packet.data;
var p = players[data.playerName];
switch(data.cmd) {
function processUpdate(update) {
if(update.oper == "WRITE" && update.data) {
var d = update.data;
var p = players[d.playerName];
switch(d.cmd) {
case "MOVE":
unDrawCurrent(p);
p.currentPiece.x=data.x;
p.currentPiece.y=data.y;
p.currentPiece.o=data.o;
p.currentPiece.x=d.x;
p.currentPiece.y=d.y;
p.currentPiece.o=d.o;
drawCurrent(p);
break;
case "CURRENT":
unDrawCurrent(p);
p.currentPiece=loadPiece(data.piece);
p.currentPiece=loadPiece(d.piece);
drawCurrent(p);
break;
case "GARBAGE":
loadGarbage(data.lines,data.space);
loadGarbage(d.lines,d.space);
drawBoard(localPlayer);
drawCurrent(p);
send("GRID");
......@@ -284,37 +284,46 @@ function playGame(profile,game) {
delete p.currentPiece;
break;
case "DEAD":
killPlayer(data.playerName);
killPlayer(d.playerName);
break;
case "NEXT":
p.nextPiece=loadMini(data.piece);
p.nextPiece=loadMini(d.piece);
drawNext(p);
break;
case "HOLD":
p.holdPiece=loadMini(data.piece);
p.holdPiece=loadMini(d.piece);
drawHold(p);
break;
case "SCORE":
p.score=data.score;
p.lines=data.lines;
p.level=data.level;
p.score=d.score;
p.lines=d.lines;
p.level=d.level;
drawScore(p);
break;
case "GRID":
p.grid=data.grid;
p.grid=d.grid;
drawBoard(p);
break;
default:
var location = update.location.split(".");
if(location.length > 0) {
switch(location.pop().toUpperCase()) {
case "STATUS":
game.status = update.data;
break;
case "WINNER":
game.winner = update.data;
break;
}
}
break;
}
}
else if(packet.oper == "SUBSCRIBE") {
if(players[packet.data.nick])
players[packet.data.nick].ready = true;
else if(update.oper == "SUBSCRIBE") {
/* player rejoining? */
}
else if(packet.oper == "UNSUBSCRIBE") {
if(players[packet.data.nick])
players[packet.data.nick].ready = false;
else if(update.oper == "UNSUBSCRIBE") {
/* player leaving? */
}
}
......@@ -990,8 +999,9 @@ function playGame(profile,game) {
function findWinner() {
if(activePlayers() <= 1) {
for(var p in players) {
if(players[p].active) {
if(players[p].active == true) {
game.winner=p;
client.write(game_id,"games." + game.gameNumber + ".winner",game.winner,2);
break;
}
}
......
......@@ -29,7 +29,7 @@ var lobby=(function() {
bbs.sys_status|=SS_MOFF;
bbs.sys_status|=SS_PAUSEOFF;
splash("synchronetris.bin");
status = {WAITING:-1,STARTING:0,PLAYING:1,FINISHED:2};
status = {WAITING:-1,STARTING:0,SYNCING:1,PLAYING:2,FINISHED:3};
data = new GameData();
chat = new JSONChat(user.number,undefined,serverAddr,serverPort);
settings=client.read(game_id,"settings",1);
......@@ -145,32 +145,42 @@ var lobby=(function() {
/* process subscription data updates */
function processUpdate(update) {
try {
switch(update.oper) {
case "WRITE":
var p=update.location.split(".");
var obj=data;
while(p.length > 1) {
var child=p.shift();
if(!obj[child])
obj[child] = {};
obj=obj[child];
var playerName = undefined;
var gameNumber = undefined;
switch(update.oper) {
case "WRITE":
var p=update.location.split(".");
var obj=data;
while(p.length > 1) {
var child=p.shift();
if(!obj[child])
obj[child] = {};
obj=obj[child];
switch(child.toUpperCase()) {
case "PLAYERS":
playerName = p[0];
break;
case "GAMES":
gameNumber = p[0];
break;
}
if(update.data == undefined)
delete obj[p.shift()];
else
obj[p.shift()] = update.data;
data.updated=true;
break;
case "SUBSCRIBE":
case "UNSUBSCRIBE":
data.online=client.who(game_id,"games");
data.updated=true;
break;
}
} catch(e) {
log(LOG_ERROR,e);
log(LOG_WARNING,update.toSource());
var child=p.shift();
if(update.data == undefined)
delete obj[child];
else
obj[child] = update.data;
data.updated=true;
if(child.toUpperCase() == "STATUS")
updateStatus(update.data,gameNumber);
break;
case "SUBSCRIBE":
case "UNSUBSCRIBE":
data.online=client.who(game_id,"games");
data.updated=true;
break;
}
}
......@@ -234,16 +244,26 @@ var lobby=(function() {
var gameStr = "\1n\1cGame \1c\1h" + game.gameNumber;
var statusStr = "";
if(game.status == status.WAITING)
switch(game.status) {
case status.WAITING:
statusStr = " \1n\1c[waiting]";
else if(game.status == status.STARTING)
break;
case status.STARTING:
statusStr = " \1n\1y[starting]";
else if(game.status == status.PLAYING)
break;
case status.SYNCING:
statusStr = " \1n\1y[syncing]";
break;
case status.PLAYING:
statusStr = " \1n\1g[playing]";
else if(game.status == status.FINISHED)
break;
case status.FINISHED:
statusStr = " \1k\1h[finished]";
else
break;
default:
statusStr = " \1n\1r[error]";
break;
}
tile.open();
tile.frame.home();
......@@ -339,15 +359,17 @@ var lobby=(function() {
data.updated=true;
}
/* check the status of your current game */
function readyToStart() {
if(!data.games[gnum])
return false;
if(!data.games[gnum].players[profile.name])
/* process a game status update */
function updateStatus(statusUpdate,gameNumber) {
if(!data.games[gameNumber])
return false;
if(data.games[gnum].status !== status.PLAYING)
return false;
return (data.games[gnum].players[profile.name].ready);
if(data.games[gameNumber].players[profile.name]) {
switch(statusUpdate) {
case status.SYNCING:
playGame(profile,data.games[gameNumber]);
break;
}
}
}
/* update data sources and display objects */
......@@ -361,8 +383,6 @@ var lobby=(function() {
listGames();
data.updated = false;
}
if(readyToStart())
playGame(profile,data.games[gnum]);
if(frame.cycle())
input.popxy();
}
......
var root = argv[0];
js.branch_limit = 0;
load("json-client.js");
load("event-timer.js");
......@@ -12,7 +11,6 @@ var client = new JSONClient("localhost",10088);
var data = {
games:client.read(game_id,"games",1),
boards:client.read(game_id,"boards",1),
players:client.read(game_id,"players",1),
timers:{}
};
......@@ -31,36 +29,41 @@ var settings = {
var status={
WAITING:-1,
STARTING:0,
PLAYING:1,
FINISHED:2
SYNCING:1,
PLAYING:2,
FINISHED:3
}
/* process inbound updates */
function processUpdate(update) {
var playerName = undefined;
var gameNumber = undefined;
var raceNumber = undefined;
var p=update.location.split(".");
var obj=data;
while(p.length > 1) {
var child=p.shift();
obj=obj[child];
switch(child.toUpperCase()) {
case "PLAYERS":
playerName = p[0];
break;
case "GAMES":
case "METADATA":
gameNumber = p[0];
break;
}
}
switch(update.oper.toUpperCase()) {
case "SUBSCRIBE":
if(gameNumber)
updatePlayers(gameNumber,update.data);
break;
case "UNSUBSCRIBE":
handleDisco(update);
break;
case "WRITE":
var p=update.location.split(".");
var obj=data;
while(p.length > 1) {
var child=p.shift();
obj=obj[child];
switch(child.toUpperCase()) {
case "PLAYERS":
playerName = p[0];
break;
case "GAMES":
gameNumber = p[0];
break;
}
}
var child = p.shift();
if(update.data == undefined)
delete obj[child];
......@@ -123,10 +126,8 @@ function updateGame(update,gameNumber,playerName) {
deletePlayer(gameNumber,playerName);
else if(pcount >= settings.min_players) {
var ready = getReady(game);
if(ready && game.status == status.WAITING || game.status == status.FINISHED)
if(ready && (game.status == status.WAITING || game.status == status.FINISHED))
startTimer(game);
// else if(!ready && game.status == status.PLAYING && pcount == 1)
// deletePlayer(gameNumber,playerName);
else if(!ready && game.status == status.STARTING)
stopTimer(game);
}
......@@ -142,11 +143,23 @@ function updateStatus(update,gameNumber) {
}
}
/* monitor player subscriptions to games */
function updatePlayers(gameNumber,subscription) {
var game = data.games[gameNumber];
game.players[subscription.nick].synced = true;
for each(var p in game.players) {
if(!p.synced)
return;
}
startGame(game);
}
/* reset all players to "not ready" */
function resetPlayers(game) {
for each(var p in game.players) {
p.ready = false;
client.write(game_id,"games." + game.gameNumber + ".players." + p.name + ".ready",p.ready,2);
client.write(game_id,"games." + game.gameNumber + ".players." +
p.name + ".ready",p.ready,2);
}
}
......@@ -160,7 +173,7 @@ function stopTimer(game) {
/* start the countdown */
function startTimer(game) {
data.timers[game.gameNumber] = timer.addEvent(settings.start_delay,false,startGame,[game]);
data.timers[game.gameNumber] = timer.addEvent(settings.start_delay,false,startSync,[game]);
game.status = status.STARTING;
client.write(game_id,"games." + game.gameNumber + ".status",game.status,2);
}
......@@ -174,19 +187,26 @@ function getReady(game) {
return true;
}
/* generate a game and start */
function startGame(game) {
/* flag game for player sync */
function startSync(game) {
/* verify that the game is still ready */
if(game.status == status.STARTING) {
log("starting game: " + game.gameNumber);
client.lock(game_id,"games." + game.gameNumber + ".status",2);
game.status = status.SYNCING;
client.subscribe(game_id,"metadata." + game.gameNumber);
client.write(game_id,"metadata." + game.gameNumber + ".queue",generatePieces(),2);
game.status = status.PLAYING;
client.write(game_id,"games." + game.gameNumber + ".status",status.PLAYING);
client.unlock(game_id,"games." + game.gameNumber + ".status");
client.write(game_id,"games." + game.gameNumber + ".status",game.status,2);
}
}
/* generate a game and start */
function startGame(game) {
game.status = status.PLAYING;
client.unsubscribe(game_id,"metadata." + game.gameNumber);
client.lock(game_id,"games." + game.gameNumber + ".status",2);
client.write(game_id,"games." + game.gameNumber + ".status",game.status);
client.unlock(game_id,"games." + game.gameNumber + ".status");
}
/* generate a game */
function generatePieces() {
var pieces = [];
......@@ -209,8 +229,6 @@ function init() {
client.callback = processUpdate;
if(!data.games)
data.games = {};
if(!data.boards)
data.boards = {};
if(!data.players)
data.players = {};
log(LOG_INFO,"Synchronetris background service initialized");
......
......@@ -5,6 +5,7 @@ function GameData()
this.games={};
this.boards={};
this.online={};
this.updated=true;
this.loadGames=function() {
this.games = client.read(game_id,"games",1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment