ares-hackathon 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1799e81dea7e55f1540eac11b78ddbb92bdf7893
4
+ data.tar.gz: cde08047e263dd1fa0ce38f5f6b76af081d1bd1b
5
+ SHA512:
6
+ metadata.gz: f2bc53d6763d83cdea850071a5799eb34d2c032ed553a564bfee1daeca89bac542806398fa28dd90755eca1a21eb430ec346dc9859adc511168e5515c34bf7cb
7
+ data.tar.gz: eba1bbccf9dd1528a103cefb7ff2d792173eb719d429d4b9a8d777fb290e287640055948031a804d582c332225d8e12dd6d53123b163887c39a0ead79aa3c06a
@@ -0,0 +1,114 @@
1
+ require 'socket'
2
+
3
+ class AresHackathon
4
+ ONGOING = 0
5
+ VICTORY = 1
6
+ DEFEAT = 2
7
+ CONNECTION_LOST = 3
8
+
9
+ attr_reader :field
10
+ attr_reader :status
11
+ attr_reader :name
12
+ attr_reader :id
13
+
14
+ def initialize
15
+ @status = ONGOING
16
+ end
17
+
18
+ def connect(url, name)
19
+ s = url.split ":"
20
+ ip = s[0]
21
+ port = s.length > 1 ? s[1].to_i : 1337
22
+
23
+ @conn = TCPSocket.new ip, port
24
+ name = name[0..24]
25
+ name = name+"\0"
26
+
27
+ @conn.send name, 0
28
+ id = @conn.recv(1)
29
+
30
+ @id = id.ord
31
+ @name = name[0..24]
32
+ end
33
+
34
+
35
+ def attack(fromX, fromY, toX, toY)
36
+ buffer = [fromX, fromY, toX, toY].pack('C*')
37
+ @conn.send buffer, 0
38
+
39
+ if fromX == 255 && fromY == 255 && toX == 255 && toY == 255 then
40
+ return
41
+ end
42
+
43
+ @field = AresHackathon::Field.read_from_socket @conn
44
+ end
45
+
46
+ def end_attacks
47
+ attack(255,255,255,255)
48
+
49
+ buffer = @conn.recv 1
50
+ @total_units = buffer[0].ord
51
+ @units = []
52
+ end
53
+
54
+ def units_remained
55
+ return @total_units - @units.length
56
+ end
57
+
58
+ def add_unit(c)
59
+ if units_remained > 0 then
60
+ @units << c
61
+ end
62
+ end
63
+
64
+ def add_units_list(cells)
65
+ cells.each do | cell |
66
+ add_unit cell
67
+ end
68
+ end
69
+
70
+ def add_units(c, count)
71
+ for i in 1..count
72
+ add_unit c
73
+ end
74
+ end
75
+
76
+ def end_adding_units
77
+ buffer = []
78
+ for i in 0..(@total_units-1)
79
+ if i < @units.length then
80
+ buffer << @units[i].x
81
+ buffer << @units[i].y
82
+ else
83
+ buffer << 255
84
+ buffer << 255
85
+ end
86
+ end
87
+ send_buffer = buffer.pack("C*")
88
+ @conn.write send_buffer
89
+
90
+ @field = AresHackathon::Field.read_from_socket @conn
91
+ end
92
+
93
+ def next_turn
94
+ @field = AresHackathon::Field.read_from_socket @conn
95
+
96
+ @status = VICTORY if @field.has_win @id
97
+ @status = DEFEAT if @field.has_lost @id
98
+ end
99
+
100
+ def cell(x,y)
101
+ return @field.cell(x,y)
102
+ end
103
+
104
+ def my_cells
105
+ return @field.owned_by @id
106
+ end
107
+
108
+ def map
109
+ return @field
110
+ end
111
+ end
112
+
113
+ require 'ares-hackathon/field'
114
+ require 'ares-hackathon/cell'
@@ -0,0 +1,13 @@
1
+ class AresHackathon::Cell
2
+ attr_reader :x
3
+ attr_reader :y
4
+ attr_reader :owner
5
+ attr_reader :power
6
+
7
+ def initialize(px,py, owner, power)
8
+ @x = px
9
+ @y = py
10
+ @owner = owner
11
+ @power = power
12
+ end
13
+ end
@@ -0,0 +1,60 @@
1
+ class AresHackathon::Field
2
+ attr_reader :size_x
3
+ attr_reader :size_y
4
+ def initialize(sx, sy, field)
5
+ @size_x = sx
6
+ @size_y = sy
7
+ @field = field
8
+ end
9
+
10
+ def self.read_from_socket(conn)
11
+ sizes = conn.recv(2)
12
+ sx = sizes[0].ord
13
+ sy = sizes[1].ord
14
+ field = []
15
+ for x in 0..(sx-1)
16
+ field[x] = []
17
+ end
18
+
19
+ for y in 0..(sy-1)
20
+ buffer = conn.recv(2*sx)
21
+ for x in 0..(sx-1)
22
+ field[x][y] = AresHackathon::Cell.new x, y, buffer[2*x].ord, buffer[2*x+1].ord
23
+ end
24
+ end
25
+
26
+ return AresHackathon::Field.new sx, sy, field
27
+ end
28
+
29
+ def cell(x,y)
30
+ return @field[x][y]
31
+ end
32
+
33
+ def has_lost(p)
34
+ @field.each do |row|
35
+ row.each do |cell|
36
+ return false if cell.owner == p
37
+ end
38
+ end
39
+ return true
40
+ end
41
+
42
+ def has_win(p)
43
+ @field.each do |row|
44
+ row.each do |cell|
45
+ return false if cell.owner != p && cell.owner != 0
46
+ end
47
+ end
48
+ return true
49
+ end
50
+
51
+ def owned_by(player)
52
+ res = @field.map do |line|
53
+ line.select do |cell|
54
+ cell.owner == player
55
+ end
56
+ end
57
+
58
+ return res.flatten
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ares-hackathon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hurter Jonathan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem for the Ares Hackathon
14
+ email: jonathan.hurter@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/ares-hackathon.rb
20
+ - lib/ares-hackathon/cell.rb
21
+ - lib/ares-hackathon/field.rb
22
+ homepage: http://ares-ensiie.eu
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.5.1
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Ares Hackathon gem
46
+ test_files: []