unknown 10 anni fa
commit
6296082120
7 ha cambiato i file con 393 aggiunte e 0 eliminazioni
  1. 1 0
      .gitignore
  2. 113 0
      board/board.go
  3. 74 0
      board/board_test.go
  4. 41 0
      board/tile.go
  5. 118 0
      boardserver/boardserver.jmx
  6. 24 0
      boardserver/main.go
  7. 22 0
      rndtest/main.go

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+*.exe

+ 113 - 0
board/board.go

@@ -0,0 +1,113 @@
+package board
+
+import (
+	"bytes"
+	"encoding/json"
+	"fmt"
+	"io"
+	"math/rand"
+	"time"
+)
+
+type Board struct {
+	Cols  int    `json:"cols"`
+	Rows  int    `json:"rows"`
+	Tiles []Tile `json:"tiles"`
+}
+
+var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
+
+const maxIceWidth = 26
+
+// Create a new randomly generated board.
+func New(cols, rows int) Board {
+	b := Board{cols, rows, make([]Tile, rows*cols)}
+
+	for y := 0; y < rows; y++ {
+		for x := 0; x < cols; x++ {
+			b.randomizeTile(x, y)
+		}
+	}
+
+	return b
+}
+
+// Set the tile in column x and row y to val.
+func (b Board) Set(x, y int, val Tile) {
+	b.Tiles[y*b.Cols+x] = val
+}
+
+// Get the tile in column x and row y.
+func (b Board) Get(x, y int) Tile {
+	return b.Tiles[y*b.Cols+x]
+}
+
+func (b Board) String() string {
+	var buffer bytes.Buffer
+
+	for y := 0; y < b.Rows; y++ {
+		for x := 0; x < b.Cols; x++ {
+			buffer.WriteString(b.Get(x, y).String())
+		}
+		buffer.WriteString(fmt.Sprintln())
+	}
+
+	return buffer.String()
+}
+
+type jsonTile struct {
+	X int `json:"x"`
+	Y int `json:"y"`
+	Type string`json:"type"`
+	Player string `json:"player,omitempty"`
+}
+
+func (b Board) WriteJSON(w io.Writer) {
+	//fmt.Fpr
+	fmt.Fprintf(w, "{rows=\"%v\",columns=\"%v\",tiles={", b.Rows, b.Cols)
+	for y := 0; y < b.Rows; y++ {
+		for x := 0; x < b.Cols; x++ {
+			t := &jsonTile{x, y, b.Get(x, y).Name(), ""}
+			bs, _ := json.Marshal(t)
+			w.Write(bs)
+			w.Write([]byte{','})
+		}
+	}
+	fmt.Fprintf(w, "}}")
+}
+
+func (b Board) randomizeTile(x, y int) {
+
+	switch {
+	case b.isIglo(x, y):
+		b.Set(x, y, Iglo)
+	case b.isRock(x, y):
+		b.Set(x, y, Rock)
+	case b.isIce(x, y):
+		b.Set(x, y, Ice)
+	default:
+		b.Set(x, y, Water)
+	}
+
+}
+
+func (b Board) isIglo(x, y int) bool {
+	return x == (b.Cols-1) && y == (b.Rows-1)/2
+}
+
+func (b Board) isRock(x, y int) bool {
+	return rnd.Intn(10) > 7
+}
+
+func (b Board) isIce(x, y int) bool {
+	leftLimit := (b.Cols - maxIceWidth) / 2
+	rightLimit := b.Cols/2 + maxIceWidth/2
+	return x > leftLimit && x < rightLimit && rnd.Intn(maxIceWidth) >= abs((b.Cols/2)-x)
+}
+
+func abs(a int) int {
+	if a < 0 {
+		return -a
+	}
+	return a
+}

+ 74 - 0
board/board_test.go

