Skip to content

Commit

Permalink
add score bard
Browse files Browse the repository at this point in the history
  • Loading branch information
halida committed Nov 26, 2011
1 parent 878537b commit ad817b8
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -9,6 +9,8 @@
*.bak
*.out

tmp

# rubyweb

rubyweb/public/assets/*.css
Expand Down
2 changes: 1 addition & 1 deletion makefile
Expand Up @@ -18,7 +18,7 @@ ai:
python srcs/ai_simple.py zero 0
# run lots of test ai
ais:
python srcs/ai_edward32tnt.py zero 0 2
python srcs/ai_simple.py zero 0 14

# pygame show local game
pygame:
Expand Down
11 changes: 11 additions & 0 deletions rubyweb/app/controllers/home_controller.rb
@@ -1,5 +1,16 @@

class HomeController < ApplicationController
require 'sqlite3'
@@db = SQLite3::Database.new '../tmp/game.db'

def index
@replays = Replay.recent.limit(5)
end

def scoreboard
@dailys = @@db.execute('select * from (select name, count(*) as count from scores where time > ? group by name) order by count desc limit 10', Date.today.to_time.to_f)
@weeklys = @@db.execute('select * from (select name, count(*) as count from scores where time > ? group by name) order by count desc limit 10', (Date.today - 7.days).to_time.to_f)
@monthlys = @@db.execute('select * from (select name, count(*) as count from scores where time > ? group by name) order by count desc limit 10', (Date.today - 30.days).to_time.to_f)
end

end
35 changes: 34 additions & 1 deletion rubyweb/app/stylesheets/main.sass
Expand Up @@ -17,6 +17,11 @@ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav,
body
line-height: 1

a
text-decoration: none
&:hover
color: #37D8F9

ol, ul
list-style: none

Expand Down Expand Up @@ -246,4 +251,32 @@ ul#maps
/*clear
.clear
clear: both
clear: both

#scoreboard-link
a
font-size: 30px
padding-bottom: 10px
padding-left: 35px
width: 100%

#scoreboard
margin: 20px auto
width: 800px
.daily, .weekly, .monthly
display: inline-block
min-width: 250px
th
min-width: 100px
height: 50px
font-size: 30px
text-align: center
td
min-width: 100px
font-size: 20px
td.name
color: #88DB99
text-align: left
td.score
color: #A5C9E7
text-align: right
2 changes: 2 additions & 0 deletions rubyweb/app/views/home/index.html.haml
Expand Up @@ -121,6 +121,8 @@
%a{:href => replay_path(item.id)}= item.title
%li.more
%a{:href => replays_path} More...
.title_bar_r
#scoreboard-link= link_to "Scoreboard", "/home/scoreboard"
.title_bar_r
%h1 Sponsors

Expand Down
11 changes: 11 additions & 0 deletions rubyweb/app/views/home/scoreboard.html.haml
@@ -0,0 +1,11 @@
#place
#scoreboard
- for name, data in [['daily', @dailys], ['weekly', @weeklys], ['monthly', @monthlys]]
%table{border: "1", cellpadding: "2", cellspacing: "2", class: name}
%tr
%th{COLSPAN: "2"}= "#{name} score"

- for name, count in data
%tr
%td.name= name
%td.score= count
2 changes: 1 addition & 1 deletion rubyweb/app/views/layouts/application.html.haml
Expand Up @@ -4,7 +4,7 @@
%meta{:content => "text/html; charset=utf-8", "http-equiv" => "Content-Type"}/
%title Ruby VS Python Official Site

= stylesheet_link_tag "main", "map_builder", :cache => "cached_main"
= stylesheet_link_tag "main.css", "map_builder.css"
= yield :styles
= javascript_include_tag "jquery", "json", "application", "map_builder", "chatroom", :cache => "cached_application"
= csrf_meta_tag
Expand Down
2 changes: 2 additions & 0 deletions rubyweb/config/routes.rb
Expand Up @@ -15,4 +15,6 @@

devise_for :users

match ':controller(/:action(/:id(.:format)))'

end
14 changes: 14 additions & 0 deletions srcs/db.py
@@ -0,0 +1,14 @@
#!/usr/bin/env python
#-*- coding:utf-8 -*-
"""
module: db
"""
import sqlite3
db = sqlite3.connect('tmp/game.db')
cursor = db.cursor()
try:
cursor.execute('create table scores (time, name)')
except:
pass


2 changes: 1 addition & 1 deletion srcs/lib.py
Expand Up @@ -3,7 +3,7 @@
"""
module: lib
"""
import time, sys, logging, json, random
import time, sys, logging, json, random, uuid

logging.basicConfig(level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s")
Expand Down
20 changes: 5 additions & 15 deletions srcs/snake_game.py
Expand Up @@ -7,9 +7,7 @@
from simple import *
from map.map import Map
from random_wall import RandomWallGen
import uuid


import db
# 蛇的方向
LEFT, UP, RIGHT, DOWN = range(4)

Expand All @@ -31,7 +29,6 @@
PYTHON = 'python'
RUBY = 'ruby'


class Snake():
def __init__(self, game, type, direction, head, length, name=""):
"""设置snake
Expand Down Expand Up @@ -117,9 +114,6 @@ def length(self):
class Game():
"""游戏场景"""
# 记录分数
scores = [('AAA', i)
for i in range(1, 10)]

def __init__(self,
enable_bean=True,
enable_wall=True,
Expand All @@ -132,15 +126,15 @@ def __init__(self,
if not map:
map = Map.load('srcs/map/flat.map')
self.setMap(map)
self.restart()
self.start()

def setMap(self, map):
self.wallgen = map.wallgen
#self.wallgen = RandomWallGen() #SimpleWallGen()
self.beangen = map.beangen
self.size = self.w, self.h = map.meta['width'], map.meta['height']

def restart(self):
def start(self):
'''
# 因为js没有(), 只好用[]
self.walls = [[10, i]
Expand Down Expand Up @@ -234,12 +228,8 @@ def check_score(self):
# 计算谁的分数最大
highest = max(lives, key=lambda s: s.length())
# 再加到最高分里面去
self.scores.append(
(highest.name, highest.length()))
self.scores.sort(key= lambda d: d[1])
# 只统计10个
if len(self.scores) > 10:
self.scores.pop(0)
db.cursor.execute('insert into scores values(?, ?)', (time.time(), highest.name))
db.db.commit()

def step(self):
"""游戏进行一步..."""
Expand Down
2 changes: 1 addition & 1 deletion srcs/zmq_game_server.py
Expand Up @@ -92,7 +92,7 @@ def run(self, loop=False, max_waits=10.0, enable_no_resp_die=True):
if loop and g.status == FINISHED:
g.loop_count += 1
if g.loop_count > 20:
g.restart()
g.start()
g.loop_count = 0

# 游戏处理
Expand Down
1 change: 1 addition & 0 deletions tmp/readme.txt
@@ -0,0 +1 @@
this dir is for temp files

0 comments on commit ad817b8

Please sign in to comment.