ソースを参照

First (non-working) code to continue devving on pc...

Taddeus Kroes 11 年 前
コミット
570838ec86
8 ファイル変更300 行追加0 行削除
  1. 5 0
      .gitignore
  2. 12 0
      Makefile
  3. 98 0
      game.coffee
  4. 90 0
      game.py
  5. 20 0
      server.py
  6. 54 0
      style.sass
  7. 10 0
      watch.sh
  8. 11 0
      www/index.html

+ 5 - 0
.gitignore

@@ -0,0 +1,5 @@
+*.swp
+*.pyc
+.sass-cache
+www/*.css
+www/*.js

+ 12 - 0
Makefile

@@ -0,0 +1,12 @@
+.PHONY: all clean
+
+all: www/style.css www/game.js
+
+www/%.css: %.sass
+	sass $< $@
+
+www/%.js: %.coffee
+	coffee -o $(@D) $<
+
+clean:
+	rm -f www/*.js www/*.css

+ 98 - 0
game.coffee

@@ -0,0 +1,98 @@
+URL = 'ws://localhost:8099'
+PLAYER_COLORS = ['blue', 'green', 'red', 'yellow']
+
+WALL_NONE   = 0
+WALL_TOP    = 1
+WALL_RIGHT  = 2
+WALL_BOTTOM = 4
+WALL_LEFT   = 8
+WALL_ALL    = WALL_TOP | WALL_RIGHT | WALL_BOTTOM | WALL_LEFT
+
+div = (cls) ->
+    elem = document.createElement 'div'
+    elem.className = cls
+    elem
+
+divin = (parent, cls) ->
+    elem = div cls
+    parent.appendChild elem
+    elem
+
+class Game
+    constructor: (@w, @h, elem) ->
+        @render elem if elem
+
+    get_wall: (x, y, direction) ->
+        x *= 2
+        y *= 2
+
+        if direction == WALL_TOP
+            x += 1
+        else if direction == WALL_RIGHT
+            x += 2
+            y += 1
+        else if direction == WALL_BOTTOM
+            x += 1
+            y += 2
+        else if direction == WALL_LEFT
+            y += 1
+
+        @board.children[y].children[x]
+
+    click_wall: (x, y, direction) ->
+        wall = @get_wall x, y, direction
+        wall.className += ' clicked'
+
+    get_room: (x, y) ->
+        @board.children[y * 2 + 1].children[x * 2 + 1]
+
+    occupy: (x, y, player) ->
+        room = @get_room x, y
+        room.style.backgroundColor = PLAYER_COLORS[player]
+
+    render: (elem) ->
+        @board = div 'board'
+        elem.appendChild @board
+
+        row = divin @board, 'row'
+        for x in [1..@w]
+            divin row, 'dot'
+            divin row, 'wall-h clicked'
+        divin row, 'dot'
+
+        for y in [1..@h]
+            row = divin @board, 'row'
+            for x in [1..@w]
+                clicked = if x == 1 then ' clicked' else ''
+                divin row, "wall-v#{clicked}"
+                divin row, 'room'
+            divin row, 'wall-v clicked'
+
+            row = divin @board, 'row'
+            clicked = if y == @h then ' clicked' else ''
+            for x in [1..@w]
+                divin row, 'dot'
+                divin row, "wall-h#{clicked}"
+            divin row, 'dot'
+
+game = new Game 6, 6
+game.render document.getElementById 'game'
+game.click_wall 2, 2, WALL_RIGHT
+game.click_wall 2, 2, WALL_BOTTOM
+game.click_wall 1, 2, WALL_RIGHT
+game.click_wall 2, 2, WALL_TOP
+game.occupy 2, 2, 2
+
+#ws = new WebSocket URL
+#
+#ws.onopen = ->
+#    console.log 'open'
+#
+#ws.onclose = ->
+#    console.log 'close'
+#
+#ws.onerror = (e) ->
+#    console.log 'error', e
+#
+#ws.onmessage = (msg) ->
+#    console.log 'msg', msg

+ 90 - 0
game.py

@@ -0,0 +1,90 @@
+WALL_NONE   = 0
+WALL_TOP    = 1
+WALL_RIGHT  = 2
+WALL_BOTTOM = 4
+WALL_LEFT   = 8
+WALL_ALL = WALL_TOP | WALL_RIGHT | WALL_BOTTOM | WALL_LEFT
+
+
+class Game:
+    def __init__(self, w, h):
+        assert w > 1 and h > 1
+        self.w = w
+        self.h = h
+        self.rooms = [[None] * w for _ in xrange(h)]
+        self.walls = [[WALL_NONE] * w for _ in xrange(h)]
+
+        for col in xrange(w):
+            self.walls[0][col] |= WALL_TOP
+            self.walls[-1][col] |= WALL_BOTTOM
+
+        for row in self.walls:
+            row[0] |= WALL_LEFT
+            row[-1] |= WALL_RIGHT
+
+    def click_wall(self, x, y, direction, player):
+        def try_click(x, y, d):
+            if 0 <= x < self.w and 0 <= y < self.h:
+                if self.walls[y][x] & d:
+                    raise ValueError('dir %d already set on %d,%d' % (d, x, y))
+
+                self.walls[y][x] |= d
+
+                if self.walls[y][x] == WALL_ALL:
+                    self.rooms[y][x] = player
+                    rooms.append((x, y))
+
+        rooms = []
+        try_click(x, y, direction)
+
+        if direction == WALL_TOP:
+            try_click(x, y - 1, WALL_BOTTOM)
+        elif direction == WALL_RIGHT:
+            try_click(x + 1, y, WALL_LEFT)
+        elif direction == WALL_BOTTOM:
+            try_click(x, y + 1, WALL_TOP)
+        elif direction == WALL_LEFT:
+            try_click(x - 1, y, WALL_RIGHT)
+
+        return rooms
+
+    def __str__(self):
+        roomwidth = 3
+
+        s = ('+' + '-' * roomwidth) * self.w + '+\n'
+
+        for y, (rrow, wrow) in enumerate(zip(self.rooms, self.walls)):
+            for x, (p, wall) in enumerate(zip(rrow, wrow)):
+                s += '|' if wall & WALL_LEFT else ' '
+                s += ('%%-%dd' % roomwidth) % p if p else ' ' * roomwidth
+
+            s += '|\n'
+
+            for wall in wrow:
+                s += '+'
+                s += ('-' if wall & WALL_BOTTOM else ' ') * roomwidth
+
+            s += '+'
+
+            if y != self.h - 1:
+                s += '\n'
+
+        return s
+
+    def is_finished(self):
+        return all(all(row) for row in self.rooms)
+
+    def scores(self):
+        indexed = {}
+
+        for row in self.rooms:
+            for player in row:
+                if player:
+                    indexed[player] = indexed.get(player, 0) + 1
+
+        return indexed
+
+    def sorted_scores(self):
+        scores = self.scores().items()
+        scores.sort(key=lambda (player, score): score, reverse=True)
+        return scores

+ 20 - 0
server.py

@@ -0,0 +1,20 @@
+#!/usr/bin/env python2
+import time
+from hashlib import sha1
+
+import wspy
+
+
+class Session:
+    def __init__(self):
+        self.sid = sha1(str(time.time()))
+        self.clients = []
+
+
+class GameServer(wspy.AsyncServer):
+    def onmessage(self, client, message):
+        pass
+
+
+if __name__ == '__main__':
+    GameServer(('', 8099)).run()

+ 54 - 0
style.sass

@@ -0,0 +1,54 @@
+$line: 7px
+$room: 40px
+$bordercolor: #000
+$bgcolor: #eee
+
+$inactivecolor: #d1d1d1
+$activecolor: $bordercolor
+$hovercolor: green
+
+@mixin elem($w, $h)
+  float: left
+  display: block
+  width: $w
+  height: $h
+
+@mixin hoverable
+  background-color: $inactivecolor
+
+  &:hover
+    background-color: $hovercolor
+    cursor: pointer
+
+.board
+  display: inline-block
+  //border: $line solid $bordercolor
+  background-color: $bgcolor
+
+  @mixin clicked
+    background-color: $bordercolor !important
+    cursor: default !important
+
+  .clicked
+    @include clicked
+
+  .dot
+    @include elem($line, $line)
+    @include clicked
+
+  .wall-h
+    @include elem($room, $line)
+    @include hoverable
+
+  .wall-v
+    @include elem($line, $room)
+    @include hoverable
+
+  .room
+    @include elem($room, $room)
+
+.message
+  display: none
+  font: normal 18px Tahoma, Arial, sans-serif
+  vertical-align: top
+  margin-top: 8px

+ 10 - 0
watch.sh

@@ -0,0 +1,10 @@
+#!/bin/sh
+hash inotifywait || (echo "Install inotify-tools first"; exit 1)
+
+make
+
+while true; do
+    inotifywait --quiet --event attrib,modify *.sass *.coffee
+    sleep 0.05s
+    make
+done

+ 11 - 0
www/index.html

@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <title>Kamertje kapen</title>
+        <link href="style.css" rel="stylesheet" type="text/css">
+    </head>
+    <body>
+        <div id="game" class="game"></div>
+        <script src="game.js"></script>
+    </body>
+</html>