@@ -0,0 +1,74 @@
+package board
+
+import (
+	"fmt"
+	"math/rand"
+	"testing"
+)
+
+var testBoard = `~~~~~~~~▲▲▲~▲▲~~*~*~~**~*▲****▲▲***▲~▲***~~▲~~▲~▲~~~~~▲~
+▲~~~~▲~~~~~~~~~~~▲▲▲*~▲▲*▲▲***▲*▲*~▲▲▲~**~▲~~~~~~~~~▲~▲~
+~~~~~~~~~~▲~~~▲~▲~~**▲~~*~**▲▲**▲~*▲~***▲▲~▲~~~~~▲~~~▲▲~
+~~▲~~~~~~▲~~~~▲▲*~~*▲~****▲▲*****▲*~****▲▲~▲~~~~~▲~~~~~~
+~▲~~~~~▲▲~~~~~~~~▲▲~~▲*▲*~*▲▲****▲*▲**~**~~~▲~~~~~~~~~~~
+~~▲~~▲▲▲~~~~~▲~▲~*~******▲▲*▲*****~*~~***~~~~~▲~~~~~~~~~
+▲~~~~~~~~▲▲~▲~~▲▲****~~**~***▲****▲*~~~*~~▲~▲~▲~~~~~~▲▲▲
+▲~~~~~~~~▲~~~~~~~**~***▲**~**~~****~*▲*~~~~▲~~▲~~~~~~~~~
+~~▲~▲~~▲~~~~~~~~~***▲**********~~**▲***~*~~~▲▲~~~▲~~▲~~▲
+▲~~~~~▲~~~~▲~▲~~*~*~~*******▲***▲**▲▲*~~~~~~▲~▲~~~~~▲▲~~
+~▲~~~~~~~~~▲~~~~****▲~*▲**▲****▲▲**~*▲***~~▲▲~▲▲▲~~~~▲~~
+~▲~▲~~~~~▲~~~~~▲**▲**~*~▲*~***▲▲*~*▲***~~~~~~~~~~~~~~~~~
+~▲~~▲~~~▲~▲~▲~~▲*~~▲*~▲**▲▲***▲▲**~*~**~~~~▲▲▲▲~~~~~~~~~
+▲~▲~~▲▲~~▲~~~▲~~~~********▲▲▲▲***▲*~***▲▲~~~~▲~~~~~~~▲~~
+~▲~~~~▲▲~~~~~▲~~▲*~*▲▲*▲****▲▲******▲*~~*~~~~▲~~~~~~~▲~~
+~~~~~~~~~~~~~~▲▲~▲~******▲*▲*▲**~*~*~**~*~~~~~~~~▲▲~~~~▲
+~~▲▲~~~~~~~~~~~▲***▲~*▲**▲*~***▲*********~~~~▲▲~~~~~▲~~~
+~~▲~~~~~~▲▲▲~~~~*~~▲~*~▲*▲*▲*****▲**▲~*▲*~▲~~~~▲▲~▲~~~~⌂
+~~~~~~~▲~~~▲▲~~~*~▲**~**▲*▲*▲***▲**▲*****~~~~▲~~~~~~~▲~~
+~~▲~~~~▲~~~~~▲~~~*~********▲**▲*~*▲*▲**▲~~~~~~~~~~~~~~▲~
+~~~~~~~~~~~~~~▲~*~*~~****~~**▲*****▲**~*~~~▲~▲~~▲~~~▲~~▲
+~▲~~~▲~▲▲~~~~~~~~*~**~~*▲**▲*▲▲***~******~▲~~~~~~▲~~~~~~
+~~~~~~~~▲~~~~~~~~*▲**▲*▲***▲▲**▲~***~▲*~*~~▲~~~~~▲~~~~~~
+~~~~▲~▲~~▲~~▲▲~~**~▲*******~***~▲~**~*~~▲~~~~~~~~▲~~~~~~
+~~▲~~~~~~~~~~~~~**~▲**~***~**▲***▲*~*▲▲*~~~~▲▲~~~~~~~▲~~
+~~~~▲~~~▲~~▲▲~▲~▲▲*▲*▲*▲**▲**▲*~******▲*▲▲~~~~~~~~~~~▲~~
+~~~▲~~~~~~~~~~~~*~~▲▲▲▲▲*******~**▲▲*~▲*~~~~~~~~~~▲~~~▲~
+▲~~~~~~▲~~~~▲~~~*▲▲▲****▲▲****▲***▲~▲▲▲*▲~~~~~~~~▲~~~~~~
+~~~~~▲~▲~~~~~~~~*~~*****▲********~~~~▲**~~~▲▲~~~~▲~~~~~~
+~~▲~~~▲~~~▲~~▲~▲▲▲******▲*******▲***▲▲***~~~~~~~~~~~▲~▲~
+~▲~▲~~~~~~▲~~~~▲*~~***~~~*****▲**▲~*~*~~~~▲▲~~~~~~▲~~~~~
+~~~▲~~~~~~~~~~~~~▲**~*~*▲*▲▲**▲~▲*****▲**▲~~~~~~▲~~▲~~~▲
+▲~▲~~~▲~▲▲▲~~~~▲~*~▲~~▲▲▲**▲********▲***~~~~~▲~~~▲~~~~~~
+~~~~▲~~~~~~▲~~~~~▲~*▲*▲*******▲**▲**▲~~*~~▲~~~~~~~~~▲▲~~
+▲~▲~▲~~▲▲~~~~~~~~▲~**▲******▲********▲~**~▲▲~~~~▲▲~~▲~~▲
+`
+
+func TestNewBoard(t *testing.T) {
+	rnd = rand.New(rand.NewSource(0))
+	b := New(56, 35)
+l
+	fmt.Println(b)
+
+	if testBoard != b.String() {
+		t.Fatalf("Incorrect board:\n %v", b)
+	}
+
+}
+
+func BenchmarkNew(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		rnd = rand.New(rand.NewSource(0))
+		New(56, 35)
+	}
+}
+
+func BenchmarkRnd(b *testing.B) {
+
+	for i := 0; i < b.N; i++ {
+		rnd = rand.New(rand.NewSource(0))
+
+		for j := 0; j < 56*35; j++ {
+			rnd.Intn(10)
+		}
+	}
+}

