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

add seed to ROSE #467

Closed
wants to merge 13 commits 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ Pipfile.lock
classroom/credentials.json
classroom/*.csv
classroom/token.pickle

seed.txt
10 changes: 10 additions & 0 deletions mydriver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
This driver does not do any action.
"""
from rose.common import obstacles, actions # NOQA

driver_name = "No Driver"


def drive(world):
return actions.NONE
7 changes: 4 additions & 3 deletions rose/client/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@

class Game(component.Component):

def __init__(self, client, name, drive_func):
def __init__(self, client, name, drive_func,seed):
self.client = client
self.drive_func = drive_func
self.name = name
self.track = track.Track()
self.players = {}
self.seed = seed
self.cars = [car.Car(1),
car.Car(2),
car.Car(3),
car.Car(4)]
self.world = world.generate_world(self)

def get_seed(self):
return self.seed
# Component interface

def update(self, info):
self.track.update(info)
self.players = {p["name"]: p for p in info['players']}
Expand Down
5 changes: 3 additions & 2 deletions rose/client/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import random
import argparse
import imp
import logging
Expand Down Expand Up @@ -41,7 +42,7 @@ class ClientFactory(protocol.ReconnectingClientFactory):
maxDelay = 2

def __init__(self, name, drive_func):
self.game = game.Game(self, name, drive_func)
self.game = game.Game(self, name, drive_func, config.track_seed)
self.client = None

# Client events
Expand Down Expand Up @@ -102,9 +103,9 @@ def main():
help="The server address to connect to."
" For example: '10.20.30.44' or 'my-server.com'."
" If not specified, localhost will be used.")

parser.add_argument("driver_file",
help="The path to the driver python module")

args = parser.parse_args()

try:
Expand Down
2 changes: 2 additions & 0 deletions rose/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
game_duration = 60
number_of_cars = 4
is_track_random = True
track_seed = ""
base_seed = ""

# Matrix

Expand Down
14 changes: 13 additions & 1 deletion rose/server/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self):
self._rate = config.game_rate
self.started = False
self.timeleft = config.game_duration
self.seed = config.track_seed

@property
def rate(self):
Expand All @@ -50,6 +51,15 @@ def start(self):
raise error.GameAlreadyStarted()
if not self.players:
raise error.ActionForbidden("start a game with no players.")

if config.base_seed == "":
seed = str(random.randint(1, 1000000))
else:
seed = config.base_seed
self.seed = seed
random.seed(self.seed)
log.info(f"The seed is {seed}")

self.track.reset()
for p in six.itervalues(self.players):
p.reset()
Expand Down Expand Up @@ -130,4 +140,6 @@ def state(self):
'track': self.track.state(),
'players': [p.state() for p in six.itervalues(self.players)],
'timeleft': self.timeleft,
'rate': self.rate}
'rate': self.rate,
'seed': "seed:" + self.seed
}
21 changes: 21 additions & 0 deletions rose/server/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import random
import socket
import logging
import argparse
Expand All @@ -20,6 +21,10 @@ def main():
default="random", choices=["random", "same"],
help="Definition of driver tracks: random or same."
"If not specified, random will be used.")
parser.add_argument("--seed", "-s", dest="seed",
default="", type=str,
help="Definition of driver tracks: random or same."
"If not specified, random will be used.")

args = parser.parse_args()
"""
Expand All @@ -32,6 +37,22 @@ def main():
else:
config.is_track_random = True

"""
If the argument is '', the seed will be generated in random, otherwise
the seed will be the seed submitted by the user.
"""

seed = args.seed
if args.seed == "":
seed = str(random.randint(1, 100000))
config.base_seed = args.seed
else:
config.base_seed = args.seed

log.info(f"The seed is {seed}")
config.track_seed = seed
random.seed(seed)

log.info('starting server')
g = game.Game()
h = net.Hub(g)
Expand Down
26 changes: 24 additions & 2 deletions rose/web/game.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,31 @@ body {
background-color: #222;
}

#start, #stop, #music_ctl {
width: 120px;
#start, #stop, #music_ctl {
width: 140px;
padding: 8px 24px;
margin: 0;

}
#seed{
font-family: helvetica, sans-serif;
font-size: 20px;
line-height: 24px;
font-weight: bold;
border: 1px solid #666;
color: #ccc;
background-color: #333;
color: #666;
width: 140px;
padding: 8px 24px;
margin: 0;
}
#seed:hover{
transition:0.2s;
background-color:#222

}


#rate_ctl {
position: absolute;
Expand Down Expand Up @@ -117,6 +137,8 @@ body {
top: 50px;
}



#time_left {
width: 105px;
font-size: 50px;
Expand Down
18 changes: 15 additions & 3 deletions rose/web/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,26 @@ var ROSE = (function() {
}

Controller.prototype.update = function(state) {
console.log(state)


$("#seed").text(state.seed);
if(state.players.length == 0){
$("#info").text("No players connected")
$("#info").text("No players connected");
$("#start").attr("disabled", "disabled");
$("#stop").attr("disabled", "disabled");
}
else if (state.started) {
$("#info").text("")
$("#info").text("");
$("#start").attr("disabled", "disabled");
$("#stop").removeAttr("disabled");

} else {
$("#info").text("")
$("#info").text("");
$("#start").removeAttr("disabled");
$("#stop").attr("disabled", "disabled");
}

}

Controller.prototype.disable = function() {
Expand Down Expand Up @@ -254,11 +260,13 @@ var ROSE = (function() {
if (player.lane == 0) {
$("#left.player .name").text(player.name)
$("#left.player .score").text(player.score)

}
if (player.lane == 1) {
$("#right.player .name").text(player.name)
$("#right.player .score").text(player.score)
}

}
}

Expand Down Expand Up @@ -302,6 +310,8 @@ var ROSE = (function() {
}
}



function Cars(loader) {
this.players = null;
this.textures = [null, null, null, null];
Expand Down Expand Up @@ -381,6 +391,8 @@ var ROSE = (function() {
});
}



Track.prototype.update = function(state) {
this.track = state.track;
if (state.started) {
Expand Down
4 changes: 4 additions & 0 deletions rose/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<button id="start" disabled>Start</button>
<button id="stop" disabled>Stop</button>
<button id="music_ctl">Play</button>
<h1 id="seed" ></h1>
<label id="info"></label>

<div id="rate_ctl">
Expand All @@ -23,8 +24,11 @@
<button id="dec_rate" disabled>&ndash;</button>
<button id="cur_rate" disabled>FPS</button>
<button id="inc_rate" disabled>+</button>

</span>

</div>

</form>
<div id="players">
<div id="left" class="player">
Expand Down
27 changes: 27 additions & 0 deletions rose/web/o
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"started": false,
"track": [
{
"name": "bike",
"x": 1,
"y": 1
},
{
"name": "bike",
"x": 5,
"y": 1
}
],
"players": [
{
"name": "No Driver",
"car": 2,
"x": 4,
"y": 6,
"lane": 1,
"score": 20
}
],
"timeleft": 58,
"rate": 1
}
Empty file added seed.txt
Empty file.