Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web design improved, database added #456

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion rose/client/track.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from rose.common import config, obstacles
from . import component


import json
class Track(component.Component):

def __init__(self):
self._track = {}
self._last_obstacle = None

# Component interface

def update(self, info):
self._track = {(obs["x"], obs["y"]): obs["name"]
for obs in info["track"]}
self._update_last_obstacle()

# Track interface

Expand All @@ -20,10 +22,25 @@ def get(self, x, y):
self._validate_pos(x, y)
return self._track.get((x, y), obstacles.NONE)

def get_last_obstacle(self):
""" Return the name of the last obstacle encountered """
return self._last_obstacle

# Private

def _validate_pos(self, x, y):
if x < 0 or x > config.matrix_width - 1:
raise IndexError('x out of range: 0-%d', config.matrix_width - 1)
if y < 0 or y > config.matrix_height - 1:
raise IndexError('y out of range: 0-%d', config.matrix_height - 1)

def _update_last_obstacle(self):
if self._track:
self._last_obstacle = list(self._track.values())[-1]
else:
self._last_obstacle = None

obstacle_count = 0 # initialize obstacle counter



7 changes: 5 additions & 2 deletions rose/common/config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import os

# Networking
duration = 60


# tworking

game_port = 8888
web_port = 8880

# Server

game_rate = 1.0
game_duration = 60
game_duration = duration
number_of_cars = 4
is_track_random = True

Expand Down
21 changes: 19 additions & 2 deletions rose/server/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

import six

import json
from twisted.internet import reactor, task

from rose.common import actions, config, error, message, obstacles # NOQA
Expand Down Expand Up @@ -37,7 +37,7 @@ def rate(self):
@rate.setter
def rate(self, value):
if value != self._rate:
log.info('change game rate to %d frames per second', value)
log.info('change game rate to %d frames per second(Speed of player)', value)
self._rate = value
if self.started:
self.looper.stop()
Expand Down Expand Up @@ -110,7 +110,24 @@ def print_stats(self):
for i, p in enumerate(top_scorers):
line = '%d %10s row:%d score:%d' % (i + 1, p.name, p.y, p.score)
lines.append(line)
def write_json(new_data, filename='/home/ori/Documents/ROSE/ROSE/rose/web/ScoreData.json'):
with open(filename, 'r+', encoding='utf-8') as file:
try:
file_data = json.load(file)
except json.JSONDecodeError:
file_data = {}
if "emp_details" not in file_data:
file_data["emp_details"] = []
file_data["emp_details"]. append(new_data)
file.seek(0)
json.dump(file_data, file, indent=4)
score_value = line[line.rfind("score:") + 6:]
y = score_value
write_json("Score: " + y)
log.info("%s", os.linesep.join(lines))




def loop(self):
self.track.update()
Expand Down
1 change: 1 addition & 0 deletions rose/web/ScoreData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
53 changes: 40 additions & 13 deletions rose/web/game.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

body {
font-family: sans-serif;
background: #444;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
background: #33867f;
color: #ccc;
margin: 0;
}
Expand All @@ -17,18 +18,18 @@ body {
}

#control button {
font-family: helvetica, sans-serif;
font-size: 20px;
font-size: 25px;
line-height: 24px;
font-weight: bold;
border: 1px solid #666;
color: #ccc;
background-color: #333;
border-radius: 20px;
border: 1px solid rgb(1, 1, 1);
color: #fff;
background-color: #c96f6f;
}

#control button:disabled, #control button:disabled:hover {
color: #666;
background-color: #333;
color: #fff;
background-color: #c96f6f;
}

#control button:hover {
Expand All @@ -54,7 +55,7 @@ body {
#rate_ctl .group {
display: inline-block;
font-size: 0;
border: 1px solid #666;

}

#rate_ctl button {
Expand All @@ -72,6 +73,7 @@ body {
width: 120px;
border-top: 0;
border-bottom: 0;

}

#players {
Expand All @@ -80,7 +82,7 @@ body {
height: 150px;
font-weight: bold;
font-family: sans-serif;
color: rgb(153, 153, 153);
color: #fff;
position: relative;
}

Expand Down Expand Up @@ -115,6 +117,7 @@ body {
position: absolute;
font-size: 40px;
top: 50px;

}

#time_left {
Expand All @@ -125,7 +128,31 @@ body {
top: 40px;
left: 400px;
}

#game {
border: 1px #666 solid;
border: 1px #fff solid;
}
h3{
text-align: center;
position: relative;
top:20px;
font-size: 25px;
line-height: 24px;
font-weight: bold;
border: 1px solid rgb(250, 250, 250);
color: #fff;
background-color: #222;

}
#box p{
text-align: center;
position: relative;
top:20px;
font-size: 25px;
line-height: 24px;
font-weight: bold;
color: #fff;


}


16 changes: 12 additions & 4 deletions rose/web/game.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

var ROSE = (function() {
"use strict";

function App() {
this.client = null;
this.controller = null;
Expand All @@ -15,7 +16,7 @@ var ROSE = (function() {

App.prototype.ready = function() {
this.controller = new Controller();
this.rate = new Rate([0.5, 1.0, 2.0, 5.0, 10.0]);
this.rate = new Rate([0.5, 1.0, 2.0, 5.0, 10.0, 20.0, 30.0,40.0,50.0]);

var image_loader = new ImageLoader(function() {
this.client = new Client(this.onmessage.bind(this), 2000);
Expand All @@ -30,6 +31,7 @@ var ROSE = (function() {
this.sound = new Sound("res/soundtrack/Nyan_Cat.ogg");
}


App.prototype.onmessage = function(m) {
var msg = JSON.parse(m.data);
if (msg.action !== "update") {
Expand Down Expand Up @@ -131,7 +133,7 @@ var ROSE = (function() {
}

Controller.prototype.update = function(state) {
if(state.players.length == 0){
if (!state.players || state.players.length == 0){
$("#info").text("No players connected")
$("#start").attr("disabled", "disabled");
$("#stop").attr("disabled", "disabled");
Expand All @@ -145,7 +147,10 @@ var ROSE = (function() {
$("#start").removeAttr("disabled");
$("#stop").attr("disabled", "disabled");
}


}


Controller.prototype.disable = function() {
$("#start").attr("disabled", "disabled");
Expand Down Expand Up @@ -416,6 +421,7 @@ var ROSE = (function() {
});
img.src = url;
}


function Sound(file_path) {
var self = this;
Expand Down Expand Up @@ -444,6 +450,8 @@ var ROSE = (function() {
this.audio.pause();
}




return new App();

}());
Loading