+ 41 - 0
board/tile.go

@@ -0,0 +1,41 @@
+package board
+
+type Tile byte
+
+const (
+	Water Tile = '~'
+	Ice   Tile = '*'
+	Rock  Tile = '^'
+	Iglo  Tile = 'O'
+)
+
+func (t Tile) String() string {
+	switch t {
+	case Water:
+		return "~"
+	case Rock:
+		return "▲"
+	case Ice:
+		return "*"
+	case Iglo:
+		return "⌂"
+	}
+
+	panic("Unknown tile type")
+}
+
+func (t Tile) Name() string {
+	switch t {
+	case Water:
+		return "WATER"
+	case Rock:
+		return "ROTS"
+	case Ice:
+		return "IJS"
+	case Iglo:
+		return "HUIS"
+	}
+
+	panic("Unknown tile type")
+
+}

+ 118 - 0
boardserver/boardserver.jmx

@@ -0,0 +1,118 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<jmeterTestPlan version="1.2" properties="2.8" jmeter="2.13 r1665067">
+  <hashTree>
+    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
+      <stringProp name="TestPlan.comments"></stringProp>
+      <boolProp name="TestPlan.functional_mode">false</boolProp>
+      <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
+      <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
+        <collectionProp name="Arguments.arguments"/>
+      </elementProp>
+      <stringProp name="TestPlan.user_define_classpath"></stringProp>
+    </TestPlan>
+    <hashTree>
+      <SetupThreadGroup guiclass="SetupThreadGroupGui" testclass="SetupThreadGroup" testname="Thread Group" enabled="true">
+        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
+        <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
+          <boolProp name="LoopController.continue_forever">false</boolProp>
+          <stringProp name="LoopController.loops">100</stringProp>
+        </elementProp>
+        <stringProp name="ThreadGroup.num_threads">50</stringProp>
+        <stringProp name="ThreadGroup.ramp_time">10</stringProp>
+        <longProp name="ThreadGroup.start_time">1441468489000</longProp>
+        <longProp name="ThreadGroup.end_time">1441468489000</longProp>
+        <boolProp name="ThreadGroup.scheduler">false</boolProp>
+        <stringProp name="ThreadGroup.duration"></stringProp>
+        <stringProp name="ThreadGroup.delay"></stringProp>
+      </SetupThreadGroup>
+      <hashTree>
+        <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true">
+          <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
+            <collectionProp name="Arguments.arguments"/>
+          </elementProp>
+          <stringProp name="HTTPSampler.domain">localhost</stringProp>
+          <stringProp name="HTTPSampler.port">8080</stringProp>
+          <stringProp name="HTTPSampler.connect_timeout"></stringProp>
+          <stringProp name="HTTPSampler.response_timeout"></stringProp>
+          <stringProp name="HTTPSampler.protocol"></stringProp>
+          <stringProp name="HTTPSampler.contentEncoding"></stringProp>
+          <stringProp name="HTTPSampler.path">/api/board</stringProp>
+          <stringProp name="HTTPSampler.method">GET</stringProp>
+          <boolProp name="HTTPSampler.follow_redirects">false</boolProp>
+          <boolProp name="HTTPSampler.auto_redirects">true</boolProp>
+          <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
+          <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
+          <boolProp name="HTTPSampler.monitor">false</boolProp>
+          <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
+        </HTTPSamplerProxy>
+        <hashTree/>
+      </hashTree>
+      <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
+        <boolProp name="ResultCollector.error_logging">false</boolProp>
+        <objProp>
+          <name>saveConfig</name>
+          <value class="SampleSaveConfiguration">
+            <time>true</time>
+            <latency>true</latency>
+            <timestamp>true</timestamp>
+            <success>true</success>
+            <label>true</label>
+            <code>true</code>
+            <message>true</message>
+            <threadName>true</threadName>
+            <dataType>true</dataType>
+            <encoding>false</encoding>
+            <assertions>true</assertions>
+            <subresults>true</subresults>
+            <responseData>false</responseData>
+            <samplerData>false</samplerData>
+            <xml>false</xml>
+            <fieldNames>false</fieldNames>
+            <responseHeaders>false</responseHeaders>
+            <requestHeaders>false</requestHeaders>
+            <responseDataOnError>false</responseDataOnError>
+            <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
+            <assertionsResultsToSave>0</assertionsResultsToSave>
+            <bytes>true</bytes>
+            <threadCounts>true</threadCounts>
+          </value>
+        </objProp>
+        <stringProp name="filename"></stringProp>
+      </ResultCollector>
+      <hashTree/>
+      <ResultCollector guiclass="SummaryReport" testclass="ResultCollector" testname="Summary Report" enabled="true">
+        <boolProp name="ResultCollector.error_logging">false</boolProp>
+        <objProp>
+          <name>saveConfig</name>
+          <value class="SampleSaveConfiguration">
+            <time>true</time>
+            <latency>true</latency>
+            <timestamp>true</timestamp>
+            <success>true</success>
+            <label>true</label>
+            <code>true</code>
+            <message>true</message>
+            <threadName>true</threadName>
+            <dataType>true</dataType>
+            <encoding>false</encoding>
+            <assertions>true</assertions>
+            <subresults>true</subresults>
+            <responseData>false</responseData>
+            <samplerData>false</samplerData>
+            <xml>false</xml>
+            <fieldNames>false</fieldNames>
+            <responseHeaders>false</responseHeaders>
+            <requestHeaders>false</requestHeaders>
+            <responseDataOnError>false</responseDataOnError>
+            <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
+            <assertionsResultsToSave>0</assertionsResultsToSave>
+            <bytes>true</bytes>
+            <threadCounts>true</threadCounts>
+          </value>
+        </objProp>
+        <stringProp name="filename"></stringProp>
+      </ResultCollector>
+      <hashTree/>
+    </hashTree>
+  </hashTree>
+</jmeterTestPlan>

+ 24 - 0
boardserver/main.go

@@ -0,0 +1,24 @@
+package main
+
+import (
+	"runtime"
+	"log"
+	"net/http"
+	"pinguin/board"
+)
+
+func main() {
+	runtime.GOMAXPROCS(1)
+	http.HandleFunc("/api/board", boardHandler)
+	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+		w.Write([]byte("Go battlecamp!"))
+	})
+
+	log.Fatal(http.ListenAndServe(":8080", nil))
+}
+
+func boardHandler(w http.ResponseWriter, req *http.Request) {
+	b := board.New(56, 35)
+	w.Header().Set("Content-Type", "text/plain;charset=utf-8")
+	b.WriteJSON(w)
+}

+ 22 - 0
rndtest/main.go

@@ -0,0 +1,22 @@
+package main
+
+var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
+
+func main() {
+	//runtime.GOMAXPROCS(1)
+	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+		b := board.New(56, 35)
+		w.Header().Set("Content-Type", "text/plain;charset=utf-8")
+		b.WriteJSON(w)
+	})
+
+	log.Fatal(http.ListenAndServe(":8080", nil))
+}
+
+func createRandomArray(size int) {
+	a := make(int[], size)
+	for i := 0; i < size; i++ {
+		a[i] = rnd.Intn(10)
+	}
+	
+}